|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 microDev |
| 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 "py/runtime.h" |
| 28 | +#include "common-hal/watchdog/WatchDogTimer.h" |
| 29 | + |
| 30 | +#include "shared-bindings/watchdog/__init__.h" |
| 31 | +#include "shared-bindings/microcontroller/__init__.h" |
| 32 | + |
| 33 | +#include "hardware/watchdog.h" |
| 34 | + |
| 35 | +void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) { |
| 36 | + watchdog_update(); |
| 37 | +} |
| 38 | + |
| 39 | +void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { |
| 40 | + if (self->mode == WATCHDOGMODE_RESET) { |
| 41 | + mp_raise_RuntimeError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET")); |
| 42 | + } else { |
| 43 | + self->mode = WATCHDOGMODE_NONE; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | +void watchdog_reset(void) { |
| 49 | + common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); |
| 50 | +} |
| 51 | +*/ |
| 52 | + |
| 53 | +mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
| 54 | + return self->timeout; |
| 55 | +} |
| 56 | + |
| 57 | +void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) { |
| 58 | + // max timeout is 8.388607 sec |
| 59 | + // this is rounded down to 8.388 sec |
| 60 | + uint64_t timeout = new_timeout * 1000; |
| 61 | + if (timeout > 8388) { |
| 62 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 63 | + } |
| 64 | + if ((uint16_t)self->timeout != timeout) { |
| 65 | + watchdog_enable(timeout, false); |
| 66 | + self->timeout = new_timeout; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) { |
| 71 | + return self->mode; |
| 72 | +} |
| 73 | + |
| 74 | +void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) { |
| 75 | + if (self->mode != new_mode) { |
| 76 | + if (new_mode == WATCHDOGMODE_RAISE) { |
| 77 | + mp_raise_NotImplementedError(translate("RAISE mode is not implemented")); |
| 78 | + } else if (new_mode == WATCHDOGMODE_NONE) { |
| 79 | + common_hal_watchdog_deinit(self); |
| 80 | + } |
| 81 | + self->mode = new_mode; |
| 82 | + } |
| 83 | +} |
0 commit comments