Using the STM32 hardware real-time clock (RTC).pdf
(
318 KB
)
Pobierz
Using the STM32 hardware real-time clock (RTC)
AN3371
Application note
Using the STM32 hardware real-time clock (RTC)
Introduction
A real-time clock (RTC) is a computer clock that keeps track of the current time. Although
RTCs are often used in personal computers, servers and embedded systems, they are also
present in almost any electronic device that requires accurate time keeping. Microcontrollers
supporting RTC can be used for chronometers, alarm clocks, watches, small electronic
agendas, and many other devices.
This application note describes the features of the real-time clock (RTC) controller
embedded in
Ultra Low Power Medium-density
and
F-2 series
devices microcontrollers,
and the steps required to configure the RTC for use with the calendar, alarm, periodic
wakeup unit and tamper detection.
Examples are provided with configuration information to enable you to quickly and correctly
configure the RTC for calendar, alarm, periodic wakeup unit, tamper detection, time stamp
and calibration applications.
Note: 1 All examples and explanations are based on the STM32L1xx and STM32F2xx firmware
libraries and reference manuals of STM32L1xx (RM0038) and STM32F2xx (RM0033).
2 STM32 refers to Ultra Low Power Medium-density devices and F-2 series devices
microcontrollers in this document.
3 Ultra Low Power Medium (ULPM) density devices are STM32L151xx and STM32L152xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
4 F-2 series devices are STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx
microcontrollers.
May 2011
Doc ID 018624 Rev 1
1/35
www.st.com
Contents
AN3371
Contents
1
Overview of the STM32 advanced RTC . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1
RTC calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1
Initializing the calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.2
Delivering a 1 Hz signal to the calendar using different clock sources . . 6
1.2
RTC alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.1
Programming the alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.2
Configuring the alarm behavior using the MSKx bits . . . . . . . . . . . . . . . . 9
1.3
RTC periodic wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.1
Programming the Auto-wakeup unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.2
Maximum and minimum RTC wakeup period . . . . . . . . . . . . . . . . . . . . 12
1.4
RTC coarse calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5
RTC reference clock detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6
Time-stamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7
Back up registers and tamper detection . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.8
RTC and low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.9
Alternate function RTC outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.9.1
RTC_CALIB output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.9.2
RTC_ALARM output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.10 RTC security aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.10.1 RTC Register write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.10.2 Enter/Exit initialization mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.10.3 RTC clock synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2
RTC features summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3
RTC firmware driver API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1
Start with RTC driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1.1
Time and Date configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1.2
Alarm configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.3
RTC Wakeup configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.4
Outputs configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.5
Coarse calibration configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.6
TimeStamp configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2/35
Doc ID 018624 Rev 1
AN3371
Contents
3.1.7
Tamper configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.8
Backup data registers configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2
Function groups and description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4
Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Doc ID 018624 Rev 1
3/35
Overview of the STM32 advanced RTC
AN3371
1
Overview of the STM32 advanced RTC
The RTC embedded in STM32 microcontrollers can be used to provide a full-featured
calendar, alarm and periodic wakeup unit, digital calibration and advanced tamper detection.
Refer to
Section 2: RTC features summary
for the complete list of features available on Ultra
Low Power Medium-density and F-2 series devices.
1.1
RTC calendar
A calendar keeps track of the time (hours, minutes and seconds) and date (day, week,
month, year). The STM32 RTC calendar offers several features to help you easily configure
and display the calendar data fields:
●
Calendar with:
– seconds
–mits
– hours in 12-hour or 24-hour format
– day of the week (day)
– day of the month (date)
–mth
– year
●
Calendar in binary-coded decimal (BCD) format
●
Automatic management of 28-, 29- (leap year), 30-, and 31-day months
●
Daylight saving time adjustment programmable by software
Figure 1. RTC calendar fields
DATE
12h or 24h
format
TIME
Date
Week
date
Month
Year
AM
PM
hh
mm
s
RTC_DR
RTC_TR
MS19524V1
1. RCT_DR, RTC_TR are RTC Date and Time registers.
A software calendar can be a software counter (usually 32 bits long) that represents the
number of seconds. Software routines convert the counter value to hours, minutes, day of
the month, day of the week, month and year. This data can be converted to BCD format and
displayed on a standard LCD, which is useful in countries that use the 12-hour format with
4/35
Doc ID 018624 Rev 1
AN3371
Overview of the STM32 advanced RTC
an
AM
/
PM
indicator (see
Figure 2
)
. Conversion routines use significant program memory
space and are CPU-time consuming, which may be critical in certain real-time applications.
When using the STM32 RTC calendar, software conversion routines are no longer needed
because their functions are performed by hardware.
STM32 RTC calendar is provided in BCD format. This avoids binary to BCD software
conversion routines, which use significant program memory space and a CPU-load that may
be critical in certain real-time applications.
Figure 2. Example of calendar display on an LCD
11:15:28 PM
WED NOV 19 2010
1.1.1
Initializing the calendar
Ta b l e 1
describes the steps required to correctly configure the calendar time and date.
Table 1.
Steps to initialize the calendar
Step
What to do
How to do it
Comments
1
Disable the RTC registers Write
protection
Write "0xCA" and then
"0x53" into the
RTC_WPR register
RTC registers can be modified
2
Enter Initialization mode
Set INIT bit to ‘1’ in
RTC_ISR register
The calendar counter is
stopped to allow update
3
Wait for the confirmation of
Initialization mode (clock
synchronization)
Poll INITF bit of in
RTC_ISR until it is set
It takes approximately 2
RTCCLK clock cycles for
medium density devices
4
Program the prescalers register
if needed
Register RTC_PRER:
Write first the
synchronous value and
then write the
asynchronous
By default, the prescalers
register RTC_PRER is
initialized to provide 1Hz to the
Calendar unit when RTCCLK =
32768Hz
5
Load time and date values in
the shadow registers
Set RTC_TR and
RTC_DR registers
6
Configure the time format (12h
or 24h)
Set FMT bit in RTC_CR
register
FMT = 0: 24 hour/day format
FMT = 1: AM/PM hour format
7
Exit Initialization mode
Clear the INIT bit in
RTC_ISR register
The current calendar counter is
automatically loaded and the
counting restarts after 4
RTCCLK clock cycles
8
Enable the RTC Registers
Write Protection
Write "0xFF" into the
RTC_WPR register
RTC Registers can no longer
be modified
Doc ID 018624 Rev 1
5/35
Plik z chomika:
masterkom
Inne pliki z tego folderu:
50896956-ARM-CORTEX-M3.pdf
(10631 KB)
37980850-STM32-Cortex-M3.pdf
(6627 KB)
stm32f10x_stdperiph_lib.zip
(21616 KB)
an2824.zip
(1505 KB)
an2821.zip
(1417 KB)
Inne foldery tego chomika:
# Elektronika
# LCD
-=Arduino=-
► AVR-USB
2007-07-03(1)
Zgłoś jeśli
naruszono regulamin