ST AN3371 Application note

AN3371
Application note
Using the hardware real-time clock (RTC)
in STM32 F0, F2, 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: 1 All 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).
2 STM32 refers to Ultra Low Power Medium-density, Ultra Low Power High-density, F0, F2
and F4 series devices in this document.
3 Ultra Low Power Medium (ULPM) density devices are STM32L151xx and STM32L152xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
4 Ultra Low Power High (ULPH) density devices are STM32L151xx, STM32L152xx and
STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes.
5 F2 series devices are STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx microcontrollers. 6 F4 series are STM32F405xx, STM32F407xx, STM32F415xx and STM32F417xx microcontrollers. 7 F0 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

Type Applicable 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 STM32L152­EVAL evaluation boards
May 2012 Doc ID 018624 Rev 4 1/44
www.st.com
Contents AN3371
Contents
1 Overview of the STM32 advanced RTC . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1 RTC calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.1 Initializing the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.2 RTC clock configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 RTC alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.1 RTC alarm configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.2 Alarm sub-second configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 RTC periodic wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3.1 Programming the Auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3.2 Maximum and minimum RTC wakeup period . . . . . . . . . . . . . . . . . . . . 15
1.4 RTC digital calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.4.1 RTC coarse calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.4.2 RTC smooth calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5 Synchronizing the RTC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.6 RTC reference clock detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7 Time-stamp function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.8 RTC tamper detection function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8.1 Edge detection on tamper input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8.2 Level detection on tamper input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.8.3 Active time-stamp on tamper detection event . . . . . . . . . . . . . . . . . . . . 25
1.9 Backup registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.10 RTC and low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.11 Alternate function RTC outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.11.1 RTC_CALIB output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.11.2 RTC_ALARM output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.12 RTC security aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.12.1 RTC register write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.12.2 Enter/exit initialization mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.12.3 RTC clock synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2 Advanced RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3 RTC firmware driver API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2/44 Doc ID 018624 Rev 4
AN3371 Contents
3.1 Start with the RTC driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1.1 Time and date configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.2 Alarm configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.3 RTC wakeup configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.4 Outputs configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.5 Digital calibration configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.6 TimeStamp configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.7 Tamper configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.8 Backup data registers configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2 Function groups and description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4 Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Doc ID 018624 Rev 4 3/44
List of tables AN3371
List of tables
Table 1. Applicable products and tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Table 2. Steps to initialize the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Table 3. Calendar clock equal to 1 Hz with different clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Table 4. Steps to configure the alarm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Table 5. Alarm combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Table 6. Alarm sub-second mask combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Table 7. Steps to configure the Auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 8. Timebase/wakeup unit period resolution with clock configuration 1 . . . . . . . . . . . . . . . . . . 15
Table 9. Timebase/wakeup unit period resolution with clock configuration 2 . . . . . . . . . . . . . . . . . . 16
Table 10. Min. and max. timebase/wakeup period when RTCCLK= 32768 . . . . . . . . . . . . . . . . . . . . 17
Table 11. Time-stamp features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Table 12. Tamper features (edge detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Table 13. Tamper features (level detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Table 14. RTC_CALIB output frequency versus clock source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Table 15. Advanced RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Table 16. RTC function groups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Table 17. Example descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Table 18. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4/44 Doc ID 018624 Rev 4
AN3371 List of figures
List of figures
Figure 1. RTC calendar fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Figure 2. Example of calendar display on an LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 3. STM32L1xx RTC clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 4. STM32F2xx or STM32F4xx RTC clock sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 5. Prescalers from RTC clock source to calendar unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 6. Alarm A fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 7. Alarm sub-second field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 8. Prescalers connected to the timebase/wakeup unit for configuration 1 . . . . . . . . . . . . . . . 15
Figure 9. Prescalers connected to the wakeup unit for configurations 2 and 3 . . . . . . . . . . . . . . . . . 16
Figure 10. Coarse calibration block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 11. Smooth calibration block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 12. RTC shift register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Figure 13. RTC reference clock detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 14. Time-stamp event procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Figure 15. Tamper with edge detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 16. Tamper with level detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Figure 17. Tamper sampling with precharge pulse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Figure 18. RTC_CALIB clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Figure 19. Alarm flag routed to RTC_ALARM output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Figure 20. Periodic wakeup routed to RTC_ALARM pinout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Doc ID 018624 Rev 4 5/44
Overview of the STM32 advanced RTC AN3371
MS19524V1
AM PM
hh mm s
ss
Week
date
Month Year
Date
RTC-DR
12h or 24h format
RTC_TR
RTC_SSR
DATE
TIME

1 Overview of the STM32 advanced RTC

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/44 Doc 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
AN3371 Overview 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 (see Figure 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
Step What to do How to do it Comments
Disable the RTC registers Write
1
protection
2 Enter 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)
7 Exit 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 4 7/44
Write "0xFF" into the RTC_WPR register
RTC Registers can no longer be modified
Overview of the STM32 advanced RTC AN3371
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 (see Figure 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/44 Doc 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.
AN3371 Overview of the STM32 advanced RTC
Synchronous 13-bit
prescaler (default=256)
RTC
Clock
PREDIV_A
PREDIV_S
Ck_Spre
Asynchronous
prescaler
Synchronous
prescaler
Calendar unit
MS19527V1
Asynchronous 7-bit
prescaler (default = 128)
Shadow registers
(RTC_TR and
RTC_DR)
ck_spre
RTCCLK
PREDIV_A 1+()PREDIV_S 1+()×
-----------------------------------------------------------------------------------------------=
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 4 9/44
Overview of the STM32 advanced RTC AN3371
MS19528V1
Day of week
Alarm date
AM PM
hh mm s
12h or 24h format
RTC_ALRMAR
Alarm time
Date
Mask2Mask3 Mask1ssMask0 Mask 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/44 Doc 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 to Ta bl e 5.
Programming the alarm
Ta bl e 4 describes the steps required to configure alarm A.
AN3371 Overview of the STM32 advanced RTC
Table 4. Steps to configure the alarm
Step What to do How to do it Comments
Disable the RTC registers Write
1
protection
2 Disable alarm A
Check that the RTC_ALRMAR
3
register can be accessed
4 Configure the alarm
5 Re-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 4 11/44
Overview of the STM32 advanced RTC AN3371
MS30110V1
Alarm sub-second
AM PM
hh mm s
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/44 Doc ID 018624 Rev 4
AN3371 Overview 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
MASKSS Alarm A sub-second behavior Example 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 4 13/44
Overview of the STM32 advanced RTC AN3371

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
Step What to do How to do it Comments
1 Disable the RTC registers Write protection
2 Disable the wakeup timer.
3
4 Program the value into the wakeup timer.
5 Select the desired clock source.
6 Re-enable the wakeup timer.
7 Enable 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/44 Doc ID 018624 Rev 4
Loading...
+ 30 hidden pages