|
| 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 | +} |
0 commit comments