RAFW Flexible Software Package Documentation  Release v2.0.1

 
Watchdog (r_wdog_w)

Functions

fsp_err_t R_WDOG_W_Open (wdt_ctrl_t *const p_ctrl, wdt_cfg_t const *const p_cfg)
 
fsp_err_t R_WDOG_W_TimeoutGet (wdt_ctrl_t *const p_ctrl, wdt_timeout_values_t *const p_timeout)
 
fsp_err_t R_WDOG_W_TimeoutSet (wdt_ctrl_t *p_ctrl, uint32_t timeout)
 
fsp_err_t R_WDOG_W_Refresh (wdt_ctrl_t *const p_ctrl)
 
fsp_err_t R_WDOG_W_StatusGet (wdt_ctrl_t *const p_ctrl, wdt_status_t *const p_status)
 
fsp_err_t R_WDOG_W_StatusClear (wdt_ctrl_t *const p_ctrl, const wdt_status_t status)
 
fsp_err_t R_WDOG_W_CounterGet (wdt_ctrl_t *const p_ctrl, uint32_t *const p_count)
 
fsp_err_t R_WDOG_W_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)
 
fsp_err_t R_WDOG_W_Freeze (wdt_ctrl_t *const p_ctrl, bool freeze)
 

Detailed Description

Driver for the WDOG peripheral on RA for Wireless (RAFW) MCUs. 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 before underflow. If the watchdog timer count is allowed to underflow, the WDOG resets the device. The WDT also provides a option to generate NMI prior to reset.

Features

The WDT HAL module has the following key features:

Configuration

Build Time Configurations for r_wdog_w

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

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
NMI Support
  • Enabled
  • Disabled
Enabled If enabled, code for NMI support is included in the build.

Configurations for Monitoring > Watchdog (r_wdog_w)

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

ConfigurationOptionsDefaultDescription
NameName must be a valid C symbolg_wdog_w0 Module name.
TimeoutTimeout value in cycles, must be a non-negative integer greater than 1.8191 Select the watchdog timeout in cycles.
Window startValue must be a non-negative integer.${enum.driver.watchdog.window_start_default} Select the watchdog window start in cycles.
Window endValue must be a non-negative integer.0 Select the watchdog window end in cycles (0-(window_start-1)).
Reset Control
  • Reset Output
  • NMI Generated
Reset Output Select what happens when the watchdog timer expires.
NMI CallbackName must be a valid C symbolNULL A user callback function must be provided if the WDOG_W is configured to generate an NMI when the timer underflows or a refresh error occurs. If this callback function is provided, it will be called from the NMI handler each time the watchdog triggers.

Clock Configuration

The WDOG clock is based on a MCU-specific 32KHz clock. The frequency is divided by 320 (fixed). Therefore, the WDOG is clocked at 100 Hz.

Pin Configuration

This module does not use I/O pins.

Usage Notes

NMI Interrupt

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

Period Calculation

The WDOG will be expired in 81.91 sec in default as detailed below.

WDOG clock frequency = 100 Hz Timeout period = 8191 cycles(default) Cycle time = 1 / 100 Hz = 10 ms Timeout = 10 ms x 8191 cycles = 81.91 sec

Limitations

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

Examples

WDOG_W Basic Example

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

void wdog_w_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the watchdog. */
err = R_WDOG_W_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
#ifdef NDEBUG
(void) err;
#endif
/* Start the watchdog by calling R_WDOG_W_Refresh. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
while (true)
{
/* Application work here. */
/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
}
}

WDOG_W Advanced Example

This example gives an example callback to handle an NMI generated by an underflow.

/* The register field indicating a reset due to watchdog differs among devices. */
#ifdef CRG_TOP_RESET_STAT_REG_M33_WDOG_STAT_Msk
#define WDT_RESET_STATUS_MSK CRG_TOP_RESET_STAT_REG_M33_WDOG_STAT_Msk
#else
#define WDT_RESET_STATUS_MSK CRG_TOP_RESET_STAT_REG_WDOGRESET_STAT_Msk
#endif
#define WDT_TIMEOUT_COUNTS (200U)
#define WDT_MAX_COUNTER (WDT_TIMEOUT_COUNTS - 1U)
#define WDT_START_WINDOW_75 ((WDT_MAX_COUNTER * 3) / 4)
/* Example callback called when a watchdog NMI occurs. */
void wdog_w_callback (wdt_callback_args_t * p_args)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Determine the source of the NMI. */
err = R_WDOG_W_StatusGet(&g_wdt0_ctrl, &status);
assert(FSP_SUCCESS == err);
#ifdef NDEBUG
(void) err;
#endif
/* (Optional) Log source of NMI and any other debug information. */
#if 0
/* User can dump those debug information. */
uint32_t * exception_args = (uint32_t *) p_args->p_context;
printf("\n");
printf("exception_args(Stack pointer)=0x%08x\n", (unsigned int) exception_args);
printf("exception_args[0](R0 )=0x%08x\n", (unsigned int) exception_args[0]); // R0
printf("exception_args[1](R1 )=0x%08x\n", (unsigned int) exception_args[1]); // R1
printf("exception_args[2](R2 )=0x%08x\n", (unsigned int) exception_args[2]); // R2
printf("exception_args[3](R3 )=0x%08x\n", (unsigned int) exception_args[3]); // R3
printf("exception_args[4](R12)=0x%08x\n", (unsigned int) exception_args[4]); // R12
printf("exception_args[5](LR )=0x%08x\n", (unsigned int) exception_args[5]); // LR
printf("exception_args[6](PC )=0x%08x\n", (unsigned int) exception_args[6]); // PC
printf("exception_args[7](PSR)=0x%08x\n", (unsigned int) exception_args[7]); // PSR
#endif
/* Call R_WDOG_W_Refresh() to continue using the watchdog after an error. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
/* (Optional) Issue a software reset to reset the MCU. */
__NVIC_SystemReset();
}
static bool r_wdog_w_test_is_wdt_reset (uint32_t status)
{
/* The watchdog reset status bit is set after a watchdog timeout, or after a power-on-reset.
* We are interested only in the first case. */
status &= WDT_RESET_STATUS_MSK | CRG_TOP_RESET_STAT_REG_PORESET_STAT_Msk;
return WDT_RESET_STATUS_MSK == status;
}
void wdog_w_advanced_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* (Optional) Check if the WDOGRESET_STAT flag is set to know if the system is
* recovering from a watchdog reset. */
uint32_t reset_status = CRG_TOP->RESET_STAT_REG;
if (r_wdog_w_test_is_wdt_reset(reset_status))
{
/* Clear the reset status. */
CRG_TOP->RESET_STAT_REG = 0;
}
/* Open the WDOG_W module. */
err = R_WDOG_W_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
#ifdef NDEBUG
(void) err;
#endif
/* Initialize other application code. */
/* Call R_WDOG_W_Refresh() to start the watchdog. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
while (true)
{
/* Application work here. */
/* (Optional) If there is a chance the application takes less time than
* the start window, verify the watchdog counter is past the start window
* before refreshing the watchdog. */
uint32_t wdt_counter = 0U;
do
{
/* Read the current watchdog counter value. */
err = R_WDOG_W_CounterGet(&g_wdt0_ctrl, &wdt_counter);
assert(FSP_SUCCESS == err);
/* (Optional) Application can freeze/unfreeze the WDT by call
* R_WDOG_W_Freeze(). */
/* Freeze */
err = R_WDOG_W_Freeze(&g_wdt0_ctrl, true);
assert(FSP_SUCCESS == err);
/* Unfreeze */
err = R_WDOG_W_Freeze(&g_wdt0_ctrl, false);
assert(FSP_SUCCESS == err);
/* (Optional) Application can change the timeout period by call
* R_WDOG_W_TimeoutSet(). */
err = R_WDOG_W_TimeoutSet(&g_wdt0_ctrl, 1024);
assert(FSP_SUCCESS == err);
} while (wdt_counter >= WDT_START_WINDOW_75);
/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
}
}

Data Structures

struct  wdog_w_instance_ctrl_t
 
struct  wdog_w_extended_cfg_t
 

Enumerations

enum  wdog_w_clk_src_t
 

Data Structure Documentation

◆ wdog_w_instance_ctrl_t

struct wdog_w_instance_ctrl_t

WDOG_W private control block. DO NOT MODIFY. Initialization occurs when wdt_api_t::open is called.

◆ wdog_w_extended_cfg_t

struct wdog_w_extended_cfg_t

WATCHDOG extended Configuration

Data Fields
wdog_w_clk_src_t wdt_clk_src CLock source setting for WDT.

Enumeration Type Documentation

◆ wdog_w_clk_src_t

WDOG_W clock source options.

Enumerator
WDOG_W_CLK_SRC_RCLP 

Use RCLP as clock source.

WDOG_W_CLK_SRC_RCX 

Use RCX as clock source.

Function Documentation

◆ R_WDOG_W_Open()

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

Configure the watchdog timer. Implements wdt_api_t::open.

This function should only be called once. Subsequent calls will have no effect.

Example:

/* Initializes the watchdog. */
err = R_WDOG_W_Open(&g_wdt0_ctrl, &g_wdt0_cfg);
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.
FSP_ERR_INVALID_STATEThe security state of the NMI and the module do not match.

◆ R_WDOG_W_TimeoutGet()

fsp_err_t R_WDOG_W_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_WDOG_W_TimeoutSet()

fsp_err_t R_WDOG_W_TimeoutSet ( wdt_ctrl_t p_ctrl,
uint32_t  timeout 
)

Set timeout value.

Return values
FSP_SUCCESSWDT timeout value is set successfully.
FSP_ERR_ASSERTIONNull Pointer.
FSP_ERR_NOT_OPENInstance control block is not initialized.
FSP_ERR_INVALID_SIZEWDT timeout value is out of range.

◆ R_WDOG_W_Refresh()

fsp_err_t R_WDOG_W_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.

Example:

/* Refresh before the counter underflows to prevent reset or NMI. */
err = R_WDOG_W_Refresh(&g_wdt0_ctrl);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSWDT successfully refreshed.
FSP_ERR_ASSERTIONp_ctrl is NULL.
FSP_ERR_NOT_OPENInstance control block is not initialized.
FSP_ERR_IN_USEThe watchdog is busy writing and the count value cannot be refreshed.
Note
When windowed mode is used: a) if the refresh is attempted before the high threshold is reached, nothing happens; b) if the refresh is attempted after the low threshold is reached, a reset is triggered.
This function reloads the watchdog timer counter value with the timeout period configured by Open.

◆ R_WDOG_W_StatusGet()

fsp_err_t R_WDOG_W_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.

Example:

/* (Optional) Determine the source of the NMI. */
err = R_WDOG_W_StatusGet(&g_wdt0_ctrl, &status);
assert(FSP_SUCCESS == err);
#ifdef NDEBUG
(void) err;
#endif
Return values
FSP_SUCCESSWDT status successfully read.
FSP_ERR_ASSERTIONNull pointer as a parameter.
FSP_ERR_NOT_OPENInstance control block is not initialized.
FSP_ERR_UNSUPPORTEDThis function is only valid if the watchdog generates an NMI when an error occurs.
Note
When the WDT is configured to output a reset on underflow or refresh error reading the status 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 NMI is enabled.

◆ R_WDOG_W_StatusClear()

fsp_err_t R_WDOG_W_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_ERR_UNSUPPORTEDThis function is not supported.

◆ R_WDOG_W_CounterGet()

fsp_err_t R_WDOG_W_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.

Example:

/* Read the current watchdog counter value. */
err = R_WDOG_W_CounterGet(&g_wdt0_ctrl, &wdt_counter);
assert(FSP_SUCCESS == err);
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.
FSP_ERR_IN_USEThe watchdog is busy writing and the count value cannot be read.

◆ R_WDOG_W_CallbackSet()

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

◆ R_WDOG_W_Freeze()

fsp_err_t R_WDOG_W_Freeze ( wdt_ctrl_t *const  p_ctrl,
bool  freeze 
)

Freeze/unfreeze the watchdog timer.

Example:

/* Freeze */
err = R_WDOG_W_Freeze(&g_wdt0_ctrl, true);
assert(FSP_SUCCESS == err);
/* Unfreeze */
err = R_WDOG_W_Freeze(&g_wdt0_ctrl, false);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSFreeze watchdog timer successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_NOT_ENABLEDFreezing watchdog timer is not allowed.
Note
The watchdog timer can only be frozen in the following configurations. Otherwise, this function has no effect.
  • NMI Support: Enabled
  • Reset Control: NMI Generated