SAM L10/L11 Real Time Counter (RTC)

Last modified by Microchip on 2023/11/21 20:59

Overview

The RTC is a 32-bit counter with a 10-bit programmable prescaler that typically runs continuously, in any sleep mode where the selected source clock is running, to keep track of time.

The RTC can wake up the device from sleep modes using the alarm/compare wake-up, periodic wake-up, or overflow wake-up mechanisms, or from the wake inputs. The RTC can generate periodic peripheral events from outputs of the prescaler, as well as alarm/compare interrupts and peripheral events, which can trigger at any counter value. Additionally, the timer can trigger an overflow interrupt and peripheral event and can be reset on the occurrence of an alarm/compare match. This allows periodic interrupts and peripheral events at very long and accurate intervals.

The 10-bit programmable prescaler can scale down the clock source, enabling a wide range of resolutions and time-out periods to be configured. With a 32.768 kHz clock source, the minimum counter tick interval is 30.5 μs, and time-out periods can range up to 36 hours. For a counter tick interval of 1 s, the maximum time-out period is more than 136 years.

Features

  • 32-bit counter with 10-bit prescaler
  • Multiple clock sources
  • 32-bit or 16-bit counter mode
  • One 32-bit or two 16-bit compare values
  • Clock/Calendar mode
    • Time in seconds, minutes, and hours (12/24)
    • Date in the day of the month, month, and year
    • Leap year correction
  • Digital prescaler correction/tuning for increased accuracy
  • Overflow, alarm/compare match and prescaler interrupts and events
    • Optional clear on alarm/compare match
  • Two general purpose registers
  • Tamper Detection
    • Timestamp on the event or up to five inputs with debouncing
    • Active layer protection

Back to top

Principle of Operation

The RTC bus clock (CLK_RTC_APB) can be enabled and disabled in the Main Clock module (MCLK), and the default state of CLK_RTC_APB can be found in the "Peripheral Clock Masking" section. A 32 kHz or 1 kHz oscillator clock (CLK_RTC_OSC) is required to clock the RTC. This clock must be configured and enabled in the 32KHz Oscillator Controller (OSC32KCTRL) before using the RTC. This oscillator clock is asynchronous to the bus clock (CLK_RTC_APB). Due to this asynchronicity, writing to certain registers will require synchronization between the clock domains.

The RTC consists of a 10-bit prescaler that feeds a 32-bit counter. The actual format of the 32-bit counter depends on the RTC operating mode.

The RTC can function in one of these modes:

  • Mode 0 - COUNT32: RTC serves as a 32-bit counter
  • Mode 1 - COUNT16: RTC serves as a 16-bit counter
  • Mode 2 - CLOCK: RTC serves as a clock/calendar with alarm functionality

Back to top

Block Diagrams

MODE 0 – 32-bit Counter

SAM L10 real time counter mode 0 32-bit counter diagram

When the RTC Operating Mode bits in the Control A register (CTRLA.MODE) are written to 0x0, the counter operates in 32-bit Counter mode. When the RTC is enabled, the counter will increment on every 0-to-1 transition of CLK_RTC_CNT. The counter will increment until it reaches the top value of 0xFFFFFFFF, and then wrap back to 0x00000000. This sets the Overflow Interrupt flag in the Interrupt Flag Status and Clear register (INTFLAG.OVF). The RTC counter value can be read from or written to the Counter Value register (COUNT) in 32-bit format.

The counter value is continuously compared with the 32-bit Compare register (COMP0). When a compare match occurs, the Compare 0 Interrupt flag in the Interrupt Flag Status and Clear register (INTFLAG.CMP0) is set on the next 0-to-1 transition of CLK_RTC_CNT. If the Clear on Match bit in the Control A register (CTRLA.MATCHCLR) is 1, the counter is cleared on the next counter cycle when a compare match with COMP0 occurs. This allows the RTC to generate periodic interrupts or events with longer periods than the prescaler events.

Note that when CTRLA.MATCHCLR is 1, INTFLAG.CMP0 and INTFLAG.OVF will both be set simultaneously on a compare match with COMP0.

Back to top

MODE 1 – 16-bit Counter

SAM L10 real time counter mode 1 16-bit counter diagram

When the RTC Operating Mode bits in the Control A register (CTRLA.MODE) are written to 0x1, the counter operates in 16-bit Counter mode. When the RTC is enabled, the counter will increment on every 0-to-1 transition of CLK_RTC_CNT. While in 16-bit Counter mode, the 16-bit Period register (PER) holds the maximum value of the counter. The counter will increment until it reaches the PER value and then wrap back to 0x0000. This sets the Overflow Interrupt flag in the Interrupt Flag Status and Clear register (INTFLAG.OVF). The RTC counter value can be read from or written to the Counter Value register (COUNT) in 16-bit format.

The counter value is continuously compared with the 16-bit Compare registers (COMPn, n=0..1). When a compare match occurs, the Compare n Interrupt flag in the Interrupt Flag Status and Clear register (INTFLAG.CMPn, n=0..1) is set on the next 0-to-1 transition of CLK_RTC_CNT.

Back to top

MODE 2 – Clock / Calendar

SAM L10 real time counter mode 2 clock/calendar diagram

When the RTC Operating Mode bits in the Control A register (CTRLA.MODE) are written to 0x2, the counter operates in Clock/Calendar mode. When the RTC is enabled, the counter will increment on every 0-to-1 transition of CLK_RTC_CNT. The selected clock source and RTC prescaler must be configured to provide a 1 Hz clock to the counter for correct operation in this mode. The time and date can be read from or written to the Clock Value register (CLOCK) in a 32-bit time/date format.

Time is represented as:

  • Seconds
  • Minutes
  • Hours

Hours can be represented in either 12- or 24-hour format, selected by the Clock Representation bit in the Control A register (CTRLA.CLKREP). This bit can be changed only while the RTC is disabled.

The date is represented in this form:

  • Day as the numeric day of the month (starting at 1)
  • Month as the numeric month of the year (1 = January, 2 = February, etc.)
  • Year as a value from 0x00 to 0x3F. This value must be added to a user-defined reference year. The reference year must be a leap year (2016, 2020, etc).
    • For example, the year value 0x2D added to the reference year 2016, represents the year 2061.

The RTC will increment until it reaches the top value of 23:59:59 December 31 of year value 0x3F, and then wrap back to 00:00:00 January 1 of year value 0x00. This will set the Overflow Interrupt flag in the Interrupt Flag Status and Clear registers (INTFLAG.OVF).

The clock value is continuously compared with the 32-bit Alarm register (ALARM0). When an alarm match occurs, the Alarm 0 Interrupt flag in the Interrupt Flag Status and Clear registers (INTFLAG.ALARM0) is set on the next 0-to-1 transition of CLK_RTC_CNT (i.e., for a 1 Hz clock counter, it means the Alarm 0 Interrupt flag is set with a delay of 1 second after the occurrence of alarm match).

A valid alarm match depends on the setting of the Alarm Mask Selection bits in the Alarm 0 Mask register (MASK0.SEL). These bits determine which time/date fields of the clock and alarm values are valid for comparison and which are ignored. If the Clear on Match bit in the Control A register (CTRLA.MATCHCLR) is set, the counter is cleared on the next counter cycle when an alarm match with ALARM0 occurs. This allows the RTC to generate periodic interrupts or events with longer periods than would be possible with the prescaler events only.

When CTRLA.MATCHCLR is 1, INTFLAG.ALARM0 and INTFLAG.OVF will both be set simultaneously on an alarm match with ALARM0.

​Refer to the "RTC – Real-Time Counter" chapter from the product datasheet to get more details.

Back to top

Code Example

/***
 *** The Example has no copyright and can be used by anyone.
 *** The following example is based on Device File Package
 *** required to compile the macro definitions used.
 *** The Device File Package is available by downloading Atmel Studio 7.
 ***/

 
/***
 *** In this example the aim is to start a 1s timer.
 *** RTC in calendar mode (mode 2) using periodic event and 
 *** using the XOSC32K 1KHZ output as closk source
 *** The RTC is also configured to trigger an ADC start conversion every seconds
 *** based on the Periodic Event PEREO7
 ***/

void RTC_Init(void) {
 /*** Enable RTC APB Clock ***/
 MCLK->APBAMASK.reg |= MCLK_APBAMASK_RTC;
 
 /*** clock for RTC : XOSC32K 1KHZ output as closk source ***/
 OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL(OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val);
 
 /*** Disable RTC: Enable Protected ***/
 RTC->MODE2.CTRLA.reg &= ~(RTC_MODE2_CTRLA_ENABLE);
 /*** (write synchronized) ***/
 while(RTC->MODE2.SYNCBUSY.reg & RTC_MODE2_SYNCBUSY_ENABLE);
 
 /*** Reset RTC ***/
 RTC->MODE2.CTRLA.reg = (RTC_MODE2_CTRLA_SWRST);
 /*** (write synchronized) ***/
 while(RTC->MODE2.SYNCBUSY.reg & RTC_MODE2_SYNCBUSY_SWRST);
 
 /*** RTC in mode 2 (calendar) 
  *** using a prescaler value of 1024, dividing the 1KHz input clock source 
  *** getting a 1s clock.
  ***/

 RTC->MODE2.CTRLA.reg = RTC_MODE2_CTRLA_PRESCALER(RTC_MODE2_CTRLA_PRESCALER_DIV1024_Val)|
                        RTC_MODE2_CTRLA_MODE(RTC_MODE2_CTRLA_MODE_CLOCK_Val);
 
 /*** RTC Periodic event control ***/
 RTC->MODE2.EVCTRL.reg = RTC_MODE2_EVCTRL_PEREO7;
 
 /*** Clear All interrupt flags ***/
 RTC->MODE2.INTFLAG.reg = 0xFFFF;
 
 /*** set interrupt every seconds ***/
 RTC->MODE2.INTENSET.reg |= RTC_MODE2_INTENSET_PER7;
 
 /*** Enable RTC interrupt at core Level (NVIC) set the highest priority ***/
 NVIC_EnableIRQ(RTC_IRQn);
 NVIC_SetPriority(RTC_IRQn,0);
 
 /*** change the clocksync ***/
 RTC->MODE2.CTRLA.bit.CLOCKSYNC = 1;
 /*** (write synchronized) ***/
 while(RTC->MODE2.SYNCBUSY.bit.CLOCKSYNC);
 
 /*** Re Enable RTC (Enable Protected) ***/
 RTC->MODE2.CTRLA.bit.ENABLE = 1;
 /*** (write synchronized) ***/
 while(RTC->MODE2.SYNCBUSY.bit.ENABLE);
}
 
/***
 *** RTC Interrupt Handler
 *** Interrupt Clear
 ***/

 
void RTC_Handler (void) 
{
 RTC->MODE2.INTFLAG.reg;
 /*** Check if an ALARM interrupt appeared ***/
 /*** Clear ALARM interrupt flag ***/
 RTC->MODE2.INTFLAG.reg |= RTC_MODE2_INTENSET_PER7;
}

Back to top