Skip to content

Commit d38cf05

Browse files
committed
samd51: Add watchdog timer
1 parent 74457d8 commit d38cf05

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 pet(void) {
38+
WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY;
39+
}
40+
41+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
42+
pet();
43+
}
44+
45+
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
46+
if (self->mode == WATCHDOGMODE_RESET) {
47+
mp_raise_RuntimeError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
48+
} else {
49+
self->mode = WATCHDOGMODE_NONE;
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 setup_wdt(int setting) {
58+
OSC32KCTRL->OSCULP32K.bit.EN1K = 1; // Enable out 1K (for WDT)
59+
60+
// disable watchdog for config
61+
WDT->CTRLA.reg = 0;
62+
while (WDT->SYNCBUSY.reg) { // Sync CTRL write
63+
}
64+
65+
WDT->INTENCLR.bit.EW = 1; // Disable early warning interrupt
66+
WDT->CONFIG.bit.PER = setting; // Set period for chip reset
67+
WDT->CTRLA.bit.WEN = 0; // Disable window mode
68+
while (WDT->SYNCBUSY.reg) { // Sync CTRL write
69+
}
70+
pet(); // Clear watchdog interval
71+
WDT->CTRLA.bit.ENABLE = 1; // Start watchdog now!
72+
while (WDT->SYNCBUSY.reg) {
73+
}
74+
}
75+
76+
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
77+
int wdt_cycles = (int)(new_timeout * 1000);
78+
if (wdt_cycles < 8) {
79+
wdt_cycles = 8;
80+
}
81+
if (wdt_cycles > 16384) {
82+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
83+
}
84+
// ceil(log2(n)) = 32 - __builtin_clz(n - 1) when n > 1 (if int is 32 bits)
85+
int log2_wdt_cycles = (sizeof(int) * CHAR_BIT) - __builtin_clz(wdt_cycles - 1);
86+
int setting = log2_wdt_cycles - 3; // CYC8_Val is 0
87+
float timeout = (8 << setting) / 1024.f;
88+
89+
if (self->mode == WATCHDOGMODE_RESET) {
90+
setup_wdt(setting);
91+
}
92+
self->timeout = timeout;
93+
}
94+
95+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
96+
return self->mode;
97+
}
98+
99+
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
100+
if (self->mode != new_mode) {
101+
if (new_mode == WATCHDOGMODE_RAISE) {
102+
mp_raise_NotImplementedError(translate("RAISE mode is not implemented"));
103+
} else if (new_mode == WATCHDOGMODE_NONE) {
104+
common_hal_watchdog_deinit(self);
105+
}
106+
self->mode = new_mode;
107+
common_hal_watchdog_set_timeout(self, self->timeout);
108+
}
109+
}
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
######################################################################

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)