From da6791e0e1743058d7429ad0b565c5a1bb1bce73 Mon Sep 17 00:00:00 2001 From: Serge Bazanski <q3k@q3k.org> Date: Mon, 5 Jun 2023 01:35:30 +0200 Subject: [PATCH] badge23: work around ctx initialization race condition Sometimes mpy code requests ctx too early, before the display (and thus ctx) are initialized. This is a quick-and-dirty workaround as we're planning on getting rid of this code path anyway. --- components/badge23/display.c | 2 +- components/badge23/include/badge23/display.h | 2 +- usermodule/mp_hardware.c | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/components/badge23/display.c b/components/badge23/display.c index afaffd34ac..b8c5da869b 100644 --- a/components/badge23/display.c +++ b/components/badge23/display.c @@ -15,7 +15,7 @@ #include "../../usermodule/uctx/uctx/ctx.h" -Ctx *the_ctx = NULL; +volatile Ctx *the_ctx = NULL; uint16_t *pixels; diff --git a/components/badge23/include/badge23/display.h b/components/badge23/include/badge23/display.h index e2946a47c7..213257aa07 100644 --- a/components/badge23/include/badge23/display.h +++ b/components/badge23/include/badge23/display.h @@ -12,4 +12,4 @@ void display_draw_pixel(uint8_t x, uint8_t y, uint16_t col); uint16_t display_get_pixel(uint8_t x, uint8_t y); void display_fill(uint16_t col); -extern Ctx *the_ctx; +extern volatile Ctx *the_ctx; diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c index 632d35ef4f..37d9994d4b 100644 --- a/usermodule/mp_hardware.c +++ b/usermodule/mp_hardware.c @@ -20,7 +20,6 @@ #include "badge23_hwconfig.h" mp_obj_t mp_ctx_from_ctx(Ctx *ctx); -mp_obj_t mp_ctx = NULL; STATIC mp_obj_t mp_init_done(void) { return mp_obj_new_int(hardware_is_initialized()); @@ -190,7 +189,12 @@ STATIC mp_obj_t mp_version(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_version_obj, mp_version); STATIC mp_obj_t mp_get_ctx(size_t n_args, const mp_obj_t *args) { - mp_ctx = mp_ctx_from_ctx(the_ctx); + // This might be called before the ctx is ready. + // HACK: this will go away with the new drawing API. + while (the_ctx == NULL) { + vTaskDelay(100 / portTICK_PERIOD_MS); + } + mp_obj_t mp_ctx = mp_ctx_from_ctx(the_ctx); return mp_ctx; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_ctx_obj, 0, 0, mp_get_ctx); -- GitLab