From 52dc48b2a0da912396bd3224cd9c906f4e6288ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Tue, 4 Jul 2023 15:35:29 +0200 Subject: [PATCH] esp32/machine_wdt: Allow feeding WDT from threads. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes the ESP32 WDT implementation to use a custom handle so that it becomes possible to reset the WDT from a thread. By default esp_task_wdt_add subscribes the task_id of the current task. That means that if we're running in a different task we are unable to reset the WDT, which prevents feeding the WDT from a thread directly, or even from a timer (which may randomly run in a different task when there's multiple threads). As an added bonus, the name we set makes the error clearly specify that it was the user-specified WDT that reset the chip. Signed-off-by: Daniƫl van de Giessen --- ports/esp32/machine_wdt.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index 2cb6c51817..bf924c35e0 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -37,9 +37,12 @@ const mp_obj_type_t machine_wdt_type; typedef struct _machine_wdt_obj_t { mp_obj_base_t base; + esp_task_wdt_user_handle_t twdt_user_handle; } machine_wdt_obj_t; -STATIC machine_wdt_obj_t wdt_default = {{&machine_wdt_type}}; +STATIC machine_wdt_obj_t wdt_default = { + {&machine_wdt_type}, 0 +}; STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_id, ARG_timeout }; @@ -68,14 +71,22 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args mp_raise_OSError(rs_code); } - esp_task_wdt_add(NULL); + if (wdt_default.twdt_user_handle == NULL) { + rs_code = esp_task_wdt_add_user("mpy_machine_wdt", &wdt_default.twdt_user_handle); + if (rs_code != ESP_OK) { + mp_raise_OSError(rs_code); + } + } return &wdt_default; } STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { (void)self_in; - esp_task_wdt_reset(); + mp_int_t rs_code = esp_task_wdt_reset_user(wdt_default.twdt_user_handle); + if (rs_code != ESP_OK) { + mp_raise_OSError(rs_code); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed);