3.7.23 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 positionPeriodic Event
71 Hz
62 Hz
54 Hz
48 Hz
316 Hz
232 Hz
164 Hz
0128 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

NameDescription
RTC_InitializeInitialize given instance of the RTC peripheral
RTC_Timer32CompareHasMatchedCheck for 32-bit Timer Compare match
RTC_Timer32Compare0HasMatchedCheck for 32-bit Timer Compare match
RTC_Timer32Compare1HasMatchedCheck for 32-bit Timer Compare match
RTC_Timer32CounterHasOverflowedCheck if the 32-bit counter overflow
RTC_Timer32StartStarts the 32-bit timer
RTC_Timer32StopStops the 32-bit timer from counting
RTC_Timer32CounterSetSet the 32-bit Timer Counter Value
RTC_Timer32CompareSetSet the 32-bit timer period value
RTC_Timer32Compare0SetSet the 32-bit timer period value
RTC_Timer32Compare1SetSet the 32-bit timer period value
RTC_Timer32PeriodGetGet 32-bit timer period Value
RTC_Timer32CounterGetGet the current 32-bit counter value
RTC_Timer32FrequencyGetReturns the frequency at which the 32-bit timer counter is operating
RTC_Timer32InterruptEnableEnable selected RTC interrupt
RTC_Timer32InterruptDisableDisable selected RTC interrupt
RTC_Timer32CallbackRegisterRegister the callback function to be called when an 32-bit Timer Interrupt occurs
RTC_Timer32TimeStampGetGet the Time stamp of Tamper Detection
RTC_PeriodicIntervalHasCompletedCheck if the configured periodic interval has expired

Data types and constants

NameTypeDescription
RTC_TIMER32_INT_MASKEnumPossible RTC 32-bit Timer Counter Mode Events
RTC_TIMER32_CALLBACKTypedefDefines the data type and function signature of the RTC 32-bit Timer Counter callback function
RTC_PERIODIC_INT_MASKEnumPossible 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 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

NameDescription
RTC_InitializeInitialize given instance of the RTC peripheral
RTC_FrequencyCorrectCalibrate for too-slow or too-fast oscillator
RTC_Timer16CounterHasOverflowedChecks if the 16-bit counter has overflowed
RTC_Timer16Compare0HasMatchedReturns true if the 16-bit Timer Compare 0 value has matched the counter
RTC_Timer16Compare1HasMatchedReturns true if the 16-bit Timer Compare 1 value has matched the counter
RTC_Timer16Compare2HasMatchedReturns true if the 16-bit Timer Compare 2 value has matched the counter
RTC_Timer16Compare3HasMatchedReturns true if the 16-bit Timer Compare 3 value has matched the counter
RTC_Timer16StartStarts the 16-bit timer
RTC_Timer16StopStops the 16-bit timer from counting
RTC_Timer16CounterSetSet the 16-bit Timer Counter Value
RTC_Timer16PeriodSetSet the 16-bit timer period value
RTC_Timer16PeriodGetGet 16-bit timer period Value
RTC_Timer16CounterGetGet the current 16-bit counter value
RTC_Timer16FrequencyGetReturns the frequency at which the 16-bit timer counter is operating
RTC_Timer16InterruptEnableEnable Selected RTC interrupt
RTC_Timer16InterruptDisableDisable Selected RTC Interrupt
RTC_Timer16Compare0SetSet the 16-Bit Counter Compare 0 Value
RTC_Timer16Compare1SetSet the 16-Bit Counter Compare 1 Value
RTC_Timer16Compare2SetSet the 16-Bit Counter Compare 2 Value
RTC_Timer16Compare3SetSet the 16-Bit Counter Compare 3 Value
RTC_Timer16TimeStampGetGet the Time stamp of Tamper Detection
RTC_Timer16CallbackRegisterRegister the callback function to be called when an 16-bit Timer Interrupt occurs
RTC_PeriodicIntervalHasCompletedCheck if the configured periodic interval has expired

Data types and constants

NameTypeDescription
RTC_TIMER16_INT_MASKEnumPossible RTC 16-bit Timer Counter Mode Events
RTC_TIMER16_CALLBACKTypedefDefines 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

NameDescription
RTC_InitializeInitialize given instance of the RTC peripheral
RTC_RTCCTimeGetGets the current time and date
RTC_RTCCTimeSetSets the Real Time Clock Calendar time and date
RTC_RTCCAlarmSetSet an alarm
RTC_RTCCCallbackRegisterRegister the callback function to be called when an RTCC Interrupt occurs
RTC_BackupRegisterSetSet the value for the selected Backup Register
RTC_BackupRegisterGetGet the value stored in the selected Backup Register
RTC_TamperSourceGetGet the Tamper source
RTC_RTCCTimeStampGetGet the Time stamp of Tamper Detection

Data types and constants

NameTypeDescription
RTC_ALARM_MASKEnumPossible RTC Alarm Mask Settings
RTC_CLOCK_EVENTEnumPossible RTC RTCC Mode Events
BACKUP_REGISTEREnumPossible Backup Register Mask
TAMPER_CHANNELEnumPossible Tamper Channel Mask
RTC_CLOCK_INT_MASKEnumPossible RTC Clock Mode Events
RTC_CALLBACKTypedefDefines the data type and function signature of the Real Time Clock Calendar callback function