|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Nick Moore 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 | + |
| 28 | +#include <math.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "py/obj.h" |
| 32 | +#include "py/objproperty.h" |
| 33 | +#include "py/runtime.h" |
| 34 | + |
| 35 | +#include "common-hal/watchdog/WatchDogTimer.h" |
| 36 | + |
| 37 | +#include "shared-bindings/microcontroller/__init__.h" |
| 38 | +#include "shared-bindings/watchdog/__init__.h" |
| 39 | +#include "shared-bindings/watchdog/WatchDogTimer.h" |
| 40 | + |
| 41 | +#include "supervisor/port.h" |
| 42 | + |
| 43 | +#include "nrf/timers.h" |
| 44 | +#include "nrf_wdt.h" |
| 45 | +#include "nrfx_wdt.h" |
| 46 | +#include "nrfx_timer.h" |
| 47 | + |
| 48 | +STATIC uint8_t timer_refcount = 0; |
| 49 | +STATIC nrfx_timer_t *timer = NULL; |
| 50 | +STATIC nrfx_wdt_t wdt = NRFX_WDT_INSTANCE(0); |
| 51 | +STATIC nrfx_wdt_channel_id wdt_channel_id; |
| 52 | + |
| 53 | +STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void *p_context) { |
| 54 | + watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(p_context); |
| 55 | + if (event_type != NRF_TIMER_EVENT_COMPARE0) { |
| 56 | + // Spurious event. |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // If the timer hits without being cleared, pause the timer and raise an exception. |
| 61 | + nrfx_timer_pause(timer); |
| 62 | + self->mode = WATCHDOGMODE_NONE; |
| 63 | + mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception)); |
| 64 | + MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception; |
| 65 | +#if MICROPY_ENABLE_SCHEDULER |
| 66 | + if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { |
| 67 | + MP_STATE_VM(sched_state) = MP_SCHED_PENDING; |
| 68 | + } |
| 69 | +#endif |
| 70 | +} |
| 71 | + |
| 72 | +static void timer_free(void) { |
| 73 | + timer_refcount--; |
| 74 | + if (timer_refcount == 0) { |
| 75 | + nrf_peripherals_free_timer(timer); |
| 76 | + timer = NULL; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// This function is called if the timer expires. The system will reboot |
| 81 | +// in 1/16384 of a second. Issue a reboot ourselves so we can do any |
| 82 | +// cleanup necessary. |
| 83 | +STATIC void watchdogtimer_watchdog_event_handler(void) { |
| 84 | + reset_cpu(); |
| 85 | +} |
| 86 | + |
| 87 | +void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) { |
| 88 | + if (self->mode == WATCHDOGMODE_RESET) { |
| 89 | + nrfx_wdt_feed(&wdt); |
| 90 | + } else if (self->mode == WATCHDOGMODE_RAISE) { |
| 91 | + nrfx_timer_clear(timer); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { |
| 96 | + if (timer) { |
| 97 | + timer_free(); |
| 98 | + } |
| 99 | + self->mode = WATCHDOGMODE_NONE; |
| 100 | +} |
| 101 | + |
| 102 | +void watchdog_reset(void) { |
| 103 | + common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); |
| 104 | +} |
| 105 | + |
| 106 | +mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
| 107 | + return self->timeout; |
| 108 | +} |
| 109 | + |
| 110 | +void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t timeout) { |
| 111 | + if (self->mode == WATCHDOGMODE_RAISE) { |
| 112 | + // If the WatchDogTimer is already running in "RAISE" mode, reset the timer |
| 113 | + // with the new value. |
| 114 | + uint64_t ticks = timeout * 31250ULL; |
| 115 | + if (ticks > UINT32_MAX) { |
| 116 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 117 | + } |
| 118 | + nrfx_timer_clear(timer); |
| 119 | + nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true); |
| 120 | + } |
| 121 | + |
| 122 | + self->timeout = timeout; |
| 123 | +} |
| 124 | + |
| 125 | +watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) { |
| 126 | + return self->mode; |
| 127 | +} |
| 128 | + |
| 129 | +void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) { |
| 130 | + watchdog_watchdogmode_t current_mode = self->mode; |
| 131 | + |
| 132 | + if (new_mode == WATCHDOGMODE_RAISE) { |
| 133 | + if (timer_refcount == 0) { |
| 134 | + timer = nrf_peripherals_allocate_timer_or_throw(); |
| 135 | + } |
| 136 | + timer_refcount++; |
| 137 | + |
| 138 | + nrfx_timer_config_t timer_config = { |
| 139 | + .frequency = NRF_TIMER_FREQ_31250Hz, |
| 140 | + .mode = NRF_TIMER_MODE_TIMER, |
| 141 | + .bit_width = NRF_TIMER_BIT_WIDTH_32, |
| 142 | + .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, |
| 143 | + .p_context = self, |
| 144 | + }; |
| 145 | + |
| 146 | + nrfx_timer_init(timer, &timer_config, &watchdogtimer_timer_event_handler); |
| 147 | + |
| 148 | + uint64_t ticks = nrfx_timer_ms_to_ticks(timer, self->timeout * 1000); |
| 149 | + if (ticks > UINT32_MAX) { |
| 150 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 151 | + } |
| 152 | + |
| 153 | + // true enables interrupt. |
| 154 | + nrfx_timer_clear(timer); |
| 155 | + nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true); |
| 156 | + nrfx_timer_resume(timer); |
| 157 | + |
| 158 | + } else if (new_mode == WATCHDOGMODE_RESET) { |
| 159 | + uint64_t ticks = self->timeout * 1000.0f; |
| 160 | + if (ticks > UINT32_MAX) { |
| 161 | + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 162 | + } |
| 163 | + |
| 164 | + nrfx_wdt_config_t config = { |
| 165 | + .reload_value = ticks, // in units of ms |
| 166 | + .behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP, |
| 167 | + NRFX_WDT_IRQ_CONFIG |
| 168 | + }; |
| 169 | + |
| 170 | + nrfx_err_t err_code; |
| 171 | + err_code = nrfx_wdt_init(&wdt, &config, watchdogtimer_watchdog_event_handler); |
| 172 | + if (err_code != NRFX_SUCCESS) { |
| 173 | + mp_raise_OSError(1); |
| 174 | + } |
| 175 | + err_code = nrfx_wdt_channel_alloc(&wdt, &wdt_channel_id); |
| 176 | + if (err_code != NRFX_SUCCESS) { |
| 177 | + mp_raise_OSError(1); |
| 178 | + } |
| 179 | + nrfx_wdt_enable(&wdt); |
| 180 | + nrfx_wdt_feed(&wdt); |
| 181 | + } |
| 182 | + |
| 183 | + // If we just switched away from RAISE, disable the timmer. |
| 184 | + if (current_mode == WATCHDOGMODE_RAISE && new_mode != WATCHDOGMODE_RAISE) { |
| 185 | + timer_free(); |
| 186 | + } |
| 187 | + |
| 188 | + self->mode = new_mode; |
| 189 | +} |
0 commit comments