Stm32f4 RTC Low Power Mode
Stm32f4 RTC Low Power Mode
Application note
Using the hardware real-time clock (RTC) in low-power modes with
STM32 microcontrollers
Introduction
A real-time clock (RTC) is a computer clock that keeps track of the current time. Although
the RTCs are often used in personal computers, servers and embedded systems, they are
also present in almost any electronic device that requires an accurate time keeping. The
microcontrollers supporting the 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 the ultra-low-power STM32 microcontrollers and the steps required to configure the RTC for
the use with the calendar, alarm, periodic wakeup unit, tamper detection, timestamp and
calibration applications.
Software examples are then provided to show how to use the RTC in the low-power modes
and how to ensure the tampering detection and timestamp while the main supply is switched
off and the MCU is supplied by an alternate battery. One example is showing also a possible
implementation of the smooth calibration features.
The X-CUBE-RTC embedded software package is delivered with this application note,
containing the source code of the RTC examples and all the embedded software modules
required to run the examples.
Table 1. Applicable products
Type
Microcontrollers
October 2016
DocID028310 Rev 2
1/51
www.st.com
Contents
AN4759
Contents
1
1.2
1.3
1.4
2/51
1.1.1
Software calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.2
1.1.3
1.1.4
1.2.2
1.3.2
1.4.2
Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5
1.6
1.7
Time-stamp function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8
1.8.2
1.8.3
1.9
Backup registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.10
1.11
RTC calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.10.1
RTC_CALIB output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.10.2
RTC_ALARM output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.11.2
1.11.3
DocID028310 Rev 2
AN4759
Contents
3.2
3.3
4.2
4.3
Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.3.2
Software setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.3.3
LED meaning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.3.4
4.3.5
5.2
5.3
Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.3.2
Software setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.3.3
5.3.4
6.2
6.3
Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.3.2
Software setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.3.3
LED meaning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.3.4
Reference documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
DocID028310 Rev 2
3/51
3
List of tables
AN4759
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.
Table 19.
4/51
Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Steps to initialize the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Calendar clock equal to 1 Hz with different clock sources . . . . . . . . . . . . . . . . . . . . . . . . . 10
Steps to configure the alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Alarm combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Alarm sub-second mask combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Steps to configure the auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Timebase/wakeup unit period resolution with clock configuration 1 . . . . . . . . . . . . . . . . . . 16
Min. and max. timebase/wakeup period when RTCCLK= 32768 . . . . . . . . . . . . . . . . . . . . 17
Time-stamp features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Tamper features (edge detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Tamper features (level detection) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
RTC_CALIB output frequency versus clock source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Advanced RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Tampering detection status when Power-On-Reset is detected. . . . . . . . . . . . . . . . . . . . . 48
Tampering detection status when tamper event is detected . . . . . . . . . . . . . . . . . . . . . . . 48
Tampering detection status when reset is detected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Tampering detection status when tamper event is detected . . . . . . . . . . . . . . . . . . . . . . . 48
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
DocID028310 Rev 2
AN4759
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.
Figure 21.
Figure 22.
Figure 23.
Figure 24.
Figure 25.
Figure 26.
DocID028310 Rev 2
5/51
5
AN4759
1.1
RTC calendar
A calendar keeps track of the time (hours, minutes and seconds) and date (day, week,
month, year). The RTC calendar offers several features to easily configure and display the
calendar data fields:
Calendar with:
Seconds
Minutes
Month
Year
Automatic management of 28-, 29- (leap year), 30-, and 31-day months
'$7(
'DWH
:HHN
GDWH
7,0(
KRUKIRUPDW
0RQWK
<HDU
$0
30
57&B'5
KK
57&B75
PP
VV
57&B665
069
6/51
DocID028310 Rev 2
AN4759
1.1.1
Software calendar
A software calendar can be a software counter (usually 32 bits long) that represents the
number of seconds. The software routines convert the counter value to hours, minutes, day
of the month, day of the week, month and year. This data can be converted to BCD format
and displayed on a standard LCD. Conversion routines use a significant program memory
space and are CPU-time consuming, which may be critical in certain real-time applications.
1.1.2
09:40:28:09 PM
SUN AUG 23 2015
1.1.3
What to do
How to do it
Comments
RTC_PRER register:
Program the prescaler values if Write first the
synchronous value and
needed
then write the
asynchronous value
DocID028310 Rev 2
7/51
50
AN4759
1.1.4
What to do
How to do it
Comments
+6(26&
0+]
+6(
+6(B57&
/6(26&
N+]
/6(
7R57&
57&&/.
57&6(/>@
/6,5&
N+]
/6,
06Y9
Note:
8/51
The RTCSEL[1:0] bits are the RCC Control/status register (RCC_CSR) [17:16] bits.
DocID028310 Rev 2
AN4759
/6,
57&6(/>@
/6,5&
N+]
7R57&
/6(26& /6(
N+]
+6(26& +6(
0+]
57&&/.
+6(B57&
06Y9
Note:
The RTCSEL[1:0] bits are the backup domain control register (RCC_BDCR) [9:8] bits.
Figure 5. STM32F0 Series and STM32F3 Series RTC clock sources
+6(
+6(26&
0+]
+6(B57&
/6(26&
N+]
7R57&
/6(
57&&/.
57&6(/>@
/6,5&
N+]
/6,
069
$V\QFKURQRXV
SUHVFDOHU
35(',9B$
6\QFKURQRXV
SUHVFDOHU
35(',9B6
$V\QFKURQRXVELW
SUHVFDOHUGHIDXOW
6\QFKURQRXVELW
SUHVFDOHUGHIDXOW
&NB6SUH
&DOHQGDUXQLW
6KDGRZUHJLVWHUV
57&B75DQG
57&B'5
069
DocID028310 Rev 2
9/51
50
AN4759
where:
Table 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
Prescalers
RTCCLK
10/51
ck_spre
Clock source
PREDIV_A[6:0]
PREDIV_S[14:0]
HSE_RTC = 1 MHz
124
(div 125)
7999
(div 8000)
1 Hz
127
(div 128)
255
(div 256)
1 Hz
LSI = 32 kHz
127
(div 128)
249
(div 250)
1 Hz
LSI = 37 kHz
124
(div 125)
295
(div 296)
1 Hz
LSI = 40 kHz
127
(div 128)
311
(div 312)
1 Hz
DocID028310 Rev 2
AN4759
1.2
RTC alarms
1.2.1
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.
KRUK
IRUPDW
$ODUPGDWH
'D\RIZHHN
$0
30
'DWH
0DVN
KK
0DVN
57&B$/50$5
$ODUPWLPH
PP
0DVN
0DVN
VV
0DVNVV
57&B$/50$665
069
1. RTC_ALRMAR is an RTC register. The same fields are also available for the RTC_ALRMBR register.
2. RTC_ALRMASSR is an RTC register. The same field is also available for the RTC_ALRMBSSR register.
3. Maskx are the bits in the RTC_ALRMAR register that enable/disable the RTC_ALARM fields used for
alarm A. For more details, refer to Table 5.
4. Mask ss are the 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 RTC alarm can be configured by hardware to generate different types of alarms. For
more details, refer to Table 5.
DocID028310 Rev 2
11/51
50
AN4759
What to do
How to do it
Comments
Disable alarm A
Configure
RTC_ALRMAR(4)
register.
Re-enable alarm A
Alarm behavior
12/51
DocID028310 Rev 2
AN4759
Alarm behavior
The hours, minutes and seconds do not matter in the alarm comparison
The alarm is set every second, each Monday, during the whole day.
The week day (or date, if selected) do not matter in the alarm comparison
The alarm occurs all the days at 23:15:07.
The week day and seconds do not matter in the alarm comparison
The week day and minutes do not matter in the alarm comparison
The week day, minutes and seconds do not matter in the alarm comparison
The week day and hours do not matter in the alarm comparison
The week day, hours and seconds do not matter in the alarm comparison
The week day, hours and minutes do not matter in the alarm comparison
Caution:
If the second 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
KRUK
IRUPDW
$0
30
7LPH
KK
PP
$ODUPVXEVHFRQG
V
VV
VV
0DVNVV
$ODUPIODJ
069
DocID028310 Rev 2
13/51
50
AN4759
Mask ss is the most significant bit in the sub-second alarm. This bit is compared to the
synchronous prescaler register.
The alarm sub-second can be configured using the mask ss bit 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 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
MASK SS
14/51
Example result
10
11
12
13
14
15
DocID028310 Rev 2
AN4759
Note:
1.3
1.3.1
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.
What to do
How to do it
Comments
RTC registers can be
modified
It takes around
2 RTCCLK clock cycles
due to clock
synchronization
Set WUT[15:0] in
RTC_WUTR register See Section 1.3.2:
Maximum and minimum
Program
WUCKSEL[2:0] bits RTC wakeup period
in RTC_CR register
DocID028310 Rev 2
15/51
50
1.3.2
AN4759
57&&/.
57&B:875
3UHVFDOHU
:8&.6(/>@
3HULRGLF
ZDNHXSIODJ
ELWZDNHXS
DXWRUHODRGWLPHU
06Y9
488.28 s
61.035 s
When RTCCLK= 32768 Hz, the minimum timebase/wakeup resolution is 61.035 s, and the
maximum resolution is 488.28 s. As a result:
16/51
DocID028310 Rev 2
AN4759
57&&ORFN
$V\QFKURQRXV
SUHVFDOHU
35(',9B$
6\QFKURQRXV
SUHVFDOHU
35(',9B6
$V\QFKELWSUHVFDOHU
GHIDXOW
6\QFKURQRXVELW
3UHVFDOHUGHIDXOW
:8&.6(/>@
:DNHXS
DXWRUHORDG
WLPHU
57&B:875
3HULRGLF
ZDNHXSIODJ
ELWZDNHXSDXWR
UHORDGWLPHU
06Y9
Minimum period
Maximum period
122.07 s
32 s
1s
18 hours
18 hours +1 s
36 hours
DocID028310 Rev 2
17/51
50
1.4
1.4.1
AN4759
The term quartz-accurate has become a familiar phrase used to describe the accuracy of
many time keeping functions. The quartz oscillators provide an accuracy far superior to that
of other conventional oscillator designs, but they are not perfect. The quartz crystals are
sensitive to temperature variations. Figure 11 shows the relationship between accuracy
(acc), temperature (T) and curvature (K) for a typical 32.768 kHz crystal. The curve follows
the general formula given below:
acc = K x (T-To), where:
Note:
To = 25 C 5 C
K = 0.032 ppm/C2
The variable K is crystal-dependent, the value indicated here is for the crystal mounted on
the STM32L476RG-Nucleo board. Refer to the crystal manufacturer for more details on this
parameter.
The clocks used in most applications require a high degree of accuracy, and there are
several factors involved in achieving this accuracy. Typically most crystals are compensated
for by adjusting the load capacitance of the oscillator. This method, though effective, has
several disadvantages:
1.
2.
Instead of this crude analog method, the STM32 products use a digital calibration feature
that gives the user software control over the calibration procedure, and makes it userfriendly.
Figure 11. Typical crystal accuracy plotted against temperature
n
n
n
n
4EMPERATURE #
n
n
n
!CCURACY PPM
n
AIB
18/51
DocID028310 Rev 2
AN4759
1.4.2
Methodology
The RTC clock frequency can be corrected using a series of small adjustments by adding or
subtracting individual RTCCLK pulses.The RTC clock can be calibrated with a resolution of
about 0.954 ppm with a range from -487.1 ppm to +488.5 ppm.
This digital smooth calibration is designed to compensate for the inaccuracy of crystal
oscillators due to the temperature, crystal aging.
Figure 12. Smooth calibration block
+]
57&B&$/,%
57&
&ORFN
6PRRWK
FDOLEUDWLRQ
+]
$V\QFKURQRXV
SUHVFDOHU
6\QFKURQRXV
SUHVFDOHU
&DOHQGDU
&NB6SUH
$V\QFKELWSUHVFDOHU
GHIDXOW
6\QFKURQRXVELW
SUHVFDOHUGHIDXOW
6KDGRZUHJLVWHUV
57&B7557&B'5
06Y9
The user can compute the clock deviation using the RTC_CALIB signal, then update the
calibration block. It is also possible to input an external 1 Hz reference and make the
adequate processing inside the MCU to correct the RTC clock. The user finds such example
in Section 5.3: Application example project.
It is possible to check the calibration result using the calibration output 512 Hz or 1 Hz for
the RTC_CALIB signal. Refer to Table 14: Advanced RTC features.
A smooth calibration consists of masking and adding N (configurable) 32 kHz pulses that
are well distributed in a configurable window (8 s, 16 s or 32 s).
The number of masked or added pulses is defined using CALP and CALM in the
RTC_CALR register.
By default, the calibration window is 32 seconds. It can be reduced to 8 or 16 seconds by
setting the CALW8 bit or the CALW16 bit in the RTC_CALR register:
Example 1: setting CALM[0] to 1, CALP=0 and using 32 seconds as a calibration window
results in exactly one pulse being masked for 32 seconds.
Example 2: setting CALM[2] to 1, CALP=0 and using 32 seconds as a calibration window
results in exactly 4 pulses being masked for 32 seconds.
Note:
Both the CALM and CALP can be used and, in this case, an offset ranging from -511 to
+512 pulses can be added for 32 seconds (calibration window).
When the asynchronous prescaler is less than 3, CALP cannot be set to 1.
The formula to calculate the effective calibrated frequency (FCAL), given the input
frequency (FRTCCLK), is:
FCAL = FRTCCLK x [1 + (CALP x 512 - CALM) / (220 + CALM - CALP x 512)].
DocID028310 Rev 2
19/51
50
AN4759
A smooth calibration can be performed on the fly so that it can be changed when the
temperature changes or if other factors are detected.
1.5
57&B&$/,%
+]
6KLIW
57&B6+,)7(5
GHOD\
57&&ORFN
DGYDQFH
6\QFKURQRXV
SUHVFDOHU
$V\QFKURQRXV
SUHVFDOHU
&DOHQGDU
&NB6SUH
6\QFKURQRXVELW
SUHVFDOHUGHIDXOW
$V\QFKELWSUHVFDOHU
GHIDXOW
6KDGRZUHJLVWHUV
57&B7557&B'5
06Y9
It is not possible to check the Synchronization shift function using the RTC_CALIB output
since the shift operation has no impact on the RTC clock, other than adding or subtracting a
few fractions from the calendar counter.
Correcting the RTC calendar time
If the RTC clock is advanced compared to the remote clock by n fractions of seconds, the
offset value must be written in SUBFS, which will be added to the synchronous prescalers
counter. As this counter counts down, this operation effectively subtracts from (delays) the
clock by:
Delay (seconds) = SUBFS / (PREDIV_S + 1)
If the RTC is delayed compared to the remote clock by n fractions of seconds, the offset
value can effectively be added to the clock (advancing the clock) when the ADD1S function
is used in conjunction with SUBFS, effectively advancing the clock by:
Advance (seconds) = (1 - (SUBFS / (PREDIV_S + 1))).
20/51
DocID028310 Rev 2
AN4759
1.6
&DOHQGDU
&.B6SUH
6KDGRZUHJLVWHUV
57&B75
57&B'5
$XWRGHWHFWLRQ
RIRU+]
57&B5(),1
9ROWDJH
$GDSWRU9
WR9RU9
06Y9
DocID028310 Rev 2
21/51
50
AN4759
The reference clock calibration and the RTC synchronization (shift feature) cannot be used
together.
The reference clock calibration is the best (ensures a high calibrated time) if the 50 Hz is
always available. If the 50 Hz input is lost, the RTC accuracy is provided by the LSE crystal.
The reference clock detection cannot be used in Vbat mode.
The reference clock calibration can only be used if the user provides a precise 50 or 60 Hz
input.
1.7
Time-stamp function
The Time-stamp feature provides the means to automatically save the current calendar.
Figure 15. Time-stamp event procedure
'$7(
57&B'5
'DWH
:HHN
GDWH
7,0(
57&B75
KRUK
IRUPDW
$0
30
0RQWK
KK
PP
6XEVHFRQG
57&B665
V
VV
&DOHQGDU8QLW
&RS\
57&B7675 57&B75
57&B76'5 57&B'5
57&B76665 57&B665
2Q7LPH6WDPS
HYHQW
'$7(
57&B76'5
'DWH
:HHN
GDWH
0RQWK
6XEVHFRQG
57&B76665
7,0(
57&B7675
<HDU
$0
30
KK
PP
VV
069
Provided that the time-stamp function is enabled, the calendar is saved in the time-stamp
registers (RTC_TSTR, RTC_TSDR, RTC_TSSSR) when an internal or external time-stamp
event is detected. When a time-stamp event occurs, the time-stamp flag bit (TSF) in the
RTC_ISR register is set.
The events that can generate a timestamp are:
22/51
A switch to VBAT when the main supply if powered off (STM32L4x devices only)
DocID028310 Rev 2
AN4759
How to do it
Comments
Detect a Time-stamp
overflow event(3)
1. TSF is set 2 ck_apre cycles after the time-stamp event occurs due to the synchronization process.
2. To avoid masking a time-stamp event occurring at the same moment, the application must not write 0 into
TSF bit unless it has already read it to1.
3. The time-stamp overflow event is not connected to an interrupt.
4. There is no delay in the setting of TSOVF. This means that if two time-stamp events are close to each
other, TSOVF can be seen as '1' while TSF is still '0'. As a consequence, it is recommended to poll TSOVF
only after TSF has been set.
1.8
Note:
The number of tamper inputs depends on product packages. Each input has a TAMPxF
individual flag in the RTC_TAMP register.
DocID028310 Rev 2
23/51
50
1.8.1
AN4759
5[
57&B7$03[
7DPSHU[
&[
VZLWFK
2SWLRQDO
FDSDFLWRU
670/
06Y9
1. If the power consumption is not a concern, the internal 40 k pull-up resistor can be used.
Note:
With the edge detection, the sampling and precharge features are deactivated.
Table 11. Tamper features (edge detection)
What to do
1.8.2
How to do it
Comments
Enable Tamper
24/51
DocID028310 Rev 2
AN4759
/HYHOGHWHFWLRQ
;
;
57&B7$03[
7DPSHU[
VZLWFK
&[
2SWLRQDO
&DSDFLWRU
670/
06Y9
Using the level detection (tamper filter set to a non-zero value), the tamper input pin can be
precharged by resetting TAMPPUDIS through an internal resistance before sampling its
state. In order to support the different capacitance values, the length of the pulse during
which the internal pull-up is applied can be 1, 2, 4 or 8 RTCCLK cycles.
Figure 18. Tamper sampling with precharge pulse
57&FORFN
VDPSOLQJ
)ORDWLQJLQSXW
6ZLWFKRSHQHG
3UHFKDUJH 57&&/.
3UHFKDUJH 57&&/.
3UHFKDUJH 57&&/.
3UHFKDUJH 57&&/.QRWVKRZQ
06Y9
Note:
When the internal pull-up is not applied, the I/Os Schmitt triggers are disabled in order to
avoid an extra consumption if the tamper switch is open.
The trade-off between the tamper detection latency and the power consumption through the
weak pull-up or external pull-down can be reduced by using a tamper sampling frequency
feature. The tamper sampling frequency is determined by configuring the TAMPFREQ bits
in the RTC_TAMPCR register. When using the LSE at 32768 Hz as the RTC clock source,
the sampling frequency can be 1, 2, 4, 8, 16, 32, 64, or 128 Hz.
DocID028310 Rev 2
25/51
50
AN4759
1.8.3
How to do it
Comments
Enable Tamper
Configure TAMPPUDIS
and TAMPPPRCH bits in
RTC_TAMPCR register
Note:
It is not necessary to enable or disable the timestamp function when using this feature.
1.9
Backup registers
The 32-bit backup registers (RTC_BKPxR) are reset when a tamper detection event occurs.
These registers are powered-on by VBAT when VDD is switched off (except for STM32L0
Series and STM32L1 Series), they are not reset by a system reset, and their contents
remain valid when the device operates in low-power mode.
For STM32L0 Series, STM32L4 Series and STM32F7 Series, the 32-bit backup registers
(RTC_BKPxR) are not reset if the TAMPxNOERASE bit is set, or if TAMPxMF (Tamper
Mask Flag) is set in the RTC_TAMPCR register. Disabling the backup register reset feature
allows to trigger a LPTIM event from the tamper pin without erasing the backup
registers.This also allows to take benefit of the tamper input digital filtering, either to
generate triggers or to generate interrupts.
Note:
26/51
The VBAT availability and the LPTIM availability depend on the number of backup registers
depends on the product. Please refer to Table 14: Advanced RTC features.
DocID028310 Rev 2
AN4759
1.10
1.10.1
RTC_ALARM, a unique output resulting from the multiplexing of the RTC alarm and
wakeup events.
RTC_CALIB output
The RTC_CALIB output can be used to generate a 1 Hz or 512 Hz signal and used to
measure the deviation of the RTC clock when compared to a more precise clock.
2.
3.
4.
2.
3.
4.
5.
57&B&$/,%
+]
&26(/
57&&ORFN
$V\QFKURQRXV
SUHVFDOHU
$V\QFKURQRXVELW
SUHVFDOHUGHIDXOW
6\QFKURQRXV
SUHVFDOHU
6\QFKURQRXVELW &NB6SUH
SUHVFDOHUGHIDXOW
&DOHQGDU
6KDGRZUHJLVWHUV
57&B75DQG
57&B'5
06Y9
DocID028310 Rev 2
27/51
50
AN4759
Maximum
(PREDIV_A[5:0] = 100 000b(1))
(div64)
(div32)
HSE_RTC = 1 MHz
15,625 kHz
30.303 kHz
LSE = 32768 Hz
512 Hz
(default output frequency)
993 Hz
LSI = 32 kHz
500 Hz
969 Hz
LSI = 37 kHz
578 Hz
1.121 kHz
LSI = 40 kHz
625 Hz
1.212 kHz
1.
1.10.2
PREDIV_A[5] must be set to 1 to enable the RTC_CALIB output signal generation. If PREDIV_A[5] bit is zero, no signal
is output on RTC_CALIB.
RTC_ALARM output
The RTC_ALARM output can be connected to the RTC alarm unit A or B to trigger an
external action, or connected to the RTC wakeup unit to wake up an external device.
28/51
DocID028310 Rev 2
AN4759
VVPP++GDWH
$ODUP$
IODJ
&DOHQGDU
57&B$/$50
RXWSXW
'D\GDWHPRQWK
\HDU
26(/>@
KKPPVV
IRUPDW
$ODUP%
IODJ
$ODUP%
VVPP++GDWH
06Y9
57&B$/$50
RXWSXW
:DNHXSXQLW
ELWDXWRUHORDG
WLPHU
3HULRGLF
ZDNHXSIODJ
06Y9
DocID028310 Rev 2
29/51
50
1.11
1.11.1
AN4759
To protect the RTC registers against possible parasitic write accesses after reset, the RTC
registers are automatically locked. They must be unlocked to update the current calendar
time and date.
Writing to the RTC registers is enabled by programming a key in the Write protection
register (RTC_WPR).
The following steps are required to unlock the write protection of the RTC register:
1.
2.
Writing an incorrect key automatically reactivates the RTC register write access protection.
1.11.2
The calendar cannot be updated while the counters are running. The RTC must
consequently be switched to the Initialization mode before updating the time and date.
When operating in this mode, the counters are stopped. They start counting from the new
value when the RTC enters the Free-running mode.
The INIT bit of the RTC_ISR register enables the user to switch from one mode to another,
and the INITF bit can be used to check the RTC current mode.
The RTC must be in Initialization mode to program the time and date registers (RTC_TR
and RTC_DR) and the prescalers register (RTC_PRER). This is done by setting the INIT bit
and waiting until the RTC_ISR_INITF flag is set.
To return to the Free-running mode and restart counting, the RTC must exit the Initialization
mode. This is done by resetting the INIT bit.
Only a power-on reset can reset the calendar. A system reset does not affect it but resets
the shadow registers that are read by the application. They are updated again when the
RSF bit is set. After a system reset, the application can check the INITS status flag in the
RTC_ISR register to verify if the calendar is already initialized. This flag is reset when the
calendar year field is set to 0x00 (power-on reset value), meaning that the calendar must be
initialized.
1.11.3
30/51
DocID028310 Rev 2
AN4759
Note:
After resetting the BYPSHAD bit, the shadow registers may be incorrect until the next
synchronization. In this case, the software should clear the RSF bit then wait for the
synchronization (RSF should be set) and finally read the shadow registers.
DocID028310 Rev 2
31/51
50
STM32L0
Series
STM32L1
STM32L1
Cat.
Cat. 1
2/3/4/5/6
STM32L4
Series
STM32F0
Series
STM32F2
Series
STM32F3 STM32F4
Series
Series
STM32F7
Series
Asynchronous
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
X (7 bits)
Synchronous
X (15 bits)
X (1 5
bits)
X (15 bits)
X (15 bits)
Prescalers
DocID028310 Rev 2
12/24 format
Hour, minutes
and seconds
Sub-second
Not
available
Not
available
Date
Daylight operation
Not
available
Not
available
Not
available
Not
available
Not
available
Time
Calendar
VBAT mode
32/51
AN4759
RTC features
STM32L1
STM32L1
Cat.
Cat. 1
2/3/4/5/6
STM32L4
Series
STM32F0
Series
STM32F2
Series
STM32F3 STM32F4
Series
Series
STM32F7
Series
Alarm B
Not
available
12/24 format
Hour, minutes
and seconds
Sub-second
Not
available
Not
available
Not
available
Not
available
Not
available
Not
available
2 inputs /
1 event
2 inputs /
2 events
2 inputs /
2 events
3 inputs/
3 events
1 input
1 input
2 inputs
2 inputs
Alarm
Time
DocID028310 Rev 2
3 inputs /
3 events
3 inputs /
3 events
1 input /
1 event
3 inputs /
3 events
2 inputs
3 inputs (for
STM32F07x
and
STM32F09x)
Not
available
Not
available
Not
available
3 inputs
1 input
33/51
Alarm A
Alarms
available
Tamper
detection
STM32L0
Series
AN4759
RTC features
STM32L0
Series
STM32L1
STM32L1
Cat.
Cat. 1
2/3/4/5/6
STM32L4
Series
STM32F0
Series
STM32F2
Series
STM32F3 STM32F4
Series
Series
STM32F7
Series
X(1)
Not
available
Not
available
X(1)
Hours, minutes
and seconds
Sub-seconds
Not
available
Not
available
Not
available
Not
available
Not
available
Not
available
Not
available
Not
available
Not
available
Alarm event
Wakeup event
512 Hz
1 Hz
Not
available
Not
available
Coarse Calibration
Not
available(2)
Not
available(2)
Not
available(2)
Not
available(2)
Not
available(2)
Smooth Calibration
Not
available
Not
available
Not
available
Not
available
Time
Time
Stamp
DocID028310 Rev 2
RTC
Calibration
RTC_
CALIB
34/51
AN4759
Backup
registers
RTC features
STM32L0
Series
STM32L1
STM32L1
Cat.
Cat. 1
2/3/4/5/6
Powered-on VBAT
Not
available
Not
available
STM32L4
Series
STM32F0
Series
STM32F2
Series
STM32F3 STM32F4
Series
Series
STM32F7
Series
Not
available
Not
available
Not
available
32(3)
20(4)
32
20
16
20
32
AN4759
DocID028310 Rev 2
35/51
AN4759
Note:
In the STM32L0 Series and STM32L1 Series, the VBAT mode is not available.
3.1
3.2
Sleep mode
3.3
a. In the STM32L4 Series, there are 3 Stop modes. In order to select the most relevant mode, please refer to [11]
and [12] for the other series.
b. Only for the STM32L4 Series.
36/51
DocID028310 Rev 2
AN4759
4.1
4.2
A firmware driver API abstracting the RTC features for the end-user. Refer to
stm32l4xx_hal_rtc.c and stm32l4xx_hal_rtc_ex.c files in
\STM32Cube_FW_L4_Vx.y.z\Drivers\STM32L4xx_HAL_Driver\
A set of example projects so that the user can quickly become familiar with the RTC
peripheral. Refer to the examples in
\STM32Cube_FW_L4_Vx.y.z\Projects\STM32L476RG-Eval\Examples\RTC
A display in the debugger (date and time, timestamp date and time) and on LEDs
(power up events, reset events, tamper events, error)
The tampering detection capability both when the main power supply (VDD) is
present and when the RTC is battery powered,
The ability to erase the backup registers that may contain sensitive data upon
tamper detection.
DocID028310 Rev 2
37/51
50
AN4759
&RQILJXUH57&
&ORFNDQG
SUHVFDOHUV
7DPSHU,QWHUUXSW
+DQGOHU
*UHHQ/('RQ
/('
7DPSHU
'HWHFWLRQGXULQJ
3RZHUGRZQ"
<HV
*UHHQ/('RQ
/('
1R
5LVHIODJIRUPDLQ
3URJUDP
&RQILJXUH7DPSHU
'HWHFWLRQRQ3$
5HWXUQWRPDLQ
SURJUDP
%DFN8S
5HJLVWHUV
(UDVHG"
<HV
:ULWHEDFNXS
UHJLVWHU
1R
3RZHU
2Q5HVHW
2FFXUUHG"
3RZHU
2Q5HVHW
2FFXUUHG"
<HV
<HV
5HG/('RQ
/('
&RQILJXUH
&DOHQGDU
1R
(QGRILQLWLDOL]DWLRQ
1R
'LVSOD\'DWHDQG
7LPH
1R
(QWHU/RZ3RZHU
0RGH6WRS
'LVSOD\7LPHVWDPS'DWHDQG7LPH
&KHFN%DFN8S5HJLVWHUVDUHFOHDUHG
7DPSHU
'HWHFWHGLQ,7
"
<HV
'HDFWLYDWH7DPSHU
$OZD\VH[HFXWHGXSRQV\VWHPUHVHW
([HFXWHGXSRQSRZHURQUHVHW
([HFXWHGXSRQEDFNXSGRPDLQUHVHW
38/51
DocID028310 Rev 2
06Y9
AN4759
4.3
4.3.1
Hardware Setup
In order to use the application example project, the user needs a STM32L476 evaluation
board (order code: STM32L476G-EVAL).
Figure 23. STM32L476 evaluation board
-3
3$
-3
-3
86%
3RZHU6XSSO\
8VHU/('V
Supply the board using the USB cable. For this example to work, the user needs to tie PA0
(tamper pin 2) to 0. In order to do that :
Note:
PC13 (tamper pin 1) connected to the blue push-button is not used in this demonstration
firmware as it is connected to an external pull-down resistor preventing the use of the
tamper detection on level with internal pull-up (lowest consumption mode).
For more information, refer to [9].
DocID028310 Rev 2
39/51
50
4.3.2
AN4759
Software setup
Open the project under the users favorite integrated development environment. For IAR
EWARM, open the project.eww file. For Keil MDK-ARM, open the project.uvprojx file,
compile and launch the debug session. For SW4STM32, open the SW4STM32 toolchain,
browse to the SW4STM32 workspace directory, select and import the project
RTC_TamperVBATT. Launching the debug session will load the program in the internal
Flash memory and execute it.
In EWARM debug session, the user can view the calendar and tamper event timestamp
(time and date) by opening a Live watch window (View -> Live watch) and observe the
following global variables:
aShowTime
aShowDate
aShowTimeStampTime
aShowTimeStampDate
The same observation can be done using Keil/MDK-ARM development tools. To open
"Watch1" window, do View->Watch Windows -> Watch1.
4.3.3
LED meaning
LD1 (Green): Tamper event detected
TFT displays: the Tamper date and the Tamper time are different from their initialization
state
LD2 (Orange): Power-On Reset occurred
TFT displays: the Tamper date and the Tamper time are at their initialization state
LD3 (Red): Error (RTC or RCC configuration error, backup registers not erased)
TFT displays: error occurred is displayed
LD4 (Blue): Reset occurred.
TFT displays: the Tamper date and the Tamper time are at their initialization state
4.3.4
40/51
DocID028310 Rev 2
AN4759
4.3.5
Open PA0
Tie PA0 to GND (this is simulating that the end-user takes care to close the box
containing the electronics before supplying it again)
The LD1 lits showing that a tamper event has been detected during the power down
and on TFT, the Tampering date and Tampering time will be updated with the
timestamp of the Tamper event
Note that if the user supplies the VBAT pin with 3V (JP12 on VDD position), the RTC is no
longer supplied when the main power supply is switched off. In this case, the application is
no longer able to detect the tampering event while the main power supply is off.
DocID028310 Rev 2
41/51
50
5.1
AN4759
A firmware driver API abstracting the TIMER features for the end-user. Refer to
stm32l4xx_hal_tim.c and stm32l4xx_hal_tim_ex.c files in
\STM32Cube_FW_L4_Vx.y.z\Drivers\STM32L4xx_HAL_Driver\
A set of example projects so that the user can quickly become familiar with the TIMER
peripheral. Refer to the examples in
\STM32Cube_FW_L4_Vx.y.z\Projects\STM32L476RG-Nucleo\Examples\TIM
5.2
A firmware driver API abstracting the RTC features for the end-user. Refer to
stm32l4xx_hal_rtc.c and stm32l4xx_hal_rtc_ex.c files in
\STM32Cube_FW_L4_Vx.y.z\Drivers\STM32L4xx_HAL_Driver\
A set of example projects so that the user can quickly become familiar with the RTC
peripheral. Refer to the examples in
\STM32Cube_FW_L4_Vx.y.z\Projects\STM32L476RG-Nucleo\Examples\RTC
A GP timer in Master mode using channel 1 in input capture mode and channel 2
in output compare mode. It is also configured to use the LSE clock thanks to the
TIM option register.
42/51
DocID028310 Rev 2
AN4759
5.3
5.3.1
Hardware setup
In order to use the application example project, the user needs a STM32L476RG Nucleo
board.
Figure 24. STM32L476RG-Nucleo board
Supply the board using the USB cable. For this example, the user needs to:
5.3.2
Connect PD2 to a signal generator driving 1 Hz clock with 3.3 V amplitude. It is advised
to control the generated frequency at few ppm.
Connect PB2 to an oscilloscope. Note that the accuracy of the frequency measurement
is ideally in the range of 1 ppm.
Software setup
Open the project under the users favorite integrated development environment. For IAR
EWARM, open the "RTC_SmoothCalib.eww" file. For Keil MDK-ARM, open the
"RTC_SmoothCalib.uvprojx" file. For SW4STM32, open the SW4STM32 toolchain, browse
to the SW4STM32 workspace directory, select and import the project RTC_SmoothCalib.
Compile and launch the debug session. It loads the program in the internal Flash memory
and executes it.
After a maximum of 64 seconds, make a frequency measurement of the CALIB_OUT clock
on PB2. The user shall get a frequency accurate at 1 ppm compared to the 1 Hz reference
clock sent on PD2.
DocID028310 Rev 2
43/51
50
AN4759
The user can then modify the PD2 clock by few ppm, wait 64 seconds and check that the
PB2 clock has been changed accordingly.
Under debugger, the user can monitor the evolution of the CALR register fields: CALP and
CALM.
5.3.3
(;775,*#+]LQWHUUXSW
,QSXW
&DSWXUH
7,0[B&&5
0
57&B&$/5
57&
&DOLEUDWLRQ
5()&/.
+]
57&
%/2&.6
57&&/.
/6(
N+]
57&&/.B&255(&7('
287387
+]
08;
57&
&DOLEUDWLRQEDVHGRQH[WHUQDOVLJQDO
06Y9
2.
3.
TIM2 is configured to increment its counter on CLK_RTC and to get the counter value
on the rising edge generated by TIM3 and stores it in the TIM2_CCR1 register. It then
reset itslef.
4.
The cortex-M4 core gets the TIM2_CCR1 value, compares it with the number of
CLK_RTC cycles expected in 32 seconds and processes the comparison results to
update the CALP and CALM fields of the RTC_CALR register.
5.3.4
2.
Wait 2 x 32 seconds
3.
Measure the output frequency. Depending of the accuracy of the generator and the
measurement device used, the user can see around 1 ppm accuracy.
44/51
The TIM2_CCR1 register (which contains the number of CLK_RTC cycles within a
32 second window)
DocID028310 Rev 2
AN4759
6.1
6.2
A firmware driver API abstracting RTC features for the end-user. Refer to
stm32l0xx_hal_rtc.c and stm32l0xx_hal_rtc_ex.c files in
\STM32Cube_FW_L0_Vx.y.z\Drivers\STM32L0xx_HAL_Driver\
A set of example projects so that the user can quickly become familiar with the RTC
peripheral. Refer to the examples in
\STM32Cube_FW_L0_Vx.y.z\Projects\STM32L053R8-Nucleo\Examples\RTC
A display in the debugger (date and time, timestamp date and time) and on a
single LED (power up events, reset events, tamper events, error)
The ability to erase the backup registers that may contain sensitive data upon
tamper detection.
DocID028310 Rev 2
45/51
50
6.3
6.3.1
Hardware setup
AN4759
In order to use the application example project, the user needs a STM32L053R8-Nucleo
board (order code STM32L053R8-NUCLEO).
Figure 25. STM32L053R8-Nucleo board
Supply the board using the USB cable. In this example, the user uses the B1 push button to
simulate the external tamper event. The user can observe the software "private variables"
on LED LD2 and CN5 Pin 6 (SCK/D13). Details regarding the LED LD2 behavior are
available in main.c file.
Figure 26. LED LD2 behavior
&13LQ
7LPH
/('/'VWDWH
/('B*UHHQB7DPSHU
/('B5HGB3RZHUB2QB5HVHW
/('B5HGB(UURU
/('B%OXHB5HVHW
06Y9
46/51
DocID028310 Rev 2
AN4759
6.3.2
Software setup
Open the project under the users favorite integrated development environment. For IAR
EWARM, open the "project.eww" file. For Keil MDK-ARM, open the "project.uvprojx" file. For
SW4STM32, open the SW4STM32 toolchain, browse to the SW4STM32 workspace
directory, select and import the project RTC_Tamper.
Compile and launch the debug session. This will load the program in the internal Flash
memory and execute it.
In EWARM debug session, the user can view the calendar and tamper event timestamp
(time and date) by opening a "Live watch" window (View -> Live Watch) and observe the
following global variables:
aShowTime
aShowDate
aShowTimeStampTime
aShowTimeStampDate
LED1_Green_Tamper_event_detected
LED2_Red_Power_On_Reset_occurred
LED3_Red_Error
LED4_Blue_Reset_occurred
The same observation can be done using Keil/MDK-ARM development tools. To open
"Watch1" window, do View->Watch Windows -> Watch1.
6.3.3
LED meaning.
Only one LED (LD2) is available on the STM32L053R8-Nucleo board. To match with the
STM32L4 examples, described in Section 4: STM32L4 API and tampering detection
application example, LED1, LED2, LED3 and LED4 are replaced by 4 integers:
uint32_t LED1_Green_Tamper_event_detected:
Set when tamper1 event is detected.
Equivalent to LED1_Green in STM32L4 examples.
uint32_t LED2_Red_Power_On_Reset_occurred:
Set when power-on reset is detected.
Equivalent to LED2_Red in STM32L4 examples.
uint32_t LED3_Red_Error:
Set in errors cases.
Equivalent to LED3_Red in STM32L4 examples.
uint32_t LED4_Blue_Reset_occurred:
Set when reset is detected (B2 black push button).
Equivalent to LED4_Blue in STM32L4 examples.
The user can observe these "private variables" using the live watch feature in the debugger.
The user can observe these "private variables" on LED LD2 and CN5 Pin 6 (SCK/D13).
Regarding the LED LD2 behavior, see details in main.c file.
DocID028310 Rev 2
47/51
50
6.3.4
AN4759
Status
LED1_Green_Tamper_event_detected
OFF
LED2_Red_Power_On_Reset_occurred
ON
LED3_Red_Error
OFF
LED4_Blue_Reset_occurred
ON
Status
LED1_Green_Tamper_event_detected
ON
LED2_Red_Power_On_Reset_occurred
ON
LED3_Red_Error
OFF
LED4_Blue_Reset_occurred
ON
Status
LED1_Green_Tamper_event_detected
OFF
LED2_Red_Power_On_Reset_occurred
OFF
LED3_Red_Error
OFF
LED4_Blue_Reset_occurred
ON
48/51
Status
LED1_Green_Tamper_event_detected
ON
LED2_Red_Power_On_Reset_occurred
OFF
LED3_Red_Error
OFF
LED4_Blue_Reset_occurred
ON
DocID028310 Rev 2
AN4759
Reference documentation
Reference documentation
[1]. STM32L4x6 advanced ARM-based 32-bit MCUs reference manual (RM0351)
[2]. Ultra-low-power STM32L0xx advanced ARM-based 32-bit MCUs reference manuals
(RM0367, RM0376, RM0377)
[3]. STM32L100xx, STM32L151xx, STM32L152xx and STM32L162xx advanced ARMbased 32-bit MCUs (RM0038)
[4]. STM32F0x1/STM32F0x2/STM32F0x8 advanced ARM-based 32-bit MCUs reference
manual (RM0091)
[5]. STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx advanced ARMbased 32-bit MCUs reference manual (RM033)
[6]. STM32F301x6/8 and STM32F318x8 advanced ARM-based 32-bit MCUs reference
manual (RM0366)
[7]. STM32F405/415, STM32F407/417, STM32F427/437 and STM32F429/439 advanced
ARM-based 32-bit MCUs reference manual (RM090)
[8]. STM32F75xxx and STM32F74xxx advanced ARM-based 32-bit MCUs reference
manual (RM0385)
[9]. Evaluation board with STM32L476ZGT6 MCU user manual (UM1855)
[10]. STM32 Nucleo-64 boards user manual (UM1724)
[11]. Optimizing power and performances with STM32L4 Series microcontrollers application
note (AN4746)
[12]. STM32L0xx ultra-low power features overview application note (AN4445)
DocID028310 Rev 2
49/51
50
Revision history
AN4759
Revision history
Table 19. Document revision history
Date
Revision
26-May-2016
Initial release.
25-Oct-2016
50/51
Changes
DocID028310 Rev 2
AN4759
DocID028310 Rev 2
51/51
51