From 6214fa3f9ecdfb146aaec09afc9d586e76257d5a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jul 2021 01:33:16 +1000 Subject: [PATCH] esp32/mphalport: Always yield at least once in delay_ms. This helps the OS switch to and give other threads processing time during the sleep. It also ensures that pending events are handled, even when sleeping for 0ms. Fixes issue #5344. Signed-off-by: Damien George --- ports/esp32/mphalport.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index e795b5c991..6e6fd26ef4 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -142,14 +142,22 @@ void mp_hal_delay_ms(uint32_t ms) { uint64_t dt; uint64_t t0 = esp_timer_get_time(); for (;;) { + mp_handle_pending(true); + MICROPY_PY_USOCKET_EVENTS_HANDLER + MP_THREAD_GIL_EXIT(); uint64_t t1 = esp_timer_get_time(); dt = t1 - t0; if (dt + portTICK_PERIOD_MS * 1000 >= us) { // doing a vTaskDelay would take us beyond requested delay time + taskYIELD(); + MP_THREAD_GIL_ENTER(); + t1 = esp_timer_get_time(); + dt = t1 - t0; break; + } else { + ulTaskNotifyTake(pdFALSE, 1); + MP_THREAD_GIL_ENTER(); } - MICROPY_EVENT_POLL_HOOK - ulTaskNotifyTake(pdFALSE, 1); } if (dt < us) { // do the remaining delay accurately