From abd01c5fbb3005fd30e41407c8f8b9d0dad5ef55 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 20 May 2020 20:09:28 +0800 Subject: [PATCH] nrf: add watchdog module Add common-hal bindings to allow the watchdog module to be used on nrf platforms. Signed-off-by: Sean Cross --- ports/nrf/common-hal/wdt/WDT.c | 40 +++++++++++++++++++++++++++++ ports/nrf/common-hal/wdt/__init__.c | 0 2 files changed, 40 insertions(+) create mode 100644 ports/nrf/common-hal/wdt/WDT.c create mode 100644 ports/nrf/common-hal/wdt/__init__.c diff --git a/ports/nrf/common-hal/wdt/WDT.c b/ports/nrf/common-hal/wdt/WDT.c new file mode 100644 index 0000000000..172bd33463 --- /dev/null +++ b/ports/nrf/common-hal/wdt/WDT.c @@ -0,0 +1,40 @@ +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/rtc/__init__.h" +#include "nrf_wdt.h" + +#define WDT_RELOAD_COUNT 2 + +void common_hal_wdt_init(uint32_t duration, bool pause_during_sleep) { + unsigned int channel; + nrf_wdt_behaviour_t behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT; + if (pause_during_sleep) { + behaviour = NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT; + } + + nrf_wdt_behaviour_set(NRF_WDT, behaviour); + + uint64_t ticks = (duration * 32768ULL) / 1000; + if (ticks > UINT32_MAX) { + mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); + } + nrf_wdt_reload_value_set(NRF_WDT, (uint32_t) ticks); + + for (channel = 0; channel < WDT_RELOAD_COUNT; channel++) { + nrf_wdt_reload_request_enable(NRF_WDT, channel); + } + + nrf_wdt_task_trigger(NRF_WDT, NRF_WDT_TASK_START); +} + +void common_hal_wdt_feed(void) { + unsigned int channel; + for (channel = 0; channel < WDT_RELOAD_COUNT; channel++) { + nrf_wdt_reload_request_set(NRF_WDT, (nrf_wdt_rr_register_t)(NRF_WDT_RR0 + channel)); + } +} + +void common_hal_wdt_disable(void) { + // mp_raise_ValueError(translate("Watchdog timer cannot be disabled -- board will reset shortly")); +} diff --git a/ports/nrf/common-hal/wdt/__init__.c b/ports/nrf/common-hal/wdt/__init__.c new file mode 100644 index 0000000000..e69de29bb2