Skip to content

Commit 80cebf6

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

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

ports/zephyr/machine_wdt.c

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

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)