CMSIS-Driver_PIC32CZ-CA70  
Peripheral Library (PLIB) Documentation
 
Loading...
Searching...
No Matches
WDT Interface

Driver API for WDT (plib_wdt.h) More...

Typedefs

typedef void(* WDT_CALLBACK_HANDLER) (uint32_t status)
 Function pointer type for the Watchdog Timer fault callback.
 

Functions

void WDT_Configure (uint16_t wdv, uint16_t wdd, uint32_t mode)
 Configures the Watchdog Timer (WDT). Programs the WDT Mode Register with the specified watchdog counter value, window value, and configuration options.
 
void WDT_Restart (void)
 Restarts the Watchdog Timer. Reloads the WDT counter by writing WDT_CR with the correct password key. This function can be called at any time to kick the watchdog.
 
void WDT_SetCallbackHandler (WDT_CALLBACK_HANDLER callback)
 Registers the callback handler to be invoked from the WDT ISR.
 
uint32_t WDT_StatusGet (void)
 Reads and returns the Watchdog Status Register (WDT_SR). Performs a single read of WDT_SR and returns the raw value. Test the result with WDT_SR_WDUNF_Msk (underflow) and WDT_SR_WDERR_Msk (error). Reading WDT_SR clears both the WDUNF and WDERR flags.
 
bool WDT_IsEnabled (void)
 Checks if the WDT is enabled.
 

Description

Driver API for WDT (plib_wdt.h)

Usage Example

The following example demonstrates typical usage of the WDT peripheral:

#include "plib_wdt.h"
#include <stdint.h>
#include <stdbool.h>
volatile bool wdt_underflow_triggered = false;
volatile bool wdt_error_triggered = false;
static void WDT_FaultCallback(uint32_t status)
{
if ((status & WDT_SR_WDUNF_Msk) != 0U)
{
wdt_underflow_triggered = true;
//user operation (e.g., log error, safe shutdown)
}
if ((status & WDT_SR_WDERR_Msk) != 0U)
{
wdt_error_triggered = true;
//user operation (e.g., log error, safe shutdown)
}
}
int32_t configure_wdt(void)
{
/* WDV/WDD are 12-bit fields (0x000-0xFFF) of WDT_MR. */
const uint16_t wdt_timeout_period_value = 0x800U;
const uint16_t wdt_window_period_value = 0x400U;
/* Register callback handler for WDT fault */
WDT_SetCallbackHandler(&WDT_FaultCallback);
/* Configure WDT with all settings in one call (write-once register)
* - Timeout period : WDV = 0x800 (~8 seconds at 32.768 kHz SLCK)
* - Window period : WDD = 0x400
* - Reset on fault : enabled
* - Fault interrupt: enabled
* - Idle halt : disabled (keep running in idle)
* - Debug halt : enabled (stop during debug)
*/
WDT_Configure(wdt_timeout_period_value, wdt_window_period_value,
WDT_MR_WDRSTEN_Msk | WDT_MR_WDFIEN_Msk | WDT_MR_WDDBGHLT_Msk);
return 0;
}
void WDT_SetCallbackHandler(WDT_CALLBACK_HANDLER callback)
Registers the callback handler to be invoked from the WDT ISR.
void WDT_Configure(uint16_t wdv, uint16_t wdd, uint32_t mode)
Configures the Watchdog Timer (WDT). Programs the WDT Mode Register with the specified watchdog count...

Typedef Documentation

◆ WDT_CALLBACK_HANDLER

typedef void(* WDT_CALLBACK_HANDLER) (uint32_t status)

Function pointer type for the Watchdog Timer fault callback.

Parameters
statusSnapshot of WDT_SR captured once by WDT_Handler(). Test it with the status masks:
  • WDT_SR_WDUNF_Msk : Watchdog Underflow occurred.
  • WDT_SR_WDERR_Msk : Watchdog Error (out-of-window reload) occurred.

Function Documentation

◆ WDT_Configure()

void WDT_Configure ( uint16_t wdv,
uint16_t wdd,
uint32_t mode )

Configures the Watchdog Timer (WDT). Programs the WDT Mode Register with the specified watchdog counter value, window value, and configuration options.

Note
The WDT_MR register is write-once after a processor reset. Writing WDT_MR also reloads and restarts the down counter, so the watchdog begins counting immediately when this function is called.
Parameters
wdvWatchdog counter value (12-bit). Valid range: 0x000 to 0xFFF. Values outside this range are silently truncated to the low 12 bits.
wddWatchdog delta/window value (12-bit). Valid range: 0x000 to 0xFFF. Values outside this range are silently truncated to the low 12 bits. Programming wdd >= wdv disables the reload-window check, allowing WDT_Restart() to be called anywhere within [0, wdv] (non-windowed mode).
modeBitwise OR of WDT_MR_WDDIS_Msk (disable watchdog), WDT_MR_WDRSTEN_Msk (reset on fault), WDT_MR_WDFIEN_Msk (fault interrupt), WDT_MR_WDIDLEHLT_Msk (halt in idle/sleep), and/or WDT_MR_WDDBGHLT_Msk (halt in debug).
Returns
void

◆ WDT_IsEnabled()

bool WDT_IsEnabled ( void )

Checks if the WDT is enabled.

Returns
true if the WDT is enabled (WDDIS = 0), false if disabled.

◆ WDT_Restart()

void WDT_Restart ( void )

Restarts the Watchdog Timer. Reloads the WDT counter by writing WDT_CR with the correct password key. This function can be called at any time to kick the watchdog.

Returns
void

◆ WDT_SetCallbackHandler()

void WDT_SetCallbackHandler ( WDT_CALLBACK_HANDLER callback)

Registers the callback handler to be invoked from the WDT ISR.

Parameters
callbackPointer to the callback function. This function sets a user-defined function that will be called when a WDT fault (underflow or error) interrupt occurs. The callback receives the WDT_SR status value as an argument.
Returns
void

◆ WDT_StatusGet()

uint32_t WDT_StatusGet ( void )

Reads and returns the Watchdog Status Register (WDT_SR). Performs a single read of WDT_SR and returns the raw value. Test the result with WDT_SR_WDUNF_Msk (underflow) and WDT_SR_WDERR_Msk (error). Reading WDT_SR clears both the WDUNF and WDERR flags.

Returns
The WDT_SR register value at the time of the read.