32
32
33
33
#include "src/rp2_common/hardware_watchdog/include/hardware/watchdog.h"
34
34
35
+ #define WATCHDOG_ENABLE watchdog_enable(self->timeout * 1000, false)
36
+
35
37
void common_hal_watchdog_feed (watchdog_watchdogtimer_obj_t * self ) {
36
38
watchdog_update ();
37
39
}
38
40
39
41
void 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 ;
44
44
}
45
+ hw_clear_bits (& watchdog_hw -> ctrl , WATCHDOG_CTRL_ENABLE_BITS );
46
+ self -> mode = WATCHDOGMODE_NONE ;
45
47
}
46
48
47
- /*
48
49
void watchdog_reset (void ) {
49
- common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
50
+ common_hal_watchdog_deinit (& common_hal_mcu_watchdogtimer_obj );
50
51
}
51
- */
52
52
53
53
mp_float_t common_hal_watchdog_get_timeout (watchdog_watchdogtimer_obj_t * self ) {
54
54
return self -> timeout ;
55
55
}
56
56
57
57
void 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 ;
63
60
}
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 ;
67
68
}
68
69
}
69
70
@@ -72,12 +73,23 @@ watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_
72
73
}
73
74
74
75
void 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 :
79
82
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 ;
82
92
}
93
+
94
+ self -> mode = new_mode ;
83
95
}
0 commit comments