RA Flexible Software Package Documentation  Release v5.6.0

 
Realtime Clock (r_rtc)

Functions

fsp_err_t R_RTC_Open (rtc_ctrl_t *const p_ctrl, rtc_cfg_t const *const p_cfg)
 
fsp_err_t R_RTC_Close (rtc_ctrl_t *const p_ctrl)
 
fsp_err_t R_RTC_ClockSourceSet (rtc_ctrl_t *const p_ctrl)
 
fsp_err_t R_RTC_CalendarTimeSet (rtc_ctrl_t *const p_ctrl, rtc_time_t *const p_time)
 
fsp_err_t R_RTC_CalendarTimeGet (rtc_ctrl_t *const p_ctrl, rtc_time_t *const p_time)
 
fsp_err_t R_RTC_CalendarAlarmSet (rtc_ctrl_t *const p_ctrl, rtc_alarm_time_t *const p_alarm)
 
fsp_err_t R_RTC_CalendarAlarmGet (rtc_ctrl_t *const p_ctrl, rtc_alarm_time_t *const p_alarm)
 
fsp_err_t R_RTC_PeriodicIrqRateSet (rtc_ctrl_t *const p_ctrl, rtc_periodic_irq_select_t const rate)
 
fsp_err_t R_RTC_InfoGet (rtc_ctrl_t *const p_ctrl, rtc_info_t *const p_rtc_info)
 
fsp_err_t R_RTC_ErrorAdjustmentSet (rtc_ctrl_t *const p_ctrl, rtc_error_adjustment_cfg_t const *const err_adj_cfg)
 
fsp_err_t R_RTC_CallbackSet (rtc_ctrl_t *const p_ctrl, void(*p_callback)(rtc_callback_args_t *), void const *const p_context, rtc_callback_args_t *const p_callback_memory)
 
fsp_err_t R_RTC_TimeCaptureSet (rtc_ctrl_t *const p_ctrl, rtc_time_capture_t *const p_time_capture)
 
fsp_err_t R_RTC_TimeCaptureGet (rtc_ctrl_t *const p_ctrl, rtc_time_capture_t *const p_time_capture)
 

Detailed Description

Driver for the RTC peripheral on RA MCUs. This module implements the RTC Interface.

Overview

The RTC HAL module configures the RTC module and controls clock, calendar and alarm functions. A callback can be used to respond to the alarm and periodic interrupt.

Features

The RTC HAL module supports three different interrupt types:

Date and Time validation

"Parameter Checking" needs to be enabled if date and time validation is required for calendarTimeSet and calendarAlarmSet APIs. If "Parameter Checking" is enabled, the 'day of the week' field is automatically calculated and updated by the driver for the provided date. When using the calendarAlarmSet API, only the fields which have their corresponding match flag set are written to the registers. Other register fields are reset to default value.

Sub-Clock error adjustment (Time Error Adjustment Function)

The time error adjustment function is used to correct errors, running fast or slow, in the time caused by variation in the precision of oscillation by the sub-clock oscillator. Because 32,768 cycles of the sub-clock oscillator constitute 1 second of operation when the sub-clock oscillator is selected, the clock runs fast if the sub-clock frequency is high and slow if the sub-clock frequency is low. The time error adjustment functions include:

The error adjustment is reset every time RTC is reconfigured or time is set.

Note
RTC driver configurations do not do error adjustment internally while initiliazing the driver. Application must make calls to the error adjustment api's for desired adjustment. See section 26.3.8 "Time Error Adjustment Function" of the RA6M3 manual R01UH0886EJ0100) for more details on this feature

Configuration

Build Time Configurations for r_rtc

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

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Set Source Clock in Open
  • Enabled
  • Disabled
Enabled If enabled RTC source clock is initialized in R_RTC_Open. If disabled, R_RTC_ClockSourceSet must be called to set the RTC source clock. Disable if user wants to control the setting of RTC source clock after warm start.

Configurations for Timers > Realtime Clock (r_rtc)

This module can be added to the Stacks tab via New Stack > Timers > Realtime Clock (r_rtc). 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_rtc0 Module name.
Clock SourceMCU Specific OptionsSelect the RTC clock source.
Frequency Comparision Value (LOCO)Value must be a positive integer between 7 and 511255 Frequency comparison value when using LOCO
Automatic Adjustment Mode
  • Enabled
  • Disabled
Enabled Enable/ Disable the Error Adjustment mode
Automatic Adjustment Period
  • 10 Seconds
  • 1 Minute
  • NONE
10 Seconds Select the Error Adjustment Period for Automatic Adjustment
Adjustment Type (Plus-Minus)
  • NONE
  • Addition
  • Subtraction
NONE Select the Error Adjustment type
Error Adjustment ValueValue must be a positive integer less than equal to 630 Specify the Adjustment Value (the number of sub-clock cycles) from the prescaler
CallbackName must be a valid C symbolNULL A user callback function can be provided. If this callback function is provided, it will be called from the interrupt service routine (ISR).
Alarm Interrupt PriorityMCU Specific OptionsSelect the alarm interrupt priority.
Period Interrupt PriorityMCU Specific OptionsSelect the period interrupt priority.
Carry Interrupt PriorityMCU Specific OptionsSelect the carry interrupt priority.

Note
See 23.2.20 Frequency Register (RFRH/RFRL) of the RA6M3 manual R01UH0886EJ0100) for more details

Interrupt Configuration

To activate interrupts for the RTC module, the desired interrupts must be enabled, The underlying implementation will be expected to handle any interrupts it can support and notify higher layers via callback.

Clock Configuration

The RTC HAL module can use the following clock sources:

Users have to select the right source for their application. LOCO is the default during configuration when it is available.

Pin Configuration

This module does not use I/O pins.

Usage Notes

System Initialization

Warning
The subclock can take seconds to stabilize. The RA startup code does not wait for subclock stabilization unless the subclock is the main clock source. When running AGT or RTC off the subclock, the application must ensure the subclock is stable before starting operation.

Limitations

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

VRTC-Domain

VRTC-domain provides power supply to SOSC and RTC on devices with IRTC:

Time Capture

The RTC is capable of storing the month, day, hour, minute and second when detecting an edge of a signal on a time capture event input pin. On RA parts up to three capture channels can be configured.

Examples

RTC Basic Example

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

/* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
rtc_time_t set_time =
{
.tm_sec = 10,
.tm_min = 11,
.tm_hour = 12,
.tm_mday = 6,
.tm_wday = 3,
.tm_mon = 11,
.tm_year = YEARS_SINCE_1900,
};
rtc_time_t get_time;
void rtc_example ()
{
fsp_err_t err = FSP_SUCCESS;
/* Open the RTC module */
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
/* Set the calendar time */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
/* Get the calendar time */
R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);
}

RTC Clock Source Set Example

This is an example of how to handle call to set the RTC clock.

void rtc_clock_source_set_example ()
{
fsp_err_t err = FSP_SUCCESS;
/* Open the RTC module*/
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* This condition can differ based on use case. */
if (R_SYSTEM->RSTSR0 == 1)
{
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
}
/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
/* Set the periodic interrupt rate to 1 second */
/* Wait for the periodic interrupt */
while (1)
{
/* Wait for interrupt */
}
}

RTC Periodic interrupt example

This is an example of periodic interrupt in RTC.

void rtc_periodic_irq_example ()
{
fsp_err_t err = FSP_SUCCESS;
/* Open the RTC module*/
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
/* Set the periodic interrupt rate to 1 second */
/* Wait for the periodic interrupt */
while (1)
{
/* Wait for interrupt */
}
}

RTC Alarm interrupt example

This is an example of alarm interrupt in RTC.

void rtc_alarm_irq_example ()
{
fsp_err_t err = FSP_SUCCESS;
/*Open the RTC module*/
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time1.time);
R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_time1);
/* Wait for the Alarm interrupt */
while (1)
{
/* Wait for interrupt */
}
}

RTC Error Adjustment example

This is an example of modifying error adjustment in RTC.

void rtc_erroradj_example ()
{
fsp_err_t err = FSP_SUCCESS;
/*Open the RTC module*/
R_RTC_Open(&g_rtc0_ctrl, &g_rtc1_cfg);
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time1.time);
/* Modify Error Adjustment after RTC is running */
err = R_RTC_ErrorAdjustmentSet(&g_rtc0_ctrl, &err_cfg2);
assert(FSP_SUCCESS == err);
}

RTC with VRTC-domain example

This is an example of calling RTC API on devices with VRTC-domain.

#if BSP_FEATURE_RTC_IS_IRTC
void rtc_vrtc_domain_checking_example ()
{
fsp_err_t err = FSP_SUCCESS;
static uint32_t rtc_opened = 0;
while (1)
{
err = R_BSP_SubclockStatusGet();
if (FSP_SUCCESS != err)
{
/* RTC API should not be called. */
/* Try to initialize SOSC. */
err = R_BSP_SubclockInitialize();
if (FSP_SUCCESS == err)
{
/* Delay for sub-clock oscillation stabilization time */
R_BSP_SoftwareDelay(BSP_CLOCK_CFG_SUBCLOCK_STABILIZATION_MS, BSP_DELAY_UNITS_MILLISECONDS);
}
}
else
{
/* VRTC-domain reset will not initialize RTC registers. */
if (0 == rtc_opened)
{
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
rtc_opened = (FSP_SUCCESS == err) ? 1 : 0;
}
/* Other RTC API can be called if needed. */
}
}
}
#endif

RTC Time Capture example

This is an example of calling Time Capture API on RTC with Independent Power Supply.

void rtc_irtc_time_capture_example ()
{
fsp_err_t err = FSP_SUCCESS;
/* Open the RTC module*/
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc1_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Configure a GPIO pin to trigger the Time Capture pin */
R_BSP_PinCfg(RTC_TIME_CAPTURE_TRIGGER_CHANNEL_0, (uint32_t) BSP_IO_DIRECTION_OUTPUT);
/* Preset trigger pin */
R_BSP_PinWrite(RTC_TIME_CAPTURE_TRIGGER_CHANNEL_0, BSP_IO_LEVEL_LOW);
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
/* Set time capture configuration for the provided channel when RTC is running */
err = R_RTC_TimeCaptureSet(&g_rtc0_ctrl, &g_rtc_time_capture);
assert(FSP_SUCCESS == err);
/* Update trigger pin */
R_BSP_PinWrite(RTC_TIME_CAPTURE_TRIGGER_CHANNEL_0, BSP_IO_LEVEL_HIGH);
/* Get the time capture for the provided channel */
err = R_RTC_TimeCaptureGet(&g_rtc0_ctrl, &g_rtc_time_capture);
assert(FSP_SUCCESS == err);
}

Data Structures

struct  rtc_extended_cfg_t
 
struct  rtc_instance_ctrl_t
 

Data Structure Documentation

◆ rtc_extended_cfg_t

struct rtc_extended_cfg_t

RTC extend configuration

Data Fields
uint8_t alarm1_ipl Alarm 1 interrupt priority.
IRQn_Type alarm1_irq Alarm 1 interrupt vector.

◆ rtc_instance_ctrl_t

struct rtc_instance_ctrl_t

Channel control block. DO NOT INITIALIZE. Initialization occurs when rtc_api_t::open is called

Data Fields

uint32_t open
 Whether or not driver is open.
 
const rtc_cfg_tp_cfg
 Pointer to initial configurations.
 
volatile bool carry_isr_triggered
 Was the carry isr triggered.
 

Function Documentation

◆ R_RTC_Open()

fsp_err_t R_RTC_Open ( rtc_ctrl_t *const  p_ctrl,
rtc_cfg_t const *const  p_cfg 
)

Opens and configures the RTC driver module. Implements rtc_api_t::open. Configuration includes clock source, and interrupt callback function.

Example:

/* Open the RTC module */
err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
Return values
FSP_SUCCESSInitialization was successful and RTC has started.
FSP_ERR_ASSERTIONInvalid p_ctrl or p_cfg pointer.
FSP_ERR_ALREADY_OPENModule is already open.
FSP_ERR_INVALID_ARGUMENTInvalid time parameter field.

◆ R_RTC_Close()

fsp_err_t R_RTC_Close ( rtc_ctrl_t *const  p_ctrl)

Close the RTC driver. Implements rtc_api_t::close

Return values
FSP_SUCCESSDe-Initialization was successful and RTC driver closed.
FSP_ERR_ASSERTIONInvalid p_ctrl.
FSP_ERR_NOT_OPENDriver not open already for close.

◆ R_RTC_ClockSourceSet()

fsp_err_t R_RTC_ClockSourceSet ( rtc_ctrl_t *const  p_ctrl)

Sets the RTC clock source. Implements rtc_api_t::clockSourceSet.

Example:

/* This condition can differ based on use case. */
if (R_SYSTEM->RSTSR0 == 1)
{
/* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
R_RTC_ClockSourceSet(&g_rtc0_ctrl);
}
Return values
FSP_SUCCESSInitialization was successful and RTC has started.
FSP_ERR_ASSERTIONInvalid p_ctrl or p_cfg pointer.
FSP_ERR_NOT_OPENDriver is not opened.
FSP_ERR_INVALID_ARGUMENTInvalid clock source.

◆ R_RTC_CalendarTimeSet()

fsp_err_t R_RTC_CalendarTimeSet ( rtc_ctrl_t *const  p_ctrl,
rtc_time_t *const  p_time 
)

Set the calendar time.

Implements rtc_api_t::calendarTimeSet.

Return values
FSP_SUCCESSCalendar time set operation was successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_INVALID_ARGUMENTInvalid time parameter field.

◆ R_RTC_CalendarTimeGet()

fsp_err_t R_RTC_CalendarTimeGet ( rtc_ctrl_t *const  p_ctrl,
rtc_time_t *const  p_time 
)

Get the calendar time.

Warning
Do not call this function from a critical section or from an interrupt with higher priority than the carry interrupt, or the time returned may be inaccurate.

Implements rtc_api_t::calendarTimeGet

Return values
FSP_SUCCESSCalendar time get operation was successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_IRQ_BSP_DISABLEDUser IRQ parameter not valid

◆ R_RTC_CalendarAlarmSet()

fsp_err_t R_RTC_CalendarAlarmSet ( rtc_ctrl_t *const  p_ctrl,
rtc_alarm_time_t *const  p_alarm 
)

Set the calendar alarm time.

Implements rtc_api_t::calendarAlarmSet.

Precondition
The calendar counter must be running before the alarm can be set.
Return values
FSP_SUCCESSCalendar alarm time set operation was successful.
FSP_ERR_INVALID_ARGUMENTInvalid time parameter field.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_IRQ_BSP_DISABLEDUser IRQ parameter not valid

◆ R_RTC_CalendarAlarmGet()

fsp_err_t R_RTC_CalendarAlarmGet ( rtc_ctrl_t *const  p_ctrl,
rtc_alarm_time_t *const  p_alarm 
)

Get the calendar alarm time.

Implements rtc_api_t::calendarAlarmGet

Return values
FSP_SUCCESSCalendar alarm time get operation was successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.

◆ R_RTC_PeriodicIrqRateSet()

fsp_err_t R_RTC_PeriodicIrqRateSet ( rtc_ctrl_t *const  p_ctrl,
rtc_periodic_irq_select_t const  rate 
)

Set the periodic interrupt rate and enable periodic interrupt.

Implements rtc_api_t::periodicIrqRateSet

Note
To start the RTC R_RTC_CalendarTimeSet must be called at least once.

Example:

/* Set the periodic interrupt rate to 1 second */
Return values
FSP_SUCCESSThe periodic interrupt rate was successfully set.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_IRQ_BSP_DISABLEDUser IRQ parameter not valid

◆ R_RTC_InfoGet()

fsp_err_t R_RTC_InfoGet ( rtc_ctrl_t *const  p_ctrl,
rtc_info_t *const  p_rtc_info 
)

Set RTC clock source and running status information ad store it in provided pointer p_rtc_info

Implements rtc_api_t::infoGet

Return values
FSP_SUCCESSGet information Successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.

◆ R_RTC_ErrorAdjustmentSet()

fsp_err_t R_RTC_ErrorAdjustmentSet ( rtc_ctrl_t *const  p_ctrl,
rtc_error_adjustment_cfg_t const *const  err_adj_cfg 
)

This function sets time error adjustment

Implements rtc_api_t::errorAdjustmentSet

Return values
FSP_SUCCESSTime error adjustment successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open for operation.
FSP_ERR_UNSUPPORTEDThe clock source is not sub-clock.
FSP_ERR_INVALID_ARGUMENTInvalid error adjustment value.

◆ R_RTC_CallbackSet()

fsp_err_t R_RTC_CallbackSet ( rtc_ctrl_t *const  p_ctrl,
void(*)(rtc_callback_args_t *)  p_callback,
void const *const  p_context,
rtc_callback_args_t *const  p_callback_memory 
)

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

Return values
FSP_SUCCESSBaud rate was successfully changed.
FSP_ERR_ASSERTIONPointer to RTC control block is NULL or the RTC is not configured to use the internal clock.
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_RTC_TimeCaptureSet()

fsp_err_t R_RTC_TimeCaptureSet ( rtc_ctrl_t *const  p_ctrl,
rtc_time_capture_t *const  p_time_capture 
)

Set time capture configuration for the provided channel.

Implements rtc_api_t::timeCaptureSet

Note
Updating capture settings requires significant software delay. Timing considerations should be carefully considered when calling this function.
Return values
FSP_SUCCESSSetting for Time capture was successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_INVALID_CHANNELInvalid input channel set.
FSP_ERR_UNSUPPORTEDHardware not support this feature.

◆ R_RTC_TimeCaptureGet()

fsp_err_t R_RTC_TimeCaptureGet ( rtc_ctrl_t *const  p_ctrl,
rtc_time_capture_t *const  p_time_capture 
)

Get time capture value of the provided channel.

Implements rtc_api_t::timeCaptureGet

Return values
FSP_SUCCESSGet time capture successful.
FSP_ERR_ASSERTIONInvalid input argument.
FSP_ERR_NOT_OPENDriver not open already for operation.
FSP_ERR_INVALID_CHANNELInvalid input channel get.
FSP_ERR_INVALID_STATEInvalid operation state.
FSP_ERR_UNSUPPORTEDHardware not support this feature.