Skip to content

Commit 8ff4081

Browse files
committed
update watchdog implementation for raspberrypi
1 parent 0acf2e8 commit 8ff4081

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

ports/raspberrypi/common-hal/watchdog/WatchDogTimer.c

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,39 @@
3232

3333
#include "src/rp2_common/hardware_watchdog/include/hardware/watchdog.h"
3434

35+
#define WATCHDOG_ENABLE watchdog_enable(self->timeout * 1000, false)
36+
3537
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
3638
watchdog_update();
3739
}
3840

3941
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;
4444
}
45+
hw_clear_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_ENABLE_BITS);
46+
self->mode = WATCHDOGMODE_NONE;
4547
}
4648

47-
/*
4849
void 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

5353
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
5454
return self->timeout;
5555
}
5656

5757
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;
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

7475
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:
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
}

ports/raspberrypi/common-hal/watchdog/WatchDogTimer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ struct _watchdog_watchdogtimer_obj_t {
3838
};
3939

4040
// This needs to be called in order to disable the watchdog
41-
// void watchdog_reset(void);
41+
void watchdog_reset(void);
4242

4343
#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H

ports/raspberrypi/supervisor/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ void reset_port(void) {
201201
ssl_reset();
202202
#endif
203203

204+
#if CIRCUITPY_WATCHDOG
205+
watchdog_reset();
206+
#endif
207+
204208
#if CIRCUITPY_WIFI
205209
wifi_reset();
206210
#endif

0 commit comments

Comments
 (0)