The MCUXpresso SDK provides a driver for the Real Time Clock (RTC) of MCUXpresso SDK devices.
Function groups
The RTC driver supports operating the module as a time counter.
Initialization and deinitialization
The function RTC_Init() initializes the RTC with specified configurations. The function RTC_GetDefaultConfig() gets the default configurations.
The function RTC_Deinit() disables the RTC timer and disables the module clock.
Set & Get Datetime
The function RTC_SetDatetime() sets the timer period in seconds. Users pass in the details in date & time format by using the below data structure.
typedef struct _rtc_datetime
{
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
The function RTC_GetDatetime() reads the current timer value in seconds, converts it to date & time format and stores it into a datetime structure passed in by the user.
Set & Get Alarm
The function RTC_SetAlarm() sets the alarm time period in seconds. Users pass in the details in date & time format by using the datetime data structure.
The function RTC_GetAlarm() reads the alarm time in seconds, converts it to date & time format and stores it into a datetime structure passed in by the user.
Start & Stop timer
The function RTC_StartTimer() starts the RTC time counter.
The function RTC_StopTimer() stops the RTC time counter.
Status
Provides functions to get and clear the RTC status.
Interrupt
Provides functions to enable/disable RTC interrupts and get current enabled interrupts.
RTC Oscillator
Some SoC's allow control of the RTC oscillator through the RTC module.
The function RTC_SetOscCapLoad() allows the user to modify the capacitor load configuration of the RTC oscillator.
Monotonic Counter
Some SoC's have a 64-bit Monotonic counter available in the RTC module.
The function RTC_SetMonotonicCounter() writes a 64-bit to the counter.
The function RTC_GetMonotonicCounter() reads the monotonic counter and returns the 64-bit counter value to the user.
The function RTC_IncrementMonotonicCounter() increments the Monotonic Counter by one.
Typical use case
RTC tick example
Example to set the RTC current time and trigger an alarm.
int main(void)
{
uint32_t sec;
uint32_t currSeconds;
BOARD_InitHardware();
BOARD_SetRtcClockSource();
PRINTF("RTC example: set up time to wake up an alarm\r\n");
EnableIRQ(RTC_IRQn);
while (1)
{
busyWait = true;
PRINTF(
"Current datetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.
year, date.
month, date.
day, date.
hour,
sec = 0;
PRINTF("Input the number of second to wait for alarm \r\n");
PRINTF("The second must be positive value\r\n");
while (sec < 1)
{
SCANF("%d", &sec);
}
currSeconds = RTC->TSR;
currSeconds += sec;
RTC->TAR = currSeconds;
PRINTF(
"Alarm will occur at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.
year, date.
month, date.
day,
while (busyWait)
{
}
PRINTF("\r\n Alarm occurs !!!! ");
}
}
|
static void | RTC_SetOscCapLoad (RTC_Type *base, uint32_t capLoad) |
| This function sets the specified capacitor configuration for the RTC oscillator. More...
|
|
static void | RTC_Reset (RTC_Type *base) |
| Performs a software reset on the RTC module. More...
|
|
uint16_t rtc_datetime_t::year |
uint8_t rtc_datetime_t::month |
uint8_t rtc_datetime_t::day |
uint8_t rtc_datetime_t::hour |
uint8_t rtc_datetime_t::minute |
uint8_t rtc_datetime_t::second |
This structure holds the configuration settings for the RTC peripheral. To initialize this structure to reasonable defaults, call the RTC_GetDefaultConfig() function and pass a pointer to your config structure instance.
The config struct can be made const so it resides in flash
Data Fields |
bool | wakeupSelect |
| true: Wakeup pin outputs the 32 KHz clock; false:Wakeup pin used to wakeup the chip
|
|
bool | updateMode |
| true: Registers can be written even when locked under certain conditions, false: No writes allowed when registers are locked
|
|
bool | supervisorAccess |
| true: Non-supervisor accesses are allowed; false: Non-supervisor accesses are not supported
|
|
uint32_t | compensationInterval |
| Compensation interval that is written to the CIR field in RTC TCR Register.
|
|
uint32_t | compensationTime |
| Compensation time that is written to the TCR field in RTC TCR Register.
|
|
Enumerator |
---|
kRTC_TimeInvalidInterruptEnable |
Time invalid interrupt.
|
kRTC_TimeOverflowInterruptEnable |
Time overflow interrupt.
|
kRTC_AlarmInterruptEnable |
Alarm interrupt.
|
kRTC_SecondsInterruptEnable |
Seconds interrupt.
|
Enumerator |
---|
kRTC_TimeInvalidFlag |
Time invalid flag.
|
kRTC_TimeOverflowFlag |
Time overflow flag.
|
kRTC_AlarmFlag |
Alarm flag.
|
Enumerator |
---|
kRTC_Capacitor_2p |
2 pF capacitor load
|
kRTC_Capacitor_4p |
4 pF capacitor load
|
kRTC_Capacitor_8p |
8 pF capacitor load
|
kRTC_Capacitor_16p |
16 pF capacitor load
|
void RTC_Init |
( |
RTC_Type * |
base, |
|
|
const rtc_config_t * |
config |
|
) |
| |
This function issues a software reset if the timer invalid flag is set.
- Note
- This API should be called at the beginning of the application using the RTC driver.
- Parameters
-
base | RTC peripheral base address |
config | Pointer to the user's RTC configuration structure. |
static void RTC_Deinit |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
The default values are as follows.
* config->wakeupSelect = false;
* config->updateMode = false;
* config->supervisorAccess = false;
* config->compensationInterval = 0;
* config->compensationTime = 0;
*
- Parameters
-
config | Pointer to the user's RTC configuration structure. |
status_t RTC_SetDatetime |
( |
RTC_Type * |
base, |
|
|
const rtc_datetime_t * |
datetime |
|
) |
| |
The RTC counter must be stopped prior to calling this function because writes to the RTC seconds register fail if the RTC counter is running.
- Parameters
-
base | RTC peripheral base address |
datetime | Pointer to the structure where the date and time details are stored. |
- Returns
- kStatus_Success: Success in setting the time and starting the RTC kStatus_InvalidArgument: Error because the datetime format is incorrect
- Parameters
-
base | RTC peripheral base address |
datetime | Pointer to the structure where the date and time details are stored. |
status_t RTC_SetAlarm |
( |
RTC_Type * |
base, |
|
|
const rtc_datetime_t * |
alarmTime |
|
) |
| |
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
-
base | RTC peripheral base address |
alarmTime | Pointer to the structure where the alarm time is stored. |
- Returns
- kStatus_Success: success in setting the RTC alarm kStatus_InvalidArgument: Error because the alarm datetime format is incorrect kStatus_Fail: Error because the alarm time has already passed
- Parameters
-
base | RTC peripheral base address |
datetime | Pointer to the structure where the alarm date and time details are stored. |
static void RTC_EnableInterrupts |
( |
RTC_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
mask | The interrupts to enable. This is a logical OR of members of the enumeration rtc_interrupt_enable_t |
static void RTC_DisableInterrupts |
( |
RTC_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
mask | The interrupts to enable. This is a logical OR of members of the enumeration rtc_interrupt_enable_t |
static uint32_t RTC_GetEnabledInterrupts |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
- Returns
- The enabled interrupts. This is the logical OR of members of the enumeration rtc_interrupt_enable_t
static uint32_t RTC_GetStatusFlags |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
- Returns
- The status flags. This is the logical OR of members of the enumeration rtc_status_flags_t
void RTC_ClearStatusFlags |
( |
RTC_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
- Parameters
-
base | RTC peripheral base address |
mask | The status flags to clear. This is a logical OR of members of the enumeration rtc_status_flags_t |
static void RTC_StartTimer |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
After calling this function, the timer counter increments once a second provided SR[TOF] or SR[TIF] are not set.
- Parameters
-
base | RTC peripheral base address |
static void RTC_StopTimer |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
RTC's seconds register can be written to only when the timer is stopped.
- Parameters
-
base | RTC peripheral base address |
static void RTC_SetOscCapLoad |
( |
RTC_Type * |
base, |
|
|
uint32_t |
capLoad |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | RTC peripheral base address |
capLoad | Oscillator loads to enable. This is a logical OR of members of the enumeration rtc_osc_cap_load_t |
static void RTC_Reset |
( |
RTC_Type * |
base | ) |
|
|
inlinestatic |
This resets all RTC registers except for the SWR bit and the RTC_WAR and RTC_RAR registers. The SWR bit is cleared by software explicitly clearing it.
- Parameters
-
base | RTC peripheral base address |