From 11cf031284d42228f6b6d4af35cf07155ac2cc4f Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Wed, 15 Mar 2023 19:03:57 +0530 Subject: [PATCH] reset watchdog conditionally --- main.c | 6 ++++++ .../common-hal/watchdog/WatchDogTimer.c | 15 ++++++++++++++- .../common-hal/watchdog/WatchDogTimer.c | 16 ++++++++++++++-- ports/nrf/common-hal/watchdog/WatchDogTimer.c | 15 +++++++++++++-- .../common-hal/watchdog/WatchDogTimer.c | 18 +++++++++++++++--- shared/runtime/pyexec.h | 4 ++++ 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 002c97c167..2b4545379e 100644 --- a/main.c +++ b/main.c @@ -243,6 +243,12 @@ void supervisor_execution_status(void) { } #endif +#if CIRCUITPY_WATCHDOG +pyexec_result_t *pyexec_result(void) { + return &_exec_result; +} +#endif + // Look for the first file that exists in the list of filenames, using mp_import_stat(). // Return its index. If no file found, return -1. STATIC const char *first_existing_file_in_list(const char *const *filenames, size_t n_filenames) { diff --git a/ports/atmel-samd/common-hal/watchdog/WatchDogTimer.c b/ports/atmel-samd/common-hal/watchdog/WatchDogTimer.c index 0c0ef85bb2..15683a1275 100644 --- a/ports/atmel-samd/common-hal/watchdog/WatchDogTimer.c +++ b/ports/atmel-samd/common-hal/watchdog/WatchDogTimer.c @@ -28,8 +28,12 @@ #include "py/runtime.h" +#include "shared/runtime/pyexec.h" + #include "shared-bindings/watchdog/__init__.h" #include "shared-bindings/watchdog/WatchDogTimer.h" +#include "shared-bindings/microcontroller/__init__.h" + #include "common-hal/watchdog/WatchDogTimer.h" #include "component/wdt.h" @@ -79,7 +83,16 @@ void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { } void watchdog_reset(void) { - common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); + watchdog_watchdogtimer_obj_t *self = &common_hal_mcu_watchdogtimer_obj; + if (self->mode == WATCHDOGMODE_RESET) { + mp_obj_t exception = pyexec_result()->exception; + if (exception != MP_OBJ_NULL && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + return; + } + } + common_hal_watchdog_deinit(self); } mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { diff --git a/ports/espressif/common-hal/watchdog/WatchDogTimer.c b/ports/espressif/common-hal/watchdog/WatchDogTimer.c index 6a7c91622e..b11da03770 100644 --- a/ports/espressif/common-hal/watchdog/WatchDogTimer.c +++ b/ports/espressif/common-hal/watchdog/WatchDogTimer.c @@ -25,11 +25,14 @@ */ #include "py/runtime.h" -#include "common-hal/watchdog/WatchDogTimer.h" + +#include "shared/runtime/pyexec.h" #include "shared-bindings/watchdog/__init__.h" #include "shared-bindings/microcontroller/__init__.h" +#include "common-hal/watchdog/WatchDogTimer.h" + #include "esp_task_wdt.h" extern void esp_task_wdt_isr_user_handler(void); @@ -66,7 +69,16 @@ void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { } void watchdog_reset(void) { - common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); + watchdog_watchdogtimer_obj_t *self = &common_hal_mcu_watchdogtimer_obj; + if (self->mode == WATCHDOGMODE_RESET) { + mp_obj_t exception = pyexec_result()->exception; + if (exception != MP_OBJ_NULL && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + return; + } + } + common_hal_watchdog_deinit(self); } static void wdt_config(uint32_t timeout, watchdog_watchdogmode_t mode) { diff --git a/ports/nrf/common-hal/watchdog/WatchDogTimer.c b/ports/nrf/common-hal/watchdog/WatchDogTimer.c index 99c360c46d..872c4642db 100644 --- a/ports/nrf/common-hal/watchdog/WatchDogTimer.c +++ b/ports/nrf/common-hal/watchdog/WatchDogTimer.c @@ -33,12 +33,14 @@ #include "py/objproperty.h" #include "py/runtime.h" -#include "common-hal/watchdog/WatchDogTimer.h" +#include "shared/runtime/pyexec.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/watchdog/__init__.h" #include "shared-bindings/watchdog/WatchDogTimer.h" +#include "common-hal/watchdog/WatchDogTimer.h" + #include "supervisor/port.h" #include "nrf/timers.h" @@ -108,7 +110,16 @@ void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { } void watchdog_reset(void) { - common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); + watchdog_watchdogtimer_obj_t *self = &common_hal_mcu_watchdogtimer_obj; + if (self->mode == WATCHDOGMODE_RESET) { + mp_obj_t exception = pyexec_result()->exception; + if (exception != MP_OBJ_NULL && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + return; + } + } + common_hal_watchdog_deinit(self); } mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { diff --git a/ports/raspberrypi/common-hal/watchdog/WatchDogTimer.c b/ports/raspberrypi/common-hal/watchdog/WatchDogTimer.c index 14ae9c7f9b..29234b46b0 100644 --- a/ports/raspberrypi/common-hal/watchdog/WatchDogTimer.c +++ b/ports/raspberrypi/common-hal/watchdog/WatchDogTimer.c @@ -25,12 +25,15 @@ */ #include "py/runtime.h" -#include "common-hal/watchdog/WatchDogTimer.h" + +#include "shared/runtime/pyexec.h" #include "shared-bindings/watchdog/__init__.h" #include "shared-bindings/microcontroller/__init__.h" -#include "src/rp2_common/hardware_watchdog/include/hardware/watchdog.h" +#include "common-hal/watchdog/WatchDogTimer.h" + +#include "hardware/watchdog.h" #define WATCHDOG_ENABLE watchdog_enable(self->timeout * 1000, false) @@ -47,7 +50,16 @@ void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { } void watchdog_reset(void) { - common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); + watchdog_watchdogtimer_obj_t *self = &common_hal_mcu_watchdogtimer_obj; + if (self->mode == WATCHDOGMODE_RESET) { + mp_obj_t exception = pyexec_result()->exception; + if (exception != MP_OBJ_NULL && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) && + exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + return; + } + } + common_hal_watchdog_deinit(self); } mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { diff --git a/shared/runtime/pyexec.h b/shared/runtime/pyexec.h index 411426eaaa..d0c012fe46 100644 --- a/shared/runtime/pyexec.h +++ b/shared/runtime/pyexec.h @@ -67,6 +67,10 @@ extern uint8_t pyexec_repl_active; int pyexec_exit_handler(const void *source, pyexec_result_t *result); #endif +#if CIRCUITPY_WATCHDOG +pyexec_result_t *pyexec_result(void); +#endif + #if MICROPY_REPL_INFO mp_obj_t pyb_set_repl_info(mp_obj_t o_value); MP_DECLARE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj);