forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
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 f471960
wdt: add watchdog support
xobs abd01c5
nrf: add watchdog module
xobs fbe1c05
nrf: simmel: enable wdt support
xobs c23f151
nrf: enable wdt for all platforms
xobs 595f638
watchdog: rename module from `wdt` and move to `microcontroller`
xobs 8f46133
nrf: watchdog: implement software watchdogtimer
xobs 87737fb
watchdog: fix documentation build error
xobs 0169ea5
nrf: discard arm exception sections
xobs ae950bc
add WatchDogTimeout exception
xobs ec99dae
tick: break on watchdog timeout exception
xobs 08362c9
watchdogtimer: refactor to new api
xobs 8c5df5f
supervisor: add a new WATCHDOG_RESET safe mode reason
xobs e738f5e
nrf: boot into safe mode sometimes for watchdog reset
xobs 589cb1a
nrf: watchdog: use nrfx_wdt driver
xobs bd086a1
Revert "add WatchDogTimeout exception"
xobs 15530a6
supervisor: tick: check for watchdog exception if enabled
xobs 108409c
nrf: common-hal: finish reworking exceptions
xobs c5c13a8
nrf: reset watchdog as part of port_reset()
xobs c7efc94
watchdog: move timeout exception to shared-bindings
xobs 5edc29c
nrf: microcontroller: use port reset path
xobs daf7c28
nrf: port: save rtc value across reboots
xobs 33001f4
nrf: simmel: shrink filesystem to fit watchdog timer
xobs d0f1b59
nrf: pca10100: disable some unused features to shrink image
xobs 296ba10
nrf: set WDT priority to 2
xobs e470376
nrf: add ticks (not subticks) to overflow count during reset
xobs dbf1bef
watchdog: support catching the timeout
xobs aac5a4f
watchdog: use common_hal_watchdog_* pattern
xobs aa04452
locale: generate strings for watchdog timer
xobs 9380c34
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt 1ed4978
Remove NONE from mode enum and doc tweaks
tannewt 198f957
Merge pull request #1 from tannewt/wdt-nrf
xobs 2fd7a43
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt 323cca8
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt 959f729
Merge remote-tracking branch 'adafruit/master' into wdt-nrf
tannewt 7e632c9
Update translations
tannewt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 "" | ||
|
@@ -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 "" | ||
|
@@ -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 "" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.