3333#include "esp_task_wdt.h"
3434
3535extern void esp_task_wdt_isr_user_handler (void );
36+
3637void esp_task_wdt_isr_user_handler (void ) {
38+ // just delete, deiniting TWDT in isr context causes a crash
39+ if (esp_task_wdt_delete (NULL ) == ESP_OK ) {
40+ watchdog_watchdogtimer_obj_t * self = & common_hal_mcu_watchdogtimer_obj ;
41+ self -> mode = WATCHDOGMODE_NONE ;
42+ }
43+
44+ // schedule watchdog timeout exception
3745 mp_obj_exception_clear_traceback (MP_OBJ_FROM_PTR (& mp_watchdog_timeout_exception ));
3846 MP_STATE_THREAD (mp_pending_exception ) = & mp_watchdog_timeout_exception ;
47+
3948 #if MICROPY_ENABLE_SCHEDULER
4049 if (MP_STATE_VM (sched_state ) == MP_SCHED_IDLE ) {
4150 MP_STATE_VM (sched_state ) = MP_SCHED_PENDING ;
@@ -44,12 +53,13 @@ void esp_task_wdt_isr_user_handler(void) {
4453}
4554
4655void common_hal_watchdog_feed (watchdog_watchdogtimer_obj_t * self ) {
47- if (esp_task_wdt_reset () != ESP_OK ) {
48- mp_raise_RuntimeError (translate ("watchdog not initialized" ));
49- }
56+ esp_task_wdt_reset ();
5057}
5158
5259void common_hal_watchdog_deinit (watchdog_watchdogtimer_obj_t * self ) {
60+ if (self -> mode == WATCHDOGMODE_NONE ) {
61+ return ;
62+ }
5363 if (esp_task_wdt_delete (NULL ) == ESP_OK && esp_task_wdt_deinit () == ESP_OK ) {
5464 self -> mode = WATCHDOGMODE_NONE ;
5565 }
@@ -59,11 +69,11 @@ void watchdog_reset(void) {
5969 common_hal_watchdog_deinit (& common_hal_mcu_watchdogtimer_obj );
6070}
6171
62- static void wdt_config (watchdog_watchdogtimer_obj_t * self ) {
72+ static void wdt_config (uint32_t timeout , watchdog_watchdogmode_t mode ) {
6373 // enable panic hanler in WATCHDOGMODE_RESET mode
6474 // initialize Task Watchdog Timer (TWDT)
65- if (esp_task_wdt_init (( uint32_t ) self -> timeout , ( self -> mode == WATCHDOGMODE_RESET ) ) != ESP_OK ) {
66- mp_raise_RuntimeError ( translate ( "Initialization failed due to lack of memory" ) );
75+ if (esp_task_wdt_init (timeout , mode == WATCHDOGMODE_RESET ) != ESP_OK ) {
76+ mp_raise_msg ( & mp_type_MemoryError , NULL );
6777 }
6878 esp_task_wdt_add (NULL );
6979}
@@ -73,12 +83,17 @@ mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
7383}
7484
7585void common_hal_watchdog_set_timeout (watchdog_watchdogtimer_obj_t * self , mp_float_t new_timeout ) {
86+ if (!(self -> timeout < new_timeout || self -> timeout > new_timeout )) {
87+ return ;
88+ }
89+
7690 if ((uint64_t )new_timeout > UINT32_MAX ) {
77- mp_raise_ValueError (translate ("timeout duration exceeded the maximum supported value" ) );
91+ mp_raise_ValueError_varg (translate ("%q must be <= %d" ), MP_QSTR_timeout , UINT32_MAX );
7892 }
79- if ((uint32_t )self -> timeout != (uint32_t )new_timeout ) {
80- self -> timeout = new_timeout ;
81- wdt_config (self );
93+ self -> timeout = new_timeout ;
94+
95+ if (self -> mode != WATCHDOGMODE_NONE ) {
96+ wdt_config (new_timeout , self -> mode );
8297 }
8398}
8499
@@ -87,8 +102,21 @@ watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_
87102}
88103
89104void common_hal_watchdog_set_mode (watchdog_watchdogtimer_obj_t * self , watchdog_watchdogmode_t new_mode ) {
90- if (self -> mode != new_mode ) {
91- self -> mode = new_mode ;
92- wdt_config (self );
105+ if (self -> mode == new_mode ) {
106+ return ;
93107 }
108+
109+ switch (new_mode ) {
110+ case WATCHDOGMODE_NONE :
111+ common_hal_watchdog_deinit (self );
112+ break ;
113+ case WATCHDOGMODE_RAISE :
114+ case WATCHDOGMODE_RESET :
115+ wdt_config (self -> timeout , new_mode );
116+ break ;
117+ default :
118+ return ;
119+ }
120+
121+ self -> mode = new_mode ;
94122}
0 commit comments