Making Watchdog Timer with NRF24LE1 Circuit
Till now in the series on NRF24LE1, we have covered many interesting and special features which differentiate NRF from others. Today we are going to discuss an important functionality of microcontrollers which helps them recover from failure. Suppose, I give you a task to solve in predefined time and you fail to do it in allotted time then you will have to start it from the beginning. The word ‘Timeout’ can be used here to clarify the concept.
But do you know that microcontroller can also keep track of time and if it fails to achieve the task in allotted time, it can restart itself. This is done in order to avoid malfunction and hanging. Or we can say in case of system failure it automatically restarts itself. This also eliminates the need of human for manual restarting. It is an essential part in designing remote and automated systems. The timer which is used to keep track of time is known as Watchdog Timer. The name is itself clear. The Mars Rover also uses this functionality to recover from system failures and malfunction.
Some features of Watchdog Timer in NRF24LE1 are:
• Minimum Watchdog timeout interval : 7.8125ms
• Maximum Watchdog timeout interval : 512 s
• Uses 32.768 KHz frequency clock
• 16- bit register timer
For controlling Watchdog we have WDSV register. It holds 16-bit counter value and is divided into two bytes- MSByte and LSByte. To write the counter value to this register we first have to write LSByte and then MSByte. Same will be for reading the register. The two bytes must be read or write continuously. We can’t read any byte while writing and vice versa. The readout of WDSV register will always give the start value of the counter or the initial value. We can’t get current value of the watchdog timer by reading WDSLV.
The watchdog timer gets activated when two bytes have been written. It counts down from WDSLV*256 to 0. When counter value gets 0,the microcontroller restarts itself. This restart is same as hardware restarting. To avoid reset we can WDSV register with new or same value.
The counter gets restarted if WDSV is written. It is done when our system is working properly and we don’t want reset.
The watchdog timeout in seconds is given by:
The watchdog gets disabled when the system is restarted or when we are using NRF in register retention or memory retention power saving modes. It uses 32.768 KHz low frequency clock. Before using timer we have to enable the clock. This frequency can be provided externally or can be derived from 16 MHz crystal oscillator. The source of 32.768 KHZ can be controlled by CLKLFCTRL (Clock Low Frequency Control) register. The contents of this register are - :
• Bit 7 – Read CLKLF(phase)
• Bit 6 – CLKLF ready to be used
• Bit 5:4 – Reserved
• Bit 3 - Read 16 MHz clock source. 1: Crystal Oscillator, 0: RC Oscillator
• Bit 2:0 – Source for CLKLF.
000: Crystal 32K
001: RC 32K
001: Synthesized from 16 MHz Crystal
011: From IO pin used as XC1 to XOSC32K (low amplitude signal)
100: From IO pin (digital rail-to-rail signal)
111: None selected
Simple steps to use the Watchdog functionality are:
• First we have to enable 32.768 KHz clock. For that we use CLKLFCTRL register. For simplicity we will use synthesized clock. We can do it by writing 001 in Bit2:1.
• Then we have to check if the clock has started or not. We can do this by checking Bit 6.
• After that we can write LSByte and then MSByte of WDSV.
We can also use functions provided by Nordic Library:
|hal_wdog_init()||16-bit start value||-||To initialize the watchdog timer with start value|
|hal_wdog_restart()||-||-||To restart the watchdog timer to avoid reset|
We have written a code to understand the functioning of Watchdog properly.