diff options
Diffstat (limited to 'stmhal/systick.c')
-rw-r--r-- | stmhal/systick.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/stmhal/systick.c b/stmhal/systick.c index ade05d74d7..eb11de9b74 100644 --- a/stmhal/systick.c +++ b/stmhal/systick.c @@ -24,20 +24,36 @@ * THE SOFTWARE. */ -#include STM32_HAL_H - -#include "py/obj.h" +#include "py/mphal.h" #include "irq.h" #include "systick.h" #include "pybthread.h" -// We provide our own version of HAL_Delay that calls __WFI while waiting, in -// order to reduce power consumption. -// Note: Upon entering this function we may or may not have the GIL. +extern __IO uint32_t uwTick; + +// We provide our own version of HAL_Delay that calls __WFI while waiting, +// and works when interrupts are disabled. This function is intended to be +// used only by the ST HAL functions. void HAL_Delay(uint32_t Delay) { if (query_irq() == IRQ_STATE_ENABLED) { // IRQs enabled, so can use systick counter to do the delay - extern __IO uint32_t uwTick; + uint32_t start = uwTick; + // Wraparound of tick is taken care of by 2's complement arithmetic. + while (uwTick - start < Delay) { + // Enter sleep mode, waiting for (at least) the SysTick interrupt. + __WFI(); + } + } else { + // IRQs disabled, use mp_hal_delay_ms routine. + mp_hal_delay_ms(Delay); + } +} + +// Core delay function that does an efficient sleep and may switch thread context. +// Note: Upon entering this function we may or may not have the GIL. +void mp_hal_delay_ms(mp_uint_t Delay) { + if (query_irq() == IRQ_STATE_ENABLED) { + // IRQs enabled, so can use systick counter to do the delay uint32_t start = uwTick; // Wraparound of tick is taken care of by 2's complement arithmetic. while (uwTick - start < Delay) { @@ -64,11 +80,11 @@ void HAL_Delay(uint32_t Delay) { } // delay for given number of microseconds -void sys_tick_udelay(uint32_t usec) { +void mp_hal_delay_us(mp_uint_t usec) { if (query_irq() == IRQ_STATE_ENABLED) { // IRQs enabled, so can use systick counter to do the delay - uint32_t start = sys_tick_get_microseconds(); - while (sys_tick_get_microseconds() - start < usec) { + uint32_t start = mp_hal_ticks_us(); + while (mp_hal_ticks_us() - start < usec) { } } else { // IRQs disabled, so need to use a busy loop for the delay @@ -92,11 +108,15 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) { } } +mp_uint_t mp_hal_ticks_ms(void) { + return uwTick; +} + // The SysTick timer counts down at 168 MHz, so we can use that knowledge // to grab a microsecond counter. // // We assume that HAL_GetTickis returns milliseconds. -uint32_t sys_tick_get_microseconds(void) { +mp_uint_t mp_hal_ticks_us(void) { mp_uint_t irq_state = disable_irq(); uint32_t counter = SysTick->VAL; uint32_t milliseconds = HAL_GetTick(); |