Skip to content

Commit a629c02

Browse files
danicamporaDaniel Campora
authored andcommitted
zephyr: Add watchdog timer implementation.
Signed-off-by: danicampora <[email protected]>
1 parent 1034b17 commit a629c02

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

ports/zephyr/machine_wdt.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
39+
typedef struct _machine_wdt_obj_t {
40+
mp_obj_base_t base;
41+
struct device *wdt;
42+
int32_t channel_id;
43+
} machine_wdt_obj_t;
44+
45+
static machine_wdt_obj_t wdt_default = {
46+
{&machine_wdt_type}, NULL, -1
47+
};
48+
49+
static machine_wdt_obj_t *mp_machine_wdt_make_new_instance(mp_int_t id, mp_int_t timeout_ms) {
50+
if (id != 0) {
51+
mp_raise_ValueError(MP_ERROR_TEXT("invalid WDT id"));
52+
}
53+
54+
if (timeout_ms <= 0) {
55+
mp_raise_ValueError(MP_ERROR_TEXT("watchdog timeout too short"));
56+
}
57+
58+
wdt_default.wdt = (struct device *)DEVICE_DT_GET(DT_ALIAS(watchdog0));
59+
60+
if (!device_is_ready(wdt_default.wdt)) {
61+
mp_raise_OSError(MP_ENODEV);
62+
}
63+
64+
struct wdt_timeout_cfg wdt_config = {
65+
/* Reset SoC when watchdog timer expires. */
66+
.flags = WDT_FLAG_RESET_SOC,
67+
68+
/* Expire watchdog after max window */
69+
.window.min = 0,
70+
.window.max = timeout_ms,
71+
};
72+
73+
wdt_default.channel_id = wdt_install_timeout(wdt_default.wdt, &wdt_config);
74+
75+
if (wdt_default.channel_id < 0) {
76+
mp_raise_OSError(-wdt_default.channel_id);
77+
}
78+
79+
mp_int_t rs_code = wdt_setup(wdt_default.wdt, WDT_OPT_PAUSE_IN_SLEEP | WDT_OPT_PAUSE_HALTED_BY_DBG);
80+
81+
if (rs_code < 0) {
82+
mp_raise_OSError(-rs_code);
83+
}
84+
85+
return &wdt_default;
86+
}
87+
88+
static void mp_machine_wdt_feed(machine_wdt_obj_t *self) {
89+
mp_int_t rs_code = wdt_feed(self->wdt, self->channel_id);
90+
if (rs_code < 0) {
91+
mp_raise_OSError(-rs_code);
92+
}
93+
}

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)