1.6.21 Real-Time Counter (RTC)
The RTC module is a 32-bit counter, with a 10-bit programmable prescaler. Typically, the RTC clock is run continuously, including in the device's low-power sleep modes, to track the current time and date information. The RTC can be used as a source to wake up the system at a scheduled time or periodically using the alarm functions.The RTC peripheral can be configured to operate in the following three modes:
-
16-bit Counter mode (or Timer mode)
-
32-bit Counter mode (or Timer mode)
-
Calendar mode
The RTC peripheral in 16-bit or 32-bit counter mode allows for an easy integration of an asynchronous counter into a user application, which is capable of operating while the device is in sleep mode.The RTC peripheral in calendar mode allows for an easy integration of a real time clock and calendar into a user application to track the passing of time and/or perform scheduled tasks.The RTC timer has 10-bit programmable prescaler, it can generate periodic events on the upper eight bits of the RTC prescaler. Table below shows the periodic event frequencies for each prescaler bit using 1 KHz clock.
| Bit position | Periodic Event | |:------------ |: --------------| | 7 | 1 Hz | | 6 | 2 Hz | | 5 | 4 Hz | | 14 | 8 Hz | | 3 | 16 Hz | | 2 | 32 Hz | | 1 | 64 Hz | | 0 | 128 Hz |
32-bit timer mode(Mode 0)
The RTC counter in 32-bit timer mode will increment until it reaches the top value of 0xFFFFFFFF, and then wrap to 0x00000000 and sets the overflow interrupt. The counter value is continuously compared with the 32-bit Compare register (COMP0), and sets the compare match interrupt when compare match occurs. If the Clear on compare match is selected, the counter is cleared when a compare match occurs. Both compare match and overflow interrupt is set on compare match. The Clear on compare match feature allow the RTC to generate periodic interrupts or events with longer periods than the prescaler events.
Using The Library
The peripheral library provides polling and callback methods to indicate compare match or timer overflow.
-
With polling, the application will need to continuously poll to check if the compare match has occurred or timer has overflowed
-
With callback, the registered callback function will be called when the compare match occurs or timer overflows (Application do not have to poll continuously)
Callback method
This example demonstrates how to use RTC to generate periodic callback using clear on compare feature.
/* This function is called after period expires */ void Timeout_Handler(RTC_TIMER32_INT_MASK intCause, uintptr_t context) { if((intCause & RTC_TIMER32_INT_MASK_CMP0) == RTC_TIMER32_INT_MASK_CMP0) { LED_Toggle(); } } int main(void) { /* Initialize all modules */ SYS_Initialize ( NULL ); RTC_Timer32CallbackRegister(Timeout_Handler,0); RTC_Timer32InterruptEnable(RTC_TIMER32_INT_MASK_CMP0); RTC_Timer32Start(); }
Library Interface
Real-Time Counter peripheral library provides the following interfaces:
Functions
Name | Description |
---|---|
RTC_Initialize | Initialize given instance of the RTC peripheral |
RTC_Timer32CompareHasMatched | Check for 32-bit Timer Compare match |
RTC_Timer32Compare0HasMatched | Check for 32-bit Timer Compare match |
RTC_Timer32Compare1HasMatched | Check for 32-bit Timer Compare match |
RTC_Timer32CounterHasOverflowed | Check if the 32-bit counter overflow |
RTC_Timer32Start | Starts the 32-bit timer |
RTC_Timer32Stop | Stops the 32-bit timer from counting |
RTC_Timer32CounterSet | Set the 32-bit Timer Counter Value |
RTC_Timer32CompareSet | Set the 32-bit timer period value |
RTC_Timer32Compare0Set | Set the 32-bit timer period value |
RTC_Timer32Compare1Set | Set the 32-bit timer period value |
RTC_Timer32PeriodGet | Get 32-bit timer period Value |
RTC_Timer32CounterGet | Get the current 32-bit counter value |
RTC_Timer32FrequencyGet | Returns the frequency at which the 32-bit timer counter is operating |
RTC_Timer32InterruptEnable | Enable selected RTC interrupt |
RTC_Timer32InterruptDisable | Disable selected RTC interrupt |
RTC_Timer32CallbackRegister | Register the callback function to be called when an 32-bit Timer Interrupt occurs |
RTC_Timer32TimeStampGet | Get the Time stamp of Tamper Detection |
RTC_PeriodicIntervalHasCompleted | Check if the configured periodic interval has expired |
Data types and constants
Name | Type | Description |
---|---|---|
RTC_TIMER32_INT_MASK | Enum | Possible RTC 32-bit Timer Counter Mode Events |
RTC_TIMER32_CALLBACK | Typedef | Defines the data type and function signature of the RTC 32-bit Timer Counter callback function |
RTC_PERIODIC_INT_MASK | Enum | Possible Periodic Interrupt Mask |
16-bit timer mode(Mode 1)
The RTC counter in 16-bit timer mode will increment until it matches with 16-bit period value, and then wrap to 0x0000. This sets the the overflow interrupt. The counter value is continuously compared with two 32-bit Compare registers (Compare 0, Compare 1), and sets the respective compare match interrupt when compare match occurs. The RTC timer has 10-bit programmable prescaler, it can generate periodic events on the upper eight bits of the RTC prescaler. The resulting periodic frequency is from 1Hz to 128 Hz depending on the selected prescaler bit position to generate periodic interrupt.
Using The Library
The peripheral library provides polling and callback methods to indicate compare match or period expiry.
-
With polling, the application will need to continuously poll to check if the compare match has occurred or counter has overflowed.
-
With callback, the registered callback function will be called when the period match or compare match occurs(his means the application do not have to poll continuously)
Callback method This example demonstrates how to use RTC to generate periodic callback.
/* This function is called after period expires */ void RTC_Callback(RTC_TIMER16_INT_MASK interruptCause, uintptr_t context) { if(interruptCause & RTC_TIMER16_INT_MASK_PERIOD_MATCH) { // The period has matched. LED_Toggle(); } } int main(void) { /* Register callback function for RTC */ RTC_Timer16CallbackRegister(RTC_Callback, (uintptr_t)NULL); /* Enable Period Interrupt */ RTC_Timer16InterruptEnable( RTC_TIMER16_INT_MASK_PERIOD_MATCH); /* Start RTC Timer */ RTC_Timer16Start(); }
Functions
Name | Description |
---|---|
RTC_Initialize | Initialize given instance of the RTC peripheral |
RTC_FrequencyCorrect | Calibrate for too-slow or too-fast oscillator |
RTC_Timer16CounterHasOverflowed | Checks if the 16-bit counter has overflowed |
RTC_Timer16Compare0HasMatched | Returns true if the 16-bit Timer Compare 0 value has matched the counter |
RTC_Timer16Compare1HasMatched | Returns true if the 16-bit Timer Compare 1 value has matched the counter |
RTC_Timer16Compare2HasMatched | Returns true if the 16-bit Timer Compare 2 value has matched the counter |
RTC_Timer16Compare3HasMatched | Returns true if the 16-bit Timer Compare 3 value has matched the counter |
RTC_Timer16Start | Starts the 16-bit timer |
RTC_Timer16Stop | Stops the 16-bit timer from counting |
RTC_Timer16CounterSet | Set the 16-bit Timer Counter Value |
RTC_Timer16PeriodSet | Set the 16-bit timer period value |
RTC_Timer16PeriodGet | Get 16-bit timer period Value |
RTC_Timer16CounterGet | Get the current 16-bit counter value |
RTC_Timer16FrequencyGet | Returns the frequency at which the 16-bit timer counter is operating |
RTC_Timer16InterruptEnable | Enable Selected RTC interrupt |
RTC_Timer16InterruptDisable | Disable Selected RTC Interrupt |
RTC_Timer16Compare0Set | Set the 16-Bit Counter Compare 0 Value |
RTC_Timer16Compare1Set | Set the 16-Bit Counter Compare 1 Value |
RTC_Timer16Compare2Set | Set the 16-Bit Counter Compare 2 Value |
RTC_Timer16Compare3Set | Set the 16-Bit Counter Compare 3 Value |
RTC_Timer16TimeStampGet | Get the Time stamp of Tamper Detection |
RTC_Timer16CallbackRegister | Register the callback function to be called when an 16-bit Timer Interrupt occurs |
RTC_PeriodicIntervalHasCompleted | Check if the configured periodic interval has expired |
Data types and constants
Name | Type | Description |
---|---|---|
RTC_TIMER16_INT_MASK | Enum | Possible RTC 16-bit Timer Counter Mode Events |
RTC_TIMER16_CALLBACK | Typedef | Defines the data type and function signature of the RTC 16-bit Timer Counter callback function |
Clock and Calendar (Mode 2)
The RTC provides a full binary-coded decimal (BCD) clock that includes century (19/20), year (with leap years), month, day,hours, minutes, and seconds. The RTC can operate in 24-hour mode or in 12-hour mode with an AM/PM indicator. The RTC continues to run in the device's low-power sleep modes, to track the current time and date information. The RTC can be used as a source to wake up the system at a scheduled time or periodically using the alarm functions.
Using The Library
The RTC keeps track of the current time and generates an alarm at the desired time. The RTC Alarm has six programmable fields: year, month, date, hours, minutes, and seconds. The alarm mask allows following options to generate alarm.
-
Alarm mask to compare seconds field (SS) - Generates alarm once per minute
-
Alarm mask to compare minutes and seconds field (MMSS) - Generates alarm once per hour
-
Alarm mask to compare hours, minutes and seconds field (HHMMSS) - Generates alarm once per day
-
Alarm mask to compare date, hours, minutes, and seconds field (DDHHMMSS) - Generates alarm once per month
-
Alarm mask to compare month, date, hours, minutes and seconds (MMDDHHMMSS) - Generates alarm once per year
-
Alarm mask to compare year, month, date, hours, minutes and seconds (YYMMDDHHMMSS) - Generates alarm on exact day and time
This example demonstrates how to set the RTC time, and alarm time to generate an alarm interrupt at the desired time of the day.
bool alarm_triggered; void RTC_Callback(RTC_CLOCK_INT_MASK int_cause , uintptr_t context) { if (int_cause & RTC_CLOCK_INT_MASK_ALARM) { alarm_triggered = true; LED_Toggle(); } } int main ( void ) { /* Initialize System Time and Alarm Time */ struct tm sys_time; struct tm alarm_time; /* Register Callback */ RTC_CallbackRegister(RTC_Callback, (uintptr_t) NULL); /* Set Time and date 15-01-2018 12:00:00 Monday */ sys_time.tm_hour = 12; /* hour [0,23] */ sys_time.tm_sec = 00; /* seconds [0,61] */ sys_time.tm_min = 00; /* minutes [0,59] */ sys_time.tm_mon = 0; /* month of year [0,11] */ sys_time.tm_year = 118; /* years since 1900 */ sys_time.tm_mday = 15; /* day of month [1,31] */ sys_time.tm_wday = 1; /* day of week [0,6] (Sunday = 0) */ /* tm_yday - day of year [0,365] */ /* tm_isdst - daylight savings flag */ RTC_TimeSet(&sys_time); /* Set Alarm Time and date. Generate alarm every day when Hour, Minute and Seconds match. 15-01-2018 12:00:20 Monday */ alarm_time.tm_hour = 12; alarm_time.tm_sec = 20; alarm_time.tm_min = 00; alarm_time.tm_mon = 0; alarm_time.tm_year = 118; alarm_time.tm_mday = 15; alarm_time.tm_wday = 1; RTC_RTCCAlarmSet(&alarm_time, RTC_ALARM_MASK_HHMMSS); while ( true ) { if(alarm_triggered == true) { printf("\n\rAlarm Triggered !!!!!!!!\n\r"); alarm_triggered = false; } } }
Functions
Name | Description |
---|---|
RTC_Initialize | Initialize given instance of the RTC peripheral |
RTC_RTCCTimeGet | Gets the current time and date |
RTC_RTCCTimeSet | Sets the Real Time Clock Calendar time and date |
RTC_RTCCAlarmSet | Set an alarm |
RTC_RTCCCallbackRegister | Register the callback function to be called when an RTCC Interrupt occurs |
RTC_BackupRegisterSet | Set the value for the selected Backup Register |
RTC_BackupRegisterGet | Get the value stored in the selected Backup Register |
RTC_TamperSourceGet | Get the Tamper source |
RTC_RTCCTimeStampGet | Get the Time stamp of Tamper Detection |
Data types and constants
Name | Type | Description |
---|---|---|
RTC_ALARM_MASK | Enum | Possible RTC Alarm Mask Settings |
RTC_CLOCK_EVENT | Enum | Possible RTC RTCC Mode Events |
BACKUP_REGISTER | Enum | Possible Backup Register Mask |
TAMPER_CHANNEL | Enum | Possible Tamper Channel Mask |
RTC_CLOCK_INT_MASK | Enum | Possible RTC Clock Mode Events |
RTC_CALLBACK | Typedef | Defines the data type and function signature of the Real Time Clock Calendar callback function |