WDT (Watchdog Timer)

group group_hal_wdt

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

    cy_rslt_t   rslt;
    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
    cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG,
                    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_system_delay_ms(100);
    cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
    cyhal_system_delay_ms(100);

    // Device has restarted due to Watchdog timeout - blink a second time
    if (CYHAL_SYSTEM_RESET_WDT == cyhal_system_get_reset_reason())
    {
        cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_ON);
        cyhal_system_delay_ms(100);
        cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
        cyhal_system_delay_ms(100);
    }

    // Initialize WDT
    rslt = cyhal_wdt_init(&wdt_obj, timeout_ms);

    for (;;)
    {
        // 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.
        cyhal_system_delay_ms(1000);

        // Reset the WDT and avoid a device reset
        cyhal_wdt_kick(&wdt_obj);
    }

Functions

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()).

Returns

CY_RSLT_SUCCESS if the operation was successfull.
Return

The status of the init request

Parameters
  • [out] obj: Pointer to a WDT object. The caller must allocate the memory for this object but the init function will initialize its contents.

  • [inout] timeout_ms: The time in milliseconds before the WDT times out (1ms - max) (see cyhal_wdt_get_max_timeout_ms())

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
  • [inout] obj: The WDT object

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
  • [inout] obj: The WDT object

void cyhal_wdt_start(cyhal_wdt_t *obj)

Start (enable) the WDT.

Return

The status of the start request

Parameters
  • [inout] obj: The WDT object

void cyhal_wdt_stop(cyhal_wdt_t *obj)

Stop (disable) the WDT.

Return

The status of the stop request

Parameters
  • [inout] obj: The WDT object

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.

Return

The time in milliseconds before the WDT times out

Parameters
  • [in] obj: The WDT object

uint32_t cyhal_wdt_get_max_timeout_ms(void)

Gets the maximum WDT timeout in milliseconds.

Return

The maximum timeout for the WDT

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.

Return

The status of WDT is_enabled request

Parameters
  • [in] obj: The WDT object