Skip to content

Commit 42318c0

Browse files
committed
zephyr: Add watchdog timer implementation.
Signed-off-by: danicampora <[email protected]>
1 parent 1034b17 commit 42318c0

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

ports/zephyr/machine_wdt.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Paul Sokolovsky
7+
* Copyright (c) 2017 Eric Poulsen
8+
* Copyright (c) 2025 Daniel Campora on behalf of REMOTE TECH LTD
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
29+
// This file is never compiled standalone, it's included directly from
30+
// extmod/machine_wdt.c via MICROPY_PY_MACHINE_WDT_INCLUDEFILE.
31+
32+
#include <zephyr/kernel.h>
33+
#include <zephyr/device.h>
34+
#include <zephyr/drivers/watchdog.h>
35+
#include <zephyr/sys/printk.h>
36+
#include <stdbool.h>
37+
38+
#include "py/mperrno.h"
39+
40+
41+
typedef struct _machine_wdt_obj_t {
42+
mp_obj_base_t base;
43+
struct device *wdt;
44+
int32_t channel_id;
45+
} machine_wdt_obj_t;
46+
47+
static machine_wdt_obj_t wdt_default = {
48+
{&machine_wdt_type}, NULL, -1
49+
};
50+
51+
static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) {
52+
if (id != 0) {
53+
mp_raise_ValueError(MP_ERROR_TEXT("Invalid WDT ID"));
54+
}
55+
56+
if (timeout_ms <= 0) {
57+
mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too short"));
58+
}
59+
60+
wdt_default.wdt = (struct device *)DEVICE_DT_GET(DT_ALIAS(watchdog0));
61+
62+
if (!device_is_ready(wdt_default.wdt)) {
63+
mp_raise_OSError(MP_ENODEV);
64+
}
65+
66+
struct wdt_timeout_cfg wdt_config = {
67+
/* Reset SoC when watchdog timer expires. */
68+
.flags = WDT_FLAG_RESET_SOC,
69+
70+
/* Expire watchdog after max window */
71+
.window.min = 0,
72+
.window.max = timeout_ms,
73+
};
74+
75+
wdt_default.channel_id = wdt_install_timeout(wdt_default.wdt, &wdt_config);
76+
77+
if (wdt_default.channel_id < 0) {
78+
mp_raise_OSError(-wdt_default.channel_id);
79+
}
80+
81+
mp_int_t rs_code = wdt_setup(wdt_default.wdt, WDT_OPT_PAUSE_IN_SLEEP | WDT_OPT_PAUSE_HALTED_BY_DBG);
82+
83+
if (rs_code < 0) {
84+
mp_raise_OSError(-rs_code);
85+
}
86+
87+
return &wdt_default;
88+
}
89+
90+
static void mp_machine_wdt_feed(machine_wdt_obj_t *self) {
91+
mp_int_t rs_code = wdt_feed(self->wdt, self->channel_id);
92+
if (rs_code < 0) {
93+
mp_raise_OSError(-rs_code);
94+
}
95+
}

ports/zephyr/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
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+
#define MICROPY_PY_MACHINE_WDT (1)
72+
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/zephyr/machine_wdt.c"
7173
#define MICROPY_PY_STRUCT (0)
7274
#ifdef CONFIG_NETWORKING
7375
// 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)