|
4 | 4 | #include <InternalFileSystem.h> |
5 | 5 | #include <SPI.h> |
6 | 6 | #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 | + |
7 | 14 | #include <assert.h> |
8 | 15 | #include <ble_gap.h> |
9 | 16 | #include <memory.h> |
|
19 | 26 | #include "BQ25713.h" |
20 | 27 | #endif |
21 | 28 |
|
| 29 | +static nrfx_wdt_t nrfx_wdt = NRFX_WDT_INSTANCE(0); |
| 30 | +static nrfx_wdt_channel_id nrfx_wdt_channel_id_nrf52_main; |
| 31 | + |
22 | 32 | static inline void debugger_break(void) |
23 | 33 | { |
24 | 34 | __asm volatile("bkpt #0x01\n\t" |
@@ -202,6 +212,15 @@ void checkSDEvents() |
202 | 212 |
|
203 | 213 | void nrf52Loop() |
204 | 214 | { |
| 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 | + |
205 | 224 | checkSDEvents(); |
206 | 225 | reportLittleFSCorruptionOnce(); |
207 | 226 | } |
@@ -269,6 +288,20 @@ void nrf52Setup() |
269 | 288 | LOG_DEBUG("Set random seed %u", seed.seed32); |
270 | 289 | randomSeed(seed.seed32); |
271 | 290 | 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); |
272 | 305 | } |
273 | 306 |
|
274 | 307 | void cpuDeepSleep(uint32_t msecToWake) |
|
0 commit comments