您的当前位置:首页正文

时钟&日历

2022-10-16 来源:步旅网
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 VDD 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 2009Doc ID 14949 Rev 21/28

www.st.com

ContentsAN2821

Contents

1

Overview of the medium-density STM32F10xxx backup domain . . . . . 6

1.11.2

Main backup domain features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Main RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

23

Configuring the RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Clock/calendar functionality features . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.1

Clock/calendar basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.1.13.1.23.1.3

Implementing the clock function on the medium-density STM32F10xxx . 9Implementing calendar function on the medium-density STM32F10xxx . 9Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.23.33.4

Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.4.13.4.2

Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Standby mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.5Tamper detection and time stamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3.5.13.5.23.5.3

TAMPER-RTC pin functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Battery tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

456

Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Running the demonstrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.1

Clock/calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.1.16.1.26.1.36.1.4

Setting the time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Setting the alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Setting the date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.2Low-power demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.2.16.2.2

Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Standby mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2/28 Doc ID 14949 Rev 2

AN2821Contents

6.3Timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.3.16.3.2

Tamper events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Battery removal and installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.46.5

Clock calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

6.5.16.5.2

50 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2160 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Appendix ACalibration table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Appendix BAdditional notes on calibration and clock accuracy . . . . . . . . . . . 257

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Doc ID 14949 Rev 23/28

List of tablesAN2821

List of tables

Table 1.Table 2.

Calibration lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4/28 Doc ID 14949 Rev 2

AN2821List 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.

STM3210B-EVAL board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1RTC block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Time system flowchart(1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Calendar algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Leap year flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Calibration process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Stop mode entry sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Standby mode entry sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14STM3210B-EVAL board description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17RTC time base connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Doc ID 14949 Rev 25/28

Overview of the medium-density STM32F10xxx backup domainAN2821

1

Overview of the medium-density STM32F10xxx backup domain

Figure2 shows the RTC block diagram.Figure 2.RTC block diagramHSE OSCLSI RCRTC alarm=RTC counterLSE OSC/ EXT CLOCKRTC prescaler1/128frtcRTC dividerRTCSel[1:0]Backup registersRTC control registerAlarmOverflowSecondITITITai154371.1 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 VBAT when the VDD main power is switched off.

is not reset due to system reset, power reset or when the microcontroller wakes up from the Standby mode.

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

AN2821Overview of the medium-density STM32F10xxx backup domain

1.2 Main RTC features

The RTC:

has three clock sources:–––

LSE (low-speed external) clock that can also be bypassed by an external clockLSI (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:–––

secondoverflow

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.

Please refer to Figure2 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.

Note:

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 7pF. Never use a crystal with a load capacitance of 12.5pF.

Doc ID 14949 Rev 27/28

Configuring the RTC registersAN2821

2 Configuring the RTC registers

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 registerEnable 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:fTRCLK = fRTCCLK/(PRL[19:0]+1), where:––

fRTCCLK is the input clock frequency

fTRCLK 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

AN2821Clock/calendar functionality features

3 Clock/calendar functionality features

3.1 Clock/calendar basics

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.

3.1.1 Implementing the clock function on the medium-density STM32F10xxx

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 Figure3 (“/” implies division, “%” implies modulus)Figure 3.Time system flowchart(1) Read RTC counter value(TimeVar)Hours = TimeVar 3600Minutes = TimeVar % 3600 60Seconds =(TimeVar % 3600) % 60ai154381.% is for modulo.

3.1.2

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 29/28

Clock/calendar functionality featuresAN2821

the system remains at the current date even after system reset, power reset or wakeup from the Standby mode.

The calendar algorithm given in Figure4 can be used to develop the date update function.Figure 4.NoCalendar algorithmMonth has31 days ?YesDay < 31?NoMonth = 12?YesMonth=1Day=1Year++NoMonth++Day=1YesDay++Month = 2?YesDay < 28?NoDay = = 28?YesIs it aleap year?NoMonth++Day=1YesDay++NoYesDay++Month has30 days ?YesDay < 31?NoMonth++Day=1NoYesDay++Day = = 29?YesMonth++Day=1ai15439Leap year correction: each time the date is updated, the possibility of a leap year also has to be considered (please refer to Figure5).Figure 5.Leap year flowchartYear divisibleby 400?NoYear divisibleby 100?NoYear divisibleby 4?YesIt is a leap yearYesNot a leap yearYesIt is a leap yearNoNot a leap yearai1544010/28 Doc ID 14949 Rev 2

AN2821Caution:

Clock/calendar functionality features

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.1.3 Summer time correction

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.

3.2 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:

fTRCLK = fRTCCLK/(PRL[19:0]+1), where:

●●

fTRCLK is the derived time base

fRTCCLK 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 220 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 featuresAN2821

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 Figure6, is the following:

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 Appendix A.)

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:12

When the RTC clock/64 is output on the TAMPER-RTC pin, the tamper detection functionality is not available.

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 processOutput RTC clock/64on TAMPER-RTC pinCalculate deviationfrom 511.968 HzCalculate error in ppm(deviation/511.968)× 1 millionFind nearestcalibration valueLoad the value intocalibration registerai1544112/28 Doc ID 14949 Rev 2

AN2821Clock/calendar functionality features

3.4 Low-power modes

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.

3.4.1 Stop mode

In the Stop mode, the:

●●●●●

CPU clock is off

peripheral clocks, PLL, HSI, HSE are disabledSRAM and register contents are preservedRTC 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 Figure7 and described below:1.

Select the regulator state in Stop mode through the LPDS bit in the PWR_CR register:––2. 3. 4.

LPDS = ‘0’, the regulator is ON

LPDS = ‘1’, the regulator is in low-power mode

Reset the PDDS bit in PWR_CR

Set the SLEEPDEEP bit in the Cortex-M3 system control register

Request Wait for interrupt or Wait for event depending on how the Stop mode is entered.

Stop mode entry sequenceSelect the regulatorstate in Stop modeFigure 7.Reset PDDSSet the SLEEPDEEPbit in the Cortex-M3system control registerRequest _WFI()or _WFE()ai15442Doc ID 14949 Rev 213/28

Clock/calendar functionality featuresAN2821

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.

3.4.2 Standby mode

In the Standby mode, the:

●●●●●

CPU clock is off

SRAM and register contents are lost except for the backup domainRTC and IWDG keep runningentire 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 Figure8 and described below:1.2. 3. 4. 5.

Clear the WAKEUP flag in the PWR_CSR register.

Enable the WAKEUP pin functionality by setting the EWUP bit in the PWR_CSR register

Set the PDDS bit in PWR_CR

Set the SLEEPDEEP bit in the cortex system control registerRequest Wait for interrupt

Standby mode entry sequenceClear the WAKEUP flagin PWR_CSRFigure 8.Enable the WAKEUPpinSet the PDDS bitSet the SLEEPDEEPbit in the Cortex-M3system control registerRequest _WFI()ai15443The 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

AN2821Clock/calendar functionality features

3.5 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.

3.5.1 TAMPER-RTC pin functionality

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.2. 3.

Set the TAMPER-RTC pin level by configuring the TPAL bit in the backup control register

Enable the TAMPER-RTC pin interrupt by setting the TPIE bit in the backup control/status register (BKP_CSR)

Enable the TAMPER-RTC pin by setting the TPE bit in the backup control register (BKP_CR)

3.5.2 Tamper timestamping

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.

3.5.3 Battery tamper timestamping

Events like battery removal or battery installation can also be timestamped and stored into the emulated EEPROM. For this purpose, the VBAT 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 VBAT 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 215/28

Firmware descriptionAN2821

4 Firmware description

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.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.

●●●●●

User source files:

●●●

●●

16/28 Doc ID 14949 Rev 2

AN2821Hardware description

5 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)JoystickTamper switchWakeup switch

PA1, PC7, PC8, PC13 pads

STM3210B-EVAL board description

Figure 9.

Doc ID 14949 Rev 217/28

Running the demonstratorAN2821

6 Running the demonstrator

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.

6.1.1 Setting the time

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.

6.1.2 Setting the alarm

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 Figure9: 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.

6.1.3 Setting the date

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.

6.1.4 Summer time correction

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, 1st April, 2008, you can verify the time correction for October 2008 and beyond, but not earlier.

6.2 Low-power demonstration

6.2.1 Stop mode

To enter the Stop mode, go to MENU->LowPower->STOP and press Sel. The LD4 LED

(see Figure9: STM3210B-EVAL board description) lights up and the MCU enters the Stop

18/28 Doc ID 14949 Rev 2

AN2821Running 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.

6.2.2 Standby mode

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 Figure9: 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.1 Tamper events

To trigger a tamper event, press the B4 tamper switch on the STM3210B-EVAL board (see

Figure9: STM3210B-EVAL board description).

6.3.2 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 Figure9: 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 65th 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 219/28

Running the demonstratorNote:

The TAMPER-RTC pin functionality is not available:

●●●●●AN2821

when the user is navigating through the MENU optionswhen user is viewing the tamper logduring the calibration process

when the microprocessor is in a low-power modewhen the main VDD supply is off

6.4 Clock calibration

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 high-precision frequency counter. The PC13 pad is available on the STM3210B-EVAL board. (Refer to Figure9: STM3210B-EVAL board description.)

Follow the steps of the procedure illustrated in Figure6: Calibration process to calculate the error in ppm. On the basis of this error, the calibration value can be inferred from the Calibration lookup table in Appendix A.

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.

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 Figure9: 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.

To calibrate the external oscillator in auto-calibration mode, do as follows:

Note:

For more details, please refer to Appendix B: Additional notes on calibration and clock accuracy on page25.

6.5 Clock source selection

By default, on startup the clock runs at the external crystal oscillator frequency of

32.768kHz. 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. Figure10 shows how to realize the RTC time base circuit.

20/28 Doc ID 14949 Rev 2

AN2821

Figure 10.RTC time base connection2 jumpers for sourceselection, to GPIORunning the demonstrator

50/60 Hz inSourceswitchdisplay (part of mainsystem)OSC inOSC outSTM32F10xxxResistivedivider32 kHz crystalswitches/joystick,to set the time/date(part of main system)Jumper for anti-tamperdemonstration,optionalBackup battery(part of main system)ai15444In this application note, the clock source selection is demonstrated by emulating an external 50/60Hz sinewave (as can be derived from the utility mains). The external 50/60Hz can be derived from a function generator and applied to the oscillator input using a connector probe (see Figure11: Clock source selection).

Caution:Note:

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.

Since the time base selection is a one-time setting, the clock is initialized every time the time base is changed.

6.5.1 50 Hz time base selection

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.Go to MENU->32768Hz and press the joystick Sel button

Remove the external clock source. The clock restarts from 09:24:00.

To return to the 32.768 kHz clock source selection:

●●

6.5.2 60 Hz time base selection

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.Go to MENU->32768Hz and press the joystick Sel button

Remove the external clock source. The clock restarts from 09:24:00.

To return to the 32.768 kHz clock source selection:

●●

Doc ID 14949 Rev 221/28

Running the demonstrator

Figure 11.Clock source selection

AN2821

22/28 Doc ID 14949 Rev 2

AN2821Calibration table

Appendix A Calibration table

Table1 gives a list of calibration values.

Table 1.

Calibration lookup table

Value in ppm Value in seconds per Value in ppm Value in seconds per

Calibration Calibration rounded to the month (30 days) rounded rounded to the month (30 days) rounded

valuevalue

nearest ppmto the nearest secondnearest ppmto the nearest second01234567891011121314151617181920212223242526

01234567891010111213141516171819192122232425262728

02571012151720222527303235374042444749495457596264676972

646566676869707172737475767778798081828384858687888990919293

616263646566676869707172727374757677787980818283848586878889

158161163166168171173176178180183185188190193195198200203205208210213215218220222225227230

272829

Doc ID 14949 Rev 223/28

Calibration tableTable 1.

Calibration lookup table (continued)

AN2821

Value in ppm Value in seconds per Value in ppm Value in seconds per

Calibration Calibration rounded to the month (30 days) rounded rounded to the month (30 days) rounded

valuevalue

nearest ppmto the nearest secondnearest ppmto the nearest second303132333435363738394041424344454647484950515253545556575859

29303131323334353637383940414243444546474849505151525354555627585960

7477798284878991949699101104106109111114116119121124126129131133136138141143146148151153156

949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127

9091929393946596979899100101102103104105106107108109101111112113113114115116117118119120121

23223523724024224524725025225525726026226426726927227427727928228428728292294297299302304307309311314

60616263

24/28 Doc ID 14949 Rev 2

AN2821Additional 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:

---------1-----------F0+--------------dF------–1 sF0

dT=----------------------------------1 s

that is:

1-------------------------------32762.7648

-------------------------------–1 s32766

dT=--------------------------------------------- = (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 225/28

Additional notes on calibration and clock accuracy

Now let us look into the calibration process:

AN2821

512.0193–32766----------------64

Error in ppm=-----------------------------------------------1000000=100.2 ppm

32766----------------64

Referring to the calibration lookup table, the calibration value that corresponds to an error of 100.2ppm is 105, which amounts to a compensation (subtraction) of 260 seconds. Let T2 be this compensation time.

(STM32 digital calibration subtracts [Calibration value] every 220 oscillator cycles. That is:

32766-----------------------60-------------60-------------24-------------30------105

202

number of seconds lost in 1 month=------------------------------------------------------------------------------------260 seconds

32766

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/).

Note:

Temperature effects and measuring instrument accuracy are not taken into account in the above calculations.

26/28 Doc ID 14949 Rev 2

AN2821Revision history

7 Revision history

Table 2.

Date23-Dec-200830-Apr-2009

Document revision history

Revision

12

Initial release.

References to the STM32F10xxx firmware library updated to

STM32F10xxx standard peripheral library.

Systick_Config updated to SystickConfig.

Changes

Doc ID 14949 Rev 227/28

AN2821

Please Read Carefully:

Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve theright to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at anytime, 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 noliability 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 thisdocument 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 productsor services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of suchthird 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 IMPLIEDWARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIEDWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWSOF 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 NOTRECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAININGAPPLICATIONS, 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 \"AUTOMOTIVEGRADE\" 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 voidany warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, anyliability 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

因篇幅问题不能全部显示,请点此查看更多更全内容