Skip to content

Commit 8395429

Browse files
nrf52: add watchdog (#8485)
* nrf52: add watchdog Main thread only for now. * bump framework-arduinoadafruitnrf52 to pick up new wdt support * clang-format the new parts of main-nrf52.cpp --------- Co-authored-by: Ben Meadors <[email protected]>
1 parent 9162142 commit 8395429

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

arch/nrf52/nrf52.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extends = arduino_base
77
platform_packages =
88
; our custom Git version until they merge our PR
99
# TODO renovate
10-
platformio/framework-arduinoadafruitnrf52 @ https://github.com/meshtastic/Adafruit_nRF52_Arduino#e13f5820002a4fb2a5e6754b42ace185277e5adf
10+
platformio/framework-arduinoadafruitnrf52 @ https://github.com/meshtastic/Adafruit_nRF52_Arduino#c770c8a16a351b55b86e347a3d9d7b74ad0bbf39
1111
; Don't renovate toolchain-gccarmnoneeabi
1212
platformio/toolchain-gccarmnoneeabi@~1.90301.0
1313

src/platform/nrf52/main-nrf52.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <InternalFileSystem.h>
55
#include <SPI.h>
66
#include <Wire.h>
7+
8+
#define NRFX_WDT_ENABLED 1
9+
#define NRFX_WDT0_ENABLED 1
10+
#define NRFX_WDT_CONFIG_NO_IRQ 1
11+
#include <nrfx_wdt.c>
12+
#include <nrfx_wdt.h>
13+
714
#include <assert.h>
815
#include <ble_gap.h>
916
#include <memory.h>
@@ -19,6 +26,9 @@
1926
#include "BQ25713.h"
2027
#endif
2128

29+
static nrfx_wdt_t nrfx_wdt = NRFX_WDT_INSTANCE(0);
30+
static nrfx_wdt_channel_id nrfx_wdt_channel_id_nrf52_main;
31+
2232
static inline void debugger_break(void)
2333
{
2434
__asm volatile("bkpt #0x01\n\t"
@@ -202,6 +212,15 @@ void checkSDEvents()
202212

203213
void nrf52Loop()
204214
{
215+
{
216+
static bool watchdog_running = false;
217+
if (!watchdog_running) {
218+
nrfx_wdt_enable(&nrfx_wdt);
219+
watchdog_running = true;
220+
}
221+
}
222+
nrfx_wdt_channel_feed(&nrfx_wdt, nrfx_wdt_channel_id_nrf52_main);
223+
205224
checkSDEvents();
206225
reportLittleFSCorruptionOnce();
207226
}
@@ -269,6 +288,20 @@ void nrf52Setup()
269288
LOG_DEBUG("Set random seed %u", seed.seed32);
270289
randomSeed(seed.seed32);
271290
nRFCrypto.end();
291+
292+
// Set up nrfx watchdog. Do not enable the watchdog yet (we do that
293+
// the first time through the main loop), so that other threads can
294+
// allocate their own wdt channel to protect themselves from hangs.
295+
nrfx_wdt_config_t wdt0_config = {
296+
.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP, .reload_value = 2000,
297+
// Note: Not using wdt interrupts.
298+
// .interrupt_priority = NRFX_WDT_DEFAULT_CONFIG_IRQ_PRIORITY
299+
};
300+
nrfx_err_t r = nrfx_wdt_init(&nrfx_wdt, &wdt0_config,
301+
nullptr // Watchdog event handler, not used, we just reset.
302+
);
303+
304+
r = nrfx_wdt_channel_alloc(&nrfx_wdt, &nrfx_wdt_channel_id_nrf52_main);
272305
}
273306

274307
void cpuDeepSleep(uint32_t msecToWake)

0 commit comments

Comments
 (0)