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

watchdog timer

Is there an app-note, tutorial, or example of how to use the watchdog timer to reset the MCU if the application hangs?

  • Please take a look at the main-file that should show the basics.

    main.c

  • The attached file does show some of the basics but not much. I took a look at nrf51.h where the data structures are defined and the comments are pretty terse there too. I want to make sure I use this timer correctly in the context of a device that sleeps, wakes on one of a number of interrupt signals, does some processing, and goes back to sleep. Should the processing hang, I'd like the watchdog to interrupt the MCU, for the watchdog handler to log a message and a stack trace to flash, and then do a reset. I would expect that everyone would want to do this for production code and I'm surprised I couldn't find an example or at least more documentation.

    For example, what is the name of the watchdog interrupt handler and what utilities exist to log a stack trace?

  • If you want to use the watchdog interrupt, this is used like any other interrupt, you enable the event to generate an interrupt in the peripheral's INTEN register, and then enable the interrupt in the NVIC.

    As you can see in section 19.1.3 of the reference manual, this will give you 2 32.768 kHz clock cycles to do your logging and stack tracing. On reset, you can use the RESETREAS register to know the cause of the reset.

    The name of all interrupts are most easy to find in the startup file, and as you can see there, the watchdog interrupt handler will by default be WDT_IRQHandler. There are no utilities available to do stack tracing, but you can take a look at the ble_debug_assert_handler() function, which does similar things.

  • The watchdog timer doesnt work on my nrf51822! I set the timer and started as in the example just like that but thje system doesnt reset or do a interrupt (for ISR i enable the interrupt).

    I also use the RTC for a simple timer in my system and use as source the RC.

  • This would probably have been better as a separate question. :-)

    A quick guess is however that you haven't done a pin reset after enabling the watchdog. The watchdog doesn't run as long as the debugger is enabled, and the debug interface stays enabled till a power-cycle or a pin-reset (which can be done with for example nrfjprog -p).

Related