This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Creating Multiple Watchdog Timers

Hi,

For my application, I would like to have multiple watchdog timers.  I am measuring several sensors (I want one short-term watchdog timer to be fed in this loop, pause during sleep mode), and then I am sleeping for a much longer time (minutes) in which I would like a longer term watchdog timer (run during sleep mode).  I tried to use a different nrf_drv_wdt_config_t for each case, but then when I try nrf_drv_wdt_init() the 2nd time, I get error code 8 (Invalid State).  It seems like there is a possibility to use multiple wdt channels, but how can I set them up with different nrf_drv_wdt_config_t?

The code looks something like this:

    // WDT for within a measurement loop
    // Default values: Pause in SLEEP, Pause in HALT; 2000 ms; IRQ 7
    nrf_drv_wdt_config_t wdt_meas_config = NRF_DRV_WDT_DEAFULT_CONFIG;
    wdt_meas_config.reload_value = WDT_TIMEOUT_MEAS;
    err_code = nrf_drv_wdt_init(&wdt_meas_config, wdt_event_handler);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_wdt_channel_alloc(&wdt_channel_id);
    APP_ERROR_CHECK(err_code);

    // WDT for long sleep cycles
    // Default values: Pause in SLEEP, Pause in HALT; 2000 ms; IRQ 7
    nrf_drv_wdt_config_t wdt_sleep_config = NRF_DRV_WDT_DEAFULT_CONFIG;
    wdt_sleep_config.reload_value = WDT_TIMEOUT_SLEEP;
    wdt_sleep_config.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP;	// keep it running while waiting
    err_code = nrf_drv_wdt_init(&wdt_sleep_config, wdt_event_handler);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_wdt_channel_alloc(&wdt_sleep_channel_id);
    APP_ERROR_CHECK(err_code);

Parents
  • Hi,

    The watchdog in the nRF52 is designed to not accept any tampering after it has been started. It is not possible to stop it or reconfigure it once it has started. Because of this, the watchdog cannot be used in different modes based on the state of the firmware as you describe.

    In general, sleeping and reliably waking from sleep is simple enough that you don't need a dedicated watchdog timer to monitor this. My recommendation is to use the watchdog to monitor the operation of the sensors and not use a long-term watchdog. To do this, you should configure the watchdog to pause during sleep. Note that there is an errata regarding reloading of such a watchdog right before sleeping, but as long as reloading the watchdog is not the last thing you do before sleeping this is not an issue.

    Best regards,
    Rune Holmgren

  • Thanks, that clears things up a bit.  My main concern is that the device goes to sleep and then never wakes up for some reason, which is what the 2nd WDT would prevent.  But it sounds like only a single watchdog configuration is allowed (although I guess you can have multiple channels of WDT but they must share the same config).  Is there any other reliable way to safeguard against this?

Reply
  • Thanks, that clears things up a bit.  My main concern is that the device goes to sleep and then never wakes up for some reason, which is what the 2nd WDT would prevent.  But it sounds like only a single watchdog configuration is allowed (although I guess you can have multiple channels of WDT but they must share the same config).  Is there any other reliable way to safeguard against this?

Children
  • If properly configured ( fault free code ), the device will always wake up from sleep. But you are able to write code that will cause the device to never wake up..

    If you want to safeguard against errors in the code you can let the watchdog run during sleep and then wake up and refresh the watchdog at a specific interval that is less than the watchdog timeout interval. This can be acheived with very little power consumption if using RTC compare interrupt, put the refresh code inside the ISR and just go right back to sleep.

    I usually turn off the watchdog during sleep. 

  • My issue is that I have sensors running for ~10s, then it sleeps for 5 min.  If I set the WDT for more than 5 min, that would solve the issue if an error occurs.  But during those 5 min, the sensors will run and drain a LOT of battery.  Ideally I would have 2 WDTs, one for the 10s loop, and one for the 5 min loop.  But it seems like we can only have a single configuration for the WDT (even if you have them on different channels?).  Is this limitation correct?

  • The nRF52 only has one watchdog timer peripheral.

    You could of course set the watchdog timeout to for instance 10 seconds and wake up just to refresh it. But if you are suppose to wake up every 5 mintues this will certainly use more power then required...

    I would leave the watchdog in pause during sleep, then use an RTC compare interrupt to wake the device up every 5 minutes. This will utilize very little power. I'm using this approach all the time when power consumption is critical. I've never had ANY issue with the device not waking up when it was suppose to.

    If you really want two watchdogs you could do something fancy externally using capacitors to create a timedelay involving the reset line, or using an external IC/controller. 

Related