33
33
#include "esp_task_wdt.h"
34
34
35
35
extern void esp_task_wdt_isr_user_handler (void );
36
+
36
37
void 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
37
45
mp_obj_exception_clear_traceback (MP_OBJ_FROM_PTR (& mp_watchdog_timeout_exception ));
38
46
MP_STATE_THREAD (mp_pending_exception ) = & mp_watchdog_timeout_exception ;
47
+
39
48
#if MICROPY_ENABLE_SCHEDULER
40
49
if (MP_STATE_VM (sched_state ) == MP_SCHED_IDLE ) {
41
50
MP_STATE_VM (sched_state ) = MP_SCHED_PENDING ;
@@ -44,12 +53,13 @@ void esp_task_wdt_isr_user_handler(void) {
44
53
}
45
54
46
55
void 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 ();
50
57
}
51
58
52
59
void common_hal_watchdog_deinit (watchdog_watchdogtimer_obj_t * self ) {
60
+ if (self -> mode == WATCHDOGMODE_NONE ) {
61
+ return ;
62
+ }
53
63
if (esp_task_wdt_delete (NULL ) == ESP_OK && esp_task_wdt_deinit () == ESP_OK ) {
54
64
self -> mode = WATCHDOGMODE_NONE ;
55
65
}
@@ -59,11 +69,11 @@ void watchdog_reset(void) {
59
69
common_hal_watchdog_deinit (& common_hal_mcu_watchdogtimer_obj );
60
70
}
61
71
62
- static void wdt_config (watchdog_watchdogtimer_obj_t * self ) {
72
+ static void wdt_config (uint32_t timeout , watchdog_watchdogmode_t mode ) {
63
73
// enable panic hanler in WATCHDOGMODE_RESET mode
64
74
// 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 );
67
77
}
68
78
esp_task_wdt_add (NULL );
69
79
}
@@ -73,12 +83,17 @@ mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
73
83
}
74
84
75
85
void 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
+
76
90
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 );
78
92
}
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 );
82
97
}
83
98
}
84
99
@@ -87,8 +102,21 @@ watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_
87
102
}
88
103
89
104
void 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 ;
93
107
}
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 ;
94
122
}
0 commit comments