diff --git a/main.c b/main.c index 9351d90529..ae8c339172 100644 --- a/main.c +++ b/main.c @@ -716,9 +716,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) { // time_to_next_change is in ms and ticks are slightly shorter so // we'll undersleep just a little. It shouldn't matter. - port_interrupt_after_ticks(time_to_next_change); - #endif + if (time_to_next_change > 0) { + port_interrupt_after_ticks(time_to_next_change); + port_idle_until_interrupt(); + } + #else + // No status LED can we sleep until we are interrupted by some + // interaction. port_idle_until_interrupt(); + #endif } } diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 2ee3a1ee98..e45fa8837a 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -257,6 +257,7 @@ uint32_t port_get_saved_word(void) { } static volatile bool ticks_enabled; +static volatile bool _woken_up; uint64_t port_get_raw_ticks(uint8_t *subticks) { uint64_t microseconds = time_us_64(); @@ -268,6 +269,7 @@ STATIC void _tick_callback(uint alarm_num) { supervisor_tick(); hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), 977)); } + _woken_up = true; } // Enable 1/1024 second tick. @@ -291,11 +293,12 @@ void port_interrupt_after_ticks(uint32_t ticks) { if (!ticks_enabled) { hardware_alarm_set_target(0, delayed_by_us(get_absolute_time(), ticks * 977)); } + _woken_up = false; } void port_idle_until_interrupt(void) { common_hal_mcu_disable_interrupts(); - if (!background_callback_pending() && !tud_task_event_ready()) { + if (!background_callback_pending() && !tud_task_event_ready() && !_woken_up) { __DSB(); __WFI(); }