RZT Flexible Software Package Documentation  Release v2.2.0

 
Watchdog Timer (r_wdt)

Functions

fsp_err_t R_WDT_Refresh (wdt_ctrl_t *const p_ctrl)
 
fsp_err_t R_WDT_Open (wdt_ctrl_t *const p_ctrl, wdt_cfg_t const *const p_cfg)
 
fsp_err_t R_WDT_StatusClear (wdt_ctrl_t *const p_ctrl, const wdt_status_t status)
 
fsp_err_t R_WDT_StatusGet (wdt_ctrl_t *const p_ctrl, wdt_status_t *const p_status)
 
fsp_err_t R_WDT_CounterGet (wdt_ctrl_t *const p_ctrl, uint32_t *const p_count)
 
fsp_err_t R_WDT_TimeoutGet (wdt_ctrl_t *const p_ctrl, wdt_timeout_values_t *const p_timeout)
 
fsp_err_t R_WDT_CallbackSet (wdt_ctrl_t *const p_ctrl, void(*p_callback)(wdt_callback_args_t *), void const *const p_context, wdt_callback_args_t *const p_callback_memory)
 

Detailed Description

Driver for the WDT peripheral on RZ microprocessor. This module implements the WDT Interface.

Overview

The watchdog timer is used to recover from unexpected errors in an application. The watchdog timer must be refreshed periodically in the permitted count window by the application. If the count is allowed to underflow or refresh occurs outside of the valid refresh period, the WDT generates an interrupt request signal.

r_wdt_operation_example.png
Watchdog Timer Operation Example (Note that the error is notified to the ICU (Interrupt Controller) instead of 'device reset or NMI'.)

Features

The WDT HAL module has the following key features:

Note
WDT channel 0 is only available on CPU0. WDT channel 1 is only available on CPU1.

Configuration

When using register start mode, configure the watchdog timer on the Stacks tab.

Build Time Configurations for r_wdt

The following build time configurations are defined in fsp_cfg/r_wdt_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Multiplex Interrupt
  • Enabled
  • Disabled
Disabled Enable multiplex interrupt for a single driver.

Configurations for Monitoring > Watchdog (r_wdt)

This module can be added to the Stacks tab via New Stack > Monitoring > Watchdog (r_wdt).

ConfigurationOptionsDefaultDescription
NameName must be a valid C symbolg_wdt0 Module name.
Timeout
  • 1,024 Cycles
  • 4,096 Cycles
  • 8,192 Cycles
  • 16,384 Cycles
16,384 Cycles Select the watchdog timeout in cycles.
Clock Division Ratio
  • PCLK/4
  • PCLK/64
  • PCLK/128
  • PCLK/512
  • PCLK/2048
  • PCLK/8192
PCLK/8192 Select the watchdog clock divisor.
Window Start Position
  • 100 (Window Position Not Specified)
  • 75
  • 50
  • 25
100 (Window Position Not Specified) Select the allowed watchdog refresh start point in %.
Window End Position
  • 0 (Window Position Not Specified)
  • 25
  • 50
  • 75
0 (Window Position Not Specified) Select the allowed watchdog refresh end point in %.
WDT CallbackName must be a valid C symbolNULL A user callback function must be provided.

Clock Configuration

The WDT clock is based on the PCLKL frequency. You can set the PCLKL frequency using the Clocks tab of the FSP Configuration editor or by using the CGC Interface at run-time. The maximum timeout period with PCLKL running at 50 MHz is approximately 2.6 seconds.

Pin Configuration

This module does not use I/O pins.

Usage Notes

Interrupt

The WDT outputs an error signal to the ICU. ICU can output PERI_ERRn interrupt (Peripherals error event n) to GIC or cause error reset when ICU accepts error signal from WDT. To use PERI_ERRn interrupt or reset at WDT, Interrupt Controller Unit (ICU) ERROR (r_icu_error) need to be configured. When the PERI_ERRn interrupt is configured to use interrupt and triggered, the callback function registered during open is called.

Note
When using the WDT in software start mode with interrupt request signal and the timer underflows, the WDT status must be reset by calling R_WDT_StatusClear before restarting the timer via R_WDT_Refresh.

Period Calculation

The WDT operates from PCLKL. With a PCLKL 50 MHz, the maximum time from the last refresh to interrupt request signal generation will be just over 2.6 seconds as detailed below.

PLCKL = 50 MHz
Clock division ratio = PCLKL / 8192
Timeout period = 16384 cycles
WDT clock frequency = 50 MHz / 8192 = 6.103 kHz
Cycle time = 1 / 6.103 kHz = 163.85 us
Timeout = 163.85 us x 16384 cycles = 2.684 seconds

Limitations

Developers should be aware of the following limitations when using the WDT:

Examples

WDT Basic Example

This is a basic example of minimal use of the WDT in an application.

/* Example callback called from ICU_ERROR driver when a watchdog event occurs. */
void wdt_error_callback (error_callback_args_t * p_args)
{
#if (3 == BSP_FEATURE_ICU_ERROR_PERIPHERAL_TYPE)
(ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDFR0 == p_args->error_status))
{
R_ICU_ERROR_StatusClear(&g_error_ctrl, ERROR_EVENT_PERIPHERAL_0, ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDFR0);
}
#else
(ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDF0 == p_args->error_status))
{
R_ICU_ERROR_StatusClear(&g_error_ctrl, ERROR_EVENT_PERIPHERAL_0, ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDF0);
}
#endif
}
void wdt_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Open the ICU_ERROR driver to configure behavior when the WDT event occurs. */
err = R_ICU_ERROR_Open(&g_error_ctrl, &g_error_cfg);
/* Handle any errors. This function should be defined by the user. */
handle_error(err);
/* Initializes the WDT module. */
err = R_WDT_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
handle_error(err);
/* In register start mode, start the watchdog by calling R_WDT_Refresh. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
handle_error(err);
while (true)
{
/* Application work here. */
/* Refresh before the counter underflows to prevent watchdog event. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
handle_error(err);
}
}

WDT Advanced Example

This example demonstrates using a start window and gives an example callback to handle an IRQ generated by an underflow or refresh error.

#define WDT_TIMEOUT_COUNTS (16384U)
#define WDT_MAX_COUNTER (WDT_TIMEOUT_COUNTS - 1U)
#define WDT_START_WINDOW_75 ((WDT_MAX_COUNTER * 3) / 4)
/* Example callback called from WDT driver when a watchdog event occurs. */
void wdt_callback (wdt_callback_args_t * p_args)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Determine the source of the watchdog event. */
err = R_WDT_StatusGet(&g_wdt0_ctrl, &status);
handle_error(err);
/* (Optional) Log source and any other debug information. */
/* (Optional) Clear the error flags. */
err = R_WDT_StatusClear(&g_wdt0_ctrl, status);
handle_error(err);
/* (Register start mode) In register start mode, call R_WDT_Refresh() to
* continue using the watchdog after an error. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
handle_error(err);
}
/* Example callback called from ICU_ERROR driver when a watchdog event occurs. */
void wdt_error_advanced_callback (error_callback_args_t * p_args)
{
#if (3 == BSP_FEATURE_ICU_ERROR_PERIPHERAL_TYPE)
(ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDFR0 == p_args->error_status))
{
R_ICU_ERROR_StatusClear(&g_error_ctrl, ERROR_EVENT_PERIPHERAL_0, ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDFR0);
}
#else
(ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDF0 == p_args->error_status))
{
R_ICU_ERROR_StatusClear(&g_error_ctrl, ERROR_EVENT_PERIPHERAL_0, ICU_ERROR_PERIPHERAL_ERROR_0_WDT_NMIUNDF0);
}
#endif
}
void wdt_advanced_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Check if the ERRF flag is set to know if the system is
* recovering from a WDT reset. */
if (R_SYSC_NS->RSTSR0_b.ERRF)
{
/* Clear the flag. */
volatile uint32_t dummy = R_SYSC_NS->RSTSR0;
R_SYSC_NS->RSTSR0 = 0x00000000U;
}
/* Open the ICU_ERROR driver to configure behavior when the WDT event occurs. */
err = R_ICU_ERROR_Open(&g_error_ctrl, &g_error_cfg);
/* Handle any errors. This function should be defined by the user. */
handle_error(err);
/* Open the WDT module. */
err = R_WDT_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
handle_error(err);
/* Initialize other application code. */
/* (Register start mode) Call R_WDT_Refresh() to start the WDT in register
* start mode. Do not call R_WDT_Refresh() in auto start mode unless the
* counter is in the acceptable refresh window. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
handle_error(err);
while (true)
{
/* Application work here. */
/* (Optional) If there is a chance the application takes less time than
* the start window, verify the WDT counter is past the start window
* before refreshing the WDT. */
uint32_t wdt_counter = 0U;
do
{
/* Read the current WDT counter value. */
err = R_WDT_CounterGet(&g_wdt0_ctrl, &wdt_counter);
handle_error(err);
} while (wdt_counter >= WDT_START_WINDOW_75);
/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDT_Refresh(&g_wdt0_ctrl);
handle_error(err);
}
}

Data Structures

struct  wdt_instance_ctrl_t
 

Data Structure Documentation

◆ wdt_instance_ctrl_t

struct wdt_instance_ctrl_t

WDT private control block. DO NOT MODIFY. Initialization occurs when R_WDT_Open() is called.

Function Documentation

◆ R_WDT_Refresh()

fsp_err_t R_WDT_Refresh ( wdt_ctrl_t *const  p_ctrl)

Refresh the watchdog timer. Implements wdt_api_t::refresh.

In addition to refreshing the watchdog counter this function can be used to start the counter in register start mode.

Return values
FSP_SUCCESSWDT successfully refreshed.
FSP_ERR_ASSERTIONp_ctrl is NULL.
FSP_ERR_NOT_OPENInstance control block is not initialized.
Note
This function only returns FSP_SUCCESS. If the refresh fails due to being performed outside of the permitted refresh period the device will trigger an ISR to run.

◆ R_WDT_Open()

fsp_err_t R_WDT_Open ( wdt_ctrl_t *const  p_ctrl,
wdt_cfg_t const *const  p_cfg 
)

Configure the WDT in register start mode. Implements wdt_api_t::open.

This function should only be called once as WDT configuration registers can only be written to once so subsequent calls will have no effect.

Return values
FSP_SUCCESSWDT successfully configured.
FSP_ERR_ASSERTIONNull pointer, or one or more configuration options is invalid.
FSP_ERR_ALREADY_OPENModule is already open. This module can only be opened once.

◆ R_WDT_StatusClear()

fsp_err_t R_WDT_StatusClear ( wdt_ctrl_t *const  p_ctrl,
const wdt_status_t  status 
)

Clear the WDT status and error flags. Implements wdt_api_t::statusClear.

Return values
FSP_SUCCESSWDT flag(s) successfully cleared.
FSP_ERR_ASSERTIONNull pointer as a parameter.
FSP_ERR_NOT_OPENInstance control block is not initialized.

◆ R_WDT_StatusGet()

fsp_err_t R_WDT_StatusGet ( wdt_ctrl_t *const  p_ctrl,
wdt_status_t *const  p_status 
)

Read the WDT status flags. Implements wdt_api_t::statusGet.

Indicates both status and error conditions.

Return values
FSP_SUCCESSWDT status successfully read.
FSP_ERR_ASSERTIONNull pointer as a parameter.
FSP_ERR_NOT_OPENInstance control block is not initialized.

◆ R_WDT_CounterGet()

fsp_err_t R_WDT_CounterGet ( wdt_ctrl_t *const  p_ctrl,
uint32_t *const  p_count 
)

Read the current count value of the WDT. Implements wdt_api_t::counterGet.

Return values
FSP_SUCCESSWDT current count successfully read.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENInstance control block is not initialized.

◆ R_WDT_TimeoutGet()

fsp_err_t R_WDT_TimeoutGet ( wdt_ctrl_t *const  p_ctrl,
wdt_timeout_values_t *const  p_timeout 
)

Read timeout information for the watchdog timer. Implements wdt_api_t::timeoutGet.

Return values
FSP_SUCCESSWDT timeout information retrieved successfully.
FSP_ERR_ASSERTIONNull Pointer.
FSP_ERR_NOT_OPENInstance control block is not initialized.

◆ R_WDT_CallbackSet()

fsp_err_t R_WDT_CallbackSet ( wdt_ctrl_t *const  p_ctrl,
void(*)(wdt_callback_args_t *)  p_callback,
void const *const  p_context,
wdt_callback_args_t *const  p_callback_memory 
)

Updates the user callback and has option of providing memory for callback structure. Implements wdt_api_t::callbackSet

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.