diff --git a/py/mkrules.mk b/py/mkrules.mk index 1ab3ee59ba37add79eb7e447f11d55114b2fd6cf..2730eff4155b4914c38bffd3b121875a73c80386 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -45,6 +45,10 @@ vpath %.c . $(TOP) $(BUILD)/%.o: %.c $(call compile_c) +$(BUILD)/%.pp: %.c + $(ECHO) "PreProcess $<" + $(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $< + # The following rule uses | to create an order only prereuisite. Order only # prerequisites only get built if they don't exist. They don't cause timestamp # checkng to be performed. diff --git a/stmhal/Makefile b/stmhal/Makefile index 1c044055c129f1fcbcf3f08f7ccf417e2f86c132..aeddca480286b2e46095dab796a1dd53552b7986 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -62,6 +62,8 @@ SRC_C = \ systick.c \ led.c \ pin.c \ + pin_map.c \ + pin_named_pins.c \ usart.c \ usb.c \ printf.c \ @@ -72,8 +74,10 @@ SRC_C = \ pybmodule.c \ import.c \ lexerfatfs.c \ + gpio.c \ + exti.c \ + usrsw.c \ -# gpio.c \ # lcd.c \ # servo.c \ # flash.c \ @@ -86,10 +90,6 @@ SRC_C = \ # adc.c \ # rtc.c \ # file.c \ -# pin_named_pins.c \ -# pin_map.c \ -# exti.c \ -# usrsw.c \ # pybwlan.c \ SRC_S = \ diff --git a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h index 9bc5149ecf85e8b9469418c6fb7043f0fac57b86..412dede1310d230bb046b4ed219f537cbca6f856 100644 --- a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h +++ b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h @@ -20,8 +20,8 @@ // USRSW is pulled low. Pressing the button makes the input go high. #define USRSW_PIN (pin_B11) -#define USRSW_PUPD (GPIO_PuPd_NOPULL) -#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising) +#define USRSW_PULL (GPIO_NOPULL) +#define USRSW_EXTI_MODE (GPIO_MODE_IT_RISING) #define USRSW_PRESSED (1) /* LED */ @@ -30,9 +30,8 @@ #define PYB_LED3 (pin_A10) // Same as Led(1) #define PYB_LED4 (pin_C13) // Same as Led(2) -#define PYB_OTYPE (GPIO_OType_PP) +#define PYB_OTYPE (GPIO_MODE_OUTPUT_PP) #define PYB_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask) #define PYB_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask) -#define HSE_VALUE (25000000) diff --git a/stmhal/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h b/stmhal/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h new file mode 100644 index 0000000000000000000000000000000000000000..a90bcf28a7ca0825a076d13a3763a4d50bef73d7 --- /dev/null +++ b/stmhal/boards/NETDUINO_PLUS_2/stm32f4xx_hal_conf.h @@ -0,0 +1,394 @@ +/** + ****************************************************************************** + * @file stm32f4xx_hal_conf.h + * @author MCD Application Team + * @version V1.0.1 + * @date 26-February-2014 + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F4xx_HAL_CONF_H +#define __STM32F4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +#define STM32F405xx +#define USE_USB_FS +#define USE_USB_OTG_ID 0 + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +#define HAL_CAN_MODULE_ENABLED +#define HAL_CRC_MODULE_ENABLED +#define HAL_CRYP_MODULE_ENABLED +#define HAL_DAC_MODULE_ENABLED +#define HAL_DCMI_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +/* #define HAL_DMA2D_MODULE_ENABLED */ +#define HAL_ETH_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_NAND_MODULE_ENABLED +#define HAL_NOR_MODULE_ENABLED +#define HAL_PCCARD_MODULE_ENABLED +#define HAL_SRAM_MODULE_ENABLED +/* #define HAL_SDRAM_MODULE_ENABLED */ +#define HAL_HASH_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +#define HAL_I2S_MODULE_ENABLED +#define HAL_IWDG_MODULE_ENABLED +#define HAL_LTDC_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_RNG_MODULE_ENABLED +#define HAL_RTC_MODULE_ENABLED +/* #define HAL_SAI_MODULE_ENABLED */ +#define HAL_SD_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +#define HAL_USART_MODULE_ENABLED +#define HAL_IRDA_MODULE_ENABLED +#define HAL_SMARTCARD_MODULE_ENABLED +#define HAL_WWDG_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_PCD_MODULE_ENABLED +#define HAL_HCD_MODULE_ENABLED + + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) + #define EXTERNAL_CLOCK_VALUE ((uint32_t)12288000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 1 +#define DATA_CACHE_ENABLE 1 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2 +#define MAC_ADDR1 0 +#define MAC_ADDR2 0 +#define MAC_ADDR3 0 +#define MAC_ADDR4 0 +#define MAC_ADDR5 0 + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB ((uint32_t)4) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB ((uint32_t)4) /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848 PHY Address*/ +#define DP83848_PHY_ADDRESS 0x01 +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY ((uint32_t)0x000000FF) +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFF) + +#define PHY_READ_TO ((uint32_t)0x0000FFFF) +#define PHY_WRITE_TO ((uint32_t)0x0000FFFF) + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ + +#define PHY_SR ((uint16_t)0x10) /*!< PHY status register Offset */ +#define PHY_MICR ((uint16_t)0x11) /*!< MII Interrupt Control Register */ +#define PHY_MISR ((uint16_t)0x12) /*!< MII Interrupt Status and Misc. Control Register */ + +#define PHY_LINK_STATUS ((uint16_t)0x0001) /*!< PHY Link mask */ +#define PHY_SPEED_STATUS ((uint16_t)0x0002) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004) /*!< PHY Duplex mask */ + +#define PHY_MICR_INT_EN ((uint16_t)0x0002) /*!< PHY Enable interrupts */ +#define PHY_MICR_INT_OE ((uint16_t)0x0001) /*!< PHY Enable output interrupt events */ + +#define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /*!< Enable Interrupt on change of link status */ +#define PHY_LINK_INTERRUPT ((uint16_t)0x2000) /*!< PHY link status interrupt mask */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f4xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED + #include "stm32f4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DMA2D_MODULE_ENABLED + #include "stm32f4xx_hal_dma2d.h" +#endif /* HAL_DMA2D_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_DCMI_MODULE_ENABLED + #include "stm32f4xx_hal_dcmi.h" +#endif /* HAL_DCMI_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED + #include "stm32f4xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED + #include "stm32f4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED + #include "stm32f4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED + #include "stm32f4xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SDRAM_MODULE_ENABLED + #include "stm32f4xx_hal_sdram.h" +#endif /* HAL_SDRAM_MODULE_ENABLED */ + +#ifdef HAL_HASH_MODULE_ENABLED + #include "stm32f4xx_hal_hash.h" +#endif /* HAL_HASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_LTDC_MODULE_ENABLED + #include "stm32f4xx_hal_ltdc.h" +#endif /* HAL_LTDC_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED + #include "stm32f4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED + #include "stm32f4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED + #include "stm32f4xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED + #include "stm32f4xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + + + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F4xx_HAL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmhal/boards/PYBOARD3/mpconfigboard.h b/stmhal/boards/PYBOARD3/mpconfigboard.h index 29e23eaab6a27a1c35dad8885d17910965a6efce..e0920f68595ec7994aef1d9ac7af47071e3c7409 100644 --- a/stmhal/boards/PYBOARD3/mpconfigboard.h +++ b/stmhal/boards/PYBOARD3/mpconfigboard.h @@ -16,8 +16,8 @@ // USRSW has no pullup or pulldown, and pressing the switch makes the input go low #define USRSW_PIN (pin_A13) -#define USRSW_PUPD (GPIO_PuPd_UP) -#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling) +#define USRSW_PULL (GPIO_PULLUP) +#define USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) #define USRSW_PRESSED (0) /* LED */ @@ -26,7 +26,7 @@ #define PYB_LED3 (pin_C4) // G1 - green #define PYB_LED4 (pin_C5) // G2 - green -#define PYB_OTYPE (GPIO_OType_PP) +#define PYB_OTYPE (GPIO_MODE_OUTPUT_PP) #define PYB_LED_ON(pin) (pin->gpio->BSRRH = pin->pin_mask) #define PYB_LED_OFF(pin) (pin->gpio->BSRRL = pin->pin_mask) diff --git a/stmhal/boards/PYBOARD4/mpconfigboard.h b/stmhal/boards/PYBOARD4/mpconfigboard.h index 155901da1b88af34de8cc023f75872e91d48b654..c3d5e119c98ea311065fc08224b26be6ccf51158 100644 --- a/stmhal/boards/PYBOARD4/mpconfigboard.h +++ b/stmhal/boards/PYBOARD4/mpconfigboard.h @@ -16,8 +16,8 @@ // USRSW has no pullup or pulldown, and pressing the switch makes the input go low #define USRSW_PIN (pin_B3) -#define USRSW_PUPD (GPIO_PuPd_UP) -#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling) +#define USRSW_PULL (GPIO_PULLUP) +#define USRSW_EXTI_MODE (GPIO_MODE_IT_FALLING) #define USRSW_PRESSED (0) /* LED */ @@ -26,7 +26,7 @@ #define PYB_LED3 (pin_A15) // yellow #define PYB_LED4 (pin_B4) // blue -#define PYB_OTYPE (GPIO_OType_PP) +#define PYB_OTYPE (GPIO_MODE_OUTPUT_PP) #define PYB_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask) #define PYB_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask) diff --git a/stmhal/boards/STM32F4DISC/mpconfigboard.h b/stmhal/boards/STM32F4DISC/mpconfigboard.h index b463c90ad3d64f0822727af424736bdd8be3443d..f713f9b756e04dd9542c9b1c28695864dd21dd44 100644 --- a/stmhal/boards/STM32F4DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F4DISC/mpconfigboard.h @@ -16,8 +16,8 @@ // USRSW is pulled low. Pressing the button makes the input go high. #define USRSW_PIN (pin_A0) -#define USRSW_PUPD (GPIO_PuPd_NOPULL) -#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising) +#define USRSW_PULL (GPIO_NOPULL) +#define USRSW_EXTI_MODE (GPIO_MODE_IT_RISING) #define USRSW_PRESSED (1) /* LED */ @@ -26,7 +26,7 @@ #define PYB_LED3 (pin_D13) // orange #define PYB_LED4 (pin_D15) // blue -#define PYB_OTYPE (GPIO_OType_PP) +#define PYB_OTYPE (GPIO_MODE_OUTPUT_PP) #define PYB_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask) #define PYB_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask) diff --git a/stmhal/boards/STM32F4DISC/stm32f4xx_hal_conf.h b/stmhal/boards/STM32F4DISC/stm32f4xx_hal_conf.h index 0953cfd2e28f72ea6a485508dcd0aff19aeeba7e..075b44dbcc88b82c87901839fa0b811637aef5ae 100644 --- a/stmhal/boards/STM32F4DISC/stm32f4xx_hal_conf.h +++ b/stmhal/boards/STM32F4DISC/stm32f4xx_hal_conf.h @@ -47,6 +47,7 @@ /* Exported constants --------------------------------------------------------*/ #define STM32F407xx +#define USE_USB_FS /* ########################## Module Selection ############################## */ /** diff --git a/stmhal/exti.c b/stmhal/exti.c new file mode 100644 index 0000000000000000000000000000000000000000..ec6d82e4fd937a90dcf11feaeb7d5a3c157252f8 --- /dev/null +++ b/stmhal/exti.c @@ -0,0 +1,346 @@ +#include <stdio.h> +#include <stddef.h> +#include <string.h> + +#include <stm32f4xx_hal.h> + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "runtime.h" +#include "nlr.h" + +#include "pin.h" +#include "exti.h" + +// Usage Model: +// +// There are a total of 22 interrupt lines. 16 of these can come from GPIO pins +// and the remaining 6 are from internal sources. +// +// For lines 0 thru 15, a given line can map to the corresponding line from an +// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and +// line 1 can map to Px1 where x is A, B, C, ... +// +// def callback(line): +// print("line =", line) +// +// # Configure the pin as a GPIO input. +// pin = pyb.Pin.board.X1 +// pyb.gpio_in(pin, pyb.PULL_UP) +// exti = pyb.Exti(pin, pyb.Exti.MODE_IRQ_FALLING, pyb.PULLUP, callback) +// +// Now every time a falling edge is seen on the X1 pin, the callback will be +// called. Caution: mechanical pushbuttons have "bounce" and pushing or +// releasing a switch will often generate multiple edges. +// See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed +// explanation, along with various techniques for debouncing. +// +// Trying to register 2 callbacks onto the same pin will throw an exception. +// +// If pin is passed as an integer, then it is assumed to map to one of the +// internal interrupt sources, and must be in the range 16 thru 22. +// +// All other pin objects go through the pin mapper to come up with one of the +// gpio pins. +// +// exti = pyb.Exti(pin, mode, pull, callback) +// +// Valid modes are pyb.Exti.MODE_IRQ_RISING, pyb.Exti.MODE_IRQ_FALLING, +// pyb.Exti.MODE_IRQ_RISING_FALLING, pyb.Exti.MODE_EVT_RISING, +// pyb.Exti.MODE_EVT_FALLING, and pyb.Exti.MODE_EVT_RISING_FALLING. +// +// Only the MODE_IRQ_xxx modes have been tested. The MODE_EVENT_xxx modes have +// something to do with sleep mode and he WFE instruction. +// +// Valid pull values are pyb.PULL_UP, pyb.PULL_DOWN, pyb.PULL_NONE. +// +// exti.line() will return the line number that pin was mapped to. +// exti.disable() can be use to disable the interrupt associated with a given +// exti object. This could be useful for debouncing. +// exti.enable() enables a disabled interrupt +// exti.swint() will allow the callback to be triggered from software. +// +// pyb.Exti.regs() will dump the values of the EXTI registers. +// +// There is also a C API, so that drivers which require EXTI interrupt lines +// can also use this code. See exti.h for the available functions and +// usrsw.h for an example of using this. + +#define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE) + +// Macro used to set/clear the bit corresponding to the line in the IMR/EMR +// register in an atomic fashion by using bitband addressing. +#define EXTI_MODE_BB(mode, line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + (mode)) * 32) + ((line) * 4))) + +#define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR) +#define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR) + +#define EXTI_SWIER_BB(line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + offsetof(EXTI_TypeDef, SWIER)) * 32) + ((line) * 4))) + +typedef struct { + mp_obj_base_t base; + mp_small_int_t line; +} exti_obj_t; + +typedef struct { + mp_obj_t callback_obj; + void *param; + uint32_t mode; +} exti_vector_t; + +static exti_vector_t exti_vector[EXTI_NUM_VECTORS]; + +#if !defined(ETH) +#define ETH_WKUP_IRQn 62 // The 405 doesn't have ETH, but we want a value to put in our table +#endif + +static const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = { + EXTI0_IRQn, EXTI1_IRQn, EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn, + EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, + EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, + EXTI15_10_IRQn, PVD_IRQn, RTC_Alarm_IRQn, OTG_FS_WKUP_IRQn, ETH_WKUP_IRQn, + OTG_HS_WKUP_IRQn, TAMP_STAMP_IRQn, RTC_WKUP_IRQn +}; + +// NOTE: param is for C callers. Python can use closure to get an object bound +// with the function. +uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t pull_obj, mp_obj_t callback_obj, void *param) { + const pin_obj_t *pin = NULL; + uint v_line; + + if (MP_OBJ_IS_INT(pin_obj)) { + // If an integer is passed in, then use it to identify lines 16 thru 22 + // We expect lines 0 thru 15 to be passed in as a pin, so that we can + // get both the port number and line number. + v_line = mp_obj_get_int(pin_obj); + if (v_line < 16) { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d < 16, use a Pin object", v_line)); + } + if (v_line >= EXTI_NUM_VECTORS) { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d >= max of %d", v_line, EXTI_NUM_VECTORS)); + } + } else { + pin = pin_map_user_obj(pin_obj); + v_line = pin->pin; + } + int mode = mp_obj_get_int(mode_obj); + if (mode != GPIO_MODE_IT_RISING && + mode != GPIO_MODE_IT_FALLING && + mode != GPIO_MODE_IT_RISING_FALLING && + mode != GPIO_MODE_EVT_RISING && + mode != GPIO_MODE_EVT_FALLING && + mode != GPIO_MODE_EVT_RISING_FALLING) { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid EXTI Mode: %d", mode)); + } + int pull = mp_obj_get_int(pull_obj); + if (pull != GPIO_NOPULL && + pull != GPIO_PULLUP && + pull != GPIO_PULLDOWN) { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid EXTI Pull: %d", pull)); + } + + exti_vector_t *v = &exti_vector[v_line]; + if (v->callback_obj != mp_const_none && callback_obj != mp_const_none) { + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "EXTI vector %d is already in use", v_line)); + } + + // We need to update callback and param atomically, so we disable the line + // before we update anything. + + exti_disable(v_line); + + v->callback_obj = callback_obj; + v->param = param; + v->mode = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000 + EXTI_Mode_Interrupt : EXTI_Mode_Event; + + if (v->callback_obj != mp_const_none) { + + GPIO_InitTypeDef exti; + exti.Pin = pin->pin_mask; + exti.Mode = mode; + exti.Pull = pull; + exti.Speed = GPIO_SPEED_FAST; + HAL_GPIO_Init(pin->gpio, &exti); + + // Calling HAL_GPIO_Init does an implicit exti_enable + + /* Enable and set NVIC Interrupt to the lowest priority */ + HAL_NVIC_SetPriority(nvic_irq_channel[v_line], 0x0F, 0x0F); + HAL_NVIC_EnableIRQ(nvic_irq_channel[v_line]); + } + return v_line; +} + +void exti_enable(uint line) { + if (line >= EXTI_NUM_VECTORS) { + return; + } + // Since manipulating IMR/EMR is a read-modify-write, and we want this to + // be atomic, we use the bit-band area to just affect the bit we're + // interested in. + EXTI_MODE_BB(exti_vector[line].mode, line) = 1; +} + +void exti_disable(uint line) { + if (line >= EXTI_NUM_VECTORS) { + return; + } + // Since manipulating IMR/EMR is a read-modify-write, and we want this to + // be atomic, we use the bit-band area to just affect the bit we're + // interested in. + EXTI_MODE_BB(EXTI_Mode_Interrupt, line) = 0; + EXTI_MODE_BB(EXTI_Mode_Event, line) = 0; +} + +void exti_swint(uint line) { + if (line >= EXTI_NUM_VECTORS) { + return; + } + EXTI->SWIER = (1 << line); +} + +static mp_obj_t exti_obj_line(mp_obj_t self_in) { + exti_obj_t *self = self_in; + return MP_OBJ_NEW_SMALL_INT(self->line); +} + +static mp_obj_t exti_obj_enable(mp_obj_t self_in) { + exti_obj_t *self = self_in; + exti_enable(self->line); + return mp_const_none; +} + +static mp_obj_t exti_obj_disable(mp_obj_t self_in) { + exti_obj_t *self = self_in; + exti_disable(self->line); + return mp_const_none; +} + +static mp_obj_t exti_obj_swint(mp_obj_t self_in) { + exti_obj_t *self = self_in; + exti_swint(self->line); + return mp_const_none; +} + +static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_line_obj, exti_obj_line); +static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_enable_obj, exti_obj_enable); +static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_disable_obj, exti_obj_disable); +static MP_DEFINE_CONST_FUN_OBJ_1(exti_obj_swint_obj, exti_obj_swint); + +static const mp_method_t exti_methods[] = { + { "line", &exti_obj_line_obj }, + { "enable", &exti_obj_enable_obj }, + { "disable", &exti_obj_disable_obj }, + { "swint", &exti_obj_swint_obj }, + { NULL, NULL }, +}; + +static mp_obj_t exti_regs(void) { + printf("EXTI_IMR %08lx\n", EXTI->IMR); + printf("EXTI_EMR %08lx\n", EXTI->EMR); + printf("EXTI_RTSR %08lx\n", EXTI->RTSR); + printf("EXTI_FTSR %08lx\n", EXTI->FTSR); + printf("EXTI_SWIER %08lx\n", EXTI->SWIER); + printf("EXTI_PR %08lx\n", EXTI->PR); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_0(exti_regs_obj, exti_regs); + +typedef struct { + const char *name; + uint val; +} exti_const_t; + +static const exti_const_t exti_const[] = { + { "MODE_IRQ_RISING", GPIO_MODE_IT_RISING }, + { "MODE_IRQ_FALLING", GPIO_MODE_IT_FALLING }, + { "MODE_IRQ_RISING_FALLING", GPIO_MODE_IT_RISING_FALLING }, + { "MODE_EVT_RISING", GPIO_MODE_EVT_RISING }, + { "MODE_EVT_FALLING", GPIO_MODE_EVT_FALLING }, + { "MODE_EVT_RISING_FALLING", GPIO_MODE_EVT_RISING_FALLING }, +}; +#define EXTI_NUM_CONST (sizeof(exti_const) / sizeof(exti_const[0])) + +static void exti_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) { + (void)self_in; + const char *attr = qstr_str(attr_qstr); + + if (strcmp(attr, "regs") == 0) { + dest[0] = (mp_obj_t)&exti_regs_obj; + return; + } + const exti_const_t *entry = &exti_const[0]; + for (; entry < &exti_const[EXTI_NUM_CONST]; entry++) { + if (strcmp(attr, entry->name) == 0) { + dest[0] = MP_OBJ_NEW_SMALL_INT(entry->val); + dest[1] = MP_OBJ_NULL; + return; + } + } +} + +// line_obj = pyb.Exti(pin, mode, trigger, callback) + +static mp_obj_t exti_call(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { + // type_in == exti_obj_type + + rt_check_nargs(n_args, 4, 4, n_kw, 0); + + exti_obj_t *self = m_new_obj(exti_obj_t); + self->base.type = type_in; + mp_obj_t line_obj = args[0]; + mp_obj_t mode_obj = args[1]; + mp_obj_t trigger_obj = args[2]; + mp_obj_t callback_obj = args[3]; + self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, NULL); + + return self; +} + +static void exti_meta_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { + (void) self_in; + print(env, "<Exti meta>"); +} + +static void exti_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { + exti_obj_t *self = self_in; + print(env, "<Exti line=%u>", self->line); +} + +static const mp_obj_type_t exti_meta_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_ExtiMeta, + .print = exti_meta_obj_print, + .call = exti_call, + .load_attr = exti_load_attr, +}; + +const mp_obj_type_t exti_obj_type = { + { &exti_meta_obj_type }, + .name = MP_QSTR_Exti, + .print = exti_obj_print, + .methods = exti_methods, +}; + +void exti_init(void) { + for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) { + v->callback_obj = mp_const_none; + v->param = NULL; + v->mode = EXTI_Mode_Interrupt; + } +} + +void Handle_EXTI_Irq(uint32_t line) { + if (__HAL_GPIO_EXTI_GET_FLAG(1 << line)) { + __HAL_GPIO_EXTI_CLEAR_FLAG(1 << line); + if (line < EXTI_NUM_VECTORS) { + exti_vector_t *v = &exti_vector[line]; + if (v->callback_obj != mp_const_none) { + rt_call_function_1(v->callback_obj, MP_OBJ_NEW_SMALL_INT(line)); + } + } + } +} diff --git a/stmhal/exti.h b/stmhal/exti.h new file mode 100644 index 0000000000000000000000000000000000000000..a6b50b26e84d773d113ec850c2376c8080c43414 --- /dev/null +++ b/stmhal/exti.h @@ -0,0 +1,38 @@ +// Vectors 0-15 are for regular pins +// Vectors 16-22 are for internal sources. +// +// Use the following constants for the internal sources: + +#define EXTI_PVD_OUTPUT (16) +#define EXTI_RTC_ALARM (17) +#define EXTI_USB_OTG_FS_WAKEUP (18) +#define EXTI_ETH_WAKEUP (19) +#define EXTI_USB_OTG_HS_WAKEUP (20) +#define EXTI_RTC_TIMESTAMP (21) +#define EXTI_RTC_WAKEUP (22) + +#define EXTI_NUM_VECTORS (23) + +#define EXTI_MODE_INTERRUPT (offsetof(EXTI_TypeDef, IMR)) +#define EXTI_MODE_EVENT (offsetof(EXTI_TypeDef, EMR)) + +#define EXTI_TRIGGER_RISING (offsetof(EXTI_TypeDef, RTSR)) +#define EXTI_TRIGGER_FALLING (offsetof(EXTI_TypeDef, FTSR)) +#define EXTI_TRIGGER_RISING_FALLING (EXTI_TRIGGER_RISING + EXTI_TRIGGER_FALLING) // just different from RISING or FALLING + +void exti_init(void); + +uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, void *param); + +void exti_enable(uint line); +void exti_disable(uint line); +void exti_swint(uint line); + +void Handle_EXTI_Irq(uint32_t line); + +typedef struct { + mp_obj_t callback; + void *param; +} exti_t; + +extern const mp_obj_type_t exti_obj_type; diff --git a/stmhal/gpio.c b/stmhal/gpio.c new file mode 100644 index 0000000000000000000000000000000000000000..be1249b37676f85d9978a42f010f8f6345bf8d17 --- /dev/null +++ b/stmhal/gpio.c @@ -0,0 +1,72 @@ +// This is a woefully inadequate set of bindings for GPIO control, and +// needs to be replaced with something much better. + +#include <stdio.h> +#include <string.h> +#include <stm32f4xx_hal.h> + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "misc.h" +#include "parse.h" +#include "obj.h" +#include "compile.h" +#include "runtime0.h" +#include "runtime.h" +#include "systick.h" +#include "gpio.h" +#include "pin.h" + +mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) { + const pin_obj_t *pin = pin_map_user_obj(args[0]); + if (n_args == 1) { + // get pin + return MP_OBJ_NEW_SMALL_INT(HAL_GPIO_ReadPin(pin->gpio, pin->pin_mask)); + } + + // set pin + HAL_GPIO_WritePin(pin->gpio, pin->pin_mask, rt_is_true(args[1])); + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio); + +mp_obj_t pyb_gpio_input(uint n_args, mp_obj_t *args) { + const pin_obj_t *pin = pin_map_user_obj(args[0]); + + uint32_t pull = GPIO_NOPULL; + if (n_args > 1) { + pull = mp_obj_get_int(args[1]); + } + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.Pin = pin->pin_mask; + GPIO_InitStructure.Mode = GPIO_MODE_INPUT; + GPIO_InitStructure.Pull = pull; + HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure); + + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_input_obj, 1, 2, pyb_gpio_input); + +mp_obj_t pyb_gpio_output(uint n_args, mp_obj_t *args) { + const pin_obj_t *pin = pin_map_user_obj(args[0]); + + uint32_t mode = GPIO_MODE_OUTPUT_PP; + if (n_args > 1) { + mode = mp_obj_get_int(args[1]) == GPIO_MODE_OUTPUT_OD ? + GPIO_MODE_OUTPUT_OD: GPIO_MODE_OUTPUT_PP; + } + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.Pin = pin->pin_mask; + GPIO_InitStructure.Mode = mode; + GPIO_InitStructure.Speed = GPIO_SPEED_FAST; + GPIO_InitStructure.Pull = GPIO_NOPULL; + HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure); + + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_output_obj, 1, 2, pyb_gpio_output); diff --git a/stmhal/gpio.h b/stmhal/gpio.h new file mode 100644 index 0000000000000000000000000000000000000000..f43adfdac0cd27cb69d522b5b7554cfdef960b36 --- /dev/null +++ b/stmhal/gpio.h @@ -0,0 +1,7 @@ +mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args); +mp_obj_t pyb_gpio_output(uint n_args, mp_obj_t *args); +mp_obj_t pyb_gpio_input(uint n_args, mp_obj_t *args); + +MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_obj); +MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_input_obj); +MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_output_obj); diff --git a/stmhal/led.c b/stmhal/led.c index a643ce84fd749089a5543ac2b4d27454d1cc075a..677c151b335ac420ccdccb0379dfdece4b4d17bc 100644 --- a/stmhal/led.c +++ b/stmhal/led.c @@ -29,7 +29,7 @@ void led_init(void) { /* Configure I/O speed, mode, output type and pull */ GPIO_InitStructure.Speed = GPIO_SPEED_LOW; - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStructure.Mode = PYB_OTYPE; GPIO_InitStructure.Pull = GPIO_NOPULL; /* Turn off LEDs and initialize */ @@ -45,6 +45,7 @@ void led_state(pyb_led_t led, int state) { return; } const pin_obj_t *led_pin = gLed[led - 1]; +//printf("led_state(%d,%d)\n", led, state); if (state == 0) { // turn LED off PYB_LED_OFF(led_pin); @@ -72,7 +73,6 @@ void led_toggle(pyb_led_t led) { } } -#if 0 /******************************************************************************/ /* Micro Python bindings */ @@ -130,4 +130,3 @@ static mp_obj_t pyb_Led(mp_obj_t led_id) { } MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led); -#endif diff --git a/stmhal/led.h b/stmhal/led.h index 9e9db16ac3101d02549d83e2940b28df6c992b40..44c68d4ebe0cb9241bf17ba66dd209e5184dd862 100644 --- a/stmhal/led.h +++ b/stmhal/led.h @@ -20,6 +20,4 @@ void led_init(void); void led_state(pyb_led_t led, int state); void led_toggle(pyb_led_t led); -#if 0 MP_DECLARE_CONST_FUN_OBJ(pyb_Led_obj); -#endif diff --git a/stmhal/main.c b/stmhal/main.c index 865b4fa59c2b265b5efe6c4e7c0c05464e7a1be8..2c82cf030b8ebe3d8000f120cdb4b13f0807090c 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -21,7 +21,6 @@ #include "misc.h" #include "systick.h" #include "pendsv.h" -#include "led.h" #include "usart.h" #include "mpconfig.h" #include "qstr.h" @@ -38,6 +37,9 @@ #include "gccollect.h" #include "pyexec.h" #include "pybmodule.h" +#include "led.h" +#include "exti.h" +#include "usrsw.h" #include "usb.h" #if 0 #include "ff.h" @@ -49,11 +51,9 @@ #include "accel.h" #include "timer.h" #include "pybwlan.h" -#include "usrsw.h" #include "rtc.h" #include "file.h" #include "pin.h" -#include "exti.h" #endif void SystemClock_Config(void); @@ -274,7 +274,8 @@ int main(void) { #if 0 pyb_led_t led = 1; - while (1) { + for (int i = 0; i < 24; i++) { + //while (1) { led_state(led, 1); usart_tx_strn_cooked(pyb_usart_global_debug, "on\n", 3); HAL_Delay(100); @@ -290,7 +291,6 @@ int main(void) { led = (led % 4) + 1; } - __fatal_error("done"); #endif int first_soft_reset = true; @@ -309,13 +309,13 @@ soft_reset: def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib); sys_path = mp_obj_new_list(3, def_path); -#if 0 exti_init(); #if MICROPY_HW_HAS_SWITCH switch_init(); #endif +#if 0 #if MICROPY_HW_HAS_LCD // LCD init (just creates class, init hardware by calling LCD()) lcd_init(); @@ -473,7 +473,7 @@ soft_reset: #endif #endif -#ifdef USE_HOST_MODE +#if defined(USE_HOST_MODE) // USB host pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 3f48c43f04f169c18c037df25d39faf235018ca4..164bc771df4a39eadf8f481ae739e98306a85832 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -38,14 +38,14 @@ typedef const void *machine_const_ptr_t; // must be of pointer size #define free gc_free #define realloc gc_realloc -// board specific definitions +#define STM32F40_41xxx +#define USE_STDPERIPH_DRIVER +#define USE_DEVICE_MODE +//#define USE_HOST_MODE +// board specific definitions #include "mpconfigboard.h" -#define STM32F40_41xxx -#define USE_STDPERIPH_DRIVER #if !defined(HSE_VALUE) #define HSE_VALUE (8000000) #endif -#define USE_DEVICE_MODE -//#define USE_HOST_MODE diff --git a/stmhal/pin_map.c b/stmhal/pin_map.c new file mode 100644 index 0000000000000000000000000000000000000000..c546231c80ec2b1e5bb6bffbe0998b530afa6b08 --- /dev/null +++ b/stmhal/pin_map.c @@ -0,0 +1,227 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <stm32f4xx_hal.h> + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "runtime.h" +#include "nlr.h" +#include "map.h" + +#include "pin.h" + +// Usage Model: +// +// All Board Pins are predefined as pyb.Pin.board.Name +// +// x1_pin = pyb.Pin.board.X1 +// +// g = pyb.gpio(pyb.Pin.board.X1, 0) +// +// CPU pins which correspond to the board pins are available +// as pyb.cpu.Name. For the CPU pins, the names are the port letter +// followed by the pin number. On the PYBOARD4, pyb.Pin.board.X1 and +// pyb.Pin.cpu.B6 are the same pin. +// +// You can also use strings: +// +// g = pyb.gpio('X1', 0) +// +// Users can add their own names: +// +// pyb.Pin("LeftMotorDir", pyb.Pin.cpu.C12) +// g = pyb.gpio("LeftMotorDir", 0) +// +// and can query mappings +// +// pin = pyb.Pin("LeftMotorDir"); +// +// Users can also add their own mapping function: +// +// def MyMapper(pin_name): +// if pin_name == "LeftMotorDir": +// return pyb.Pin.cpu.A0 +// +// pyb.Pin.mapper(MyMapper) +// +// So, if you were to call: pyb.gpio("LeftMotorDir", 0) +// then "LeftMotorDir" is passed directly to the mapper function. +// +// To summarize, the following order determines how things get mapped into +// an ordinal pin number: +// +// 1 - Directly specify a pin object +// 2 - User supplied mapping function +// 3 - User supplied mapping (object must be usable as a dictionary key) +// 4 - Supply a string which matches a board pin +// 5 - Supply a string which matches a CPU port/pin +// +// You can set pyb.Pin.debug(True) to get some debug information about +// how a particular object gets mapped to a pin. + +static void pin_map_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { + (void)self_in; + print(env, "<PinMap>"); +} + +static mp_obj_t pin_map_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { + pin_map_obj_t *self = self_in; + rt_check_nargs(n_args, 1, 2, n_kw, false); + + if (n_args > 1) { + if (!self->map_dict) { + self->map_dict = mp_obj_new_dict(1); + } + mp_obj_dict_store(self->map_dict, args[0], args[1]); + return mp_const_none; + } + + // Run an argument through the mapper and return the result. + return (mp_obj_t)pin_map_user_obj(args[0]); +} + +static mp_obj_t pin_map_obj_mapper(uint n_args, mp_obj_t *args) { + pin_map_obj_t *self = args[0]; + if (n_args > 1) { + self->mapper = args[1]; + return mp_const_none; + } + return self->mapper; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_map_obj_mapper_obj, 1, 2, pin_map_obj_mapper); + +static mp_obj_t pin_map_obj_debug(uint n_args, mp_obj_t *args) { + pin_map_obj_t *self = args[0]; + if (n_args > 1) { + self->debug = rt_is_true(args[1]); + return mp_const_none; + } + return MP_BOOL(self->debug); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_map_obj_debug_obj, 1, 2, pin_map_obj_debug); + +static void pin_map_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) { + (void)self_in; + const char *attr = qstr_str(attr_qstr); + + if (strcmp(attr, "mapper") == 0) { + dest[0] = (mp_obj_t)&pin_map_obj_mapper_obj; + dest[1] = self_in; + } + if (strcmp(attr, "debug") == 0) { + dest[0] = (mp_obj_t)&pin_map_obj_debug_obj; + dest[1] = self_in; + } + if (strcmp(attr, pin_board_pins_obj.name) == 0) { + dest[0] = (mp_obj_t)&pin_board_pins_obj; + dest[1] = MP_OBJ_NULL; + } + if (strcmp(attr, pin_cpu_pins_obj.name) == 0) { + dest[0] = (mp_obj_t)&pin_cpu_pins_obj; + dest[1] = MP_OBJ_NULL; + } +} + +static const mp_obj_type_t pin_map_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_PinMap, + .print = pin_map_obj_print, + .call = pin_map_call, + .load_attr = pin_map_load_attr, +}; + +static const pin_map_obj_t pin_map_obj_init = { + { &pin_map_obj_type }, + .mapper = MP_OBJ_NULL, + .map_dict = MP_OBJ_NULL, + .debug = false, +}; + +pin_map_obj_t pin_map_obj; + +void pin_map_init(void) { + pin_map_obj = pin_map_obj_init; +} + +// C API used to convert a user-supplied pin name into an ordinal pin number. +const pin_obj_t *pin_map_user_obj(mp_obj_t user_obj) { + const pin_obj_t *pin_obj; + + // If a pin was provided, then use it + if (MP_OBJ_IS_TYPE(user_obj, &pin_obj_type)) { + pin_obj = user_obj; + if (pin_map_obj.debug) { + printf("Pin map passed pin "); + mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); + printf("\n"); + } + return pin_obj; + } + + if (pin_map_obj.mapper) { + pin_obj = rt_call_function_1(pin_map_obj.mapper, user_obj); + if (pin_obj != mp_const_none) { + if (!MP_OBJ_IS_TYPE(pin_obj, &pin_obj_type)) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin.mapper didn't return a Pin object")); + } + if (pin_map_obj.debug) { + printf("Pin.mapper maps "); + mp_obj_print(user_obj, PRINT_REPR); + printf(" to "); + mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); + printf("\n"); + } + return pin_obj; + } + // The pin mapping function returned mp_const_none, fall through to + // other lookup methods. + } + + if (pin_map_obj.map_dict) { + mp_map_t *pin_map_map = mp_obj_dict_get_map(pin_map_obj.map_dict); + mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP); + if (elem != NULL && elem->value != NULL) { + pin_obj = elem->value; + if (pin_map_obj.debug) { + printf("Pin.map_dict maps "); + mp_obj_print(user_obj, PRINT_REPR); + printf(" to "); + mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); + printf("\n"); + } + return pin_obj; + } + } + + // See if the pin name matches a board pin + const char *pin_name = mp_obj_str_get_str(user_obj); + pin_obj = pin_find_named_pin(pin_board_pins, pin_name); + if (pin_obj) { + if (pin_map_obj.debug) { + printf("Pin.board maps "); + mp_obj_print(user_obj, PRINT_REPR); + printf(" to "); + mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); + printf("\n"); + } + return pin_obj; + } + + // See if the pin name matches a cpu pin + pin_obj = pin_find_named_pin(pin_cpu_pins, pin_name); + if (pin_obj) { + if (pin_map_obj.debug) { + printf("Pin.cpu maps "); + mp_obj_print(user_obj, PRINT_REPR); + printf(" to "); + mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); + printf("\n"); + } + return pin_obj; + } + + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin '%s' not a valid pin identifier", pin_name)); +} diff --git a/stmhal/pin_named_pins.c b/stmhal/pin_named_pins.c new file mode 100644 index 0000000000000000000000000000000000000000..48906ad8007c8aa4860f04c17191756144089aa1 --- /dev/null +++ b/stmhal/pin_named_pins.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <stm32f4xx_hal.h> + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "runtime.h" + +#include "pin.h" + +static void pin_named_pins_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { + pin_named_pins_obj_t *self = self_in; + print(env, "<Pin.%s>", self->name); +} + +static void pin_named_pins_obj_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) { + pin_named_pins_obj_t *self = self_in; + const char *attr = qstr_str(attr_qstr); + const pin_obj_t *pin = pin_find_named_pin(self->named_pins, attr); + if (pin) { + dest[0] = (mp_obj_t)pin; + dest[1] = MP_OBJ_NULL; + } +} + +static const mp_obj_type_t pin_named_pins_obj_type = { + { &mp_type_type }, + .name = MP_QSTR_PinNamed, + .print = pin_named_pins_obj_print, + .load_attr = pin_named_pins_obj_load_attr, +}; + +const pin_named_pins_obj_t pin_board_pins_obj = { + { &pin_named_pins_obj_type }, + .name = "board", + .named_pins = pin_board_pins, +}; + +const pin_named_pins_obj_t pin_cpu_pins_obj = { + { &pin_named_pins_obj_type }, + .name = "cpu", + .named_pins = pin_cpu_pins, +}; + +const pin_obj_t *pin_find_named_pin(const pin_named_pin_t *named_pins, const char *name) { + const pin_named_pin_t *named_pin = named_pins; + while (named_pin->name) { + if (!strcmp(name, named_pin->name)) { + return named_pin->pin; + } + named_pin++; + } + return NULL; +} + +const pin_af_obj_t *pin_find_af(const pin_obj_t *pin, uint8_t fn, uint8_t unit, uint8_t type) { + const pin_af_obj_t *af = pin->af; + for (int i = 0; i < pin->num_af; i++, af++) { + if (af->fn == fn && af->unit == unit && af->type == type) { + return af; + } + } + return NULL; +} diff --git a/stmhal/pybmodule.c b/stmhal/pybmodule.c index b0395143941421d08f6d556bc4718bbce53769d2..0d2b914ded3522a7a0ab6aeb4954c1199cedd86f 100644 --- a/stmhal/pybmodule.c +++ b/stmhal/pybmodule.c @@ -15,22 +15,22 @@ #include "gccollect.h" #include "systick.h" #include "pyexec.h" +#include "led.h" +#include "gpio.h" +#include "pin.h" +#include "exti.h" +#include "usrsw.h" #if 0 #include "rtc.h" #include "servo.h" #include "storage.h" #include "usb.h" -#include "usrsw.h" #include "sdcard.h" #include "accel.h" -#include "led.h" #include "i2c.h" #include "usart.h" #include "adc.h" #include "audio.h" -#include "pin.h" -#include "gpio.h" -#include "exti.h" #endif #include "pybmodule.h" @@ -252,11 +252,13 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_servo), (mp_obj_t)&pyb_servo_set_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_Servo), (mp_obj_t)&pyb_Servo_obj }, #endif +#endif #if MICROPY_HW_HAS_SWITCH { MP_OBJ_NEW_QSTR(MP_QSTR_switch), (mp_obj_t)&pyb_switch_obj }, #endif +#if 0 #if MICROPY_HW_HAS_SDCARD { MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj }, #endif @@ -268,8 +270,9 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { #endif { MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj }, - +#endif { MP_OBJ_NEW_QSTR(MP_QSTR_Led), (mp_obj_t)&pyb_Led_obj }, +#if 0 { MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_I2C_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_Usart), (mp_obj_t)&pyb_Usart_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ADC_all), (mp_obj_t)&pyb_ADC_all_obj }, @@ -277,6 +280,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { #if MICROPY_HW_ENABLE_AUDIO { MP_OBJ_NEW_QSTR(MP_QSTR_Audio), (mp_obj_t)&pyb_Audio_obj }, +#endif #endif // pin mapper @@ -286,15 +290,14 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_gpio), (mp_obj_t)&pyb_gpio_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gpio_in), (mp_obj_t)&pyb_gpio_input_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gpio_out), (mp_obj_t)&pyb_gpio_output_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_NOPULL) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_UP) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_DOWN) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_PUSH_PULL), MP_OBJ_NEW_SMALL_INT(GPIO_OType_PP) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN_DRAIN), MP_OBJ_NEW_SMALL_INT(GPIO_OType_OD) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_NOPULL) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULLUP) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PULLDOWN) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PUSH_PULL), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT_PP) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN_DRAIN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT_OD) }, // EXTI bindings { MP_OBJ_NEW_QSTR(MP_QSTR_Exti), (mp_obj_t)&exti_obj_type }, -#endif }; STATIC const mp_map_t pyb_module_globals = { diff --git a/stmhal/stm32f4xx_it.c b/stmhal/stm32f4xx_it.c index 4be78deddb7c3485a316d0027ed0f47e60bc7d6b..89764f07d63d7b4d48df2a83a9d9ee4098903f99 100644 --- a/stmhal/stm32f4xx_it.c +++ b/stmhal/stm32f4xx_it.c @@ -43,6 +43,12 @@ #include "stm32f4xx_it.h" #include "stm32f4xx_hal.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "exti.h" + /** @addtogroup STM32F4xx_HAL_Examples * @{ */ @@ -182,25 +188,28 @@ void SysTick_Handler(void) * @param None * @retval None */ -#ifdef USE_USB_FS -void OTG_FS_IRQHandler(void) +#if defined(USE_USB_FS) +#define OTG_XX_IRQHandler OTG_FS_IRQHandler +#define OTG_XX_WKUP_IRQHandler OTG_FS_WKUP_IRQHandler #elif defined(USE_USB_HS) -void OTG_HS_IRQHandler(void) +#define OTG_XX_IRQHandler OTG_HS_IRQHandler +#define OTG_XX_WKUP_IRQHandler OTG_HS_WKUP_IRQHandler #endif + +#if defined(OTG_XX_IRQHandler) +void OTG_XX_IRQHandler(void) { HAL_PCD_IRQHandler(&hpcd); } +#endif /** * @brief This function handles USB OTG FS or HS Wakeup IRQ Handler. * @param None * @retval None */ -#ifdef USE_USB_FS -void OTG_FS_WKUP_IRQHandler(void) -#elif defined(USE_USB_HS) -void OTG_HS_WKUP_IRQHandler(void) -#endif +#if defined(OTG_XX_WKUP_IRQHandler) +void OTG_XX_WKUP_IRQHandler(void) { if((&hpcd)->Init.low_power_enable) @@ -242,6 +251,7 @@ void OTG_HS_WKUP_IRQHandler(void) #endif } +#endif /** * @brief This function handles PPP interrupt request. @@ -252,13 +262,90 @@ void OTG_HS_WKUP_IRQHandler(void) { }*/ - -/** - * @} - */ - /** - * @} + * @brief These functions handle the EXTI interrupt requests. + * @param None + * @retval None */ +void EXTI0_IRQHandler(void) { + Handle_EXTI_Irq(0); +} + +void EXTI1_IRQHandler(void) { + Handle_EXTI_Irq(1); +} + +void EXTI2_IRQHandler(void) { + Handle_EXTI_Irq(2); +} + +void EXTI3_IRQHandler(void) { + Handle_EXTI_Irq(3); +} + +void EXTI4_IRQHandler(void) { + Handle_EXTI_Irq(4); +} + +void EXTI9_5_IRQHandler(void) { + Handle_EXTI_Irq(5); + Handle_EXTI_Irq(6); + Handle_EXTI_Irq(7); + Handle_EXTI_Irq(8); + Handle_EXTI_Irq(9); +} + +void EXTI15_10_IRQHandler(void) { + Handle_EXTI_Irq(10); + Handle_EXTI_Irq(11); + Handle_EXTI_Irq(12); + Handle_EXTI_Irq(13); + Handle_EXTI_Irq(14); + Handle_EXTI_Irq(15); + +#if 0 + // for CC3000 support, needs to be re-written to use new EXTI code + if (EXTI_GetITStatus(EXTI_Line14) != RESET) { + led_toggle(PYB_LED_G2); + /* these are needed for CC3000 support + extern void SpiIntGPIOHandler(void); + extern uint32_t exti14_enabled; + extern uint32_t exti14_missed; + //printf("-> EXTI14 en=%lu miss=%lu\n", exti14_enabled, exti14_missed); + if (exti14_enabled) { + exti14_missed = 0; + SpiIntGPIOHandler(); // CC3000 interrupt + } else { + exti14_missed = 1; + } + */ + EXTI_ClearITPendingBit(EXTI_Line14); + //printf("<- EXTI14 done\n"); + } +#endif +} + +void PVD_IRQHandler(void) { + Handle_EXTI_Irq(EXTI_PVD_OUTPUT); +} + +void RTC_Alarm_IRQHandler(void) { + Handle_EXTI_Irq(EXTI_RTC_ALARM); +} + + +#if defined(ETH) // The 407 has ETH, the 405 doesn't +void ETH_WKUP_IRQHandler(void) { + Handle_EXTI_Irq(EXTI_ETH_WAKEUP); +} +#endif + +void TAMP_STAMP_IRQHandler(void) { + Handle_EXTI_Irq(EXTI_RTC_TIMESTAMP); +} + +void RTC_WKUP_IRQHandler(void) { + Handle_EXTI_Irq(EXTI_RTC_WAKEUP); +} /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmhal/system_stm32f4xx.c b/stmhal/system_stm32f4xx.c index 4e5490db563430ae89296d841fb42e77eec58699..f5a97c0ef3c9b7f1770192a9a7a0707180eab60e 100644 --- a/stmhal/system_stm32f4xx.c +++ b/stmhal/system_stm32f4xx.c @@ -267,8 +267,8 @@ void SystemCoreClockUpdate(void) * AHB Prescaler = 1 * APB1 Prescaler = 4 * APB2 Prescaler = 2 - * HSE Frequency(Hz) = 8000000 - * PLL_M = 8 + * HSE Frequency(Hz) = HSE_VALUE + * PLL_M = HSE_VALUE/1000000 * PLL_N = 336 * PLL_P = 2 * PLL_Q = 7 @@ -296,7 +296,7 @@ void SystemClock_Config(void) RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLM = 8; + RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; diff --git a/stmhal/usbd_conf.c b/stmhal/usbd_conf.c index 8594db13af9d8372c5b2ea022c52263b2632189f..4a4414afe268c63f2c9c9dbe475744a60ca76f79 100644 --- a/stmhal/usbd_conf.c +++ b/stmhal/usbd_conf.c @@ -29,6 +29,10 @@ #include "stm32f4xx_hal.h" #include "usbd_core.h" +#if !defined(USE_USB_OTG_ID) +#define USE_USB_OTG_ID 1 +#endif + /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ @@ -68,11 +72,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#if USE_USB_OTG_ID GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#endif /* Enable USB FS Clocks */ __USB_OTG_FS_CLK_ENABLE(); diff --git a/stmhal/usrsw.c b/stmhal/usrsw.c new file mode 100644 index 0000000000000000000000000000000000000000..69a03b2cc33435d94ed5017e48c13eb09030b9fb --- /dev/null +++ b/stmhal/usrsw.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stm32f4xx_hal.h> + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "runtime.h" +#include "usrsw.h" + +#include "exti.h" +#include "gpio.h" +#include "pin.h" +#include "build/pins.h" + +// Usage Model: +// +// pyb.switch() returns True if the user switch is pressed, False otherwise. +// +// pyb.switch(callback) will register a callback to be called when the user +// switch is pressed. +// +// pyb.switch(None) will remove the callback. +// +// Example: +// +// def switch_pressed(): +// print("User Switch pressed") +// +// pyb.switch(switch_pressed) + +static mp_obj_t switch_user_callback_obj; + +static mp_obj_t switch_callback(mp_obj_t line) { + if (switch_user_callback_obj != mp_const_none) { + rt_call_function_0(switch_user_callback_obj); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback); + +void switch_init(void) { + switch_user_callback_obj = mp_const_none; + exti_register((mp_obj_t)&USRSW_PIN, + MP_OBJ_NEW_SMALL_INT(USRSW_EXTI_MODE), + MP_OBJ_NEW_SMALL_INT(USRSW_PULL), + (mp_obj_t)&switch_callback_obj, + NULL); +} + +int switch_get(void) { + int val = ((USRSW_PIN.gpio->IDR & USRSW_PIN.pin_mask) != 0); + return val == USRSW_PRESSED; +} + +/******************************************************************************/ +/* Micro Python bindings */ + +static mp_obj_t pyb_switch(uint n_args, mp_obj_t *args) { + if (n_args == 0) { + return switch_get() ? mp_const_true : mp_const_false; + } + switch_user_callback_obj = args[0]; + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_switch_obj, 0, 1, pyb_switch); diff --git a/stmhal/usrsw.h b/stmhal/usrsw.h new file mode 100644 index 0000000000000000000000000000000000000000..2da8f069bacc325b4952596eb1dc40a34c6c19c0 --- /dev/null +++ b/stmhal/usrsw.h @@ -0,0 +1,4 @@ +void switch_init(void); +int switch_get(void); + +MP_DECLARE_CONST_FUN_OBJ(pyb_switch_obj);