summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xports/stm/Makefile3
-rw-r--r--ports/stm/boards/feather_stm32f405_express/mpconfigboard.h2
-rw-r--r--ports/stm/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h68
-rw-r--r--ports/stm/boards/stm32f4_discovery/mpconfigboard.h2
-rw-r--r--ports/stm/boards/stm32f4_discovery/stm32f4xx_hal_conf.h66
-rw-r--r--ports/stm/common-hal/busio/UART.c1
-rw-r--r--ports/stm/common-hal/displayio/ParallelBus.c2
-rw-r--r--ports/stm/common-hal/microcontroller/__init__.c24
-rw-r--r--ports/stm/common-hal/neopixel_write/__init__.c25
-rw-r--r--ports/stm/common-hal/pulseio/PulseIn.c10
-rw-r--r--ports/stm/common-hal/pulseio/PulseOut.c5
-rw-r--r--ports/stm/mphalport.c15
-rw-r--r--ports/stm/peripherals/stm32f4/stm32f405xx/clocks.c4
-rw-r--r--ports/stm/peripherals/stm32f4/stm32f412zx/clocks.c2
-rw-r--r--ports/stm/supervisor/internal_flash.c4
-rw-r--r--ports/stm/supervisor/port.c134
-rw-r--r--ports/stm/supervisor/usb.c7
-rw-r--r--ports/stm/tick.c81
-rw-r--r--ports/stm/tick.h44
-rw-r--r--supervisor/shared/flash.c6
-rw-r--r--supervisor/shared/tick.c4
21 files changed, 238 insertions, 271 deletions
diff --git a/ports/stm/Makefile b/ports/stm/Makefile
index f8c05fd80..f53fc7b93 100755
--- a/ports/stm/Makefile
+++ b/ports/stm/Makefile
@@ -172,6 +172,8 @@ SRC_STM32 = \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c \
+ st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c \
+ st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
st_driver/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
@@ -193,7 +195,6 @@ SRC_C += \
background.c \
fatfs_port.c \
mphalport.c \
- tick.c \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
peripherals/stm32f4/$(MCU_SUB_VARIANT)/pins.c \
diff --git a/ports/stm/boards/feather_stm32f405_express/mpconfigboard.h b/ports/stm/boards/feather_stm32f405_express/mpconfigboard.h
index 6600fcf67..0e66a32bd 100644
--- a/ports/stm/boards/feather_stm32f405_express/mpconfigboard.h
+++ b/ports/stm/boards/feather_stm32f405_express/mpconfigboard.h
@@ -36,6 +36,8 @@
#define BOARD_OSC_DIV (12)
+#define BOARD_RTC_CLOCK RCC_RTCCLKSOURCE_LSE
+
// On-board flash
#define SPI_FLASH_MOSI_PIN (&pin_PB05)
#define SPI_FLASH_MISO_PIN (&pin_PB04)
diff --git a/ports/stm/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h b/ports/stm/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h
index 8031403a9..0d5612cde 100644
--- a/ports/stm/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h
+++ b/ports/stm/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h
@@ -2,7 +2,7 @@
******************************************************************************
* @file stm32f4xx_hal_conf_template.h
* @author MCD Application Team
- * @brief HAL configuration template file.
+ * @brief HAL configuration template file.
* This file should be copied to the application folder and renamed
* to stm32f4xx_hal_conf.h.
******************************************************************************
@@ -17,7 +17,7 @@
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
- */
+ */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_HAL_CONF_H
@@ -32,9 +32,9 @@
/* ########################## Module Selection ############################## */
/**
- * @brief This is the list of modules to be used in the HAL driver
+ * @brief This is the list of modules to be used in the HAL driver
*/
-#define HAL_MODULE_ENABLED
+#define HAL_MODULE_ENABLED
#define HAL_ADC_MODULE_ENABLED
/* #define HAL_CRYP_MODULE_ENABLED */
@@ -56,13 +56,13 @@
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_LTDC_MODULE_ENABLED */
#define HAL_RNG_MODULE_ENABLED
-/* #define HAL_RTC_MODULE_ENABLED */
+#define HAL_RTC_MODULE_ENABLED
/* #define HAL_SAI_MODULE_ENABLED */
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
#define HAL_SPI_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
-#define HAL_UART_MODULE_ENABLED
+#define HAL_UART_MODULE_ENABLED
#define HAL_USART_MODULE_ENABLED
/* #define HAL_IRDA_MODULE_ENABLED */
/* #define HAL_SMARTCARD_MODULE_ENABLED */
@@ -90,9 +90,9 @@
/**
* @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).
+ * (when HSE is used as system clock source, directly or through the PLL).
*/
-#if !defined (HSE_VALUE)
+#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)12000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
@@ -103,7 +103,7 @@
/**
* @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).
+ * (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*/
@@ -112,8 +112,8 @@
/**
* @brief Internal Low Speed oscillator (LSI) value.
*/
-#if !defined (LSI_VALUE)
- #define LSI_VALUE ((uint32_t)40000)
+#if !defined (LSI_VALUE)
+ #define LSI_VALUE ((uint32_t)40000)
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations
in voltage and temperature. */
@@ -131,8 +131,8 @@
/**
* @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.
+ * 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)12288000U) /*!< Value of the External audio frequency in Hz*/
@@ -145,16 +145,16 @@
/**
* @brief This is the HAL system configuration section
*/
-#define VDD_VALUE ((uint32_t)3300U) /*!< Value of VDD in mv */
-#define TICK_INT_PRIORITY ((uint32_t)0U) /*!< tick interrupt priority */
-#define USE_RTOS 0U
+#define VDD_VALUE ((uint32_t)3300U) /*!< Value of VDD in mv */
+#define TICK_INT_PRIORITY ((uint32_t)0U) /*!< tick interrupt priority */
+#define USE_RTOS 0U
#define PREFETCH_ENABLE 1U
#define INSTRUCTION_CACHE_ENABLE 1U
#define DATA_CACHE_ENABLE 1U
/* ########################## Assert Selection ############################## */
/**
- * @brief Uncomment the line below to expanse the "assert_param" macro in the
+ * @brief Uncomment the line below to expanse the "assert_param" macro in the
* HAL drivers code
*/
/* #define USE_FULL_ASSERT 1U */
@@ -171,7 +171,7 @@
#define MAC_ADDR4 0U
#define MAC_ADDR5 0U
-/* Definition of the Ethernet driver buffers size and count */
+/* 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)4U) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
@@ -179,9 +179,9 @@
/* Section 2: PHY configuration section */
-/* DP83848_PHY_ADDRESS Address*/
+/* DP83848_PHY_ADDRESS Address*/
#define DP83848_PHY_ADDRESS 0x01U
-/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
+/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
#define PHY_RESET_DELAY ((uint32_t)0x000000FFU)
/* PHY Configuration delay */
#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFFU)
@@ -193,7 +193,7 @@
#define PHY_BCR ((uint16_t)0x0000U) /*!< Transceiver Basic Control Register */
#define PHY_BSR ((uint16_t)0x0001U) /*!< Transceiver Basic Status Register */
-
+
#define PHY_RESET ((uint16_t)0x8000U) /*!< PHY Reset */
#define PHY_LOOPBACK ((uint16_t)0x4000U) /*!< Select loop-back mode */
#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100U) /*!< Set the full-duplex mode at 100 Mb/s */
@@ -208,7 +208,7 @@
#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020U) /*!< Auto-Negotiation process completed */
#define PHY_LINKED_STATUS ((uint16_t)0x0004U) /*!< Valid link established */
#define PHY_JABBER_DETECTION ((uint16_t)0x0002U) /*!< Jabber condition detected */
-
+
/* Section 4: Extended PHY Registers */
#define PHY_SR ((uint16_t)0x10U) /*!< PHY status register Offset */
@@ -226,7 +226,7 @@
/* Includes ------------------------------------------------------------------*/
/**
- * @brief Include module's header file
+ * @brief Include module's header file
*/
#ifdef HAL_RCC_MODULE_ENABLED
@@ -244,7 +244,7 @@
#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 */
@@ -262,7 +262,7 @@
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_CRYP_MODULE_ENABLED
- #include "stm32f4xx_hal_cryp.h"
+ #include "stm32f4xx_hal_cryp.h"
#endif /* HAL_CRYP_MODULE_ENABLED */
#ifdef HAL_DMA2D_MODULE_ENABLED
@@ -284,7 +284,7 @@
#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 */
@@ -299,11 +299,11 @@
#ifdef HAL_PCCARD_MODULE_ENABLED
#include "stm32f4xx_hal_pccard.h"
-#endif /* HAL_PCCARD_MODULE_ENABLED */
-
+#endif /* HAL_PCCARD_MODULE_ENABLED */
+
#ifdef HAL_SDRAM_MODULE_ENABLED
#include "stm32f4xx_hal_sdram.h"
-#endif /* HAL_SDRAM_MODULE_ENABLED */
+#endif /* HAL_SDRAM_MODULE_ENABLED */
#ifdef HAL_HASH_MODULE_ENABLED
#include "stm32f4xx_hal_hash.h"
@@ -384,7 +384,7 @@
#ifdef HAL_HCD_MODULE_ENABLED
#include "stm32f4xx_hal_hcd.h"
#endif /* HAL_HCD_MODULE_ENABLED */
-
+
#ifdef HAL_DSI_MODULE_ENABLED
#include "stm32f4xx_hal_dsi.h"
#endif /* HAL_DSI_MODULE_ENABLED */
@@ -412,14 +412,14 @@
#ifdef HAL_LPTIM_MODULE_ENABLED
#include "stm32f4xx_hal_lptim.h"
#endif /* HAL_LPTIM_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.
+ * line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
@@ -428,13 +428,13 @@
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
-#endif /* USE_FULL_ASSERT */
+#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus
}
#endif
#endif /* __STM32F4xx_HAL_CONF_H */
-
+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/ports/stm/boards/stm32f4_discovery/mpconfigboard.h b/ports/stm/boards/stm32f4_discovery/mpconfigboard.h
index fcd7aa09c..32b17e1cd 100644
--- a/ports/stm/boards/stm32f4_discovery/mpconfigboard.h
+++ b/ports/stm/boards/stm32f4_discovery/mpconfigboard.h
@@ -33,3 +33,5 @@
#define FLASH_PAGE_SIZE (0x4000)
#define BOARD_OSC_DIV (8)
+
+#define BOARD_RTC_CLOCK RCC_RTCCLKSOURCE_LSE
diff --git a/ports/stm/boards/stm32f4_discovery/stm32f4xx_hal_conf.h b/ports/stm/boards/stm32f4_discovery/stm32f4xx_hal_conf.h
index ee5832d49..6076324f0 100644
--- a/ports/stm/boards/stm32f4_discovery/stm32f4xx_hal_conf.h
+++ b/ports/stm/boards/stm32f4_discovery/stm32f4xx_hal_conf.h
@@ -2,7 +2,7 @@
******************************************************************************
* @file stm32f4xx_hal_conf_template.h
* @author MCD Application Team
- * @brief HAL configuration template file.
+ * @brief HAL configuration template file.
* This file should be copied to the application folder and renamed
* to stm32f4xx_hal_conf.h.
******************************************************************************
@@ -17,7 +17,7 @@
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
- */
+ */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_HAL_CONF_H
@@ -32,9 +32,9 @@
/* ########################## Module Selection ############################## */
/**
- * @brief This is the list of modules to be used in the HAL driver
+ * @brief This is the list of modules to be used in the HAL driver
*/
-#define HAL_MODULE_ENABLED
+#define HAL_MODULE_ENABLED
#define HAL_ADC_MODULE_ENABLED
/* #define HAL_CRYP_MODULE_ENABLED */
@@ -56,13 +56,13 @@
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_LTDC_MODULE_ENABLED */
#define HAL_RNG_MODULE_ENABLED
-/* #define HAL_RTC_MODULE_ENABLED */
+#define HAL_RTC_MODULE_ENABLED
/* #define HAL_SAI_MODULE_ENABLED */
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
#define HAL_SPI_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
-#define HAL_UART_MODULE_ENABLED
+#define HAL_UART_MODULE_ENABLED
#define HAL_USART_MODULE_ENABLED
/* #define HAL_IRDA_MODULE_ENABLED */
/* #define HAL_SMARTCARD_MODULE_ENABLED */
@@ -90,9 +90,9 @@
/**
* @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).
+ * (when HSE is used as system clock source, directly or through the PLL).
*/
-#if !defined (HSE_VALUE)
+#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
@@ -103,7 +103,7 @@
/**
* @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).
+ * (when HSI is used as system clock source, directly or through the PLL).
*/
#if !defined (HSI_VALUE)
#define HSI_VALUE ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
@@ -112,7 +112,7 @@
/**
* @brief Internal Low Speed oscillator (LSI) value.
*/
-#if !defined (LSI_VALUE)
+#if !defined (LSI_VALUE)
#define LSI_VALUE ((uint32_t)32000U) /*!< LSI Typical Value in Hz*/
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations
@@ -130,8 +130,8 @@
/**
* @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.
+ * 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)12288000U) /*!< Value of the External audio frequency in Hz*/
@@ -144,16 +144,16 @@
/**
* @brief This is the HAL system configuration section
*/
-#define VDD_VALUE ((uint32_t)3300U) /*!< Value of VDD in mv */
-#define TICK_INT_PRIORITY ((uint32_t)0U) /*!< tick interrupt priority */
-#define USE_RTOS 0U
+#define VDD_VALUE ((uint32_t)3300U) /*!< Value of VDD in mv */
+#define TICK_INT_PRIORITY ((uint32_t)0U) /*!< tick interrupt priority */
+#define USE_RTOS 0U
#define PREFETCH_ENABLE 1U
#define INSTRUCTION_CACHE_ENABLE 1U
#define DATA_CACHE_ENABLE 1U
/* ########################## Assert Selection ############################## */
/**
- * @brief Uncomment the line below to expanse the "assert_param" macro in the
+ * @brief Uncomment the line below to expanse the "assert_param" macro in the
* HAL drivers code
*/
/* #define USE_FULL_ASSERT 1U */
@@ -170,7 +170,7 @@
#define MAC_ADDR4 0U
#define MAC_ADDR5 0U
-/* Definition of the Ethernet driver buffers size and count */
+/* 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)4U) /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
@@ -178,9 +178,9 @@
/* Section 2: PHY configuration section */
-/* DP83848_PHY_ADDRESS Address*/
+/* DP83848_PHY_ADDRESS Address*/
#define DP83848_PHY_ADDRESS 0x01U
-/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
+/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
#define PHY_RESET_DELAY ((uint32_t)0x000000FFU)
/* PHY Configuration delay */
#define PHY_CONFIG_DELAY ((uint32_t)0x00000FFFU)
@@ -192,7 +192,7 @@
#define PHY_BCR ((uint16_t)0x0000U) /*!< Transceiver Basic Control Register */
#define PHY_BSR ((uint16_t)0x0001U) /*!< Transceiver Basic Status Register */
-
+
#define PHY_RESET ((uint16_t)0x8000U) /*!< PHY Reset */
#define PHY_LOOPBACK ((uint16_t)0x4000U) /*!< Select loop-back mode */
#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100U) /*!< Set the full-duplex mode at 100 Mb/s */
@@ -207,7 +207,7 @@
#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020U) /*!< Auto-Negotiation process completed */
#define PHY_LINKED_STATUS ((uint16_t)0x0004U) /*!< Valid link established */
#define PHY_JABBER_DETECTION ((uint16_t)0x0002U) /*!< Jabber condition detected */
-
+
/* Section 4: Extended PHY Registers */
#define PHY_SR ((uint16_t)0x10U) /*!< PHY status register Offset */
@@ -225,7 +225,7 @@
/* Includes ------------------------------------------------------------------*/
/**
- * @brief Include module's header file
+ * @brief Include module's header file
*/
#ifdef HAL_RCC_MODULE_ENABLED
@@ -243,7 +243,7 @@
#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 */
@@ -261,7 +261,7 @@
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_CRYP_MODULE_ENABLED
- #include "stm32f4xx_hal_cryp.h"
+ #include "stm32f4xx_hal_cryp.h"
#endif /* HAL_CRYP_MODULE_ENABLED */
#ifdef HAL_DMA2D_MODULE_ENABLED
@@ -283,7 +283,7 @@
#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 */
@@ -298,11 +298,11 @@
#ifdef HAL_PCCARD_MODULE_ENABLED
#include "stm32f4xx_hal_pccard.h"
-#endif /* HAL_PCCARD_MODULE_ENABLED */
-
+#endif /* HAL_PCCARD_MODULE_ENABLED */
+
#ifdef HAL_SDRAM_MODULE_ENABLED
#include "stm32f4xx_hal_sdram.h"
-#endif /* HAL_SDRAM_MODULE_ENABLED */
+#endif /* HAL_SDRAM_MODULE_ENABLED */
#ifdef HAL_HASH_MODULE_ENABLED
#include "stm32f4xx_hal_hash.h"
@@ -383,7 +383,7 @@
#ifdef HAL_HCD_MODULE_ENABLED
#include "stm32f4xx_hal_hcd.h"
#endif /* HAL_HCD_MODULE_ENABLED */
-
+
#ifdef HAL_DSI_MODULE_ENABLED
#include "stm32f4xx_hal_dsi.h"
#endif /* HAL_DSI_MODULE_ENABLED */
@@ -411,14 +411,14 @@
#ifdef HAL_LPTIM_MODULE_ENABLED
#include "stm32f4xx_hal_lptim.h"
#endif /* HAL_LPTIM_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.
+ * line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
@@ -427,13 +427,13 @@
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
-#endif /* USE_FULL_ASSERT */
+#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus
}
#endif
#endif /* __STM32F4xx_HAL_CONF_H */
-
+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/ports/stm/common-hal/busio/UART.c b/ports/stm/common-hal/busio/UART.c
index 0b434fd02..e32fa5635 100644
--- a/ports/stm/common-hal/busio/UART.c
+++ b/ports/stm/common-hal/busio/UART.c
@@ -35,7 +35,6 @@
#include "py/stream.h"
#include "supervisor/shared/translate.h"
-#include "tick.h"
#include "stm32f4xx_hal.h"
#define ALL_UARTS 0xFFFF
diff --git a/ports/stm/common-hal/displayio/ParallelBus.c b/ports/stm/common-hal/displayio/ParallelBus.c
index 1b808ec02..314b72ff7 100644
--- a/ports/stm/common-hal/displayio/ParallelBus.c
+++ b/ports/stm/common-hal/displayio/ParallelBus.c
@@ -33,8 +33,6 @@
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/__init__.h"
-#include "tick.h"
-
void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* self,
const mcu_pin_obj_t* data0, const mcu_pin_obj_t* command, const mcu_pin_obj_t* chip_select,
const mcu_pin_obj_t* write, const mcu_pin_obj_t* read, const mcu_pin_obj_t* reset) {
diff --git a/ports/stm/common-hal/microcontroller/__init__.c b/ports/stm/common-hal/microcontroller/__init__.c
index 0c680eb05..b20f49e59 100644
--- a/ports/stm/common-hal/microcontroller/__init__.c
+++ b/ports/stm/common-hal/microcontroller/__init__.c
@@ -41,31 +41,9 @@
#include "stm32f4xx_hal.h"
-//tested divisor value for busy loop in us delay
-#define LOOP_TICKS 12
-
-STATIC uint32_t get_us(void) {
- uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq()/1000000;
- uint32_t micros, sys_cycles;
- do {
- micros = supervisor_ticks_ms32();
- sys_cycles = SysTick->VAL; //counts backwards
- } while (micros != supervisor_ticks_ms32()); //try again if ticks_ms rolled over
- return (micros * 1000) + (ticks_per_us * 1000 - sys_cycles) / ticks_per_us;
-}
-
void common_hal_mcu_delay_us(uint32_t delay) {
if (__get_PRIMASK() == 0x00000000) {
- //by default use ticks_ms
- uint32_t start = get_us();
- while (get_us()-start < delay) {
- __asm__ __volatile__("nop");
- }
} else {
- //when SysTick is disabled, approximate with busy loop
- const uint32_t ucount = HAL_RCC_GetSysClockFreq() / 1000000 * delay / LOOP_TICKS;
- for (uint32_t count = 0; ++count <= ucount;) {
- }
}
}
@@ -171,7 +149,7 @@ STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_PB10), MP_ROM_PTR(&pin_PB10) },
#if MCU_PACKAGE == 144 || defined STM32F405xx
{ MP_ROM_QSTR(MP_QSTR_PB11), MP_ROM_PTR(&pin_PB11) },
-#endif
+#endif
{ MP_ROM_QSTR(MP_QSTR_PB12), MP_ROM_PTR(&pin_PB12) },
{ MP_ROM_QSTR(MP_QSTR_PB13), MP_ROM_PTR(&pin_PB13) },
{ MP_ROM_QSTR(MP_QSTR_PB14), MP_ROM_PTR(&pin_PB14) },
diff --git a/ports/stm/common-hal/neopixel_write/__init__.c b/ports/stm/common-hal/neopixel_write/__init__.c
index 6740a0d34..c8852403b 100644
--- a/ports/stm/common-hal/neopixel_write/__init__.c
+++ b/ports/stm/common-hal/neopixel_write/__init__.c
@@ -24,18 +24,17 @@
* THE SOFTWARE.
*/
-#include "py/mphal.h"
#include "shared-bindings/neopixel_write/__init__.h"
-#include "tick.h"
#include "py/mperrno.h"
+#include "py/mphal.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_gpio.h"
+#include "supervisor/port.h"
-uint64_t next_start_tick_ms = 0;
-uint32_t next_start_tick_us = 1000;
+uint64_t next_start_raw_ticks = 0;
//sysclock divisors
#define MAGIC_800_INT 900000 // ~1.11 us -> 1.2 field
@@ -45,7 +44,7 @@ uint32_t next_start_tick_us = 1000;
#pragma GCC push_options
#pragma GCC optimize ("Os")
-void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels,
+void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels,
uint32_t numBytes) {
uint8_t *p = pixels, *end = p + numBytes, pix = *p++, mask = 0x80;
uint32_t start = 0;
@@ -59,9 +58,9 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
uint32_t t0 = (sys_freq/MAGIC_800_T0H);
uint32_t t1 = (sys_freq/MAGIC_800_T1H);
- // This must be called while interrupts are on in case we're waiting for a
- // future ms tick.
- wait_until(next_start_tick_ms, next_start_tick_us);
+ // Wait to make sure we don't append onto the last transmission. This should only be a tick or
+ // two.
+ while (port_get_raw_ticks(NULL) < next_start_raw_ticks) {}
GPIO_TypeDef * p_port = pin_port(digitalinout->pin->port);
uint32_t p_mask = pin_mask(digitalinout->pin->number);
@@ -90,13 +89,7 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
__enable_irq();
// Update the next start.
- current_tick(&next_start_tick_ms, &next_start_tick_us);
- if (next_start_tick_us < 100) {
- next_start_tick_ms += 1;
- next_start_tick_us = 100 - next_start_tick_us;
- } else {
- next_start_tick_us -= 100;
- }
+ next_start_raw_ticks = port_get_raw_ticks(NULL) + 4;
}
-#pragma GCC pop_options \ No newline at end of file
+#pragma GCC pop_options
diff --git a/ports/stm/common-hal/pulseio/PulseIn.c b/ports/stm/common-hal/pulseio/PulseIn.c
index 8428a2278..371ac42ed 100644
--- a/ports/stm/common-hal/pulseio/PulseIn.c
+++ b/ports/stm/common-hal/pulseio/PulseIn.c
@@ -32,7 +32,6 @@
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/pulseio/PulseIn.h"
-#include "tick.h"
#include "stm32f4xx_hal.h"
@@ -47,9 +46,8 @@ static void pulsein_handler(uint8_t num) {
EXTI->PR = 1 << num;
// Grab the current time first.
- uint32_t current_us;
- uint64_t current_ms;
- current_tick(&current_ms, &current_us);
+ uint32_t current_us = 0;
+ uint64_t current_ms = 0;
// current_tick gives us the remaining us until the next tick but we want the number since the last ms.
current_us = 1000 - current_us;
@@ -116,10 +114,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
// Allocate pulse buffer
self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) {
- mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"),
+ mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"),
maxlen * sizeof(uint16_t));
}
-
+
// Set internal variables
self->pin = pin;
self->maxlen = maxlen;
diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c
index d82525ea1..137ddaf9d 100644
--- a/ports/stm/common-hal/pulseio/PulseOut.c
+++ b/ports/stm/common-hal/pulseio/PulseOut.c
@@ -37,7 +37,6 @@
#include "stm32f4xx_hal.h"
#include "common-hal/microcontroller/Pin.h"
-#include "tick.h"
// A single timer is shared amongst all PulseOut objects under the assumption that
// the code is single threaded.
@@ -60,7 +59,7 @@ STATIC void turn_off(pulseio_pulseout_obj_t *pulseout) {
// Turn off PWM
HAL_TIM_PWM_Stop(&(pulseout->pwmout->handle), pulseout->pwmout->channel);
// Make sure pin is low.
- HAL_GPIO_WritePin(pin_port(pulseout->pwmout->tim->pin->port),
+ HAL_GPIO_WritePin(pin_port(pulseout->pwmout->tim->pin->port),
pin_mask(pulseout->pwmout->tim->pin->number), 0);
}
@@ -196,7 +195,7 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu
// mp_raise_RuntimeError(translate("Error: Send Timeout"));
// }
}
- //turn off timer counter.
+ //turn off timer counter.
t7_handle.Instance->CR1 &= ~TIM_CR1_CEN;
}
diff --git a/ports/stm/mphalport.c b/ports/stm/mphalport.c
index a3cf501c0..cd8592be3 100644
--- a/ports/stm/mphalport.c
+++ b/ports/stm/mphalport.c
@@ -35,21 +35,6 @@
#include "supervisor/shared/tick.h"
#include "stm32f4xx_hal.h"
-void mp_hal_delay_ms(mp_uint_t delay) {
- uint64_t start_tick = supervisor_ticks_ms64();
- uint64_t duration = 0;
- while (duration < delay) {
- RUN_BACKGROUND_TASKS;
- // Check to see if we've been CTRL-Ced by autoreload or the user.
- if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) ||
- MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
- break;
- }
- duration = (supervisor_ticks_ms64() - start_tick);
- // TODO(tannewt): Go to sleep for a little while while we wait.
- }
-}
-
void mp_hal_delay_us(mp_uint_t delay) {
common_hal_mcu_delay_us(delay);
}
diff --git a/ports/stm/peripherals/stm32f4/stm32f405xx/clocks.c b/ports/stm/peripherals/stm32f4/stm32f405xx/clocks.c
index 2afca64e8..874a7dbf7 100644
--- a/ports/stm/peripherals/stm32f4/stm32f405xx/clocks.c
+++ b/ports/stm/peripherals/stm32f4/stm32f405xx/clocks.c
@@ -28,7 +28,7 @@
#include "py/mpconfig.h"
void stm32f4_peripherals_clocks_init(void) {
- //TODO: All parameters must be moved to board level, due to relationship with HSE Osc.
+ //TODO: All parameters must be moved to board level, due to relationship with HSE Osc.
//System clock init
RCC_ClkInitTypeDef RCC_ClkInitStruct;
@@ -58,7 +58,9 @@ void stm32f4_peripherals_clocks_init(void) {
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+ // APB1 must always be on so that we can talk to the RTC for timing.
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
+ // TODO: Only turn on APB2 when it is needed to save power.
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
diff --git a/ports/stm/peripherals/stm32f4/stm32f412zx/clocks.c b/ports/stm/peripherals/stm32f4/stm32f412zx/clocks.c
index b208f9dfb..b4075027a 100644
--- a/ports/stm/peripherals/stm32f4/stm32f412zx/clocks.c
+++ b/ports/stm/peripherals/stm32f4/stm32f412zx/clocks.c
@@ -65,7 +65,7 @@ void stm32f4_peripherals_clocks_init(void) {
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
* clocks dividers */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
- RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
+ RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
diff --git a/ports/stm/supervisor/internal_flash.c b/ports/stm/supervisor/internal_flash.c
index 046b26baf..4e3c56494 100644
--- a/ports/stm/supervisor/internal_flash.c
+++ b/ports/stm/supervisor/internal_flash.c
@@ -64,7 +64,7 @@ static const flash_layout_t flash_layout[] = {
static uint8_t sector_copy[0x4000] __attribute__((aligned(4)));
-//Return the sector of a given flash address.
+//Return the sector of a given flash address.
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
if (addr >= flash_layout[0].base_address) {
uint32_t sector_index = 0;
@@ -100,7 +100,7 @@ uint32_t supervisor_flash_get_block_count(void) {
return INTERNAL_FLASH_FILESYSTEM_NUM_BLOCKS;
}
-void supervisor_flash_flush(void) {
+void port_internal_flash_flush(void) {
}
static int32_t convert_block_to_flash_addr(uint32_t block) {
diff --git a/ports/stm/supervisor/port.c b/ports/stm/supervisor/port.c
index 948e9f2e4..25000eca8 100644
--- a/ports/stm/supervisor/port.c
+++ b/ports/stm/supervisor/port.c
@@ -28,7 +28,7 @@
#include <stdint.h>
#include "supervisor/port.h"
#include "boards/board.h"
-#include "tick.h"
+#include "lib/timeutils/timeutils.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/busio/I2C.h"
@@ -43,6 +43,8 @@
#include "stm32f4xx_hal.h"
+static RTC_HandleTypeDef _hrtc;
+
safe_mode_t port_init(void) {
HAL_Init();
__HAL_RCC_SYSCFG_CLK_ENABLE();
@@ -51,11 +53,33 @@ safe_mode_t port_init(void) {
stm32f4_peripherals_clocks_init();
stm32f4_peripherals_gpio_init();
- tick_init();
+ HAL_PWR_EnableBkUpAccess();
+ __HAL_RCC_LSE_CONFIG(RCC_LSE_ON);
+ while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {}
+
+ __HAL_RCC_RTC_CONFIG(BOARD_RTC_CLOCK);
+ __HAL_RCC_RTC_ENABLE();
+ _hrtc.Instance = RTC;
+ _hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
+ // Divide async as little as possible so that we have 32768 count in subseconds.
+ _hrtc.Init.AsynchPrediv = 0x0;
+ _hrtc.Init.SynchPrediv = 0x7fff; // 32768 ticks per second
+ _hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
+
+ HAL_StatusTypeDef result = HAL_RTC_Init(&_hrtc);
+ if (result != HAL_OK) {
+ asm("bkpt");
+ }
return NO_SAFE_MODE;
}
+void SysTick_Handler(void) {
+ // Read the CTRL register to clear the SysTick interrupt.
+ SysTick->CTRL;
+ HAL_IncTick();
+}
+
void reset_port(void) {
reset_all_pins();
i2c_reset();
@@ -106,3 +130,109 @@ void HardFault_Handler(void) {
asm("nop;");
}
}
+
+// This function is called often for timing so we cache the seconds elapsed computation based on the
+// register value. The STM HAL always does shifts and conversion if we use it directly.
+volatile uint32_t seconds_to_date = 0;
+volatile uint32_t cached_date = 0;
+volatile uint32_t seconds_to_minute = 0;
+volatile uint32_t cached_hours_minutes = 0;
+uint64_t port_get_raw_ticks(uint8_t* subticks) {
+ uint32_t subseconds = 0x8000 - (uint32_t)(RTC->SSR);
+ uint32_t time = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
+ uint32_t date = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
+ if (date != cached_date) {
+ uint32_t year = (uint8_t)((date & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
+ uint8_t month = (uint8_t)((date & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
+ uint8_t day = (uint8_t)(date & (RTC_DR_DT | RTC_DR_DU));
+ // Add 2000 since the year is only the last two digits.
+ year = 2000 + (uint32_t)RTC_Bcd2ToByte(year);
+ month = (uint8_t)RTC_Bcd2ToByte(month);
+ day = (uint8_t)RTC_Bcd2ToByte(day);
+ seconds_to_date = timeutils_seconds_since_2000(year, month, day, 0, 0, 0);
+ cached_date = date;
+ }
+ uint32_t hours_minutes = time & (RTC_TR_HT | RTC_TR_HU | RTC_TR_MNT | RTC_TR_MNU);
+ if (hours_minutes != cached_hours_minutes) {
+ uint8_t hours = (uint8_t)((time & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
+ uint8_t minutes = (uint8_t)((time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
+ hours = (uint8_t)RTC_Bcd2ToByte(hours);
+ minutes = (uint8_t)RTC_Bcd2ToByte(minutes);
+ seconds_to_minute = 60 * (60 * hours + minutes);
+ }
+ uint8_t seconds = (uint8_t)(time & (RTC_TR_ST | RTC_TR_SU));
+ seconds = (uint8_t)RTC_Bcd2ToByte(seconds);
+ if (subticks != NULL) {
+ *subticks = subseconds % 32;
+ }
+
+ return ((uint64_t) 1024) * (seconds_to_date + seconds_to_minute + seconds) + subseconds / 32;
+}
+
+void RTC_WKUP_IRQHandler(void) {
+ supervisor_tick();
+ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&_hrtc, RTC_FLAG_WUTF);
+ __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
+}
+
+void RTC_Alarm_IRQHandler(void) {
+ RTC->ISR = ~RTC_FLAG_ALRAF;
+ HAL_RTC_DeactivateAlarm(&_hrtc, RTC_ALARM_A);
+}
+
+// Enable 1/1024 second tick.
+void port_enable_tick(void) {
+ HAL_StatusTypeDef result = HAL_RTCEx_SetWakeUpTimer_IT(&_hrtc, 32768 / 1024 / 2, RTC_WAKEUPCLOCK_RTCCLK_DIV2);
+ if (result != HAL_OK) {
+ asm("bkpt");
+ }
+ HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 1, 0U);
+ HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
+}
+extern volatile uint32_t autoreload_delay_ms;
+
+// Disable 1/1024 second tick.
+void port_disable_tick(void) {
+ if (autoreload_delay_ms > 1) {
+ asm("bkpt");
+ }
+ HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
+ HAL_RTCEx_DeactivateWakeUpTimer(&_hrtc);
+}
+
+void port_interrupt_after_ticks(uint32_t ticks) {
+ uint64_t raw_ticks = port_get_raw_ticks(NULL) + ticks;
+
+ RTC_AlarmTypeDef alarm;
+ if (ticks > 1024) {
+ timeutils_struct_time_t tm;
+ timeutils_seconds_since_2000_to_struct_time(raw_ticks / 1024, &tm);
+ alarm.AlarmTime.Hours = tm.tm_hour;
+ alarm.AlarmTime.Minutes = tm.tm_min;
+ alarm.AlarmTime.Seconds = tm.tm_sec;
+ alarm.AlarmDateWeekDay = tm.tm_mday;
+ alarm.AlarmMask = RTC_ALARMMASK_ALL;
+ } else {
+ alarm.AlarmMask = RTC_ALARMMASK_NONE;
+ }
+
+ alarm.AlarmTime.SubSeconds = 32768 - ((raw_ticks % 1024) * 32);
+ alarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+ alarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_SET;
+ alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
+ alarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
+ alarm.Alarm = RTC_ALARM_A;
+
+ HAL_RTC_SetAlarm_IT(&_hrtc, &alarm, RTC_FORMAT_BIN);
+}
+
+void port_sleep_until_interrupt(void) {
+ // Clear the FPU interrupt because it can prevent us from sleeping.
+ if (__get_FPSCR() & ~(0x9f)) {
+ __set_FPSCR(__get_FPSCR() & ~(0x9f));
+ (void) __get_FPSCR();
+ }
+ // Call wait for interrupt ourselves if the SD isn't enabled.
+ __WFI();
+}
+
diff --git a/ports/stm/supervisor/usb.c b/ports/stm/supervisor/usb.c
index 358c2de5b..c61b6e959 100644
--- a/ports/stm/supervisor/usb.c
+++ b/ports/stm/supervisor/usb.c
@@ -26,7 +26,6 @@
*/
-#include "tick.h"
#include "supervisor/usb.h"
#include "lib/utils/interrupt_char.h"
#include "lib/mp-readline/readline.h"
@@ -59,13 +58,13 @@ STATIC void init_usb_vbus_sense(void) {
}
void init_usb_hardware(void) {
- //TODO: if future chips overload this with options, move to peripherals management.
+ //TODO: if future chips overload this with options, move to peripherals management.
GPIO_InitTypeDef GPIO_InitStruct = {0};
/**USB_OTG_FS GPIO Configuration
PA10 ------> USB_OTG_FS_ID
PA11 ------> USB_OTG_FS_DM
- PA12 ------> USB_OTG_FS_DP
+ PA12 ------> USB_OTG_FS_DP
*/
__HAL_RCC_GPIOA_CLK_ENABLE();
@@ -103,7 +102,7 @@ void init_usb_hardware(void) {
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
never_reset_pin_number(0, 8);
#endif
-
+
/* Peripheral clock enable */
__HAL_RCC_USB_OTG_FS_CLK_DISABLE();
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
diff --git a/ports/stm/tick.c b/ports/stm/tick.c
deleted file mode 100644
index f4adf183a..000000000
--- a/ports/stm/tick.c
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "tick.h"
-
-#include "supervisor/filesystem.h"
-#include "supervisor/shared/tick.h"
-#include "shared-bindings/microcontroller/Processor.h"
-
-#include "stm32f4xx.h"
-
-void SysTick_Handler(void) {
- // SysTick interrupt handler called when the SysTick timer reaches zero
- // (every millisecond).
-
- // Do things common to all ports when the tick occurs
- supervisor_tick();
-}
-
-uint32_t HAL_GetTick(void) //override ST HAL
-{
- return (uint32_t)supervisor_ticks_ms32();
-}
-
-void tick_init() {
- uint32_t ticks_per_ms = SystemCoreClock/ 1000;
- SysTick_Config(ticks_per_ms); // interrupt is enabled
-
- NVIC_EnableIRQ(SysTick_IRQn);
- // Bump up the systick interrupt so nothing else interferes with timekeeping.
- NVIC_SetPriority(SysTick_IRQn, 0);
- NVIC_SetPriority(OTG_FS_IRQn, 1);
-}
-
-void tick_delay(uint32_t us) {
- uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
- uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
- uint64_t start_ms = supervisor_ticks_ms64();
- while (us > 1000) {
- while (supervisor_ticks_ms64() == start_ms) {}
- us -= us_between_ticks;
- start_ms = supervisor_ticks_ms64();
- us_between_ticks = 1000;
- }
- while (SysTick->VAL > ((us_between_ticks - us) * ticks_per_us)) {}
-}
-
-// us counts down!
-void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
- uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
- *ms = supervisor_ticks_ms32();
- *us_until_ms = SysTick->VAL / ticks_per_us;
-}
-
-void wait_until(uint64_t ms, uint32_t us_until_ms) {
- uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
- while(supervisor_ticks_ms64() <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
-}
diff --git a/ports/stm/tick.h b/ports/stm/tick.h
deleted file mode 100644
index 999acc7a3..000000000
--- a/ports/stm/tick.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-#ifndef MICROPY_INCLUDED_STM32F4_TICK_H
-#define MICROPY_INCLUDED_STM32F4_TICK_H
-
-#include "py/mpconfig.h"
-
-#include <stdint.h>
-
-extern struct timer_descriptor ms_timer;
-
-void tick_init(void);
-
-void tick_delay(uint32_t us);
-
-void current_tick(uint64_t* ms, uint32_t* us_until_ms);
-// Do not call this with interrupts disabled because it may be waiting for
-// ticks_ms to increment.
-void wait_until(uint64_t ms, uint32_t us_until_ms);
-
-#endif // MICROPY_INCLUDED_STM32F4_TICK_H
diff --git a/supervisor/shared/flash.c b/supervisor/shared/flash.c
index e0bf83490..298e7d83e 100644
--- a/supervisor/shared/flash.c
+++ b/supervisor/shared/flash.c
@@ -111,7 +111,7 @@ mp_uint_t flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_bloc
return supervisor_flash_read_blocks(dest, block_num - PART1_START_BLOCK, num_blocks);
}
-volatile bool filesystem_dirty;
+volatile bool filesystem_dirty = false;
mp_uint_t flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
if (block_num == 0) {
@@ -137,7 +137,9 @@ void supervisor_flash_flush(void) {
supervisor_external_flash_flush();
#endif
// Turn off ticks now that our filesystem has been flushed.
- supervisor_disable_tick();
+ if (filesystem_dirty) {
+ supervisor_disable_tick();
+ }
filesystem_dirty = false;
}
diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c
index 80bd6f204..de56b64f5 100644
--- a/supervisor/shared/tick.c
+++ b/supervisor/shared/tick.c
@@ -118,18 +118,22 @@ void mp_hal_delay_ms(mp_uint_t delay) {
volatile size_t tick_enable_count = 0;
extern void supervisor_enable_tick(void) {
+ common_hal_mcu_disable_interrupts();
if (tick_enable_count == 0) {
port_enable_tick();
}
tick_enable_count++;
+ common_hal_mcu_enable_interrupts();
}
extern void supervisor_disable_tick(void) {
+ common_hal_mcu_disable_interrupts();
if (tick_enable_count > 0) {
tick_enable_count--;
}
if (tick_enable_count == 0) {
port_disable_tick();
}
+ common_hal_mcu_enable_interrupts();
}