|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Jeff Epler for Adafruit Industries |
| 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 | +#include <math.h> |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +#include "shared-bindings/watchdog/__init__.h" |
| 32 | +#include "shared-bindings/watchdog/WatchDogTimer.h" |
| 33 | +#include "common-hal/watchdog/WatchDogTimer.h" |
| 34 | + |
| 35 | +#include "component/wdt.h" |
| 36 | + |
| 37 | +void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) { |
| 38 | + WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY; |
| 39 | +} |
| 40 | + |
| 41 | +void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { |
| 42 | + if (self->mode == WATCHDOGMODE_RESET) { |
| 43 | + mp_raise_RuntimeError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET")); |
| 44 | + } else { |
| 45 | + self->mode = WATCHDOGMODE_NONE; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
| 50 | + return self->timeout; |
| 51 | +} |
| 52 | + |
| 53 | +void setup_wdt(watchdog_watchdogtimer_obj_t *self, int setting) { |
| 54 | + OSC32KCTRL->OSCULP32K.bit.EN1K = 1; // Enable out 1K (for WDT) |
| 55 | + |
| 56 | + // disable watchdog for config |
| 57 | + WDT->CTRLA.reg = 0; |
| 58 | + while (WDT->SYNCBUSY.reg) { // Sync CTRL write |
| 59 | + } |
| 60 | + |
| 61 | + WDT->INTENCLR.bit.EW = 1; // Disable early warning interrupt |
| 62 | + WDT->CONFIG.bit.PER = setting; // Set period for chip reset |
| 63 | + WDT->CTRLA.bit.WEN = 0; // Disable window mode |
| 64 | + while (WDT->SYNCBUSY.reg) { // Sync CTRL write |
| 65 | + } |
| 66 | + common_hal_watchdog_feed(self); // Clear watchdog interval |
| 67 | + WDT->CTRLA.bit.ENABLE = 1; // Start watchdog now! |
| 68 | + while (WDT->SYNCBUSY.reg) { |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) { |
| 73 | + int wdt_cycles = (int)(new_timeout * 1000); |
| 74 | + if (wdt_cycles < 8) { |
| 75 | + wdt_cycles = 8; |
| 76 | + } |
| 77 | + if (wdt_cycles > 16384) { |
| 78 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 79 | + } |
| 80 | + // ceil(log2(n)) = 32 - __builtin_clz(n - 1) when n > 1 (if int is 32 bits) |
| 81 | + int log2_wdt_cycles = (sizeof(int) * CHAR_BIT) - __builtin_clz(wdt_cycles - 1); |
| 82 | + int setting = log2_wdt_cycles - 3; // CYC8_Val is 0 |
| 83 | + float timeout = (8 << setting) / 1024.f; |
| 84 | + |
| 85 | + if (self->mode == WATCHDOGMODE_RESET) { |
| 86 | + setup_wdt(self, setting); |
| 87 | + } |
| 88 | + self->timeout = timeout; |
| 89 | +} |
| 90 | + |
| 91 | +watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) { |
| 92 | + return self->mode; |
| 93 | +} |
| 94 | + |
| 95 | +void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) { |
| 96 | + if (self->mode != new_mode) { |
| 97 | + if (new_mode == WATCHDOGMODE_RAISE) { |
| 98 | + mp_raise_NotImplementedError(translate("RAISE mode is not implemented")); |
| 99 | + } else if (new_mode == WATCHDOGMODE_NONE) { |
| 100 | + common_hal_watchdog_deinit(self); |
| 101 | + } |
| 102 | + self->mode = new_mode; |
| 103 | + common_hal_watchdog_set_timeout(self, self->timeout); |
| 104 | + } |
| 105 | +} |
0 commit comments