From 986ad6bf1d35cbc6f8073e847aa6b87156eefc4b Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 25 Aug 2022 12:27:49 +1000 Subject: [PATCH] stm32/boardctrl: Use HAL_Delay instead of mp_hal_delay_ms. Not safe to use mp_hal_delay_ms before boot if threading is enabled, because threading will not have been initialised, and MICROPY_EVENT_POLL_HOOK assumes threading is initialised. HAL_Delay doesn't call MICROPY_EVENT_POLL_HOOK, but is still power-efficient like mp_hal_delay_ms (unlike mp_hal_delay_us). Fixes #7816. Signed-off-by: Jim Mussared --- ports/stm32/boardctrl.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 406a983814..85a42a240f 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -89,6 +89,9 @@ void boardctrl_maybe_enter_mboot(size_t n_args, const void *args_in) { #if !MICROPY_HW_USES_BOOTLOADER STATIC uint update_reset_mode(uint reset_mode) { + // Note: Must use HAL_Delay here as MicroPython is not yet initialised + // and mp_hal_delay_ms will attempt to invoke the scheduler. + #if MICROPY_HW_HAS_SWITCH if (switch_get()) { @@ -100,7 +103,7 @@ STATIC uint update_reset_mode(uint reset_mode) { led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); for (uint j = 0; j < 30; ++j) { - mp_hal_delay_ms(20); + HAL_Delay(20); if (!switch_get()) { goto select_mode; } @@ -115,13 +118,13 @@ STATIC uint update_reset_mode(uint reset_mode) { led_state(2, 0); led_state(3, 0); led_state(4, 0); - mp_hal_delay_ms(50); + HAL_Delay(50); led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); - mp_hal_delay_ms(50); + HAL_Delay(50); } - mp_hal_delay_ms(400); + HAL_Delay(400); #elif defined(MICROPY_HW_LED1) @@ -134,11 +137,11 @@ STATIC uint update_reset_mode(uint reset_mode) { break; } led_state(1, 1); - mp_hal_delay_ms(100); + HAL_Delay(100); led_state(1, 0); - mp_hal_delay_ms(200); + HAL_Delay(200); } - mp_hal_delay_ms(400); + HAL_Delay(400); if (!switch_get()) { break; } @@ -150,11 +153,11 @@ STATIC uint update_reset_mode(uint reset_mode) { for (uint i = 0; i < 2; i++) { for (uint j = 0; j < reset_mode; j++) { led_state(1, 1); - mp_hal_delay_ms(100); + HAL_Delay(100); led_state(1, 0); - mp_hal_delay_ms(200); + HAL_Delay(200); } - mp_hal_delay_ms(400); + HAL_Delay(400); } #else #error Need a reset mode update method