This section describes the programming interface of the RTC Peripheral Driver. The RTC Peripheral driver sets and gets the RTC clock, sets and reads the RTC alarm time, and receives notifications when an alarm is triggered.
RTC Peripheral Driver Initialization
To initialize, the user calls the RTC_DRV_Init() function with the RTC instance number. Most SoCs have only one RTC instance. Therefore, the instance number is zero. The driver initialization function un-gates the RTC module clock, initializes the RTC HAL layer driver, and enables the RTC interrupts.
This is an example how to create the rtc_init() function.
IRTCS Setting and reading the IRTC time
The RTC driver uses an instantiation of the rtc_datetime_t structure either to configure or read the date and time. Call the RTC_DRV_SetDatetime() function to configure the current date and time and call the RTC_DRV_GetDatetime() function to read the current date and time at a later time. This example shows how to use these functions.
datetimeToSet.
year = 2013;
datetimeToSet.
month = 10;
printf("Current datetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n",
datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second);
RTC Triggering an Alarm
Call the RTC_DRV_SetAlarm() function to set the alarm time and call the RTC_DRV_GetAlarm() function to read the configured alarm time. Set the current time using the steps mentioned earlier prior to using the call to set the alarm time. The user can enable the option to trigger an interrupt when an alarm occurs. This is done by either calling the RTC_DRV_SetAlarmIntCmd() function or through an argument to the RTC_DRV_SetAlarm() function. This is an example to set an RTC alarm. This example causes an alarm interrupt to be triggered after 5 minutes.
datetimeToSet.
year = 2013;
datetimeToSet.
month = 10;
alrmTimeToSet.
year = datetimeToSet.
year;
alrmTimeToSet.
day = datetimeToSet.
day;
RTC Repeated alarm
To request a repeated alarm, a configuration structure is available to configure the repeat alarm information. These are the structure details:
typedef struct RtcRepeatAlarmState
{
Call the RTC_DRV_InitRepeatAlarm() function and provide the repeat alarm configuration structure to the RTC driver. The user should not free this structure because the RTC driver stores the pointer to this structure to configure the future alarm times.
This is an example to set a RTC repeat alarm. The alarm interrupt is triggered after 5 minutes and every minute after that.
RTC Enable and Disable Alarm Interrupts
Call the RTC_DRV_SetAlarmIntCmd() function to enable or disable the RTC alarm interrupt. Call the RTC_DRV_GetAlarmIntCmd() function to get the state of the RTC alarm interrupt bit.
RTC Interrupt handler
The RTC driver provides an interrupt handler for the seconds and alarm interrupts. These handlers clear the status bits. When the repeated alarm is requested, the alarm interrupt handler uses the information provided in the rtc_repeat_alarm_state_t structure to schedule the next alarm.
To add more actions to the default handler, add calls to the functions inside the interrupt handlers RTC_IRQHandler() and RTC_Seconds_IRQHandler() functions.
|
RTC_Type *const | g_rtcBase [RTC_INSTANCE_COUNT] |
| Table of base addresses for RTC instances. More...
|
|
const IRQn_Type | g_rtcIrqId [RTC_INSTANCE_COUNT] |
| Table to save RTC Alarm IRQ numbers for RTC instances. More...
|
|
const IRQn_Type | g_rtcSecondsIrqId [RTC_INSTANCE_COUNT] |
| Table to save RTC Seconds IRQ numbers for RTC instances. More...
|
|
struct rtc_repeat_alarm_state_t |
Enables the RTC clock and interrupts if requested by the user.
- Parameters
-
instance | The RTC peripheral instance number. |
- Returns
- kStatusRtcSuccess means success; Otherwise means failed.
void RTC_DRV_Deinit |
( |
uint32_t |
instance | ) |
|
- Parameters
-
instance | The RTC peripheral instance number. |
bool RTC_DRV_IsCounterEnabled |
( |
uint32_t |
instance | ) |
|
The function checks the TCE bit in the RTC control register.
- Parameters
-
instance | The RTC peripheral instance number. |
- Returns
- true: The RTC counter is enabled
false: The RTC counter is disabled
bool RTC_DRV_SetDatetime |
( |
uint32_t |
instance, |
|
|
rtc_datetime_t * |
datetime |
|
) |
| |
The RTC counter is started after the time is set.
- Parameters
-
instance | The RTC peripheral instance number. |
datetime | [in] pointer to structure where the date and time details to set are stored. |
- Returns
- true: success in setting the time and starting the RTC
false: failure. An error occurs because the datetime format is incorrect.
void RTC_DRV_GetDatetime |
( |
uint32_t |
instance, |
|
|
rtc_datetime_t * |
datetime |
|
) |
| |
- Parameters
-
instance | The RTC peripheral instance number. |
datetime | [out] pointer to structure where the date and time details are stored. |
void RTC_DRV_SetSecsIntCmd |
( |
uint32_t |
instance, |
|
|
bool |
secondsEnable |
|
) |
| |
- Parameters
-
instance | The RTC peripheral instance number. |
secondsEnable | Takes true or false
true: indicates seconds interrupt should be enabled
false: indicates seconds interrupt should be disabled |
bool RTC_DRV_SetAlarm |
( |
uint32_t |
instance, |
|
|
rtc_datetime_t * |
alarmTime, |
|
|
bool |
enableAlarmInterrupt |
|
) |
| |
The function checks whether the specified alarm time is greater than the present time. If not, the function does not set the alarm and returns an error.
- Parameters
-
instance | The RTC peripheral instance number. |
alarmTime | [in] pointer to structure where the alarm time is stored. |
enableAlarmInterrupt | Takes true of false
true: indicates alarm interrupt should be enabled
false: indicates alarm interrupt should be disabled |
- Returns
- true: success in setting the RTC alarm
false: error in setting the RTC alarm. Error occurs because the alarm datetime format is incorrect.
- Parameters
-
instance | The RTC peripheral instance number. |
date | [out] pointer to structure where the alarm date and time details are stored. |
The RTC driver uses this user-provided structure to store the alarm state information.
- Parameters
-
instance | The RTC peripheral instance number. |
repeatAlarmState | Pointer to structure where the alarm state is stored |
- Parameters
-
instance | The RTC peripheral instance number. |
alarmTime | Pointer to structure where the alarm time is provided. |
alarmRepInterval | pointer to structure with the alarm repeat interval. |
- Returns
- true: success in setting the RTC alarm
false: error in setting the RTC alarm. Error occurs because the alarm datetime format is incorrect.
void RTC_DRV_DeinitRepeatAlarm |
( |
uint32_t |
instance | ) |
|
- Parameters
-
instance | The RTC peripheral instance number. |
void RTC_DRV_SetAlarmIntCmd |
( |
uint32_t |
instance, |
|
|
bool |
alarmEnable |
|
) |
| |
- Parameters
-
instance | The RTC peripheral instance number. |
alarmEnable | Takes true or false
true: indicates alarm interrupt should be enabled
false: indicates alarm interrupt should be disabled |
bool RTC_DRV_GetAlarmIntCmd |
( |
uint32_t |
instance | ) |
|
- Parameters
-
instance | The RTC peripheral instance number. |
- Returns
- true: indicates alarm interrupt is enabled
false: indicates alarm interrupt is disabled
bool RTC_DRV_IsAlarmPending |
( |
uint32_t |
instance | ) |
|
- Parameters
-
instance | The RTC peripheral instance number. |
- Returns
- returns alarm status, for example, returns whether the alarm triggered
true: indicates alarm has occurred
false: indicates alarm has not occurred
void RTC_DRV_SetTimeCompensation |
( |
uint32_t |
instance, |
|
|
uint32_t |
compensationInterval, |
|
|
uint32_t |
compensationTime |
|
) |
| |
- Parameters
-
instance | The RTC peripheral instance number. |
compensationInterval | User-specified compensation interval that is written to the CIR field in RTC Time Compensation Register (TCR) |
compensationTime | User-specified compensation time that is written to the TCR field in RTC Time Compensation Register (TCR) |
void RTC_DRV_GetTimeCompensation |
( |
uint32_t |
instance, |
|
|
uint32_t * |
compensationInterval, |
|
|
uint32_t * |
compensationTime |
|
) |
| |
- Parameters
-
instance | The RTC peripheral instance number. |
compensationInterval | User-specified pointer to store the compensation interval counter. This value is read from the CIC field in RTC Time Compensation Register (TCR) |
compensationTime | User-specified pointer to store the compensation time value. This value is read from the TCV field in RTC Time Compensation Register (TCR) |
void RTC_IRQHandler |
( |
void |
| ) |
|
Handles the RTC alarm interrupt and invokes any callback related to the RTC alarm.
void RTC_Seconds_IRQHandler |
( |
void |
| ) |
|
Handles the RTC seconds interrupt and invokes any callback related to the RTC second tick.
void RTC_DRV_AlarmIntAction |
( |
uint32_t |
instance | ) |
|
To receive alarms periodically, the RTC_TAR register is updated using the repeat interval.
- Parameters
-
instance | The RTC peripheral instance number. |
void RTC_DRV_SecsIntAction |
( |
uint32_t |
instance | ) |
|
Disables the time seconds interrupt (TSIE) bit.
- Parameters
-
instance | The RTC peripheral instance number. |
RTC_Type* const g_rtcBase[RTC_INSTANCE_COUNT] |
const IRQn_Type g_rtcIrqId[RTC_INSTANCE_COUNT] |
const IRQn_Type g_rtcSecondsIrqId[RTC_INSTANCE_COUNT] |