From a0074c36d88cc3bb5a26fe9c5767ddc94132e6a8 Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Sun, 7 Nov 2021 14:19:01 +0100 Subject: [PATCH] Improved accuracy of common_hal_mcu_delay_us of STM port. SysTick Current Value Register must be cleared before enabling. --- ports/stm/common-hal/microcontroller/__init__.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/stm/common-hal/microcontroller/__init__.c b/ports/stm/common-hal/microcontroller/__init__.c index 28e06c9135..3bb850f5d3 100644 --- a/ports/stm/common-hal/microcontroller/__init__.c +++ b/ports/stm/common-hal/microcontroller/__init__.c @@ -41,13 +41,14 @@ #include "supervisor/shared/safe_mode.h" void common_hal_mcu_delay_us(uint32_t delay) { - uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq() / 1000000; + uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq() / 1000000UL; delay *= ticks_per_us; + SysTick->VAL = 0UL; SysTick->LOAD = delay; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) { } - SysTick->CTRL = 0; + SysTick->CTRL = 0UL; } volatile uint32_t nesting_count = 0;