From 30b6ce86be384ac04566d8f7bbf67d5906d6f307 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 13 Sep 2021 10:43:42 +1000 Subject: [PATCH] windows: Add micropython.schedule support. --- ports/windows/mpconfigport.h | 12 ++++++++++++ ports/windows/windows_mphal.c | 12 +++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ports/windows/mpconfigport.h b/ports/windows/mpconfigport.h index 1aae5a750026..88746744aeb9 100644 --- a/ports/windows/mpconfigport.h +++ b/ports/windows/mpconfigport.h @@ -69,6 +69,9 @@ #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) +#ifndef MICROPY_ENABLE_SCHEDULER +#define MICROPY_ENABLE_SCHEDULER (1) +#endif #define MICROPY_VFS_POSIX_FILE (1) #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_DESCRIPTORS (1) @@ -216,6 +219,15 @@ extern const struct _mp_obj_module_t mp_module_time; #define MICROPY_MPHALPORT_H "windows_mphal.h" +#if MICROPY_ENABLE_SCHEDULER +#define MICROPY_EVENT_POLL_HOOK \ + do { \ + extern void mp_handle_pending(bool); \ + mp_handle_pending(true); \ + mp_hal_delay_us(500); \ + } while (0); +#endif + // We need to provide a declaration/definition of alloca() #include diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c index 7a3d881a3443..5398e2da5106 100644 --- a/ports/windows/windows_mphal.c +++ b/ports/windows/windows_mphal.c @@ -262,8 +262,14 @@ uint64_t mp_hal_time_ns(void) { 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); + #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 + SleepEx(ms, TRUE); + #endif }