2020-05-21 01:47:23 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Nick Moore for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-05-21 05:46:01 -04:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-05-21 01:47:23 -04:00
|
|
|
#include "py/obj.h"
|
2020-05-21 05:46:01 -04:00
|
|
|
#include "py/objproperty.h"
|
2020-05-21 01:47:23 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "common-hal/watchdog/WatchDogTimer.h"
|
2020-05-21 05:46:01 -04:00
|
|
|
|
2020-05-22 06:44:22 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2020-05-21 05:46:01 -04:00
|
|
|
#include "shared-bindings/watchdog/__init__.h"
|
|
|
|
#include "shared-bindings/watchdog/WatchDogTimer.h"
|
|
|
|
|
2020-05-22 05:29:54 -04:00
|
|
|
#include "supervisor/port.h"
|
|
|
|
|
|
|
|
#include "nrf/timers.h"
|
2020-05-21 01:47:23 -04:00
|
|
|
#include "nrf_wdt.h"
|
2020-05-22 05:29:54 -04:00
|
|
|
#include "nrfx_wdt.h"
|
2020-05-21 05:46:01 -04:00
|
|
|
#include "nrfx_timer.h"
|
2020-05-21 01:47:23 -04:00
|
|
|
|
2020-05-21 05:46:01 -04:00
|
|
|
STATIC uint8_t timer_refcount = 0;
|
|
|
|
STATIC nrfx_timer_t *timer = NULL;
|
2020-05-22 05:29:54 -04:00
|
|
|
STATIC nrfx_wdt_t wdt = NRFX_WDT_INSTANCE(0);
|
|
|
|
STATIC nrfx_wdt_channel_id wdt_channel_id;
|
2020-05-21 01:47:23 -04:00
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
|
2020-05-22 06:44:22 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(p_context);
|
2020-05-21 05:46:01 -04:00
|
|
|
if (event_type != NRF_TIMER_EVENT_COMPARE0) {
|
|
|
|
// Spurious event.
|
|
|
|
return;
|
|
|
|
}
|
2020-05-21 01:47:23 -04:00
|
|
|
|
2020-05-21 05:46:01 -04:00
|
|
|
// If the timer hits without being cleared, pause the timer and raise an exception.
|
|
|
|
nrfx_timer_pause(timer);
|
2020-05-22 06:44:22 -04:00
|
|
|
self->mode = WATCHDOGMODE_NONE;
|
2020-05-22 05:43:05 -04:00
|
|
|
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
|
|
|
|
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
|
2020-05-21 05:46:01 -04:00
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
|
|
|
|
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-21 01:47:23 -04:00
|
|
|
|
2020-05-22 05:29:54 -04:00
|
|
|
// This function is called if the timer expires. The system will reboot in 1/16384 of a second.
|
|
|
|
// Issue a reboot ourselves so we can do any cleanup necessary.
|
|
|
|
STATIC void watchdogtimer_watchdog_event_handler(void) {
|
|
|
|
reset_cpu();
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:47:23 -04:00
|
|
|
//| def feed(self):
|
|
|
|
//| """Feed the watchdog timer. This must be called regularly, otherwise
|
2020-05-22 00:35:29 -04:00
|
|
|
//| the timer will expire."""
|
2020-05-21 01:47:23 -04:00
|
|
|
//| ...
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
|
2020-05-21 05:46:01 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
if (self->mode == WATCHDOGMODE_RESET) {
|
2020-05-22 05:29:54 -04:00
|
|
|
nrfx_wdt_feed(&wdt);
|
2020-05-22 00:35:29 -04:00
|
|
|
} else if (self->mode == WATCHDOGMODE_RAISE) {
|
2020-05-21 05:46:01 -04:00
|
|
|
nrfx_timer_clear(timer);
|
2020-05-22 00:35:29 -04:00
|
|
|
} else if (self->mode == WATCHDOGMODE_NONE) {
|
|
|
|
mp_raise_ValueError(translate("WatchDogTimer is not currently running"));
|
2020-05-21 05:46:01 -04:00
|
|
|
}
|
2020-05-21 01:47:23 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_feed);
|
|
|
|
|
|
|
|
//| def deinit(self):
|
|
|
|
//| """Stop the watchdog timer. This may raise an error if the watchdog
|
|
|
|
//| timer cannot be disabled on this platform."""
|
|
|
|
//| ...
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
|
2020-05-21 05:46:01 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
if (self->mode == WATCHDOGMODE_RAISE) {
|
2020-05-21 05:46:01 -04:00
|
|
|
timer_refcount--;
|
|
|
|
if (timer_refcount == 0) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
timer = NULL;
|
|
|
|
}
|
2020-05-22 06:44:22 -04:00
|
|
|
self->mode = WATCHDOGMODE_NONE;
|
2020-05-22 00:35:29 -04:00
|
|
|
} else if (self->mode == WATCHDOGMODE_RESET) {
|
|
|
|
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
|
2020-05-21 05:46:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-21 01:47:23 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_deinit_obj, watchdog_watchdogtimer_deinit);
|
|
|
|
|
2020-05-22 06:44:22 -04:00
|
|
|
void watchdog_reset(void) {
|
|
|
|
if (common_hal_mcu_watchdogtimer_obj.mode == WATCHDOGMODE_RAISE) {
|
|
|
|
common_hal_mcu_watchdogtimer_obj.mode = WATCHDOGMODE_NONE;
|
|
|
|
timer_refcount--;
|
|
|
|
if (timer_refcount == 0) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
//| timeout: float = ...
|
|
|
|
//| """The maximum number of seconds that can elapse between calls
|
2020-05-21 01:47:23 -04:00
|
|
|
//| to feed()"""
|
|
|
|
//|
|
2020-05-22 00:35:29 -04:00
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_timeout(mp_obj_t self_in) {
|
2020-05-21 01:47:23 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-05-21 05:46:01 -04:00
|
|
|
return mp_obj_new_float(self->timeout);
|
2020-05-21 01:47:23 -04:00
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_timeout_obj, watchdog_watchdogtimer_obj_get_timeout);
|
|
|
|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_t timeout_obj) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_float_t timeout = mp_obj_get_float(timeout_obj);
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
|
|
|
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mode == WATCHDOGMODE_RESET) {
|
|
|
|
// If the WatchDogTimer is already running in "RESET" mode, raise an error
|
|
|
|
// since the mode cannot be changed once started.
|
2020-05-22 06:44:22 -04:00
|
|
|
mp_raise_TypeError(translate("cannot change the timeout once mode is WatchDogMode.RESET"));
|
2020-05-22 00:35:29 -04:00
|
|
|
} else if (self->mode == WATCHDOGMODE_RAISE) {
|
|
|
|
// If the WatchDogTimer is already running in "RAISE" mode, reset the timer
|
|
|
|
// with the new value.
|
|
|
|
uint64_t ticks = timeout * 31250ULL;
|
|
|
|
if (ticks > UINT32_MAX) {
|
|
|
|
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
|
|
|
|
}
|
|
|
|
nrfx_timer_clear(timer);
|
|
|
|
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
self->timeout = timeout;
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_timeout_obj, watchdog_watchdogtimer_obj_set_timeout);
|
2020-05-21 01:47:23 -04:00
|
|
|
|
|
|
|
const mp_obj_property_t watchdog_watchdogtimer_timeout_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
2020-05-21 05:46:01 -04:00
|
|
|
.proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_timeout_obj,
|
2020-05-22 00:35:29 -04:00
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_set_timeout_obj,
|
2020-05-21 01:47:23 -04:00
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
//| mode: watchdog.WatchDogMode = ...
|
|
|
|
//| """The current operating mode of the WatchDogTimer `watchdog.WatchDogMode`.
|
|
|
|
//|
|
|
|
|
//| Setting a WatchDogMode activates the WatchDog::
|
|
|
|
//|
|
|
|
|
//| import microcontroller
|
|
|
|
//| import watchdog
|
|
|
|
//|
|
|
|
|
//| w = microcontroller.watchdog
|
|
|
|
//| w.timeout = 5
|
|
|
|
//| w.mode = watchdog.WatchDogMode.RAISE
|
|
|
|
//|
|
|
|
|
//|
|
|
|
|
//| Once set, the WatchDogTimer will perform the specified action if the timer expires.
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode(mp_obj_t self_in) {
|
2020-05-21 05:46:01 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-05-22 00:35:29 -04:00
|
|
|
switch (self->mode) {
|
|
|
|
case WATCHDOGMODE_NONE: default: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_none_obj);
|
|
|
|
case WATCHDOGMODE_RAISE: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
|
|
|
|
case WATCHDOGMODE_RESET: return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
|
2020-05-21 05:46:01 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
|
|
|
|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t mode_obj) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
watchdog_watchdogmode_obj_t *mode = MP_OBJ_TO_PTR(mode_obj);
|
|
|
|
if (mode == MP_ROM_PTR(&watchdog_watchdogmode_none_obj)) {
|
|
|
|
if (self->mode == WATCHDOGMODE_RESET) {
|
|
|
|
mp_raise_TypeError(translate("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET"));
|
|
|
|
}
|
|
|
|
else if (self->mode == WATCHDOGMODE_RAISE) {
|
|
|
|
timer_refcount--;
|
|
|
|
if (timer_refcount == 0) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self->mode = WATCHDOGMODE_NONE;
|
|
|
|
|
|
|
|
} else if (mode == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
|
|
|
|
if (self->timeout <= 0) {
|
|
|
|
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
|
|
|
|
}
|
|
|
|
if (self->mode == WATCHDOGMODE_RESET) {
|
|
|
|
mp_raise_ValueError(translate("WatchDogTimer mode cannot be changed once set to WatchDogMode.RESET"));
|
|
|
|
}
|
2020-05-22 06:44:22 -04:00
|
|
|
else if (self->mode == WATCHDOGMODE_NONE || self->mode == WATCHDOGMODE_RAISE) {
|
2020-05-22 00:35:29 -04:00
|
|
|
if (timer_refcount == 0) {
|
|
|
|
timer = nrf_peripherals_allocate_timer_or_throw();
|
|
|
|
}
|
2020-05-22 06:44:22 -04:00
|
|
|
if (timer == NULL) {
|
|
|
|
mp_raise_RuntimeError(translate("timer was null"));
|
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
timer_refcount++;
|
|
|
|
|
|
|
|
nrfx_timer_config_t timer_config = {
|
|
|
|
.frequency = NRF_TIMER_FREQ_31250Hz,
|
|
|
|
.mode = NRF_TIMER_MODE_TIMER,
|
|
|
|
.bit_width = NRF_TIMER_BIT_WIDTH_32,
|
|
|
|
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
|
|
|
|
.p_context = self,
|
|
|
|
};
|
|
|
|
|
|
|
|
nrfx_timer_init(timer, &timer_config, &watchdogtimer_timer_event_handler);
|
|
|
|
|
2020-05-22 06:44:22 -04:00
|
|
|
uint64_t ticks = nrfx_timer_ms_to_ticks(timer, self->timeout * 1000);
|
|
|
|
if (ticks > UINT32_MAX) {
|
|
|
|
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:35:29 -04:00
|
|
|
// true enables interrupt.
|
|
|
|
nrfx_timer_clear(timer);
|
|
|
|
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
|
|
|
|
nrfx_timer_resume(timer);
|
2020-05-21 05:46:01 -04:00
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
self->mode = WATCHDOGMODE_RAISE;
|
|
|
|
|
|
|
|
} else if (mode == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
|
|
|
|
if (self->timeout <= 0) {
|
|
|
|
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
|
|
|
|
}
|
|
|
|
if (self->mode == WATCHDOGMODE_RAISE) {
|
|
|
|
timer_refcount--;
|
|
|
|
if (timer_refcount == 0) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 05:29:54 -04:00
|
|
|
|
|
|
|
uint64_t ticks = self->timeout * 1000.0f;
|
|
|
|
if (ticks > UINT32_MAX) {
|
|
|
|
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nrfx_wdt_config_t config = {
|
|
|
|
.reload_value = ticks, // in units of ms
|
|
|
|
.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP,
|
|
|
|
NRFX_WDT_IRQ_CONFIG
|
|
|
|
};
|
|
|
|
|
|
|
|
nrfx_err_t err_code;
|
|
|
|
err_code = nrfx_wdt_init(&wdt, &config, watchdogtimer_watchdog_event_handler);
|
|
|
|
if (err_code != NRFX_SUCCESS) {
|
|
|
|
mp_raise_OSError(1);
|
|
|
|
}
|
|
|
|
err_code = nrfx_wdt_channel_alloc(&wdt, &wdt_channel_id);
|
|
|
|
if (err_code != NRFX_SUCCESS) {
|
|
|
|
mp_raise_OSError(1);
|
|
|
|
}
|
|
|
|
nrfx_wdt_enable(&wdt);
|
|
|
|
nrfx_wdt_feed(&wdt);
|
2020-05-22 00:35:29 -04:00
|
|
|
self->mode = WATCHDOGMODE_RESET;
|
2020-05-21 05:46:01 -04:00
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
|
2020-05-21 05:46:01 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2020-05-22 00:35:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);
|
|
|
|
|
|
|
|
const mp_obj_property_t watchdog_watchdogtimer_mode_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&watchdog_watchdogtimer_get_mode_obj,
|
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_set_mode_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
2020-05-21 05:46:01 -04:00
|
|
|
|
2020-05-21 01:47:23 -04:00
|
|
|
STATIC const mp_rom_map_elem_t watchdog_watchdogtimer_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&watchdog_watchdogtimer_feed_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&watchdog_watchdogtimer_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&watchdog_watchdogtimer_timeout_obj) },
|
2020-05-22 00:35:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&watchdog_watchdogtimer_mode_obj) },
|
2020-05-21 01:47:23 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogtimer_locals_dict, watchdog_watchdogtimer_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t watchdog_watchdogtimer_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_WatchDogTimer,
|
2020-05-22 00:35:29 -04:00
|
|
|
// .make_new = watchdog_watchdogtimer_make_new,
|
2020-05-21 01:47:23 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&watchdog_watchdogtimer_locals_dict,
|
|
|
|
};
|