|
| 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/microcontroller/__init__.h" |
| 31 | + |
| 32 | +#include "esp_task_wdt.h" |
| 33 | + |
| 34 | +void esp_task_wdt_isr_user_handler(void) { |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) { |
| 39 | + if (esp_task_wdt_reset() != ESP_OK) { |
| 40 | + mp_raise_RuntimeError(translate("watchdog not initialized")); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { |
| 45 | + if (esp_task_wdt_deinit() == ESP_OK) { |
| 46 | + self->mode = WATCHDOGMODE_NONE; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void watchdog_reset(void) { |
| 51 | + common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); |
| 52 | +} |
| 53 | + |
| 54 | +static void wdt_config(watchdog_watchdogtimer_obj_t *self) { |
| 55 | + // enable panic hanler in WATCHDOGMODE_RESET mode |
| 56 | + // initialize Task Watchdog Timer (TWDT) |
| 57 | + if (esp_task_wdt_init((uint32_t)self->timeout, (self->mode == WATCHDOGMODE_RESET)) != ESP_OK) { |
| 58 | + mp_raise_RuntimeError(translate("Initialization failed due to lack of memory")); |
| 59 | + } |
| 60 | + esp_task_wdt_add(NULL); |
| 61 | +} |
| 62 | + |
| 63 | +mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
| 64 | + return self->timeout; |
| 65 | +} |
| 66 | + |
| 67 | +void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) { |
| 68 | + if ((uint64_t)new_timeout > UINT32_MAX) { |
| 69 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 70 | + } |
| 71 | + if ((uint32_t)self->timeout != (uint32_t)new_timeout) { |
| 72 | + self->timeout = new_timeout; |
| 73 | + wdt_config(self); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) { |
| 78 | + return self->mode; |
| 79 | +} |
| 80 | + |
| 81 | +void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) { |
| 82 | + if (self->mode != new_mode) { |
| 83 | + self->mode = new_mode; |
| 84 | + wdt_config(self); |
| 85 | + } |
| 86 | +} |
0 commit comments