3232
3333#include "src/rp2_common/hardware_watchdog/include/hardware/watchdog.h"
3434
35+ #define WATCHDOG_ENABLE watchdog_enable(self->timeout * 1000, false)
36+
3537void common_hal_watchdog_feed (watchdog_watchdogtimer_obj_t * self ) {
3638 watchdog_update ();
3739}
3840
3941void common_hal_watchdog_deinit (watchdog_watchdogtimer_obj_t * self ) {
40- if (self -> mode == WATCHDOGMODE_RESET ) {
41- mp_raise_RuntimeError (translate ("WatchDogTimer cannot be deinitialized once mode is set to RESET" ));
42- } else {
43- self -> mode = WATCHDOGMODE_NONE ;
42+ if (self -> mode == WATCHDOGMODE_NONE ) {
43+ return ;
4444 }
45+ hw_clear_bits (& watchdog_hw -> ctrl , WATCHDOG_CTRL_ENABLE_BITS );
46+ self -> mode = WATCHDOGMODE_NONE ;
4547}
4648
47- /*
4849void watchdog_reset (void ) {
49- common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
50+ common_hal_watchdog_deinit (& common_hal_mcu_watchdogtimer_obj );
5051}
51- */
5252
5353mp_float_t common_hal_watchdog_get_timeout (watchdog_watchdogtimer_obj_t * self ) {
5454 return self -> timeout ;
5555}
5656
5757void common_hal_watchdog_set_timeout (watchdog_watchdogtimer_obj_t * self , mp_float_t new_timeout ) {
58- // max timeout is 8.388607 sec
59- // this is rounded down to 8.388 sec
60- uint64_t timeout = new_timeout * 1000 ;
61- if (timeout > 8388 ) {
62- mp_raise_ValueError (translate ("timeout duration exceeded the maximum supported value" ));
58+ if (!(self -> timeout < new_timeout || self -> timeout > new_timeout )) {
59+ return ;
6360 }
64- if ((uint16_t )self -> timeout != timeout ) {
65- watchdog_enable (timeout , false);
66- self -> timeout = new_timeout ;
61+
62+ // max timeout is 8.388607 sec, this is rounded down to 8 sec
63+ mp_arg_validate_int_max (new_timeout , 8 , MP_QSTR_timeout );
64+ self -> timeout = new_timeout ;
65+
66+ if (self -> mode == WATCHDOGMODE_RESET ) {
67+ WATCHDOG_ENABLE ;
6768 }
6869}
6970
@@ -72,12 +73,23 @@ watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_
7273}
7374
7475void common_hal_watchdog_set_mode (watchdog_watchdogtimer_obj_t * self , watchdog_watchdogmode_t new_mode ) {
75- if (self -> mode != new_mode ) {
76- if (new_mode == WATCHDOGMODE_RAISE ) {
77- mp_raise_NotImplementedError (translate ("RAISE mode is not implemented" ));
78- } else if (new_mode == WATCHDOGMODE_NONE ) {
76+ if (self -> mode == new_mode ) {
77+ return ;
78+ }
79+
80+ switch (new_mode ) {
81+ case WATCHDOGMODE_NONE :
7982 common_hal_watchdog_deinit (self );
80- }
81- self -> mode = new_mode ;
83+ break ;
84+ case WATCHDOGMODE_RAISE :
85+ mp_raise_NotImplementedError (NULL );
86+ break ;
87+ case WATCHDOGMODE_RESET :
88+ WATCHDOG_ENABLE ;
89+ break ;
90+ default :
91+ return ;
8292 }
93+
94+ self -> mode = new_mode ;
8395}
0 commit comments