RA Flexible Software Package Documentation  Release v5.6.0

 
Independent Watchdog (r_iwdt)

Functions

fsp_err_t R_IWDT_Open (wdt_ctrl_t *const p_api_ctrl, wdt_cfg_t const *const p_cfg)
 
fsp_err_t R_IWDT_Refresh (wdt_ctrl_t *const p_api_ctrl)
 
fsp_err_t R_IWDT_StatusGet (wdt_ctrl_t *const p_api_ctrl, wdt_status_t *const p_status)
 
fsp_err_t R_IWDT_StatusClear (wdt_ctrl_t *const p_api_ctrl, const wdt_status_t status)
 
fsp_err_t R_IWDT_CounterGet (wdt_ctrl_t *const p_api_ctrl, uint32_t *const p_count)
 
fsp_err_t R_IWDT_TimeoutGet (wdt_ctrl_t *const p_api_ctrl, wdt_timeout_values_t *const p_timeout)
 
fsp_err_t R_IWDT_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 IWDT peripheral on RA MCUs. This module implements the WDT Interface.

Overview

The independent watchdog timer is used to recover from unexpected errors in an application. The 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 IWDT resets the device or generates an NMI.

Features

The IWDT HAL module has the following key features:

Selecting a Watchdog

RA MCUs have two watchdog peripherals: the watchdog timer (WDT) and the independent watchdog timer (IWDT). When selecting between them, consider these factors:

WDT IWDT
Start ModeThe WDT can be started from the application (register start mode) or configured by hardware to start automatically (auto start mode). On most of MCUs, the IWDT can only be configured by hardware to start automatically1.
Clock SourceThe WDT runs off a peripheral clock.The IWDT has its own clock source which improves safety.
Note
1. Refer to the MCU hardware user's manual or datasheet to determine if IWDT supports register start mode.

Configuration

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

Note
When using auto start mode, configurations on the Stacks tab are ignored. Configure the watchdog using the OFS settings on the BSP tab. These settings include the following:
  • Start Mode
  • Timeout Period
  • Dedicated Clock Frequency Divisor
  • Window End Position
  • Window Start Position
  • Reset Interrupt Request Select
  • Stop Control
Review the OFS0 properties window to see additional details.

Build Time Configurations for r_iwdt

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

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Register Start NMI SupportMCU Specific OptionsIf enabled, code for NMI support in register start mode is included in the build.

Configurations for Monitoring > Independent Watchdog (r_iwdt)

This module can be added to the Stacks tab via New Stack > Monitoring > Independent Watchdog (r_iwdt). Non-secure callable guard functions can be generated for this module by right clicking the module in the RA Configuration tool and checking the "Non-secure Callable" box.

ConfigurationOptionsDefaultDescription
NameName must be a valid C symbolg_wdt0 Module name.
TimeoutMCU Specific OptionsSelect the independent watchdog timeout in cycles.
Clock Division RatioMCU Specific OptionsSelect the independent watchdog clock divisor.
Window Start PositionMCU Specific OptionsSelect the allowed independent watchdog refresh start point in %.
Window End PositionMCU Specific OptionsSelect the allowed independent watchdog refresh end point in %.
Reset ControlMCU Specific OptionsSelect what happens when the independent watchdog timer expires.
Stop ControlMCU Specific OptionsSelect the independent watchdog state in low power mode.
NMI callbackName must be a valid C symbolNULL A user callback function can be provided here. If this callback function is provided, it is called from the interrupt service routine (ISR) when the watchdog triggers.

Clock Configuration

The IWDT clock is based on the IWDTCLK frequency. You can set the IWDTCLK frequency divider using the BSP tab of the RA Configuration editor.

Pin Configuration

This module does not use I/O pins.

Usage Notes

NMI Interrupt

The independent watchdog timer uses the NMI, which is enabled by default. No special configuration is required. When the NMI is triggered, the callback function registered during open is called.

Note
When using the IWDT in software start mode with NMI and the timer underflows, the IWDT status must be reset by calling R_IWDT_StatusClear before restarting the timer via R_IWDT_Refresh.

Period Calculation

The IWDT operates from IWDTCLK. With a IWDTCLK of 15000 Hz, the maximum time from the last refresh to device reset or NMI generation will be just below 35 seconds as detailed below.

IWDTCLK = 15000 Hz
Clock division ratio = IWDTCLK / 256
Timeout period = 2048 cycles
WDT clock frequency = 15000 Hz / 256 = 58.59 Hz
Cycle time = 1 / 58.59 Hz = 17.067 ms
Timeout = 17.067 ms x 2048 cycles = 34.95 seconds

Limitations

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

Examples

IWDT Basic Example

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

void iwdt_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* In auto start mode, the IWDT starts counting immediately when the MCU is powered on. */
/* Initializes the module. */
err = R_IWDT_Open(&g_iwdt0_ctrl, &g_iwdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
while (true)
{
/* Application work here. */
/* Refresh before the counter underflows to prevent reset or NMI based on the setting. */
(void) R_IWDT_Refresh(&g_iwdt0_ctrl);
}
}

IWDT Advanced Example

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

#define IWDT_TIMEOUT_COUNTS (2048U)
#define IWDT_MAX_COUNTER (IWDT_TIMEOUT_COUNTS - 1U)
#define IWDT_START_WINDOW_75 ((IWDT_MAX_COUNTER * 3) / 4)
/* Example callback called when a watchdog NMI occurs. */
void iwdt_callback (wdt_callback_args_t * p_args)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Determine the source of the NMI. */
err = R_IWDT_StatusGet(&g_iwdt0_ctrl, &status);
assert(FSP_SUCCESS == err);
/* (Optional) Log source of NMI and any other debug information. */
/* (Optional) Clear the error flags. */
err = R_IWDT_StatusClear(&g_iwdt0_ctrl, status);
assert(FSP_SUCCESS == err);
/* (Optional) Issue a software reset to reset the MCU. */
__NVIC_SystemReset();
}
void iwdt_advanced_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Enable the IWDT to count and generate NMI or reset when the
* debugger is connected. */
R_DEBUG->DBGSTOPCR_b.DBGSTOP_IWDT = 0;
#if defined(R_SYSTEM_RSTSR1_IWDTRF_Msk)
/* (Optional) Check if the IWDTRF flag is set to know if the system is
* recovering from a IWDT reset. */
if (R_SYSTEM->RSTSR1_b.IWDTRF)
{
/* Clear the flag. */
R_SYSTEM->RSTSR1 = 0U;
}
#endif
/* Open the module. */
err = R_IWDT_Open(&g_iwdt0_ctrl, &g_iwdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Initialize other application code. */
/* Do not call R_IWDT_Refresh() in auto start mode unless the
* counter is in the acceptable refresh window. */
(void) R_IWDT_Refresh(&g_iwdt0_ctrl);
while (true)
{
/* Application work here. */
/* (Optional) If there is a chance the application takes less time than
* the start window, verify the IWDT counter is past the start window
* before refreshing the IWDT. */
uint32_t iwdt_counter = 0U;
do
{
/* Read the current IWDT counter value. */
err = R_IWDT_CounterGet(&g_iwdt0_ctrl, &iwdt_counter);
assert(FSP_SUCCESS == err);
} while (iwdt_counter >= IWDT_START_WINDOW_75);
/* Refresh before the counter underflows to prevent reset or NMI. */
(void) R_IWDT_Refresh(&g_iwdt0_ctrl);
}
}

Data Structures

struct  iwdt_instance_ctrl_t
 

Data Structure Documentation

◆ iwdt_instance_ctrl_t

struct iwdt_instance_ctrl_t

IWDT control block. DO NOT INITIALIZE. Initialization occurs when wdt_api_t::open is called.

Data Fields

uint32_t wdt_open
 Indicates whether the open() API has been successfully called.
 
void const * p_context
 Placeholder for user data. Passed to the user callback in wdt_callback_args_t.
 
void(* p_callback )(wdt_callback_args_t *p_args)
 Callback provided when a WDT NMI ISR occurs.
 

Function Documentation

◆ R_IWDT_Open()

fsp_err_t R_IWDT_Open ( wdt_ctrl_t *const  p_api_ctrl,
wdt_cfg_t const *const  p_cfg 
)

Register the IWDT NMI callback.

Example:

/* Initializes the module. */
err = R_IWDT_Open(&g_iwdt0_ctrl, &g_iwdt0_cfg);
Return values
FSP_SUCCESSIWDT successfully configured.
FSP_ERR_ASSERTIONNull Pointer.
FSP_ERR_NOT_ENABLEDAn attempt to open the IWDT when the OFS0 register is not configured for auto-start mode.
FSP_ERR_ALREADY_OPENModule is already open. This module can only be opened once.
FSP_ERR_INVALID_STATEThe security state of the NMI and the module do not match.

◆ R_IWDT_Refresh()

fsp_err_t R_IWDT_Refresh ( wdt_ctrl_t *const  p_api_ctrl)

Refresh the Independent Watchdog Timer. If the refresh fails due to being performed outside of the permitted refresh period the device will either reset or trigger an NMI ISR to run.

Example:

/* Refresh before the counter underflows to prevent reset or NMI based on the setting. */
(void) R_IWDT_Refresh(&g_iwdt0_ctrl);
Return values
FSP_SUCCESSIWDT successfully refreshed.
FSP_ERR_ASSERTIONOne or more parameters are NULL pointers.
FSP_ERR_NOT_OPENThe driver has not been opened. Perform R_IWDT_Open() first.

◆ R_IWDT_StatusGet()

fsp_err_t R_IWDT_StatusGet ( wdt_ctrl_t *const  p_api_ctrl,
wdt_status_t *const  p_status 
)

Read the IWDT status flags.

Indicates both status and error conditions.

Example:

/* (Optional) Determine the source of the NMI. */
err = R_IWDT_StatusGet(&g_iwdt0_ctrl, &status);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSIWDT status successfully read.
FSP_ERR_ASSERTIONNull pointer as a parameter.
FSP_ERR_NOT_OPENThe driver has not been opened. Perform R_IWDT_Open() first.
FSP_ERR_UNSUPPORTEDThis function is only valid if the IWDT generates an NMI when an error occurs.
Note
When the IWDT is configured to output a reset on underflow or refresh error reading the status and error flags serves no purpose as they will always indicate that no underflow has occurred and there is no refresh error. Reading the status and error flags is only valid when interrupt request output is enabled.

◆ R_IWDT_StatusClear()

fsp_err_t R_IWDT_StatusClear ( wdt_ctrl_t *const  p_api_ctrl,
const wdt_status_t  status 
)

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

Example:

/* (Optional) Clear the error flags. */
err = R_IWDT_StatusClear(&g_iwdt0_ctrl, status);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSIWDT flag(s) successfully cleared.
FSP_ERR_ASSERTIONNull pointer as a parameter.
FSP_ERR_NOT_OPENThe driver has not been opened. Perform R_IWDT_Open() first.
FSP_ERR_UNSUPPORTEDThis function is only valid if the IWDT generates an NMI when an error occurs.
Note
When the IWDT is configured to output a reset on underflow or refresh error reading the status and error flags serves no purpose as they will always indicate that no underflow has occurred and there is no refresh error. Reading the status and error flags is only valid when interrupt request output is enabled.

◆ R_IWDT_CounterGet()

fsp_err_t R_IWDT_CounterGet ( wdt_ctrl_t *const  p_api_ctrl,
uint32_t *const  p_count 
)

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

Example:

/* Read the current IWDT counter value. */
err = R_IWDT_CounterGet(&g_iwdt0_ctrl, &iwdt_counter);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSIWDT current count successfully read.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENThe driver has not been opened. Perform R_IWDT_Open() first.

◆ R_IWDT_TimeoutGet()

fsp_err_t R_IWDT_TimeoutGet ( wdt_ctrl_t *const  p_api_ctrl,
wdt_timeout_values_t *const  p_timeout 
)

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

Return values
FSP_SUCCESSIWDT timeout information retrieved successfully.
FSP_ERR_ASSERTIONOne or more parameters are NULL pointers.
FSP_ERR_NOT_OPENThe driver has not been opened. Perform R_IWDT_Open() first.

◆ R_IWDT_CallbackSet()

fsp_err_t R_IWDT_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.
FSP_ERR_NO_CALLBACK_MEMORYp_callback is non-secure and p_callback_memory is either secure or NULL.