AN3371
Application note
Using the hardware real-time clock (RTC)
in STM32 F0, F2, F3, F4 and L1 series of MCUs
Introduction
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:
All examples and explanations are based on the STM32L1xx, STM32F0xx, STM32F2xx
STM32F4xx and STM32F3xx firmware libraries and reference manuals of STM32L1xx
(RM0038), STM32F0xx (RM0091), STM32F2xx (RM0033), STM32F4xx (RM0090),
STM32F37x (RM0313) and STM32F30x(RM0316).
STM32 refers to Ultra Low Power Medium-density, Ultra Low Power High-density, F0, F2
and F4 series devices in this document.
Ultra Low Power Medium (ULPM) density devices are STM32L151xx and STM32L152xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
Ultra Low Power High (ULPH) density devices are STM32L151xx, STM32L152xx and
STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes.
F2 series devices are STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx
microcontrollers.
STM32F3xx refers to STM32F30x, STM32F31x, STM32F37x and STM32F38x devices.
F4 series are STM32F405xx, STM32F407xx, STM32F415xx and STM32F417xx microcontrollers.
F0 series devices are microcontrollers.
Table 1
lists the microcontrollers concerned by this application note.
Table 1.
Type
Applicable products
Applicable products
STM32 F0
STM32 F2
STM32 F3 (STM32F30x, STM32F31x, STM32F37x, STM32F38x)
STM32 F4 (STM32F405xx, STM32F407xx, STM32F415xx, STM32F417xx)
STM32 L1
Microcontrollers
September 2012
Doc ID 018624 Rev 5
1/45
www.st.com
Contents
AN3371
Contents
1
Overview of the STM32 advanced RTC . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1
RTC calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.1
1.1.2
Initializing the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
RTC clock configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2
RTC alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.1
1.2.2
RTC alarm configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Alarm sub-second configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3
RTC periodic wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3.1
1.3.2
Programming the Auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Maximum and minimum RTC wakeup period . . . . . . . . . . . . . . . . . . . . 15
1.4
RTC digital calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.4.1
1.4.2
RTC coarse calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
RTC smooth calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5
1.6
1.7
1.8
Synchronizing the RTC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
RTC reference clock detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Time-stamp function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
RTC tamper detection function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8.1
1.8.2
1.8.3
Edge detection on tamper input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Level detection on tamper input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Active time-stamp on tamper detection event . . . . . . . . . . . . . . . . . . . . 25
1.9
1.10
1.11
Backup registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
RTC and low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Alternate function RTC outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.11.1
1.11.2
RTC_CALIB output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
RTC_ALARM output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.12
RTC security aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.12.1
1.12.2
1.12.3
RTC register write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Enter/exit initialization mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
RTC clock synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2
3
Advanced RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
RTC firmware driver API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2/45
Doc ID 018624 Rev 5
AN3371
Contents
3.1
Start with the RTC driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
3.1.7
3.1.8
Time and date configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Alarm configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
RTC wakeup configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Outputs configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Digital calibration configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
TimeStamp configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Tamper configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Backup data registers configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2
Function groups and description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4
5
Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Doc ID 018624 Rev 5
3/45
List of tables
AN3371
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8.
Table 9.
Table 10.
Table 11.
Table 12.
Table 13.
Table 14.
Table 15.
Table 16.
Table 17.
Table 18.
Applicable products and tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Steps to initialize the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Calendar clock equal to 1 Hz with different clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Steps to configure the alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Alarm combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Alarm sub-second mask combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Steps to configure the Auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Timebase/wakeup unit period resolution with clock configuration 1 . . . . . . . . . . . . . . . . . . 15
Timebase/wakeup unit period resolution with clock configuration 2 . . . . . . . . . . . . . . . . . . 16
Min. and max. timebase/wakeup period when RTCCLK= 32768 . . . . . . . . . . . . . . . . . . . . 17
Time-stamp features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Tamper features (edge detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Tamper features (level detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
RTC_CALIB output frequency versus clock source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Advanced RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
RTC function groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Example descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4/45
Doc ID 018624 Rev 5
AN3371
List of figures
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Figure 14.
Figure 15.
Figure 16.
Figure 17.
Figure 18.
Figure 19.
Figure 20.
RTC calendar fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Example of calendar display on an LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
STM32L1xx RTC clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
STM32F2xx or STM32F4xx RTC clock sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Prescalers from RTC clock source to calendar unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Alarm A fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Alarm sub-second field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Prescalers connected to the timebase/wakeup unit for configuration 1 . . . . . . . . . . . . . . . 15
Prescalers connected to the wakeup unit for configurations 2 and 3 . . . . . . . . . . . . . . . . . 16
Coarse calibration block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Smooth calibration block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
RTC shift register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
RTC reference clock detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Time-stamp event procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Tamper with edge detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Tamper with level detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Tamper sampling with precharge pulse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
RTC_CALIB clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Alarm flag routed to RTC_ALARM output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Periodic wakeup routed to RTC_ALARM pinout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Doc ID 018624 Rev 5
5/45