@@ -69,88 +69,46 @@ STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void
69
69
#endif
70
70
}
71
71
72
- // This function is called if the timer expires. The system will reboot in 1/16384 of a second.
73
- // Issue a reboot ourselves so we can do any cleanup necessary.
72
+ static void timer_free (void ) {
73
+ timer_refcount -- ;
74
+ if (timer_refcount == 0 ) {
75
+ nrf_peripherals_free_timer (timer );
76
+ timer = NULL ;
77
+ }
78
+ }
79
+
80
+ // This function is called if the timer expires. The system will reboot
81
+ // in 1/16384 of a second. Issue a reboot ourselves so we can do any
82
+ // cleanup necessary.
74
83
STATIC void watchdogtimer_watchdog_event_handler (void ) {
75
84
reset_cpu ();
76
85
}
77
86
78
- //| def feed(self):
79
- //| """Feed the watchdog timer. This must be called regularly, otherwise
80
- //| the timer will expire."""
81
- //| ...
82
- //|
83
- STATIC mp_obj_t watchdog_watchdogtimer_feed (mp_obj_t self_in ) {
84
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
85
-
87
+ void common_hal_watchdog_feed (watchdog_watchdogtimer_obj_t * self ) {
86
88
if (self -> mode == WATCHDOGMODE_RESET ) {
87
89
nrfx_wdt_feed (& wdt );
88
90
} else if (self -> mode == WATCHDOGMODE_RAISE ) {
89
91
nrfx_timer_clear (timer );
90
- } else if (self -> mode == WATCHDOGMODE_NONE ) {
91
- mp_raise_ValueError (translate ("WatchDogTimer is not currently running" ));
92
92
}
93
- return mp_const_none ;
94
93
}
95
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (watchdog_watchdogtimer_feed_obj , watchdog_watchdogtimer_feed );
96
-
97
- //| def deinit(self):
98
- //| """Stop the watchdog timer. This may raise an error if the watchdog
99
- //| timer cannot be disabled on this platform."""
100
- //| ...
101
- //|
102
- STATIC mp_obj_t watchdog_watchdogtimer_deinit (mp_obj_t self_in ) {
103
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
104
94
105
- if (self -> mode == WATCHDOGMODE_RAISE ) {
106
- timer_refcount -- ;
107
- if (timer_refcount == 0 ) {
108
- nrf_peripherals_free_timer (timer );
109
- timer = NULL ;
110
- }
111
- self -> mode = WATCHDOGMODE_NONE ;
112
- } else if (self -> mode == WATCHDOGMODE_RESET ) {
113
- mp_raise_NotImplementedError (translate ("WatchDogTimer cannot be deinitialized once mode is set to RESET" ));
95
+ void common_hal_watchdog_deinit (watchdog_watchdogtimer_obj_t * self ) {
96
+ if (timer ) {
97
+ timer_free ();
114
98
}
115
-
116
- return mp_const_none ;
99
+ self -> mode = WATCHDOGMODE_NONE ;
117
100
}
118
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (watchdog_watchdogtimer_deinit_obj , watchdog_watchdogtimer_deinit );
119
101
120
102
void watchdog_reset (void ) {
121
- if (common_hal_mcu_watchdogtimer_obj .mode == WATCHDOGMODE_RAISE ) {
122
- common_hal_mcu_watchdogtimer_obj .mode = WATCHDOGMODE_NONE ;
123
- timer_refcount -- ;
124
- if (timer_refcount == 0 ) {
125
- nrf_peripherals_free_timer (timer );
126
- timer = NULL ;
127
- }
128
- }
103
+ common_hal_watchdog_deinit (& common_hal_mcu_watchdogtimer_obj );
129
104
}
130
105
131
- //| timeout: float = ...
132
- //| """The maximum number of seconds that can elapse between calls
133
- //| to feed()"""
134
- //|
135
- STATIC mp_obj_t watchdog_watchdogtimer_obj_get_timeout (mp_obj_t self_in ) {
136
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
137
- return mp_obj_new_float (self -> timeout );
106
+ mp_float_t common_hal_watchdog_get_timeout (watchdog_watchdogtimer_obj_t * self ) {
107
+ return self -> timeout ;
138
108
}
139
- MP_DEFINE_CONST_FUN_OBJ_1 (watchdog_watchdogtimer_get_timeout_obj , watchdog_watchdogtimer_obj_get_timeout );
140
-
141
- STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout (mp_obj_t self_in , mp_obj_t timeout_obj ) {
142
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
143
- mp_float_t timeout = mp_obj_get_float (timeout_obj );
144
-
145
- if (timeout <= 0 ) {
146
- mp_raise_ValueError (translate ("watchdog timeout must be greater than 0" ));
147
- }
148
109
149
- if (self -> mode == WATCHDOGMODE_RESET ) {
150
- // If the WatchDogTimer is already running in "RESET" mode, raise an error
151
- // since the mode cannot be changed once started.
152
- mp_raise_TypeError (translate ("cannot change the timeout once mode is WatchDogMode.RESET" ));
153
- } else if (self -> mode == WATCHDOGMODE_RAISE ) {
110
+ void common_hal_watchdog_set_timeout (watchdog_watchdogtimer_obj_t * self , mp_float_t timeout ) {
111
+ if (self -> mode == WATCHDOGMODE_RAISE ) {
154
112
// If the WatchDogTimer is already running in "RAISE" mode, reset the timer
155
113
// with the new value.
156
114
uint64_t ticks = timeout * 31250ULL ;
@@ -162,108 +120,42 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_
162
120
}
163
121
164
122
self -> timeout = timeout ;
165
- return mp_const_none ;
166
123
}
167
- MP_DEFINE_CONST_FUN_OBJ_2 (watchdog_watchdogtimer_set_timeout_obj , watchdog_watchdogtimer_obj_set_timeout );
168
-
169
- const mp_obj_property_t watchdog_watchdogtimer_timeout_obj = {
170
- .base .type = & mp_type_property ,
171
- .proxy = {(mp_obj_t )& watchdog_watchdogtimer_get_timeout_obj ,
172
- (mp_obj_t )& watchdog_watchdogtimer_set_timeout_obj ,
173
- (mp_obj_t )& mp_const_none_obj },
174
- };
175
124
176
- //| mode: watchdog.WatchDogMode = ...
177
- //| """The current operating mode of the WatchDogTimer `watchdog.WatchDogMode`.
178
- //|
179
- //| Setting a WatchDogMode activates the WatchDog::
180
- //|
181
- //| import microcontroller
182
- //| import watchdog
183
- //|
184
- //| w = microcontroller.watchdog
185
- //| w.timeout = 5
186
- //| w.mode = watchdog.WatchDogMode.RAISE
187
- //|
188
- //|
189
- //| Once set, the WatchDogTimer will perform the specified action if the timer expires.
190
- //|
191
- STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode (mp_obj_t self_in ) {
192
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
193
- switch (self -> mode ) {
194
- case WATCHDOGMODE_NONE : default : return (mp_obj_t )MP_ROM_PTR (& watchdog_watchdogmode_none_obj );
195
- case WATCHDOGMODE_RAISE : return (mp_obj_t )MP_ROM_PTR (& watchdog_watchdogmode_raise_obj );
196
- case WATCHDOGMODE_RESET : return (mp_obj_t )MP_ROM_PTR (& watchdog_watchdogmode_reset_obj );
197
- }
125
+ watchdog_watchdogmode_t common_hal_watchdog_get_mode (watchdog_watchdogtimer_obj_t * self ) {
126
+ return self -> mode ;
198
127
}
199
- MP_DEFINE_CONST_FUN_OBJ_1 (watchdog_watchdogtimer_get_mode_obj , watchdog_watchdogtimer_obj_get_mode );
200
128
201
- STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode (mp_obj_t self_in , mp_obj_t mode_obj ) {
202
- watchdog_watchdogtimer_obj_t * self = MP_OBJ_TO_PTR (self_in );
203
- watchdog_watchdogmode_obj_t * mode = MP_OBJ_TO_PTR (mode_obj );
204
- if (mode == MP_ROM_PTR (& watchdog_watchdogmode_none_obj )) {
205
- if (self -> mode == WATCHDOGMODE_RESET ) {
206
- mp_raise_TypeError (translate ("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET" ));
207
- }
208
- else if (self -> mode == WATCHDOGMODE_RAISE ) {
209
- timer_refcount -- ;
210
- if (timer_refcount == 0 ) {
211
- nrf_peripherals_free_timer (timer );
212
- timer = NULL ;
213
- }
214
- }
215
- self -> mode = WATCHDOGMODE_NONE ;
129
+ void common_hal_watchdog_set_mode (watchdog_watchdogtimer_obj_t * self , watchdog_watchdogmode_t new_mode ) {
130
+ watchdog_watchdogmode_t current_mode = self -> mode ;
216
131
217
- } else if (mode == MP_ROM_PTR (& watchdog_watchdogmode_raise_obj )) {
218
- if (self -> timeout <= 0 ) {
219
- mp_raise_ValueError (translate ("watchdog timeout must be greater than 0" ));
220
- }
221
- if (self -> mode == WATCHDOGMODE_RESET ) {
222
- mp_raise_ValueError (translate ("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET" ));
132
+ if (new_mode == WATCHDOGMODE_RAISE ) {
133
+ if (timer_refcount == 0 ) {
134
+ timer = nrf_peripherals_allocate_timer_or_throw ();
223
135
}
224
- else if (self -> mode == WATCHDOGMODE_NONE || self -> mode == WATCHDOGMODE_RAISE ) {
225
- if (timer_refcount == 0 ) {
226
- timer = nrf_peripherals_allocate_timer_or_throw ();
227
- }
228
- if (timer == NULL ) {
229
- mp_raise_RuntimeError (translate ("timer was null" ));
230
- }
231
- timer_refcount ++ ;
232
-
233
- nrfx_timer_config_t timer_config = {
234
- .frequency = NRF_TIMER_FREQ_31250Hz ,
235
- .mode = NRF_TIMER_MODE_TIMER ,
236
- .bit_width = NRF_TIMER_BIT_WIDTH_32 ,
237
- .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY ,
238
- .p_context = self ,
239
- };
240
-
241
- nrfx_timer_init (timer , & timer_config , & watchdogtimer_timer_event_handler );
136
+ timer_refcount ++ ;
137
+
138
+ nrfx_timer_config_t timer_config = {
139
+ .frequency = NRF_TIMER_FREQ_31250Hz ,
140
+ .mode = NRF_TIMER_MODE_TIMER ,
141
+ .bit_width = NRF_TIMER_BIT_WIDTH_32 ,
142
+ .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY ,
143
+ .p_context = self ,
144
+ };
242
145
243
- uint64_t ticks = nrfx_timer_ms_to_ticks (timer , self -> timeout * 1000 );
244
- if (ticks > UINT32_MAX ) {
245
- mp_raise_ValueError (translate ("timeout duration exceeded the maximum supported value" ));
246
- }
146
+ nrfx_timer_init (timer , & timer_config , & watchdogtimer_timer_event_handler );
247
147
248
- // true enables interrupt.
249
- nrfx_timer_clear (timer );
250
- nrfx_timer_compare (timer , NRF_TIMER_CC_CHANNEL0 , ticks , true);
251
- nrfx_timer_resume (timer );
148
+ uint64_t ticks = nrfx_timer_ms_to_ticks (timer , self -> timeout * 1000 );
149
+ if (ticks > UINT32_MAX ) {
150
+ mp_raise_ValueError (translate ("timeout duration exceeded the maximum supported value" ));
252
151
}
253
- self -> mode = WATCHDOGMODE_RAISE ;
254
152
255
- } else if (mode == MP_ROM_PTR (& watchdog_watchdogmode_reset_obj )) {
256
- if (self -> timeout <= 0 ) {
257
- mp_raise_ValueError (translate ("watchdog timeout must be greater than 0" ));
258
- }
259
- if (self -> mode == WATCHDOGMODE_RAISE ) {
260
- timer_refcount -- ;
261
- if (timer_refcount == 0 ) {
262
- nrf_peripherals_free_timer (timer );
263
- timer = NULL ;
264
- }
265
- }
153
+ // true enables interrupt.
154
+ nrfx_timer_clear (timer );
155
+ nrfx_timer_compare (timer , NRF_TIMER_CC_CHANNEL0 , ticks , true);
156
+ nrfx_timer_resume (timer );
266
157
158
+ } else if (new_mode == WATCHDOGMODE_RESET ) {
267
159
uint64_t ticks = self -> timeout * 1000.0f ;
268
160
if (ticks > UINT32_MAX ) {
269
161
mp_raise_ValueError (translate ("timeout duration exceeded the maximum supported value" ));
@@ -286,31 +178,12 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t m
286
178
}
287
179
nrfx_wdt_enable (& wdt );
288
180
nrfx_wdt_feed (& wdt );
289
- self -> mode = WATCHDOGMODE_RESET ;
290
181
}
291
182
292
- return mp_const_none ;
293
- }
294
- MP_DEFINE_CONST_FUN_OBJ_2 (watchdog_watchdogtimer_set_mode_obj , watchdog_watchdogtimer_obj_set_mode );
295
-
296
- const mp_obj_property_t watchdog_watchdogtimer_mode_obj = {
297
- .base .type = & mp_type_property ,
298
- .proxy = {(mp_obj_t )& watchdog_watchdogtimer_get_mode_obj ,
299
- (mp_obj_t )& watchdog_watchdogtimer_set_mode_obj ,
300
- (mp_obj_t )& mp_const_none_obj },
301
- };
302
-
303
- STATIC const mp_rom_map_elem_t watchdog_watchdogtimer_locals_dict_table [] = {
304
- { MP_ROM_QSTR (MP_QSTR_feed ), MP_ROM_PTR (& watchdog_watchdogtimer_feed_obj ) },
305
- { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& watchdog_watchdogtimer_deinit_obj ) },
306
- { MP_ROM_QSTR (MP_QSTR_timeout ), MP_ROM_PTR (& watchdog_watchdogtimer_timeout_obj ) },
307
- { MP_ROM_QSTR (MP_QSTR_mode ), MP_ROM_PTR (& watchdog_watchdogtimer_mode_obj ) },
308
- };
309
- STATIC MP_DEFINE_CONST_DICT (watchdog_watchdogtimer_locals_dict , watchdog_watchdogtimer_locals_dict_table );
183
+ // If we just switched away from RAISE, disable the timmer.
184
+ if (current_mode == WATCHDOGMODE_RAISE && new_mode != WATCHDOGMODE_RAISE ) {
185
+ timer_free ();
186
+ }
310
187
311
- const mp_obj_type_t watchdog_watchdogtimer_type = {
312
- { & mp_type_type },
313
- .name = MP_QSTR_WatchDogTimer ,
314
- // .make_new = watchdog_watchdogtimer_make_new,
315
- .locals_dict = (mp_obj_dict_t * )& watchdog_watchdogtimer_locals_dict ,
316
- };
188
+ self -> mode = new_mode ;
189
+ }
0 commit comments