I ran watchdog with this code:
void wdt_init(void)
{
NRF_WDT->CONFIG = (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) | ( WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos);
NRF_WDT->CRV = 1*32768; //1 sec. timeout
NRF_WDT->RREN |= WDT_RREN_RR0_Msk; //Enable reload register 0
NRF_WDT->TASKS_START = 1; //triggering of watchdog
}
Because I want to save some data to flash before the nrf51822 reset, I tried to use WDT_IRQn (generated on the TIMEOUT event). Unfortunately, my code doesn't work.
APP_TIMER_DEF(wdt_timer_id);
//some code
static void WDT_IRQHandler(void * p_context)
{
LEDS_OFF(BSP_LED_4_MASK);
LEDS_OFF(BSP_LED_3_MASK);
LEDS_OFF(BSP_LED_2_MASK);
LEDS_OFF(BSP_LED_1_MASK);
LEDS_OFF(BSP_LED_0_MASK);
while(1)
{
}
}
void wdt_interrupt_init(void)
{
uint32_t err_code;
NRF_WDT->INTENSET = WDT_INTENSET_TIMEOUT_Msk;
sd_nvic_SetPriority(WDT_IRQn, 1);
sd_nvic_EnableIRQ(WDT_IRQn);
err_code = app_timer_create(&wdt_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
WDT_IRQHandler);
APP_ERROR_CHECK(err_code);
}
Adding WDT_IRQ doesn't change behavior of MCU, it restarts without executing of the WDT_IRQHandler.
I am not sure where I should place app_timer_start(wdt_timer_id, NRF_WDT->CRV, NULL) function. Up to now it is placed right beneath the place where the watchdog is being reloaded.