A real-time clock (RTC) is a computer clock that keeps track of the current time. Although
RTCs are often used in personal computers, servers and embedded systems, they are also
present in almost any electronic device that requires accurate time keeping. Microcontrollers
supporting RTC can be used for chronometers, alarm clocks, watches, small electronic
agendas, and many other devices.
This application note describes the features of the real-time clock (RTC) controller
embedded in Ultra Low Power Medium-density, Ultra Low Power High-density, F0, F2 and
F4 series devices microcontrollers, and the steps required to configure the RTC for use with
the calendar, alarm, periodic wakeup unit, tamper detection, timestamp and calibration
applications.
Examples are provided with configuration information to enable you to quickly and correctly
configure the RTC for calendar, alarm, periodic wakeup unit, tamper detection, time stamp
and calibration applications.
Note:1All examples and explanations are based on the STM32L1xx, STM32F0xx, STM32F2xx and
STM32F4xx firmware libraries and reference manuals of STM32L1xx (RM0038),
STM32F0xx (RM0091), STM32F2xx (RM0033) and STM32F4xx (RM0090).
2STM32 refers to Ultra Low Power Medium-density, Ultra Low Power High-density, F0, F2
and F4 series devices in this document.
3Ultra Low Power Medium (ULPM) density devices are STM32L151xx and STM32L152xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
4Ultra Low Power High (ULPH) density devices are STM32L151xx, STM32L152xx and
STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes.
5F2 series devices are STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx microcontrollers.
6F4 series are STM32F405xx, STM32F407xx, STM32F415xx and STM32F417xx microcontrollers.
7F0 series devices are microcontrollers.
Ta bl e 1 lists the microcontrollers and development tools concerned by this application note.
Table 1.Applicable products and tools
TypeApplicable products
STM32 F0 series Entry-level Cortex™-M0 MCUs
Microcontrollers
Development tools
STM32 F2 series of high-performance MCUs
STM32 F4 series of high-performance MCUs with DSP and FPU instructions
STM32 L1 ultra-low-power 32-bit MCU series
STM3240G-EVAL, STM3220G-EVAL, STM32L152D-EVAL and STM32L152EVAL evaluation boards
The real-time clock (RTC) embedded in STM32 microcontrollers acts as an independent
BCD timer/ counter. The RTC can be used to provide a full-featured calendar, alarm,
periodic wakeup unit, digital calibration, synchronization, time stamp, and advanced tamper
detection.
Refer to Table 15: Advanced RTC features for the complete list of features available on each
device.
1.1 RTC calendar
A calendar keeps track of the time (hours, minutes and seconds) and date (day, week,
month, year). The STM32 RTC calendar offers several features to easily configure and
display the calendar data fields:
●Calendar with:
–sub-seconds (not programmable)
–seconds
–minutes
–hours in 12-hour or 24-hour format
–day of the week (day)
–day of the month (date)
–month
–year
●Calendar in binary-coded decimal (BCD) format
●Automatic management of 28-, 29- (leap year), 30-, and 31-day months
●Daylight saving time adjustment programmable by software
Figure 1.RTC calendar fields
1. RCT_DR, RTC_TR are RTC Date and Time registers.
2. The sub-second field is the value of the synchronous prescaler’s counter. This field is not writable.
6/44Doc ID 018624 Rev 4
A software calendar can be a software counter (usually 32 bits long) that represents the
number of seconds. Software routines convert the counter value to hours, minutes, day of
AN3371Overview of the STM32 advanced RTC
11:15:28:09 PM
WED OCT 26 2011
the month, day of the week, month and year. This data can be converted to BCD format and
displayed on a standard LCD, which is useful in countries that use the 12-hour format with
an AM/PM indicator (seeFigure 2). Conversion routines use significant program memory
space and are CPU-time consuming, which may be critical in certain real-time applications.
When using the STM32 RTC calendar, software conversion routines are no longer needed
because their functions are performed by hardware.
The STM32 RTC calendar is provided in BCD format. This avoids binary to BCD software
conversion routines, which use significant program memory space and a CPU-load that may
be critical in certain real-time applications.
Figure 2.Example of calendar display on an LCD
1.1.1 Initializing the calendar
Ta bl e 2 describes the steps required to correctly configure the calendar time and date.
Table 2.Steps to initialize the calendar
StepWhat to doHow to do itComments
Disable the RTC registers Write
1
protection
2Enter Initialization mode
Wait for the confirmation of
3
Initialization mode (clock
synchronization)
Program the prescalers register
4
if needed
Load time and date values in
5
the shadow registers
Configure the time format (12h
6
or 24h)
7Exit Initialization mode
Write "0xCA" and then
"0x53" into the
RTC_WPR register
Set INIT bit to ‘1’ in
RTC_ISR register
Poll INITF bit of in
RTC_ISR until it is set
RTC_PRER register:
Write first the
synchronous value and
then write the
asynchronous
Set RTC_TR and
RTC_DR registers
Set FMT bit in RTC_CR
register
Clear the INIT bit in
RTC_ISR register
RTC registers can be modified
The calendar counter is
stopped to allow update
It takes approximately 2
RTCCLK clock cycles for
medium density devices
By default, the RTC_PRER
prescalers register is initialized
to provide 1Hz to the Calendar
unit when RTCCLK = 32768Hz
FMT = 0: 24 hour/day format
FMT = 1: AM/PM hour format
The current calendar counter is
automatically loaded and the
counting restarts after 4
RTCCLK clock cycles
Enable the RTC Registers
8
Write Protection
Doc ID 018624 Rev 47/44
Write "0xFF" into the
RTC_WPR register
RTC Registers can no longer
be modified
Overview of the STM32 advanced RTCAN3371
MS19525V1
HSE OSC
1-24 MHz
LSE OSC
32.768 kHz
LSI RC
37 kHz
HSE_RTC
HSE
LSE
LSI
/2, 4,
8,16
To RTC
RTCCLK
RTCSEL[1:0]
MS19526V1
LSI RC
32 kHz
LSE OSC
32.768 kHz
HSE OSC
4-26 MHz
HSE_RTC
LSI
LSE
HSE
To RTC
RTCCLK
RTCSEL[1:0]
LSI RC
32 kHz
/2 to 31
1.1.2 RTC clock configuration
RTC clock source
The RTC calendar can be driven by three clock sources LSE, LSI or HSE (seeFigure 3 and
Figure 4).
Figure 3.STM32L1xx RTC clock sources
Note:RTCSEL[1:0] bits are the RCC Control/status register (RCC_CSR) [17:16] bits
Figure 4.STM32F2xx or STM32F4xx RTC clock sources
8/44Doc ID 018624 Rev 4
How to adjust the RTC calendar clock
The RTC features several prescalers that allow delivering a 1 Hz clock to calendar unit,
regardless of the clock source.
Figure 5.Prescalers from RTC clock source to calendar unit
Note:The length of the synchronous prescaler depends on the product. For this section, it is
represented on 13 bits.
The formula to calculate ck_spre is:
where:
●RTCCLK can be any clock source: HSE_RTC, LSE or LSI
●PREDIV_A can be 1,2,3,..., or 127
●PREDIV_S can be 0,1,2,..., or 8191
Ta bl e 3 shows several ways to obtain the calendar clock (ck_spre) = 1 Hz.
Table 3.Calendar clock equal to 1 Hz with different clock sources
RTCCLK
Clock source
HSE_RTC = 1MHz
LSE = 32.768 kHz
LSI = 32 kHz
LSI = 37 kHz
1. For STM32L1xx, LSI = 37 KHz, but LSI accuracy is not suitable for calendar application.
2. For STM32F2xx and STM32F4xx, LSI = 32 KHz, but LSI accuracy is not suitable for calendar application.
(1)
(2)
PREDIV_A[6:0]PREDIV_S[12:0]
124
(div125)
127
(div128)
127
(div128)
124
(div125)
Prescalers
7999
(div8000)
255
(div256)
249
(div250)
295
(div296)
ck_spre
1 Hz
1 Hz
1 Hz
1 Hz
Doc ID 018624 Rev 49/44
Overview of the STM32 advanced RTCAN3371
MS19528V1
Day of week
Alarm date
AM
PM
hhmms
12h or 24h
format
RTC_ALRMAR
Alarm time
Date
Mask2Mask3Mask1ssMask0Mask ss
RTC_ALRMASSR
1.2 RTC alarms
1.2.1 RTC alarm configuration
STM32 RTC embeds two alarms, alarm A and alarm B, which are similar. An alarm can be
generated at a given time or/and date programmed by the user.
The STM32 RTC provides a rich combination of alarms settings, and offers many features to
make it easy to configure and display these alarms settings.
Each alarm unit provides the following features:
●Fully programmable alarm: sub-second (this is discussed later), seconds, minutes,
hours and date fields can be independently selected or masked to provide a rich
combination of alarms.
●Ability to exit the device from low power modes when the alarm occurs.
●The alarm event can be routed to a specific output pin with configurable polarity.
●Dedicated alarm flags and interrupt.
Figure 6.Alarm A fields
10/44Doc ID 018624 Rev 4
1. RTC_ALRMAR is an RTC register. The same fields are also available for the RTC_ALRMBR register.
2. RT_ARMASSR is an RTC register. The same field is also available for the RTC_ALRMBR register.
3. Maskx are bits in the RTC_ALRMAR register that enable/disable the RTC_ALARM fields used for alarm A
and calendar comparison. For more details, refer to Table 5.
4. Mask ss are bits in the RTC_ALRMASSR register.
An alarm consists of a register with the same length as the RTC time counter. When the
RTC time counter reaches the value programmed in the alarm register, a flag is set to
indicate that an alarm event occurred.
The STM32 RTC alarm can be configured by hardware to generate different types of
alarms. For more details, refer toTa bl e 5.
Programming the alarm
Ta bl e 4 describes the steps required to configure alarm A.
AN3371Overview of the STM32 advanced RTC
Table 4.Steps to configure the alarm
StepWhat to doHow to do itComments
Disable the RTC registers Write
1
protection
2Disable alarm A
Check that the RTC_ALRMAR
3
register can be accessed
4Configure the alarm
5Re-enable alarm A
Enable the RTC registers Write
6
protection
1. Respectively ALRBE bit for alarm B.
2. Respectively ALRBWF bit for alarm B.
3. Respectively RTC_ALRMBR register for alarm B.
4. As an example, if the alarm is configured to occur at 3:00:00 PM, the alarm will not occur even if the
calendar time is 15:00:00, because the RTC calendar is 24-hour format and the alarm is 12-hour format.
5. Respectively ALRBE bit for alarm B.
6. RTC alarm registers can only be written when the corresponding RTC alarm is disabled or during RTC
Initialization mode.
Write "0xCA" and then
"0x53" into the
RTC_WPR register
(1)
Clear ALRAE
bit in
RTC_CR register.
(2)
Poll ALRAWF
bit until
it is set in RTC_ISR.
Configure
RTC_ALRMAR
(3)
register.
(5)
Set ALRAE
bit in
RTC_CR register.
Write "0xFF" into the
RTC_WPR register
RTC registers can be modified
It takes approximately two
RTCCLK clock cycles (clock
synchronization).
The alarm hour format must be
the same
(4)
as the RTC
Calendar in RTC_ALRMAR.
RTC registers can no longer be
modified
Configuring the alarm behavior using the MSKx bits
The alarm behavior can be configured using the MSKx bits (x = 1, 2, 3, 4) of the
RTC_ALRMAR register for alarm A (RTC_ALRMBR register for alarm B).
Ta bl e 5 shows all the possible alarm settings. As an example, to configure the alarm time to
23:15:07 on Monday (assuming that the WDSEL = 1), MSKx bits must be set to 0000b.
When the WDSEL = 0, all cases are similar, except that the Alarm Mask field compares with
the day number and not the day of the week, and MSKx bits must be set to 0000b.
Table 5.Alarm combinations
MSK3 MSK2 MSK1 MSK0
0000
0001
All fields are used in alarm comparison:
Alarm occurs at 23:15:07, each Monday.
Seconds do not matter in alarm comparison
The alarm occurs every second of 23:15, each Monday.
Minutes do not matter in alarm comparison
0010
The alarm occurs at the 7th second of every minute of 23:XX, each
Monday.
0011Minutes and seconds do not matter in alarm comparison
0100Hours do not matter in alarm comparison
0101Hours and seconds do not matter in alarm comparison
Alarm behavior
Doc ID 018624 Rev 411/44
Overview of the STM32 advanced RTCAN3371
MS30110V1
Alarm sub-second
AM
PM
hhmms
12h or 24h
format
Alarm flag
Time
ss
ss
Mask ss
=
Table 5.Alarm combinations (continued)
MSK3 MSK2 MSK1 MSK0
0110Hours and minutes do not matter in alarm comparison
0111
1000
Hours, minutes and seconds do not matter in alarm comparison
The alarm is set every second, each Monday, during the whole day.
Week day (or date, if selected) do not matter in alarm comparison
Alarm occurs all days at
1001Week day and seconds do not matter in alarm comparison
1010Week day and minutes do not matter in alarm comparison
1011Week day, minutes and seconds do not matter in alarm comparison
1100Week day and hours do not matter in alarm comparison
1101Week day, hours and seconds do not matter in alarm comparison
1110Week day, hours and minutes do not matter in alarm comparison
1111Alarm occurs every second
Alarm behavior
23:15:07.
Caution:If the seconds field is selected (MSK0 bit reset in RTC_ALRMAR or RTC_ALRMBR), the
synchronous prescaler division factor PREDIV_S set in the RTC_PRER register must be at
least 3 to ensure a correct behavior.
1.2.2 Alarm sub-second configuration
The STM32 RTC unit provides programmable alarms, sub-second A and B, which are
similar. They generate alarms with a high resolution (for the second division).
The value programmed in the Alarm sub-second register is compared to the content of the
sub-second field in the calendar unit.
The sub-second field counter counts down from the value configured in the synchronous
prescaler to zero, and then reloads a value in the RTC_SPRE register.
Figure 7.Alarm sub-second field
Note:Mask ss is the most significant bit in the sub-second alarm. These are compared to the
synchronous prescaler register.
12/44Doc ID 018624 Rev 4
AN3371Overview of the STM32 advanced RTC
The Alarm sub-second can be configured using the mask ss bits in the alarm sub-second
register. Table 6: Alarm sub-second mask combinations shows the configuration possibilities
for the mask register and provides an example with the following settings:
●Select LSE as the RTC clock source (for example LSE = 32768 Hz).
●Set the Asynchronous prescaler to 127.
●Set the Synchronous prescaler to 255 (the Calendar clock is equal to 1Hz).
●Set the alarm A sub-second to 255 (put 255 in the SS[14:0] field).
Table 6.Alarm sub-second mask combinations
MASKSSAlarm A sub-second behaviorExample result
0
1
2
3
4
5
6
7
8
9
10
11
There is no comparison on sub-second for alarm. The alarm is
activated when the second unit is incremented.
Only the AlarmA_SS[0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[1:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[2:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[3:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[4:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[5:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[6:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[7:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[8:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[9:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[10:0] bit is compared to the RTC sub-second
register RTC_SSR
The alarm is activated every
1 second
The alarm is activated every
(1/128) s
The alarm is activated every
(1/64) s
The alarm is activated every
(1/32) s
The alarm is activated every
(1/16) s
The alarm is activated every
125 ms
The alarm is activated every
250 ms
The alarm is activated every
500 ms
The alarm is activated every 1 s
The alarm is activated every 1 s
The alarm is activated every 1 s
The alarm is activated every 1 s
12
13
14
15
Only the AlarmA_SS[11:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[12:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[13:0] bit is compared to the RTC sub-second
register RTC_SSR
Only the AlarmA_SS[14:0] bit is compared to the RTC sub-second
register RTC_SSR
The alarm is activated every 1 s
The alarm is activated every 1 s
The alarm is activated every 1 s
The alarm is activated every 1 s
Note:The overflow bits in the sub-second register bit (15,16 and 17) are never compared.
Doc ID 018624 Rev 413/44
Overview of the STM32 advanced RTCAN3371
1.3 RTC periodic wakeup unit
Like many STMicroelectronics microcontrollers, the STM32 provides several low power
modes to reduce the power consumption.
The STM32 features a periodic timebase and wakeup unit that can wake up the system
when the STM32 operates in low power modes. This unit is a programmable downcounting
auto-reload timer. When this counter reaches zero, a flag and an interrupt (if enabled) are
generated.
The wakeup unit has the following features:
●Programmable downcounting auto-reload timer.
●Specific flag and interrupt capable of waking up the device from low power modes.
●Wakeup alternate function output which can be routed to RTC_ALARM output (unique
pad for alarm A, alarm B or Wakeup events) with configurable polarity.
●A full set of prescalers to select the desired waiting period.
1.3.1 Programming the Auto-wakeup unit
Ta bl e 7 describes the steps required to configure the Auto-wakeup unit.
Table 7.Steps to configure the Auto-wakeup unit
StepWhat to doHow to do itComments
1Disable the RTC registers Write protection
2Disable the wakeup timer.
3
4Program the value into the wakeup timer.
5Select the desired clock source.
6Re-enable the wakeup timer.
7Enable the RTC registers Write protection
Ensure access to Wakeup auto-reload
counter and bits WUCKSEL[2:0] is allowed.
Write "0xCA" and
then "0x53" into the
RTC_WPR register
Clear WUTE bit in
RTC_CR register
Poll WUTWF until it
is set in RTC_ISR
Set WUT[15:0] in
RTC_WUTR register
Program
WUCKSEL[2:0] bits
in RTC_CR register
Set WUTE bit in
RTC_CR register
Write "0xFF" into the
RTC_WPR register
RTC registers can be
modified
It takes approximately
2 RTCCLK clock cycles
See Section 1.3.2:
Maximum and minimum
RTC wakeup period
The wakeup timer
restarts downcounting
RTC registers can no
more be modified
14/44Doc ID 018624 Rev 4
Loading...
+ 30 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.