From 4559bcb4679e04e0a5e24030675676ff6a9803f2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Tue, 3 Nov 2020 23:21:18 +1100 Subject: [PATCH] unix: Make mp_hal_delay_ms run MICROPY_EVENT_POLL_HOOK. Signed-off-by: Jim Mussared --- ports/unix/mphalport.h | 5 ----- ports/unix/unix_mphal.c | 14 ++++++++++++++ ports/windows/windows_mphal.c | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ports/unix/mphalport.h b/ports/unix/mphalport.h index 95ad63221..89d23ca5d 100644 --- a/ports/unix/mphalport.h +++ b/ports/unix/mphalport.h @@ -64,11 +64,6 @@ static inline int mp_hal_readline(vstr_t *vstr, const char *p) { #endif -// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: -// "The useconds argument shall be less than one million." -static inline void mp_hal_delay_ms(mp_uint_t ms) { - usleep((ms) * 1000); -} static inline void mp_hal_delay_us(mp_uint_t us) { usleep(us); } diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 3fe2fa9fe..28a4ca2c4 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -220,3 +220,17 @@ uint64_t mp_hal_time_ns(void) { gettimeofday(&tv, NULL); return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } + +void mp_hal_delay_ms(mp_uint_t ms) { + #ifdef MICROPY_EVENT_POLL_HOOK + mp_uint_t start = mp_hal_ticks_ms(); + while (mp_hal_ticks_ms() - start < ms) { + // MICROPY_EVENT_POLL_HOOK does mp_hal_delay_us(500) (i.e. usleep(500)). + MICROPY_EVENT_POLL_HOOK + } + #else + // TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: + // "The useconds argument shall be less than one million." + usleep(ms * 1000); + #endif +} diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index b442b5914..49daa0542 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -261,3 +261,9 @@ uint64_t mp_hal_time_ns(void) { gettimeofday(&tv, NULL); return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL; } + +// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep: +// "The useconds argument shall be less than one million." +void mp_hal_delay_ms(mp_uint_t ms) { + usleep((ms) * 1000); +}