diff options
author | Damien George <damien.p.george@gmail.com> | 2018-04-26 20:21:33 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-04-26 20:21:33 +1000 |
commit | 4ed586528047d3eced28a9f4af11dbbe64fa99bb (patch) | |
tree | 2f422b70613553c657d6f76f7af4d468d9724e05 | |
parent | e1fe3abd0928c5e3545c546b02317aff72c72590 (diff) |
esp32/mphalport: Improve mp_hal_delay_us so it handles pending events.
Thanks to @bboser for the initial idea and implementation.
-rw-r--r-- | ports/esp32/mphalport.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index e299a196f..76f3e2413 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -113,7 +113,28 @@ void mp_hal_delay_ms(uint32_t ms) { } void mp_hal_delay_us(uint32_t us) { - ets_delay_us(us); + // these constants are tested for a 240MHz clock + const uint32_t this_overhead = 5; + const uint32_t pend_overhead = 150; + + // return if requested delay is less than calling overhead + if (us < this_overhead) { + return; + } + us -= this_overhead; + + uint64_t t0 = esp_timer_get_time(); + for (;;) { + uint64_t dt = esp_timer_get_time() - t0; + if (dt >= us) { + return; + } + if (dt + pend_overhead < us) { + // we have enough time to service pending events + // (don't use MICROPY_EVENT_POLL_HOOK because it also yields) + mp_handle_pending(); + } + } } // this function could do with improvements (eg use ets_delay_us) |