RX Flexible Software Package Documentation  Release v1.0.0

 
Independent Watchdog (r_iwdt)

Functions

fsp_err_t R_IWDT_Refresh (wdt_ctrl_t *const p_api_ctrl)
 
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_StatusClear (wdt_ctrl_t *const p_api_ctrl, const wdt_status_t status)
 
fsp_err_t R_IWDT_StatusGet (wdt_ctrl_t *const p_api_ctrl, wdt_status_t *const p_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 p_context, wdt_callback_args_t *const p_callback_memory)
 

Detailed Description

Driver for the IWDT peripheral on RX 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 IRQ.

Features

The IWDT HAL module has the following key features:

Selecting a Watchdog

RX 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

The configuration of start mode setting is on the BSP tab. The default setting for the start mode is register start mode.

When using register start mode, no need to update the configuration of start mode on the BSP tab. Configure the independence 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.
NMI SupportMCU Specific OptionsNMI is not supported.
IRQ SupportMCU Specific OptionsIf enabled, code for IRQ support 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).

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.(NMI request output is not supported)
Stop ControlMCU Specific OptionsSelect the independent watchdog state in low power mode.
NMI/IRQ 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.
IRQ PriorityMCU Specific OptionsSelect the IWDT interrupt priority.

Clock Configuration

The IWDT clock is based on the IWDTCLK frequency. You can set the IWDTCLK frequency divider using the BSP tab of the RX 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
NMI request output is not supported in IWDT module.
We can use IRQ instead of NMI in IWDT module.

IRQ Interrupt

The watchdog timer uses an IRQ, and the interrupt priority level must be configured before use. When the IRQ is triggered, the callback function registered during open is called.

Note
When using the IWDT in software start mode with IRQ 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 120000 Hz, the maximum time from the last refresh to device reset or IRQ generation will be just below 35 seconds as detailed below.

IWDTCLK = 120000 Hz
Clock division ratio = IWDTCLK / 256
Timeout period = 16384 cycles
WDT clock frequency = 120000 Hz / 256 = 468.75 Hz
Cycle time = 1 / 468.75 Hz = 2.1333 ms
Timeout = 2.1333 ms x 16384 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 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 for refreshing before the counter underflows to prevent reset.

#define IWDT_TIMEOUT_COUNTS (2048U)
#define IWDT_MAX_COUNTER (IWDT_TIMEOUT_COUNTS - 1U)
#define IWDT_START_WINDOW_75 ((IWDT_MAX_COUNTER * 3) / 4)
void iwdt_advanced_example (void)
{
fsp_err_t err = FSP_SUCCESS;
#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
 
struct  iwdt_extended_cfg_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 * 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.
 

◆ iwdt_extended_cfg_t

struct iwdt_extended_cfg_t

IWDT Extended Configuration

Data Fields
IRQn_Type irq IWUNI IRQ number.
uint8_t ipl IWUNI interrupt priority.

Function Documentation

◆ 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/IRQ ISR to run.

Example:

/* Refresh before the counter underflows to prevent reset 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_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.

◆ 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.

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/IRQ 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_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.

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_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  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.