|
4 | 4 | #include <InternalFileSystem.h> |
5 | 5 | #include <SPI.h> |
6 | 6 | #include <Wire.h> |
| 7 | + |
| 8 | +#define APP_WATCHDOG_SECS 90 |
| 9 | +#define NRFX_WDT_ENABLED 1 |
| 10 | +#define NRFX_WDT0_ENABLED 1 |
| 11 | +#define NRFX_WDT_CONFIG_NO_IRQ 1 |
| 12 | +#include <nrfx_wdt.c> |
| 13 | +#include <nrfx_wdt.h> |
| 14 | + |
7 | 15 | #include <assert.h> |
8 | 16 | #include <ble_gap.h> |
9 | 17 | #include <memory.h> |
|
22 | 30 | #include "BQ25713.h" |
23 | 31 | #endif |
24 | 32 |
|
| 33 | +static nrfx_wdt_t nrfx_wdt = NRFX_WDT_INSTANCE(0); |
| 34 | +static nrfx_wdt_channel_id nrfx_wdt_channel_id_nrf52_main; |
| 35 | + |
25 | 36 | static inline void debugger_break(void) |
26 | 37 | { |
27 | 38 | __asm volatile("bkpt #0x01\n\t" |
@@ -205,6 +216,15 @@ void checkSDEvents() |
205 | 216 |
|
206 | 217 | void nrf52Loop() |
207 | 218 | { |
| 219 | + { |
| 220 | + static bool watchdog_running = false; |
| 221 | + if (!watchdog_running) { |
| 222 | + nrfx_wdt_enable(&nrfx_wdt); |
| 223 | + watchdog_running = true; |
| 224 | + } |
| 225 | + } |
| 226 | + nrfx_wdt_channel_feed(&nrfx_wdt, nrfx_wdt_channel_id_nrf52_main); |
| 227 | + |
208 | 228 | checkSDEvents(); |
209 | 229 | reportLittleFSCorruptionOnce(); |
210 | 230 | } |
@@ -272,6 +292,22 @@ void nrf52Setup() |
272 | 292 | LOG_DEBUG("Set random seed %u", seed.seed32); |
273 | 293 | randomSeed(seed.seed32); |
274 | 294 | nRFCrypto.end(); |
| 295 | + |
| 296 | + // Set up nrfx watchdog. Do not enable the watchdog yet (we do that |
| 297 | + // the first time through the main loop), so that other threads can |
| 298 | + // allocate their own wdt channel to protect themselves from hangs. |
| 299 | + nrfx_wdt_config_t wdt0_config = { |
| 300 | + .behaviour = NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT, .reload_value = APP_WATCHDOG_SECS * 1000, |
| 301 | + // Note: Not using wdt interrupts. |
| 302 | + // .interrupt_priority = NRFX_WDT_DEFAULT_CONFIG_IRQ_PRIORITY |
| 303 | + }; |
| 304 | + nrfx_err_t r = nrfx_wdt_init(&nrfx_wdt, &wdt0_config, |
| 305 | + nullptr // Watchdog event handler, not used, we just reset. |
| 306 | + ); |
| 307 | + assert(r == NRFX_SUCCESS); |
| 308 | + |
| 309 | + r = nrfx_wdt_channel_alloc(&nrfx_wdt, &nrfx_wdt_channel_id_nrf52_main); |
| 310 | + assert(r == NRFX_SUCCESS); |
275 | 311 | } |
276 | 312 |
|
277 | 313 | void cpuDeepSleep(uint32_t msecToWake) |
|
0 commit comments