The Watchdog timer (WDT) has a 16-bit free-running up-counter. More...
Modules | |
Macros | |
Functions | |
The Watchdog timer (WDT) has a 16-bit free-running up-counter.
The functions and other declarations used in this driver are in cy_wdt.h. You can include cy_pdl.h to get access to all functions and declarations in the PDL.
The WDT can issue counter match interrupts, and a device reset if its interrupts are not handled. Use the Watchdog timer for two main purposes:
The primary use case is recovering from a CPU or firmware failure. A timeout period is set up in the Watchdog timer, and if a timeout occurs, the device is reset (WRES).
You can use the WDT to generate periodic interrupts. However, the Internal Low-Speed Oscillator (ILO) is the only clock source for the WDT, so its accuracy should be considered. Use the ILO compensation technique to increase accuracy, see Cy_SysClk_IloCompensate.
Other options may provide a more precise periodic interrupt. See WDC (Watchdog Counters) (using Watch Crystal Oscillator (WCO) as clock source) or SysTick (ARM System Timer) (in active power mode).
A "reset cause" register exists, and the firmware should check this register at a start-up. An appropriate action can be taken if a WRES reset is detected.
The user's firmware periodically resets the timeout period (clears or "feeds" the watchdog) before a timeout occurs. If the firmware fails to do so, that is considered to be a CPU crash or a firmware failure, and the reason for a device reset. The WDT can generate an interrupt instead of a device reset. The Interrupt Service Routine (ISR) can handle the interrupt either as a periodic interrupt, or as an early indication of a firmware failure and respond accordingly. However, it is not recommended to use the WDT for periodic interrupt generation. The Watchdog Counter (WDC) can be used to generate periodic interrupts if such are presented in the device.
Functional Description
The WDT generates an interrupt when the count value in the counter equals the configured match value.
Note that the counter is not reset on a match. In such case the WDT reset period is: WDT_Reset_Period = ILO_Period * (2*2^(16-IgnoreBits) + MatchValue); When the counter reaches a match value, it generates an interrupt and then keeps counting up until it overflows and rolls back to zero and reaches the match value again, at which point another interrupt is generated.
To use a WDT to generate a periodic interrupt, the match value should be incremented in the ISR. As a result, the next WDT interrupt is generated when the counter reaches a new match value.
You can also reduce the entire WDT counter period by specifying the number of most significant bits that are ignored in the WDT counter. For example, if the Cy_WDT_SetIgnoreBits() function is called with parameter 3, the WDT counter becomes a 13-bit free-running up-counter.
Power Modes
WDT can operate in all possible low power modes.
In CPU Active mode, an interrupt request from the WDT is sent to the CPU. In CPU Sleep, CPU Deep Sleep mode, the CPU subsystem is powered down, so the interrupt request from the WDT is sent directly to the WakeUp Interrupt Controller (WIC) which will then wake up the CPU. The CPU then acknowledges the interrupt request and executes the ISR.
Clock Source
The WDT is clocked by the ILO. The WDT must be disabled before disabling the ILO. Need to consider ILO accuracy while configuring WDT intervals to make sure that unwanted device resets do not occur on some devices.
Refer to the device datasheet for more information on the oscillator accuracy.
Clearing WDT
The ILO clock is asynchronous to the SysClk. Therefore it generally takes three ILO cycles for WDT register changes to come into effect. It is important to remember that a WDT should be cleared at least four cycles (3 + 1 for sure) before a timeout occurs, especially when small match values / low-toggle bit numbers are used.
Reset Detection
Use the Cy_SysLib_GetResetReason() function to detect whether the WDT has triggered a device reset.
Interrupt Configuration
If the WDT is configured to generate an interrupt, pending interrupts must be cleared within the ISR (otherwise, the interrupt will be generated continuously). A pending interrupt to the WDT block must be cleared by calling the Cy_WDT_ClearInterrupt() function. The call to the function will clear the unhandled WDT interrupt counter.
Use the WDT ISR as a timer to trigger certain actions and to change a next WDT match value.
Ensure that the interrupts from the WDT are passed to the CPU to avoid unregistered interrupts. Unregistered WDT interrupts result in a continuous device reset. To avoid this, call Cy_WDT_UnmaskInterrupt(). After that, call the WDT API functions for interrupt handling/clearing.
To start the WDT, make sure that ILO is enabled. After the ILO is enabled, ensure that the watchdog reset is disabled by calling the Cy_WDT_Disable() function. Set the WDT match value by calling Cy_WDT_SetMatch() with the required match value. If needed, set the ignore bits for reducing the WDT counter period by calling Cy_WDT_SetIgnoreBits() function. After the WDT configuration is set, call Cy_WDT_Enable().
When the WDT is used to protect against system crashes, the WDT interrupt should be cleared by a portion of the code that is not directly associated with the WDT interrupt. Otherwise, it is possible that the main firmware loop has crashed or is in an endless loop, but the WDT interrupt vector continues to operate and service the WDT. The user should:
Set parameters of the WDT.
Create an interrupt config structure and interrupt handler.
Hook the interrupt service routine and enable interrupt than config the WDT.
In this case, the WDT resets every third WDT event of the system(1.2287s at ILO 40kHz).
As part of this code, the snippet uses the formula from the Functional Description .
For more information on the WDT peripheral, refer to the technical reference manual (TRM).
Version | Changes | Reason for Change |
---|---|---|
1.0.3 | Updated code snippets. | Documentation enhancement. |
1.0.2 | Update the paths to the code snippets. | PDL structure update. |
1.0.1 | Code snippets added. | Documentation enhancement. |
1.0 | Initial version |