Skip to content

Commit 859b6ef

Browse files
danicamporadpgeorge
authored andcommitted
zephyr/machine_wdt: Add watchdog timer implementation.
Simple implementation in-line with the rest of the MicroPython ports Tested on the nRF52832 and the nRF5340. Signed-off-by: danicampora <[email protected]>
1 parent f5b4545 commit 859b6ef

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

ports/zephyr/machine_wdt.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Daniel Campora on behalf of REMOTE TECH LTD
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+
// This file is never compiled standalone, it's included directly from
28+
// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE.
29+
30+
#include <zephyr/kernel.h>
31+
#include <zephyr/device.h>
32+
#include <zephyr/drivers/watchdog.h>
33+
#include <zephyr/sys/printk.h>
34+
#include <stdbool.h>
35+
36+
#include "py/mperrno.h"
37+
38+
typedef struct _machine_wdt_obj_t {
39+
mp_obj_base_t base;
40+
struct device *wdt;
41+
int32_t channel_id;
42+
} machine_wdt_obj_t;
43+
44+
static machine_wdt_obj_t wdt_default = {
45+
{&machine_wdt_type}, NULL, -1
46+
};
47+
48+
static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) {
49+
if (id != 0) {
50+
mp_raise_ValueError(MP_ERROR_TEXT("invalid WDT id"));
51+
}
52+
53+
if (timeout_ms <= 0) {
54+
mp_raise_ValueError(MP_ERROR_TEXT("watchdog timeout too short"));
55+
}
56+
57+
wdt_default.wdt = (struct device *)DEVICE_DT_GET(DT_ALIAS(watchdog0));
58+
59+
if (!device_is_ready(wdt_default.wdt)) {
60+
mp_raise_OSError(MP_ENODEV);
61+
}
62+
63+
struct wdt_timeout_cfg wdt_config = {
64+
/* Reset SoC when watchdog timer expires. */
65+
.flags = WDT_FLAG_RESET_SOC,
66+
67+
/* Expire watchdog after max window */
68+
.window.min = 0,
69+
.window.max = timeout_ms,
70+
};
71+
72+
wdt_default.channel_id = wdt_install_timeout(wdt_default.wdt, &wdt_config);
73+
74+
if (wdt_default.channel_id < 0) {
75+
mp_raise_OSError(-wdt_default.channel_id);
76+
}
77+
78+
mp_int_t rs_code = wdt_setup(wdt_default.wdt, WDT_OPT_PAUSE_IN_SLEEP | WDT_OPT_PAUSE_HALTED_BY_DBG);
79+
80+
if (rs_code < 0) {
81+
mp_raise_OSError(-rs_code);
82+
}
83+
84+
return &wdt_default;
85+
}
86+
87+
static void mp_machine_wdt_feed(machine_wdt_obj_t *self) {
88+
mp_int_t rs_code = wdt_feed(self->wdt, self->channel_id);
89+
if (rs_code < 0) {
90+
mp_raise_OSError(-rs_code);
91+
}
92+
}

ports/zephyr/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
6969
#define MICROPY_PY_MACHINE_UART (1)
7070
#define MICROPY_PY_MACHINE_UART_INCLUDEFILE "ports/zephyr/machine_uart.c"
71+
#ifdef CONFIG_WATCHDOG
72+
#define MICROPY_PY_MACHINE_WDT (1)
73+
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/zephyr/machine_wdt.c"
74+
#endif
7175
#define MICROPY_PY_STRUCT (0)
7276
#ifdef CONFIG_NETWORKING
7377
// If we have networking, we likely want errno comfort

ports/zephyr/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ CONFIG_NET_BUF_POOL_USAGE=y
7979
CONFIG_MICROPY_CONFIGFILE="mpconfigport.h"
8080
CONFIG_MICROPY_VFS_FAT=y
8181
CONFIG_MICROPY_VFS_LFS2=y
82+
83+
CONFIG_WATCHDOG=y
84+
CONFIG_WDT_DISABLE_AT_BOOT=y

0 commit comments

Comments
 (0)