Skip to content

Commit a54b57e

Browse files
committed
watchdog implementation for rp2040
1 parent 45b3c9a commit a54b57e

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

ports/raspberrypi/common-hal/microcontroller/__init__.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
112112
};
113113
#endif
114114

115+
#if CIRCUITPY_WATCHDOG
116+
// The singleton watchdog.WatchDogTimer object.
117+
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
118+
.base = {
119+
.type = &watchdog_watchdogtimer_type,
120+
},
121+
.timeout = 0.0f,
122+
.mode = WATCHDOGMODE_NONE,
123+
};
124+
#endif
125+
115126
// This maps MCU pin names to pin objects.
116127
const mp_rom_map_elem_t mcu_pin_global_dict_table[TOTAL_GPIO_COUNT] = {
117128
{ MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) },
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No watchdog module functions.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "common-hal/watchdog/WatchDogTimer.h"
29+
30+
#include "shared-bindings/watchdog/__init__.h"
31+
#include "shared-bindings/microcontroller/__init__.h"
32+
33+
#include "hardware/watchdog.h"
34+
35+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
36+
watchdog_update();
37+
}
38+
39+
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;
44+
}
45+
}
46+
47+
/*
48+
void watchdog_reset(void) {
49+
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
50+
}
51+
*/
52+
53+
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
54+
return self->timeout;
55+
}
56+
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"));
63+
}
64+
if ((uint16_t)self->timeout != timeout) {
65+
watchdog_enable(timeout, false);
66+
self->timeout = new_timeout;
67+
}
68+
}
69+
70+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
71+
return self->mode;
72+
}
73+
74+
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) {
79+
common_hal_watchdog_deinit(self);
80+
}
81+
self->mode = new_mode;
82+
}
83+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
28+
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
29+
30+
#include "py/obj.h"
31+
#include "shared-bindings/watchdog/WatchDogMode.h"
32+
#include "shared-bindings/watchdog/WatchDogTimer.h"
33+
34+
struct _watchdog_watchdogtimer_obj_t {
35+
mp_obj_base_t base;
36+
mp_float_t timeout;
37+
watchdog_watchdogmode_t mode;
38+
};
39+
40+
// This needs to be called in order to disable the watchdog
41+
// void watchdog_reset(void);
42+
43+
#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No watchdog module functions.

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ CIRCUITPY_NVM = 0
3636
CIRCUITPY_PULSEIO = 0 # Use PIO interally
3737
CIRCUITPY_ROTARYIO = 0 # Use PIO interally
3838
CIRCUITPY_RTC = 0
39+
CIRCUITPY_WATCHDOG = 1
3940

4041
# Things that are unsupported by the hardware.
4142
CIRCUITPY_AUDIOIO = 0

0 commit comments

Comments
 (0)