Skip to content

Commit a7a6b34

Browse files
authored
Merge pull request #5483 from jepler/samd51-watchdog
samd51: implement, enable watchdog
2 parents ec7e88b + 3979543 commit a7a6b34

File tree

8 files changed

+164
-1
lines changed

8 files changed

+164
-1
lines changed

ports/atmel-samd/common-hal/microcontroller/__init__.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
103103
};
104104
#endif
105105

106+
#if CIRCUITPY_WATCHDOG
107+
// The singleton watchdog.WatchDogTimer object.
108+
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
109+
.base = {
110+
.type = &watchdog_watchdogtimer_type,
111+
},
112+
.timeout = 0.0f,
113+
.mode = WATCHDOGMODE_NONE,
114+
};
115+
#endif
116+
106117
// This maps MCU pin names to pin objects.
107118
STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
108119
#if defined(PIN_PA00) && !defined(IGNORE_PIN_PA00)

ports/atmel-samd/common-hal/watchdog/WatchDogMode.c

Whitespace-only changes.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler for Adafruit Industries
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 <math.h>
28+
29+
#include "py/runtime.h"
30+
31+
#include "shared-bindings/watchdog/__init__.h"
32+
#include "shared-bindings/watchdog/WatchDogTimer.h"
33+
#include "common-hal/watchdog/WatchDogTimer.h"
34+
35+
#include "component/wdt.h"
36+
37+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
38+
WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY;
39+
}
40+
41+
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
42+
if (self->mode == WATCHDOGMODE_RESET) {
43+
mp_raise_RuntimeError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
44+
} else {
45+
self->mode = WATCHDOGMODE_NONE;
46+
}
47+
}
48+
49+
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
50+
return self->timeout;
51+
}
52+
53+
void setup_wdt(watchdog_watchdogtimer_obj_t *self, int setting) {
54+
OSC32KCTRL->OSCULP32K.bit.EN1K = 1; // Enable out 1K (for WDT)
55+
56+
// disable watchdog for config
57+
WDT->CTRLA.reg = 0;
58+
while (WDT->SYNCBUSY.reg) { // Sync CTRL write
59+
}
60+
61+
WDT->INTENCLR.bit.EW = 1; // Disable early warning interrupt
62+
WDT->CONFIG.bit.PER = setting; // Set period for chip reset
63+
WDT->CTRLA.bit.WEN = 0; // Disable window mode
64+
while (WDT->SYNCBUSY.reg) { // Sync CTRL write
65+
}
66+
common_hal_watchdog_feed(self); // Clear watchdog interval
67+
WDT->CTRLA.bit.ENABLE = 1; // Start watchdog now!
68+
while (WDT->SYNCBUSY.reg) {
69+
}
70+
}
71+
72+
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
73+
int wdt_cycles = (int)(new_timeout * 1000);
74+
if (wdt_cycles < 8) {
75+
wdt_cycles = 8;
76+
}
77+
if (wdt_cycles > 16384) {
78+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
79+
}
80+
// ceil(log2(n)) = 32 - __builtin_clz(n - 1) when n > 1 (if int is 32 bits)
81+
int log2_wdt_cycles = (sizeof(int) * CHAR_BIT) - __builtin_clz(wdt_cycles - 1);
82+
int setting = log2_wdt_cycles - 3; // CYC8_Val is 0
83+
float timeout = (8 << setting) / 1024.f;
84+
85+
if (self->mode == WATCHDOGMODE_RESET) {
86+
setup_wdt(self, setting);
87+
}
88+
self->timeout = timeout;
89+
}
90+
91+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
92+
return self->mode;
93+
}
94+
95+
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
96+
if (self->mode != new_mode) {
97+
if (new_mode == WATCHDOGMODE_RAISE) {
98+
mp_raise_NotImplementedError(translate("RAISE mode is not implemented"));
99+
} else if (new_mode == WATCHDOGMODE_NONE) {
100+
common_hal_watchdog_deinit(self);
101+
}
102+
self->mode = new_mode;
103+
common_hal_watchdog_set_timeout(self, self->timeout);
104+
}
105+
}
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

ports/atmel-samd/common-hal/watchdog/__init__.c

Whitespace-only changes.

ports/atmel-samd/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ CIRCUITPY_PS2IO ?= 1
102102
CIRCUITPY_SAMD ?= 1
103103
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FULL_BUILD)
104104
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
105+
CIRCUITPY_WATCHDOG ?= 1
105106

106107
endif # samd51
107108
######################################################################

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
15561556
char ch = mp_obj_get_int(arg);
15571557
mp_print_strn(&print, &ch, 1, flags, ' ', width);
15581558
} else {
1559-
mp_raise_TypeError(MP_ERROR_TEXT("integer required"));
1559+
mp_raise_TypeError(MP_ERROR_TEXT("%%c requires int or char"));
15601560
}
15611561
break;
15621562

shared-bindings/watchdog/__init__.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
2929

30+
#include "py/obj.h"
31+
#include "py/objexcept.h"
32+
3033
extern const mp_obj_module_t watchdog_module;
3134
extern mp_obj_exception_t mp_watchdog_timeout_exception;
3235
extern const mp_obj_type_t mp_type_WatchDogTimeout;

0 commit comments

Comments
 (0)