Hardware Abstraction Layer (HAL)
All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
WDT (Watchdog Timer)

General Description


High level interface to the Watchdog Timer (WDT).

The WDT can be used for recovering from a CPU or firmware failure. The WDT is initialized with a timeout interval. Once the WDT is started, cyhal_wdt_kick must be called at least once within each timeout interval to reset the count. In case the firmware fails to do so, it is considered to be a CPU crash or firmware failure and the device will be reset.

Features

WDT resets the device if the WDT is not "kicked" using cyhal_wdt_kick within the configured timeout interval.

Quick Start

cyhal_wdt_init() is used to initialize the WDT by providing the WDT object (obj) and the timeout (timeout_ms) value in milliseconds. The timeout parameter can have a minimum value of 1ms. The maximum value of the timeout parameter can be obtained using the cyhal_wdt_get_max_timeout_ms().

Code Snippet

Snippet 1: Initialize the WDT and kick periodically

The following snippet initializes the WDT and illustrates how to reset the WDT within the timeout interval.

#define LED_ON 0u
#define LED_OFF 1u
cyhal_wdt_t wdt_obj;
uint32_t timeout_ms = 2000; // Minimum value of timeout_ms is 1ms
// Initializing WDT
rslt = cyhal_wdt_init(&wdt_obj, timeout_ms);
CY_ASSERT(CY_RSLT_SUCCESS == rslt);
// Initialize the pin connected to an LED on the board
CYBSP_LED_STATE_OFF);
// Device power-on reset or XRES event - blink LED once
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_ON);
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
// Device has restarted due to Watchdog timeout - blink a second time
{
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_ON);
cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
}
// Initialize WDT
rslt = cyhal_wdt_init(&wdt_obj, timeout_ms);
while (loop)
{
// This delay emulates an application task executed between subsequent WDT kicks. Increasing
// this delay to beyond the programmed WDT timeout will cause a device reset.
// Reset the WDT and avoid a device reset
cyhal_wdt_kick(&wdt_obj);
}

API Reference

 WDT HAL Results
 WDT specific return codes.
 

Functions

cy_rslt_t cyhal_wdt_init (cyhal_wdt_t *obj, uint32_t timeout_ms)
 Initialize and start the WDT. More...
 
void cyhal_wdt_free (cyhal_wdt_t *obj)
 Free the WDT. More...
 
void cyhal_wdt_kick (cyhal_wdt_t *obj)
 Resets the WDT. More...
 
void cyhal_wdt_start (cyhal_wdt_t *obj)
 Start (enable) the WDT. More...
 
void cyhal_wdt_stop (cyhal_wdt_t *obj)
 Stop (disable) the WDT. More...
 
uint32_t cyhal_wdt_get_timeout_ms (cyhal_wdt_t *obj)
 Get the WDT timeout. More...
 
uint32_t cyhal_wdt_get_max_timeout_ms (void)
 Gets the maximum WDT timeout in milliseconds. More...
 
bool cyhal_wdt_is_enabled (cyhal_wdt_t *obj)
 Check if WDT is enabled. More...
 

Function Documentation

◆ cyhal_wdt_init()

cy_rslt_t cyhal_wdt_init ( cyhal_wdt_t obj,
uint32_t  timeout_ms 
)

Initialize and start the WDT.

Note
The specified timeout must be at least 1ms and at most the WDT's maximum timeout (see cyhal_wdt_get_max_timeout_ms()).
Parameters
[out]objPointer to a WDT object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in,out]timeout_msThe time in milliseconds before the WDT times out (1ms - max) (see cyhal_wdt_get_max_timeout_ms())
Returns
The status of the init request

Returns CY_RSLT_SUCCESS if the operation was successfull.

◆ cyhal_wdt_free()

void cyhal_wdt_free ( cyhal_wdt_t obj)

Free the WDT.

Powers down the WDT. Releases object (obj). After calling this function no other WDT functions should be called except cyhal_wdt_init().

Parameters
[in,out]objThe WDT object

◆ cyhal_wdt_kick()

void cyhal_wdt_kick ( cyhal_wdt_t obj)

Resets the WDT.

This function must be called periodically to prevent the WDT from timing out and resetting the device.

See Snippet 1: Initialize the WDT and kick periodically

Parameters
[in,out]objThe WDT object

◆ cyhal_wdt_start()

void cyhal_wdt_start ( cyhal_wdt_t obj)

Start (enable) the WDT.

Parameters
[in,out]objThe WDT object
Returns
The status of the start request

◆ cyhal_wdt_stop()

void cyhal_wdt_stop ( cyhal_wdt_t obj)

Stop (disable) the WDT.

Parameters
[in,out]objThe WDT object
Returns
The status of the stop request

◆ cyhal_wdt_get_timeout_ms()

uint32_t cyhal_wdt_get_timeout_ms ( cyhal_wdt_t obj)

Get the WDT timeout.

Gets the configured time, in milliseconds, before the WDT times out.

Parameters
[in]objThe WDT object
Returns
The time in milliseconds before the WDT times out

◆ cyhal_wdt_get_max_timeout_ms()

uint32_t cyhal_wdt_get_max_timeout_ms ( void  )

Gets the maximum WDT timeout in milliseconds.

Returns
The maximum timeout for the WDT

◆ cyhal_wdt_is_enabled()

bool cyhal_wdt_is_enabled ( cyhal_wdt_t obj)

Check if WDT is enabled.

This will return true after cyhal_wdt_start is called. It will return false before the WDT is started, or after cyhal_wdt_stop is called.

Parameters
[in]objThe WDT object
Returns
The status of WDT is_enabled request