STM32 Eeprom PDF
STM32 Eeprom PDF
STM32 Eeprom PDF
Application note
EEPROM emulation
in STM32F10x microcontrollers
Introduction
Many applications require EEPROM (electrically erasable programmable read-only
memory) for non-volatile data storage. For low-cost purposes, the STM32F10x devices do
not use EEPROM. Instead, they implement EEPROM emulation using the embedded Flash
memory.
This application note explains the differences between external EEPROM and embedded
Flash memory, and it describes a software method for emulating EEPROM using the onchip Flash memory of the STM32F10x devices.
This document also focuses on some embedded aspects in emulated EEPROM data
storage, that the reader is assumed to know.
Glossary
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F10x and STM32F103xx microcontrollers where the
Flash memory density ranges between 32 and 128 Kbytes.
High-density devices are STM32F10x and STM32F103xx microcontrollers where the Flash
memory density ranges between 256 and 512 Kbytes.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
August 2009
1/10
www.st.com
Contents
AN2594
Contents
1
1.2
1.3
Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.2
3.2
3.1.2
2/16
3.3
3.4
Cycling capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
AN2594
List of tables
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
3/16
List of figures
AN2594
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
4/16
AN2594
Feature
1.1
External EEPROM
Write time
a few ms
random byte: 5 to 10 ms
page: a hundred s per word (5 to
10 ms per page)
Erase time
N/A
Write method
serial: a hundred s
Read access random word: 92 s
page: 22.5 s per byte
parallel: a hundred ns
very few CPU cycles per word.
Access time: 35 ns
Write/Erase
cycles
5/10
1.2
AN2594
1.3
Standalone external EEPROM: once started by the CPU, the writing of a word cannot
be interrupted by a CPU reset. Only supply failure will interrupt the write process, so
properly sizing the decoupling capacitors can secure the complete writing process
inside a standalone EEPROM.
Emulated EEPROM using an embedded Flash memory: once started by the CPU, the
write process can be interrupted by a power failure and by a CPU reset. This difference
should be analyzed by system designers to understand the possible impact(s) on their
applications and to determine a proper handling method.
6/10
AN2594
2.1
Principle
EEPROM emulation is performed in various ways by considering the Flash memory
limitations and product requirements. The approach detailed below requires at least two
Flash memory pages of identical size allocated to non-volatile data. One that is initially
erased, and offers byte-by-byte programmability; the other that is ready to take over when
the former page needs to be garbage-collected. A header field that occupies the first 16-bit
half word of each page indicates the page status.
The header field is located at the base address of each page and gives the page status
information.
Each page has three possible states:
RECEIVE_DATA: the page is receiving data from the other full page.
VALID_PAGE: the page contains valid data and this state does not change until all
valid data are completely transferred to the erased page.
Figure 1 shows how the page statuses change with respect to each other.
Figure 1.
Page0 Valid
Page1 Erased
Page0 Full
Page0 Valid
Page1 Receive
Erase Page0
Page0 Erased
Page1 Valid
Page1 Full
Page0 Receive
Page1 Valid
Erase Page1
ai14606
Generally, when using this method, the user does not know in advance the variable update
frequency.
The software and implementation described in this document use two Flash memory pages
to emulate EEPROM.
Each variable element is defined by a virtual address and a value to be stored in Flash
memory for subsequent retrieval or update (in the implemented software both virtual
address and data are 16 bits long). When data is modified, the modified data associated
with the earlier virtual address is stored into a new Flash memory location. Data retrieval
returns the modified data in the latest Flash memory location.
7/10
AN2594
page1
ai14608b
2.1.1
Application example
The following example shows the software management of three EEPROM variables (Var1,
Var2 and Var3) with the following virtual addresses:
Var1: 5555h, Var2: 6666h and Var3: 7777h
Figure 3.
Page1
FF FF
FF FF
FF FF
FF FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
Page0
12 32
77
FF
Add Var3 FF
FF
=1232h
FF
FF
FF
FF FF FF FF
Active Page = Page0
77
FF
FF
FF
FF
FF
FF
Page0
12 32
77 77
12 45
Page1
FF FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
Add Var3
=1245h
FF FF FF FF
Active Page = Page0
77 77
FF FF
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Add Var1
=BCBCh
Page0
12 32
77 77
12 45
77 77
BC BC
55 55
FF FF
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF FF FF
Active Page = Page0
FF FF FF FF
Active Page = Page0
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
66 66
FF FF
FF FF FF FF
Active Page = Page0
Page0
12 32
77 77
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
32 32
77 77
22 45
66 66
BD BD
55 55
54 54
77 77 FF FF
Active Page = Page0
Page0
12 32
77 77
Page1
64 64
66 66
54 54
77 77
BD BD
55 55
FF FF
FF FF
32 32
77 77
22 45
66 66
BD BD
55 55
54 54
77 77 FF FF
Active Page = Page1
Add Var2
=6464h
Page0
FF FF
FF FF
Erase
Page0
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
FF
Page1
64 64
66 66
54 54
77 77
BD BD
55 55
FF FF
FF FF
FF FF FF FF
Active Page = Page1
ai14609
2.1.2
8/10
AN2594
main.c: this application program is an example using the described routines in order to
write to and read from the EEPROM.
EE_Init()
Sector header corruption is possible in the event of power loss during data update or
sector erase / transfer. In this case, the EE_Init() function will attempt to restore the
database to a known good state. This function should be called prior to accessing the
database after each power-down. It accepts no parameters. The process is described
in Table 2.
EE_Format()
This function erases page0 and page1 and writes a VALID_PAGE header to page0.
EE_FindValidPage()
This function reads both page headers and returns the valid page number. The passed
parameter indicates if the valid page is sought for a write or read operation
(READ_FROM_VALID_PAGE or WRITE_IN_VALID_PAGE).
EE_VerifyPageFullWriteVariable()
It implements the write process that must either update or create the first instance of a
variable. It consists in finding the first empty location on the active page, starting from
the end, and filling it with the passed virtual address and data of the variable. In the
case the active page is full, the PAGE_FULL value is returned. This routine uses the
parameters below:
Virtual address: may be any of the three declared variables virtual addresses
(Var1, Var2 or Var3)
EE_ReadVariable()
This function returns the data corresponding to the virtual address passed as a
parameter. Only the last update is read. The function enters in a loop in which it reads
the variable entries until the last one. If no occurrence of the variable is found, the
ReadStatus variable is returned with the value 1, otherwise it is reset to indicate that
9/10
AN2594
the variable has been found and the variable value is returned on the Read_data
variable.
EE_PageTransfer()
It transfers the most recent data (last variable updates) from a full page to an empty
one. At the beginning, it determines the active page, which is the page the data is to be
transferred from. The new page header field is defined and written (new page status is
RECEIVE_DATA given that it is in the process of receiving data). When the data
transfer is complete, the new page header is VALID_PAGE, the old page is erased and
its header becomes ERASED.
EE_WriteVariable(..)
This function is called by the user application to update a variable. It uses the
EE_VerifyPageFullWriteVariable(), and EE_PageTransfer() routines that
have already been described.
Figure 4 shows the procedure for updating a variable entry in the EEPROM.
Figure 4.
WriteVariable flowchart
Function call
EE_PageTransfer()
current
active page
full
No
Add new element at
the 1st empty element
place in the current
active page
EE_ReadVariable()
Erase previous active page
End
Key features
10/10
ai14610b
AN2594
3.1
3.1.1
3.1.2
3.2
3.2.1
11/10
AN2594
Page3 to be full, the device goes back to Page0, Page3 is garbage-collected then erased
and so on (refer to Figure 5).
Figure 5.
32
Page1
Page2
Page3
FF FF
FF
FF
FF
FF
55
55
FF FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
FF
FF
FF FF
FF
FF
FF
FF
Active Page
Erased
Erased
Erased
ai14611
3.3
12/10
AN2594
Page1
ERASED
ERASED
Invalid state
Erase both pages
and format page0
VALID_PAGE
3.4
RECEIVE_DATA
VALID_PAGE
Invalid state
Erase both pages and
format page0
Cycling capability
A program/erase cycle consists of one or more write accesses and one page erase
operation.
When the EEPROM technology is used, each byte can be programmed and erased a finite
number of times, typically in the range of 10 000 to 100 000.
However, in embedded Flash memory, the minimum erase size is the page and the number
of program/erase cycles applied to a page is the number of possible erase cycles. The
STM32F10xxxs electrical characteristics guarantee 10 000 program/erase cycles per page.
The maximum lifetime of the emulated EEPROM is thereby limited by the update rate of the
most frequently written parameter.
The cycling capability is dependent of the amount/size of data that the user wants to handle.
In this example, two pages (of 1 Kbyte for Medium-density devices or 2 Kbyte for Highdensity and Connectivity line devices) are used and programmed with 16-bit data. To each
variable corresponds a 16-bit virtual address. That is, each variable occupies a word of
storage space. A page can store 1 Kbyte (for Medium-density devices) or 2 Kbyte (for Highdensity and Connectivity line devices) multiplied by the Flash memory endurance of 10 000
cycles gives a total of 10 000 Kbytes (for Medium-density devices) or 20 000 Kbytes (for
High-density and Connectivity line devices) of data storage capacity for the lifetime of one
page in the emulated Flash memory. Consequently, 20 000 Kbytes (for Medium-density
devices) or 40 000 Kbytes (for High-density and Connectivity line devices) can be stored in
the emulated EEPROM provided that two pages are used in the emulation process. If more
than two pages are used, this number is multiplied accordingly.
Knowing the data width of a stored variable, it is possible to calculate the total number of
variables that can be stored in the emulated EEPROM area during its lifetime.
Table 3 gives an idea of the number of variables that can be stored in the emulated
EEPROM according to the variable virtual address and data sizes.
13/10
AN2594
2 1 Kbyte pages
2 2 Kbyte pages
Unit
10 000 (210 2)
10 000 (211 2)
Variable
10
5000 (2
10
2500 (2
4)
8)
11
4)
Variable
11
8)
Variable
5000 (2
2500 (2
1. These maximum numbers of variables do not include their corresponding virtual address.
2. The value subtracted from the maximum number of variables that can be written using two pages,
corresponds to the page status located at the top of the page. Depending on the variable granularity and,
to preserve the alignment, some empty bytes are added to the page status. These bytes are also
subtracted from this maximum number.
14/10
AN2594
Revision history
Revision history
Table 4.
Date
Revision
Changes
05-Oct-2007
Initial release.
24-Jun-2008
04-Aug-2009
15/10
AN2594
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 STs 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 STS 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 USERS 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.
16/16