Skip to content

Add Watchdog timer and add support for NRF watchdog #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
17ef2df
nrf: ld: add ARM.exidx to output image
xobs May 20, 2020
f471960
wdt: add watchdog support
xobs May 20, 2020
abd01c5
nrf: add watchdog module
xobs May 20, 2020
fbe1c05
nrf: simmel: enable wdt support
xobs May 20, 2020
c23f151
nrf: enable wdt for all platforms
xobs May 20, 2020
595f638
watchdog: rename module from `wdt` and move to `microcontroller`
xobs May 21, 2020
8f46133
nrf: watchdog: implement software watchdogtimer
xobs May 21, 2020
87737fb
watchdog: fix documentation build error
xobs May 21, 2020
0169ea5
nrf: discard arm exception sections
xobs May 21, 2020
ae950bc
add WatchDogTimeout exception
xobs May 21, 2020
ec99dae
tick: break on watchdog timeout exception
xobs May 21, 2020
08362c9
watchdogtimer: refactor to new api
xobs May 22, 2020
8c5df5f
supervisor: add a new WATCHDOG_RESET safe mode reason
xobs May 22, 2020
e738f5e
nrf: boot into safe mode sometimes for watchdog reset
xobs May 22, 2020
589cb1a
nrf: watchdog: use nrfx_wdt driver
xobs May 22, 2020
bd086a1
Revert "add WatchDogTimeout exception"
xobs May 22, 2020
15530a6
supervisor: tick: check for watchdog exception if enabled
xobs May 22, 2020
108409c
nrf: common-hal: finish reworking exceptions
xobs May 22, 2020
c5c13a8
nrf: reset watchdog as part of port_reset()
xobs May 22, 2020
c7efc94
watchdog: move timeout exception to shared-bindings
xobs May 22, 2020
5edc29c
nrf: microcontroller: use port reset path
xobs May 22, 2020
daf7c28
nrf: port: save rtc value across reboots
xobs May 22, 2020
33001f4
nrf: simmel: shrink filesystem to fit watchdog timer
xobs May 22, 2020
d0f1b59
nrf: pca10100: disable some unused features to shrink image
xobs May 23, 2020
296ba10
nrf: set WDT priority to 2
xobs May 23, 2020
e470376
nrf: add ticks (not subticks) to overflow count during reset
xobs May 23, 2020
dbf1bef
watchdog: support catching the timeout
xobs May 27, 2020
aac5a4f
watchdog: use common_hal_watchdog_* pattern
xobs May 27, 2020
aa04452
locale: generate strings for watchdog timer
xobs May 27, 2020
9380c34
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt May 27, 2020
1ed4978
Remove NONE from mode enum and doc tweaks
tannewt May 27, 2020
198f957
Merge pull request #1 from tannewt/wdt-nrf
xobs May 28, 2020
2fd7a43
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt May 29, 2020
323cca8
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt Jun 1, 2020
959f729
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt Jun 2, 2020
7e632c9
Update translations
tannewt Jun 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-01 08:56-0500\n"
"POT-Creation-Date: 2020-06-01 17:10-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1652,6 +1652,26 @@ msgstr ""
msgid "WARNING: Your code filename has two extensions\n"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "WatchDogTimer cannot be deinitialized once mode is set to RESET"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "WatchDogTimer is not currently running"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "WatchDogTimer.mode cannot be changed once set to WatchDogMode.RESET"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "WatchDogTimer.timeout must be greater than 0"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid "Watchdog timer expired."
msgstr ""

#: py/builtinhelp.c
#, c-format
msgid ""
Expand Down Expand Up @@ -3059,6 +3079,10 @@ msgstr ""
msgid "time.struct_time() takes a 9-sequence"
msgstr ""

#: ports/nrf/common-hal/watchdog/WatchDogTimer.c
msgid "timeout duration exceeded the maximum supported value"
msgstr ""

#: shared-bindings/busio/UART.c
msgid "timeout must be 0.0-100.0 seconds"
msgstr ""
Expand Down Expand Up @@ -3212,6 +3236,10 @@ msgstr ""
msgid "value_count must be > 0"
msgstr ""

#: shared-bindings/watchdog/WatchDogTimer.c
msgid "watchdog timeout must be greater than 0"
msgstr ""

#: shared-bindings/_bleio/Adapter.c
msgid "window must be <= interval"
msgstr ""
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ SRC_NRFX = $(addprefix nrfx/,\
drivers/src/nrfx_gpiote.c \
drivers/src/nrfx_rtc.c \
drivers/src/nrfx_nvmc.c \
drivers/src/nrfx_wdt.c \
)

ifdef EXTERNAL_FLASH_DEVICES
Expand Down
22 changes: 8 additions & 14 deletions ports/nrf/boards/common.template.ld
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ SECTIONS
_etext = .; /* define a global symbol at end of code */
} >FLASH_FIRMWARE

/*
.ARM.extab :
{
*(.ARM.extab* .gnu.linkonce.armextab.*)
} >FLASH_FIRMWARE

.ARM :
{
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH_FIRMWARE
*/

/* used by the startup to initialize data */
_sidata = .;

Expand Down Expand Up @@ -147,6 +133,14 @@ SECTIONS
. = ALIGN(4);
} >RAM

/* Remove exception unwinding information, since Circuit Python
does not support this GCC feature. */
/DISCARD/ :
{
*(.ARM.extab* .gnu.linkonce.armextab.*)
*(.ARM.exidx*)
}

/* Remove information from the standard libraries */
/*
/DISCARD/ :
Expand Down
6 changes: 6 additions & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ MCU_CHIP = nrf52833
INTERNAL_FLASH_FILESYSTEM = 1

CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_BUSIO = 1
CIRCUITPY_COUNTIO = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CSLAVE = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
CIRCUITPY_PIXELBUF = 0
Expand All @@ -20,6 +24,8 @@ CIRCUITPY_RTC = 1
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0

SUPEROPT_GC = 0

# These defines must be overridden before mpconfigboard.h is included, which is
# why they are passed on the command line.
CFLAGS += -DSPIM3_BUFFER_SIZE=0 -DSOFTDEVICE_RAM_SIZE='(32*1024)'
2 changes: 1 addition & 1 deletion ports/nrf/boards/simmel/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#endif

#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (80*1024)
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (76*1024)

#define BOOTLOADER_SIZE (0x4000) // 12 kiB
#define CIRCUITPY_BLE_CONFIG_SIZE (12*1024)
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/simmel/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 1
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_WATCHDOG = 1

# Enable micropython.native
#CIRCUITPY_ENABLE_MPY_NATIVE = 1
Expand Down
14 changes: 12 additions & 2 deletions ports/nrf/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {

void common_hal_mcu_reset(void) {
filesystem_flush();
NVIC_SystemReset();
reset_cpu();
}

// The singleton microcontroller.Processor object, bound to microcontroller.cpu
Expand All @@ -106,7 +106,6 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
};

#if CIRCUITPY_INTERNAL_NVM_SIZE > 0

// The singleton nvm.ByteArray object.
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
.base = {
Expand All @@ -117,6 +116,17 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
};
#endif

#if CIRCUITPY_WATCHDOG
// The singleton watchdog.WatchDogTimer object.
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
.base = {
.type = &watchdog_watchdogtimer_type,
},
.timeout = 0.0f,
.mode = WATCHDOGMODE_NONE,
};
#endif

STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) },
{ MP_ROM_QSTR(MP_QSTR_P0_01), MP_ROM_PTR(&pin_P0_01) },
Expand Down
Empty file.
189 changes: 189 additions & 0 deletions ports/nrf/common-hal/watchdog/WatchDogTimer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* 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.
*/


#include <math.h>
#include <string.h>

#include "py/obj.h"
#include "py/objproperty.h"
#include "py/runtime.h"

#include "common-hal/watchdog/WatchDogTimer.h"

#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/watchdog/__init__.h"
#include "shared-bindings/watchdog/WatchDogTimer.h"

#include "supervisor/port.h"

#include "nrf/timers.h"
#include "nrf_wdt.h"
#include "nrfx_wdt.h"
#include "nrfx_timer.h"

STATIC uint8_t timer_refcount = 0;
STATIC nrfx_timer_t *timer = NULL;
STATIC nrfx_wdt_t wdt = NRFX_WDT_INSTANCE(0);
STATIC nrfx_wdt_channel_id wdt_channel_id;

STATIC void watchdogtimer_timer_event_handler(nrf_timer_event_t event_type, void *p_context) {
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(p_context);
if (event_type != NRF_TIMER_EVENT_COMPARE0) {
// Spurious event.
return;
}

// If the timer hits without being cleared, pause the timer and raise an exception.
nrfx_timer_pause(timer);
self->mode = WATCHDOGMODE_NONE;
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
#if MICROPY_ENABLE_SCHEDULER
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
}
#endif
}

static void timer_free(void) {
timer_refcount--;
if (timer_refcount == 0) {
nrf_peripherals_free_timer(timer);
timer = NULL;
}
}

// 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();
}

void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
if (self->mode == WATCHDOGMODE_RESET) {
nrfx_wdt_feed(&wdt);
} else if (self->mode == WATCHDOGMODE_RAISE) {
nrfx_timer_clear(timer);
}
}

void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
if (timer) {
timer_free();
}
self->mode = WATCHDOGMODE_NONE;
}

void watchdog_reset(void) {
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
}

mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
return self->timeout;
}

void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t timeout) {
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;
}

watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
return self->mode;
}

void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
watchdog_watchdogmode_t current_mode = self->mode;

if (new_mode == WATCHDOGMODE_RAISE) {
if (timer_refcount == 0) {
timer = nrf_peripherals_allocate_timer_or_throw();
}
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);

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"));
}

// true enables interrupt.
nrfx_timer_clear(timer);
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, ticks, true);
nrfx_timer_resume(timer);

} else if (new_mode == WATCHDOGMODE_RESET) {
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);
}

// If we just switched away from RAISE, disable the timmer.
if (current_mode == WATCHDOGMODE_RAISE && new_mode != WATCHDOGMODE_RAISE) {
timer_free();
}

self->mode = new_mode;
}
Loading