- No category
advertisement
AN2821
Application note
Clock/calendar implementation on the STM32F10xxx microcontroller RTC
The internal RTC (real-time clock) of the 32-bit, ARM
®
Cortex™-M3-based medium-density
STM32F10xxx microcontroller is an independent timer. It provides a set of continuously running counters, which, using suitable software, can be used to implement a clock/calendar function. The RTC and backup registers are supplied through a switch that draws power from either the V
DD
supply (when present) or the backup battery. Thus the RTC runs even when the main supply is switched off.
This application note gives an example of how to implement the clock/calendar functionality using the medium-density STM32F10xxx microcontroller in low-power and standard applications. The firmware example also performs leap year counting and tamper time stamping. It provides a low-power mode demonstration and a basic RTC calibration routine to compensate for crystal and temperature variations.
The STM3210B-EVAL board supports the firmware example described in this application note without any modification to its hardware.
Figure 1.
STM3210B-EVAL board
April 2009 Doc ID 14949 Rev 2 1/28
www.st.com
Contents
Contents
2/28
AN2821
Overview of the medium-density STM32F10xxx backup domain . . . . . 6
Main backup domain features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Configuring the RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Clock/calendar functionality features . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Implementing the clock function on the medium-density STM32F10xxx . 9
Implementing calendar function on the medium-density STM32F10xxx . 9
Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Tamper detection and time stamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
TAMPER-RTC pin functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Battery tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Running the demonstrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Low-power demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Doc ID 14949 Rev 2
AN2821 Contents
Battery removal and installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
50 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
60 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Appendix B Additional notes on calibration and clock accuracy . . . . . . . . . . . 25
Doc ID 14949 Rev 2 3/28
List of tables
List of tables
AN2821
4/28 Doc ID 14949 Rev 2
AN2821
List of figures
List of figures
(1)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Doc ID 14949 Rev 2 5/28
Overview of the medium-density STM32F10xxx backup domain
1 Overview of the medium-density STM32F10xxx backup domain
AN2821
Figure 2.
RTC block diagram
RTC alarm
=
RTC counter
HSE OSC
LSI RC
LSE OSC/ EXT CLOCK
RTC prescaler
RTCSel
[1:0] frtc
RTC divider
1.1
Backup registers
RTC control register
Alarm
IT
Overflow
IT
Second
IT ai15437
Main backup domain features
●
●
The backup domain of the medium-density STM32F10xxx:
● includes ten 16-bit backup data registers, backup control/status registers and RTC registers.
● is powered by V
BAT
when the V is not reset due to system reset, power reset or when the microcontroller wakes up from the Standby mode.
DD
main power is switched off.
is protected from parasitic write access after reset (access to the backup domain is disabled after reset)
● consists of two main units:
– an APB1 interface used to interface with the APB1 bus. The backup domain registers are accessible through the APB1 bus in read/write access mode.
– an RTC core which is clocked by the RTC clock.
6/28 Doc ID 14949 Rev 2
AN2821
1.2
Note:
Overview of the medium-density STM32F10xxx backup domain
Main RTC features
The RTC:
● has three clock sources:
– LSE (low-speed external) clock that can also be bypassed by an external clock
– LSI (low-speed internal) RC oscillator
– HSE (high-speed external) clock divided by 128.
●
● has a 20-bit programmable prescaler that can be programmed to generate the RTC time base. The prescaler load register contains the period counting value of the prescaler.
has three interrupt sources:
– second
– overflow
●
– alarm (also connected to EXTI Line 17 for auto-wakeup from Stop mode) its clock can be output on the TAMPER-RTC pin for calibration purposes.
for an illustration of the medium-density STM32F10xxx RTC block diagram.
All of the above mentioned features of the medium-density STM32F10xxx backup domain can be used to develop a clock/calendar application equivalent to dedicated real-time clocks.
Be careful when choosing the 32.768 kHz crystal for your application. It is strongly recommended to use a crystal with a load capacitance less than or equal to 7 pF. Never use a crystal with a load capacitance of 12.5 pF.
Doc ID 14949 Rev 2 7/28
Configuring the RTC registers
2 Configuring the RTC registers
AN2821
●
●
●
●
●
●
On startup, follow the steps below to configure the RTC to generate an interrupt every second:
● Enable the APB1 backup domain and power interface clocks by writing the BKPEN and
PWREN bits to ‘1’ in the RCC_APB1ENR register
Enable access to backup domain by writing the DBP bit to ‘1’ in the PWR_CR register
Enable the LSE clock by writing the LSEON bit to ‘1’ (also write LSEBYP to “1” when the external clock has to be bypassed)
●
●
Poll the LSERDY flag in the RCC_BDCR register until the LSE clock is ready (if the external crystal is used as the clock source).
Select LSE as the RTC clock source by writing ‘01’ to the RTCSEL bits in the
RCC_BDCR register.
Enable the RTC clock by setting the RTCEN bit in the RCC_BDCR register
Poll the RSF bit in the RTC_CRL register until the RTC registers are synchronized (if a
50/60 Hz external clock source is used this step may take up to a minute to complete)
●
●
Poll the RTOFF bit in the RTC_CRL register until the last operation on the RTC registers is over
Enable the RTC second global interrupt by setting the SECIE bit in the RTC_CRH register
Wait for the last task to complete
Set the RTC prescaler value using the following formula: f
TRCLK
= f
RTCCLK
/(PRL[19:0]+1), where:
– f
RTCCLK is the input clock frequency
– f
TRCLK is the time base generated from the prescaler block
For example, if an external 32.768 kHz (32 kHz) crystal oscillator is used, set the prescaler to 32767. If an external 50 Hz supply is used set the prescaler value to 49.
8/28 Doc ID 14949 Rev 2
AN2821
3
Clock/calendar functionality features
Clock/calendar functionality features
A real-time clock keeps track of the time (hours, minutes, seconds) and date (day, month, year). It should also take into account leap years. A leap year is a year where an extra day is added to the calendar in order to synchronize it to the seasons. Since the tropical year is
365.242190 days long, a leap day must be added roughly once every 4 years (4 × 0.242190
= 0.968760). Thus, every four years, the month of February counts 29 days instead of 28.
The used registers are RTC registers: prescaler register, counter register and alarm register.
The medium-density STM32F10xxx RTC peripheral consists of a chain of programmable counters consisting of two main units:
● the RTC prescaler block generates the RTC time base. Depending on the clock period applied to the LSE input, this prescaler can be adjusted to generate a time base
(RTC_CLK) of 1 second by writing to the prescaler load register.
● the 32-bit programmable counter can be initialized to the current system time. The system time is incremented at the RTC_CLK rate and compared with the alarm register value. When it reaches the alarm register value, an interrupt is generated.
The current system time can be inferred from the value in the counter register by following the steps shown in
(“/” implies division, “%” implies modulus)
Figure 3.
Time system flowchart
(1)
Read RTC counter value
(TimeVar)
Hours = TimeVar
3600
3.1.2
Minutes = TimeVar % 3600
60
Seconds =
(TimeVar % 3600) % 60 ai15438
1.
% is for modulo.
Implementing calendar function on the medium-density STM32F10xxx
The used registers are the 16-bit backup data registers.
When the 32-bit counter register value reaches 86399, this means that one day has elapsed, and that the date has to be updated. The counter value is then reset. Whenever the system date is updated, the new date is stored into the 16-bit backup data registers so that
Doc ID 14949 Rev 2 9/28
Clock/calendar functionality features AN2821
the system remains at the current date even after system reset, power reset or wakeup from the Standby mode.
The calendar algorithm given in
can be used to develop the date update function.
Figure 4.
Calendar algorithm
No
Month has
31 days ?
Yes
Month = 2
?
Yes
Day < 31
?
No
Yes
Day++
Day < 28?
Yes
Day++
No
Month = 12
?
Yes
Month=1
Day=1
Year++
No
Month++
Day=1
Day = = 28
?
Yes
Is it a leap year?
No
Month++
Day=1
No
Yes
Day++
Month has
30 days ?
Yes
No
Day < 31
?
No
Month++
Day=1
Yes
Day++
Day = = 29
?
Yes
Month++
Day=1 ai15439
Leap year correction: each time the date is updated, the possibility of a leap year also has to be considered (please refer to
Figure 5.
Leap year flowchart
Year divisible by 400?
No
Yes
It is a leap year
Year divisible by 100?
No
Yes
Not a leap year
Year divisible by 4?
No
Not a leap year
Yes
It is a leap year ai15440
10/28 Doc ID 14949 Rev 2
AN2821 Clock/calendar functionality features
Caution:
In the case of a device in low-power mode or whose external supply is off when the counter reaches 86399, the counter cannot be reset and so the date is not updated. This is why, just after power reset, it is necessary to check the counter value and update the date as many times as the number of days during which the device remained in low-power mode or had its external main supply switched off.
3.2
This correction is also known as daylight saving and it is used in some countries to save power. The principle is to increment the time of a given country by an hour in summer time so that the sun rises an hour later in the morning and daylight is present longer in the evening. Typically, an hour is added to all clocks near the start of spring and, likewise, an hour is removed in autumn. Exact time and date for summer time correction differs from location to location.
Clock source selection
The external LSE (low-speed external) crystal oscillator can be a crystal oscillator usually with a frequency of 32.768 kHz (32 kHz), which provides a low-power and highly accurate clock source to the RTC peripheral for clock calendar applications. The external LSE can also be bypassed by an external clock. Thus, in countries where the mains power line frequency is significantly stable, a 50 Hz or 60 Hz clock can also be used to provide the RTC clock. Assuming that there is already an isolating circuit and an overcurrent protection in the system, all that is needed is to bypass the LSE crystal and adjust the prescaler load register value so as to generate a time base of 1 second, and hence the second interrupt, if configured.
This application only covers the case of an emulated 50/60 Hz clock source.
●
●
The RTC time base of 1 second is derived from the following formula: f
TRCLK
= f
RTCCLK
/(PRL[19:0]+1), where: f
TRCLK is the derived time base f
RTCCLK is the clock frequency applied on the low-speed oscillator input
50 Hz selection: the prescaler load register value is set to 49 (0x31)
60 Hz selection: the prescaler load register value is set to 59 (0x3B)
32.768 kHz selection: the prescaler load register value is set to 32767 (0x7FFF).
3.3 Calibration
Real-clock precision is a requirement in most embedded applications. The external crystal oscillator used to provide the time base is subject to frequency variations due to external conditions. It is therefore necessary to compensate for crystal and temperature variations.
The calibration routine used in this application is based on RTC calibration as described in application note AN2604.
The medium-density STM32F10xxx RTC comes with a digital calibration circuit that gives the user software control over the calibration process. The digital calibration circuit removes
0 to 127 cycles every 2
20
clock cycles. The number of times the pulses are blanked depends on the value loaded into the seven least significant bits of the RTC clock calibration register.
The calibration circuit can only subtract clock cycles, which means that only higher
Doc ID 14949 Rev 2 11/28
Clock/calendar functionality features AN2821
frequencies can be compensated whereas lower frequencies cannot. The frequency range that can be calibrated is [32772, 32768]. The workaround consists in setting the prescaler to
32766 (instead of 32767), in which case the frequency range that can be calibrated shifts from [32772, 32768] to [32770, 32766].
The calibration process, illustrated in
First, the TAMPER-RTC pin functionality is disabled and the clock calibration register ASOS and ASOE bits are cleared. The (RTC clock) /64) frequency is output on the TAMPER-RTC pin by setting the CCO (clock calibration output) bit in the clock calibration register.
There are two modes of calibration: auto-calibration and manual calibration. Manual calibration consists in actually measuring the LSE/64 value with an external calibrated frequency counter, calculating and manually entering the calibration value via the joystick.
● Auto-calibration:
The frequency output on the TAMPER-RTC pin is fed to the timer input. The frequency is calculated by using the PWM input mode of the medium-density STM32F10xxx general-purpose timer peripheral. The calibration value to be entered in the calibration register can be inferred on the basis of the deviation from 511.968 Hz (the calibration
table giving calibration values for various error values in ppm is given in
● Manual calibration:
In this case, the frequency output on the TAMPER-RTC pin has to be measured by using an external precision instrument. The calibration value has to be calculated, and then entered manually using the joystick.
Note: 1 When the RTC clock/64 is output on the TAMPER-RTC pin, the tamper detection functionality is not available.
2 When the auto-calibration feature is used, the obtained calibration accuracy depends on the accuracy of the on-board HSE clock. This is because the HSE is used as the time base to measure the LSE frequency. As a consequence, if a very accurate oscilloscope or frequency counter is available, it is recommended to implement manual calibration.
Figure 6.
Calibration process
Output RTC clock/64 on TAMPER-RTC pin
Calculate deviation from 511.968 Hz
Calculate error in ppm
(deviation/511.968)
× 1 million
Find nearest calibration value
Load the value into calibration register ai15441
12/28 Doc ID 14949 Rev 2
AN2821 Clock/calendar functionality features
After a system or power reset, the microcontroller is in Run mode. In this mode, the CPU is clocked by HCLK, and the CPU and its peripherals are running. Several low-power modes are available to save power when there is no need for the CPU to be running.
The medium-density STM32F10xxx low-power modes have no effect on the internal RTC, whose counter values keep being updated. The RTC alarm can also be used to auto-wake up the microprocessor from the low-power mode.
The Stop and Standby low-power modes are described in the following paragraphs.
●
●
●
●
●
In the Stop mode, the:
CPU clock is off peripheral clocks, PLL, HSI, HSE are disabled
SRAM and register contents are preserved
RTC and IWDG are kept running voltage regulator can be on or in low-power mode
Any of the EXTI line source, RTC Alarm, PVD or USB Wakeup can be used to release the microcontroller from the Stop mode. On exiting the Stop mode, the clock configuration returns to its reset state (HSI used as the system clock).
Entering the Stop mode
The sequence used to enter the Stop mode is illustrated in
and described below:
1. Select the regulator state in Stop mode through the LPDS bit in the PWR_CR register:
– LPDS = ‘0’, the regulator is ON
– LPDS = ‘1’, the regulator is in low-power mode
2. Reset the PDDS bit in PWR_CR
3. Set the SLEEPDEEP bit in the Cortex-M3 system control register
4. Request Wait for interrupt or Wait for event depending on how the Stop mode is entered.
Figure 7.
Stop mode entry sequence
Select the regulator state in Stop mode
Reset PDDS
Set the SLEEPDEEP bit in the Cortex-M3 system control register
Request _WFI() or _WFE() ai15442
Doc ID 14949 Rev 2 13/28
Clock/calendar functionality features AN2821
After the microcontroller has exited the Stop mode, the basic reset and clock control circuitry has to be reconfigured and access to the backup domain has to be re-enabled.
In the Standby mode, the:
●
●
●
●
●
CPU clock is off
SRAM and register contents are lost except for the backup domain
RTC and IWDG keep running entire 1.8 V domain is off
Exit through WAKEUP pin rising edge, RTC Alarm, external reset.
The Standby mode is exited on the WAKEUP signal’s rising edge, by external reset or by triggering an RTC Alarm.
Entering the Standby mode
The sequence used to enter the Standby mode is illustrated in
and described below:
1.
Clear the WAKEUP flag in the PWR_CSR register.
2. Enable the WAKEUP pin functionality by setting the EWUP bit in the PWR_CSR register
3. Set the PDDS bit in PWR_CR
4. Set the SLEEPDEEP bit in the cortex system control register
5. Request Wait for interrupt
Figure 8.
Standby mode entry sequence
Clear the WAKEUP flag in PWR_CSR
Enable the WAKEUP pin
Set the PDDS bit
Set the SLEEPDEEP bit in the Cortex-M3 system control register
Request _WFI() ai15443
The Standby mode is the lowest power consumption mode. After the microcontroller wakes up from the Standby mode, program execution restarts in the same way as after a system/power reset.
14/28 Doc ID 14949 Rev 2
AN2821
3.5
Clock/calendar functionality features
Tamper detection and time stamping
The medium-density STM32F10xxx features an internal supervisor (in the backup domain) that can be used to detect external tampering.
The TAMPER-RTC pin generates a tamper detection event when the level applied to it changes from ‘0’ to ‘1’ or from ‘1’ to ‘0’ depending on the TPAL bit in the backup control register (BKP_CR). A tamper detection event resets all the backup data registers.
Configuring the TAMPER-RTC pin:
1.
Set the TAMPER-RTC pin level by configuring the TPAL bit in the backup control register
2. Enable the TAMPER-RTC pin interrupt by setting the TPIE bit in the backup control/status register (BKP_CSR)
3. Enable the TAMPER-RTC pin by setting the TPE bit in the backup control register
(BKP_CR)
3.5.3
The tamper detection feature and the RTC of the medium-density STM32F10xxx can be used to timestamp the tamper events, that is, to store the exact time and date of each tamper event. In this application note, the microprocessor’ s internal Flash memory was emulated as an EEPROM. The last few pages of the Flash memory can be used to store these timestamps. Every time a tamper event is detected, the backup data registers are cleared, which means that after every tamper event the calendar information has to be rewritten to the backup registers.
Battery tamper timestamping
Events like battery removal or battery installation can also be timestamped and stored into the emulated EEPROM. For this purpose, the V
BAT
pin (which is connected to the external battery) can be connected to a general-purpose I/O. Configured in interrupt mode, this I/O generates an interrupt on both the falling and rising edges of the V
BAT
signal so as to detect battery removal and battery installation events. The exact times and dates of the events can be stored into the memory.
Doc ID 14949 Rev 2 15/28
16/28
Firmware description AN2821
The example firmware consists of a clock/calendar driver that includes all the functions needed to realize a clock and calendar application on the medium-density STM32F10xxx
RTC peripheral. Other features like RTC calibration, low-power mode demonstration, timestamping for tamper detection and battery removal/installation and, clock source selection (between external crystal and 50/60 Hz) are also supported by the clock/calendar driver. The source code example is based on the STM32F10xxx standard peripheral library.
●
●
User include files:
● clock_calendar.h: contains the prototypes of the basic functions used to implement the calendar function on the STM32F10xxx’s internal RTC. Also includes some macros for summer-time correction, system start default time and date.
●
●
application_rtc.h: contains the RCC_Configuration, GPIO_Configuration and
SystickConfig
function prototypes.
eeprom.h: contains the prototypes of the functions used to emulate EEPROM in the
STM32F10xxx’s internal Flash memory, and for the timestamping application.
●
lcd.h: contains the prototypes of the functions used to drive the TFT LCD present on the STM3210B-EVAL board.
fonts.h: includes various LCD font size definitions.
menu.h: contains the prototypes of the functions needed to implement the menu.
●
●
User source files:
● clock_calendar.c: contains the basic routines for calendar implementation and RTC calibration.
●
●
application_rtc.c: contains the routines for system clock, peripheral clock, GPIO and
Systick timer initialization.
eeprom.c: contains the functions used to emulate EEPROM in the STM32F10xxx’s internal Flash memory. The 1 Kbyte internal Flash memory page 124 (address
0x0801F000 – 0x0801F3FF) is used to store timestamping information. The page address and size are defined in the eeprom.h header file.
lcd.c: contains the functions used to drive the display on the STM3210B-EVAL board.
menu.c: contains the functions used to implement the menu. Various features can be accessed through the menu options displayed on the LCD screen.
Doc ID 14949 Rev 2
AN2821 Hardware description
●
●
●
●
●
●
This application can be fully demonstrated on the STM3210B-EVAL board. The main hardware sections used are:
TFT LCD
LED’s (LD1, LD2, LD3, LD4)
Joystick
Tamper switch
Wakeup switch
PA1, PC7, PC8, PC13 pads
Figure 9.
STM3210B-EVAL board description
Doc ID 14949 Rev 2 17/28
Running the demonstrator AN2821
6.1 Clock/calendar
The home screen displays the clock, date and day on the LCD screen. The MENU provides various options. When the application is run for the first time, some default settings are applied. The current system time and date can be set using various MENU options.
Press joystick Sel to select, joystick right/left to move to the next/previous position and joystick up/down to increment/decrement the value. If you want to exit the current menu, do not press any key for eight seconds, the display will automatically return to the home screen.
To set the system time, go to MENU->Time, and use joystick up/down to increment/decrement the value, joystick right/left to move to the next/previous position and joystick Sel to set the time and return to the home screen. The clock will then start with the newly set time.
To set the alarm, go to MENU->Alarm, use joystick left/right, and up/down to enter the alarm time. Then, press Sel. The system will now prompt you to set the alarm date. Enter the
alarm date and press Sel. When the Alarm condition is reached, the LD1 LED (see
STM3210B-EVAL board description
) lights up and ‘ALARM’ appears on the LCD screen.
After five seconds, LD1 turns off and the display returns to the home screen.
To set the system date, go to MENU->Date and enter the current date using the joystick.
After entering the date, press the joystick Sel button to set the current date and return to the home screen. The updated date is then displayed on the screen.
If a wrong date is entered, an error message is displayed on the LCD screen.
The clock is set ahead by 1 hour at 2 am on the last Sunday of March and it is delayed by 1 hour on the last Sunday of October. Assumedly, the correction for March has already been done and the correction for October has to be done. This is a one-time assumption only, further corrections being automatically handled. The date and time have to be after the current system date and time, that is, for instance if the initial time and date were set to
12:00 hours, 1 st
April, 2008, you can verify the time correction for October 2008 and beyond, but not earlier.
18/28
To enter the Stop mode, go to MENU->LowPower->STOP and press Sel. The LD4 LED
Figure 9: STM3210B-EVAL board description
) lights up and the MCU enters the Stop
Doc ID 14949 Rev 2
AN2821 Running the demonstrator
mode. A confirmation message is displayed on the LCD screen. Press the joystick Sel button to exit the Stop mode. LD4 turns off and the display returns to the home screen.
To enter the Standby mode, go to MENU->LowPower->STANDBY and press Sel. A confirmation message is displayed on the screen. The Standby mode is exited by pressing the Wakeup switch (see
Figure 9: STM3210B-EVAL board description
The RTC alarm can also be used to auto-wake up the microcontroller from the Stop or
Standby mode. For that purpose, first configure the RTC Alarm and then place the microcontroller in a low-power mode. When the alarm sets off, the microprocessor wakes up from the low-power mode.
6.3 Timestamping
6.3.2
To trigger a tamper event, press the B4 tamper switch on the STM3210B-EVAL board (see
Figure 9: STM3210B-EVAL board description
Battery removal and installation
The battery removal and installation conditions are simulated on the STM3210B-EVAL board. The “battery in place” condition is simulated by placing a jumper between PC7 and
PC8 whereas the “battery removed” condition is simulated by removing this jumper. PC7 and PC8 are available on the STM3210B-EVAL board (see
Figure 9: STM3210B-EVAL board description
).
Viewing tamper events
To view the tamper log, go to MENU->Tamper->VIEW. If there is no tamper event stored, the corresponding message is displayed on the LCD screen. A maximum of 64 events can be stored. Each page shows up to 10 events. Subsequent pages can be displayed by pressing the joystick SEL button. The tamper view screen is exited from the last page by pressing the joystick SEL button once more.
Erasing the tamper log
The user can also erase the tamper log. To do so, go to MENU->Tamper->ERASE. This erases the tamper log.
Since only a page (1 Kbyte) in Flash memory is used to store timestamps, a maximum of 64 timestamps can be stored. Whenever the 65 th
tamper occurs the memory page is erased.
The LCD screen cannot display more than 10 events at a time. The difference between timestamps created by pressing the tamper switch and battery tamper timestamps is that the former type of timestamps shows a “seconds” field, whereas the others show “BI” or
“BO” (to mean “battery in” and “battery out”, respectively).
Doc ID 14949 Rev 2 19/28
Running the demonstrator
Note: The TAMPER-RTC pin functionality is not available:
●
●
●
●
●
when the user is navigating through the MENU options when user is viewing the tamper log during the calibration process when the microprocessor is in a low-power mode when the main V
DD
supply is off
AN2821
Note:
6.5
To calibrate the external oscillator in manual mode, do as follows:
● Go to MENU -> CALIBRATE-> MANUAL. A message appears on the LCD screen:
“LSE/64 is available on the PC13/TAMPER-RTC pin. Measure the frequency and press
Sel to proceed”.
The frequency output on the TAMPER-RTC pin can be measured by using a highprecision frequency counter. The PC13 pad is available on the STM3210B-EVAL board. (Refer to
Figure 9: STM3210B-EVAL board description
.)
●
Follow the steps of the procedure illustrated in
to calculate the error in ppm. On the basis of this error, the calibration value can be inferred from the
.
Once this is done, press Sel. You will be prompted to enter the calibration value. To do so, use the joystick up/down and left/right buttons, then press Sel again. If the entered value is within the range (0-121), it is stored into the RTC clock/calibration register and the application returns to the home screen. Otherwise, the value is invalid. This generates an error message and the application returns to the home screen without modifying the value in the RTC clock/calibration register.
To calibrate the external oscillator in auto-calibration mode, do as follows:
● First, connect a wire link from PC13 (RTC clock output on the TAMPER-RTC pin) to
PA1 (timer input). Both of these pads are available on the STM3210B-EVAL board.
(Refer to
Figure 9: STM3210B-EVAL board description
●
●
Select MENU->CALIBRATE->AUTO: the calibration value is automatically calculated and fed into the calibration register. A confirmation message will appear on the LCD screen.
After the calibration process, the application returns to normal clock operation.
Clock source selection
By default, on startup the clock runs at the external crystal oscillator frequency of
32.768 kHz. This frequency is provided by the clock source of the STM3210B-EVAL board.
An external clock source can also be used to provide the time base for the RTC.
shows how to realize the RTC time base circuit.
20/28 Doc ID 14949 Rev 2
AN2821 Running the demonstrator
Figure 10.
RTC time base connection
2 jumpers for source selection, to GPIO
50/60 Hz in
Resistive divider
Source switch
OSC in
OSC out
STM32F10xxx display (part of main system) switches/joystick, to set the time/date
(part of main system)
32 kHz crystal
Jumper for anti-tamper demonstration, optional
Backup battery
(part of main system) ai15444
In this application note, the clock source selection is demonstrated by emulating an external
50/60 Hz sinewave (as can be derived from the utility mains). The external 50/60 Hz can be derived from a function generator and applied to the oscillator input using a connector probe
Figure 11: Clock source selection
Caution:
Make sure that the amplitude of the applied clock signal is less than 3 V peak-to-peak and that the corresponding DC offset is about half the full-scale value.
Note: Since the time base selection is a one-time setting, the clock is initialized every time the time base is changed.
To select an external 50 Hz clock source:
●
●
Apply the external clock to the oscillator input. The clock will seem to stop running.
Go to MENU->50Hz and press the joystick Sel button. This sets the prescaler accordingly and the clock restarts from 09:24:00.
To return to the 32.768 kHz clock source selection:
●
●
Go to MENU->32768Hz and press the joystick Sel button
Remove the external clock source. The clock restarts from 09:24:00.
To select an external 60 Hz source:
●
●
Apply the external clock to the oscillator input
Go to MENU->60Hz and press the joystick Sel button. this sets the prescaler accordingly and the clock restarts from 09:24:00.
To return to the 32.768 kHz clock source selection:
●
●
Go to MENU->32768Hz and press the joystick Sel button
Remove the external clock source. The clock restarts from 09:24:00.
Doc ID 14949 Rev 2 21/28
Running the demonstrator
Figure 11.
Clock source selection
AN2821
22/28 Doc ID 14949 Rev 2
AN2821
Appendix A Calibration table
Calibration table
gives a list of calibration values.
21
22
23
18
19
20
13
14
15
10
11
12
16
17
27
28
29
24
25
26
7
8
9
4
5
6
0
1
2
3
Table 1.
Calibration lookup table
Calibration rounded to the value
Value in ppm nearest ppm
Value in seconds per month (30 days) rounded to the nearest second
Calibration rounded to the value
Value in ppm nearest ppm
Value in seconds per month (30 days) rounded to the nearest second
19
21
22
17
18
19
12
13
14
10
10
11
15
16
26
27
28
23
24
25
7
8
9
4
5
6
0
1
2
3
49
54
57
44
47
49
32
35
37
25
27
30
40
42
67
69
72
59
62
64
17
20
22
10
12
15
0
2
5
7
81
82
83
78
79
80
73
74
75
71
72
72
76
77
87
88
89
84
85
86
68
69
70
65
66
67
61
62
63
64
85
86
87
82
83
84
77
78
79
74
75
76
80
81
91
92
93
88
89
90
71
72
73
68
69
70
64
65
66
67
203
205
208
210
213
215
183
185
188
190
193
195
198
200
218
220
222
225
227
230
168
171
173
176
178
180
158
161
163
166
Doc ID 14949 Rev 2 23/28
Calibration table AN2821
46
47
48
49
50
43
44
45
40
41
42
37
38
39
34
35
36
30
31
32
33
60
61
62
57
58
59
63
54
55
56
51
52
53
Table 1.
Calibration lookup table (continued)
Calibration rounded to the value
Value in ppm nearest ppm
Value in seconds per month (30 days) rounded to the nearest second
Calibration rounded to the value
Value in ppm nearest ppm
Value in seconds per month (30 days) rounded to the nearest second
44
45
46
47
48
41
42
43
38
39
40
35
36
37
32
33
34
29
30
31
31
27
58
59
54
55
56
60
51
52
53
49
50
51
114
116
119
121
124
99
101
104
106
109
111
91
94
96
84
87
89
74
77
79
82
141
143
146
148
151
153
156
126
129
131
133
136
138
105
106
107
108
109
99
100
101
102
103
104
96
97
98
93
94
65
90
91
92
93
115
116
117
118
119
120
121
101
111
112
113
113
114
110
111
112
113
114
104
105
106
107
108
109
98
99
100
101
102
103
94
95
96
97
121
122
123
124
125
126
127
115
116
117
118
119
120
272
274
277
279
282
257
260
262
264
267
269
242
245
247
250
252
255
232
235
237
240
299
302
304
307
309
311
314
284
287
28
292
294
297
24/28 Doc ID 14949 Rev 2
AN2821 Additional notes on calibration and clock accuracy
Appendix B Additional notes on calibration and clock accuracy
The purpose of this appendix is to provide an effective calibration and drift calculation scheme by a mathematical approach.
It is easy to verify the actual (observed) result using the theoretical value obtained. The procedure first consists in calculating how much the clock drifts ahead (due to crystal frequency variation) in a month when not calibrated. This is the amount that the clock must
“lose” to keep time correctly.
Next, the procedure consists in calculating how much the clock actually loses as a result of calibration.
Example of calculation
To implement this method, switch to calibration mode, and measure the frequency output on the TAMPER-RTC pin using a suitable and very accurate frequency counter. Let F1 be the result obtained with this counter. Let us assume that F1 = 512.0193 Hz.
Let F2 be the corresponding effective LSE frequency.
F2 = 64 × F1 = 64 × 512.0193 Hz = 32769.2352 Hz.
Let us put dF = F0 – F2, where F0 = 32766 in our case, because the RTC prescalor is set to
32766 and not 32768. This means that we get a tick every 32766 oscillator cycles.
Thus, dF = 32766 – 32769.2352 = –3.2352
Let us now calculate the time drift or deviation generated by crystal variations only. It is given by: dT =
F0 +
F0 dF
–
1 s
-----------------------------------
1 s that is: dT =
32762.7648
–
1 s
= (1.00009875) – 1 = 0.00009875 s = 98.75 µs
1 s
So over 1 second, the positive time drift is of 98.75 µs.
Thus, over 1 month the drift becomes: 0.00009875 s × 60 × 60 × 24 × 30 = 255.95 s.
Let T1 be this positive time drift.
Therefore, if we want to compensate for this time drift, we must, as a result of the calibration process, lose T1.
Doc ID 14949 Rev 2 25/28
Additional notes on calibration and clock accuracy
Note:
AN2821
Now let us look into the calibration process:
Error in ppm
=
512.0193
–
64
64
1000000
=
100.2 ppm
Referring to the calibration lookup table, the calibration value that corresponds to an error of
100.2 ppm is 105, which amounts to a compensation (subtraction) of 260 seconds. Let T2 be this compensation time.
(STM32 digital calibration subtracts [Calibration value] every 2
20
oscillator cycles. That is: number of seconds lost in 1 month =
32766
60
60
24
30
105
2
20
-------------------------------------------------------------------------------------
32766
260 seconds
This means that the clock will run slower by (T2 – T1), that is 4 seconds at the end of a month. Thus, ideally, an average drift of 4/30 (about 0.133) second a day is achievable.
You can refer to a precise and accurate web clock to synchronize your clock, for instance the
US Naval Observatory Master Clock (http://www.usno.navy.mil/).
Temperature effects and measuring instrument accuracy are not taken into account in the above calculations.
26/28 Doc ID 14949 Rev 2
AN2821 Revision history
Table 2.
Date
23-Dec-2008
30-Apr-2009
Document revision history
Revision
1
2
Changes
Initial release.
References to the STM32F10xxx firmware library updated to
STM32F10xxx standard peripheral library.
Systick_Config
updated to SystickConfig.
Doc ID 14949 Rev 2 27/28
AN2821
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
© 2009 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan -
Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
28/28 Doc ID 14949 Rev 2
advertisement
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Related manuals
advertisement
Table of contents
- 6 Overview of the medium-density STM32F10xxx backup domain
- 6 Main backup domain features
- 7 Main RTC features
- 8 Configuring the RTC registers
- 9 Clock/calendar functionality features
- 9 Clock/calendar basics
- 11 Summer time correction
- 11 Clock source selection
- 11 Calibration
- 13 Low-power modes
- 13 Stop mode
- 14 Standby mode
- 15 Tamper detection and time stamping
- 15 TAMPER-RTC pin functionality
- 15 Tamper timestamping
- 15 Battery tamper timestamping
- 16 Firmware description
- 17 Hardware description
- 18 Running the demonstrator
- 18 Clock/calendar
- 18 Setting the time
- 18 Setting the alarm
- 18 Setting the date
- 18 Summer time correction
- 18 Low-power demonstration
- 18 Stop mode
- 19 Standby mode