AN2594
Application note
EEPROM emulation
in STM32F101xx and STM32F103xx microcontrollers
Introduction
Many applications require EEPROM (electrically erasable programmable read-only
memory) for non-volatile data storage. For low-cost purposes, the STM32F101xx and
STM32F103xx 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 on-
chip Flash memory of the STM32F101xx and STM32F103xx devices.
This document also focuses on some embedded aspects in emulated EEPROM data
storage, that the reader is assumed to know.
Glossary
Medium-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 32 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
June 2008
Rev 2
1/16
www.st.com
Contents
Contents
AN2594
1
2
3
Embedded Flash memory versus EEPROM: main differences . . . . . . . 5
Difference in write access time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1
1.2
Difference in writing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Difference in erase time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3
Implementing EEPROM emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.1
2.1.2
EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Embedded application aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1
Programming on a word-by-word basis . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.1
Programming on a byte-by-byte basis . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.2
3.2 Wear leveling: Flash memory endurance improvement . . . . . . . . . . . . . . 11
3.2.1
Wear-leveling implementation example . . . . . . . . . . . . . . . . . . . . . . . . . 11
Page header recovery in case of power loss . . . . . . . . . . . . . . . . . . . . . . 12
Cycling capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3
3.4
4
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2/16
AN2594
List of tables
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Differences between embedded Flash memory and EEPROM . . . . . . . . . . . . . . . . . . . . . . 5
Status combinations and actions to be taken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Maximum number of variables stored in emulated EEPROM (with 10 000 cycles) . . . . . . 14
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3/16
List of figures
List of figures
AN2594
Header status switching between page0 and page1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 1.
EEPROM variable format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 2.
Figure 3.
Data update flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 4. WriteVariable flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Page swap scheme with four pages (wear leveling). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 5.
4/16
AN2594
1
Embedded Flash memory versus EEPROM: main differences
Embedded Flash memory versus EEPROM: main
differences
Electrically erasable and programmable read-only memory (EEPROM) is a key component
of many embedded applications that require non-volatile storage of data updated with a byte
or word granularity during run time.
On the other hand, the microcontrollers used in those systems are each time more often
based on embedded Flash memory. To eliminate components, save silicon space and
reduce system cost, the STM32F10xxx Flash memory may be used instead of EEPROM for
simultaneous code and data storage.
Unlike Flash memory, however, external EEPROM does not require an erase operation to
free up space before data can be rewritten. Hence a special software management is
required to store data into embedded Flash memory.
Obviously the emulation software scheme depends on many factors, including the EEPROM
reliability, the architecture of the Flash memory used, and the product requirements.
The main differences between embedded Flash memory and external serial EEPROM are
generic to any microcontroller that use the same Flash memory technology (it is not specific
to the STM32F10xxx family products). The major differences are summarized in Table 1.
Table 1.
Differences between embedded Flash memory and EEPROM
Feature
External EEPROM
Emulated EEPROM using on-chip Flash
memory
Write time
– a few ms
– random byte: 5 to 10 ms
– page: a hundred µs per word (5 to
10 ms per page)
Word program time: 20 µs
Erase time
N/A
Page/Mass Erase time: 20 ms
Write method
– once started, is not CPU-dependent
– only needs proper supply.
once started, is CPU-dependent: a CPU
reset will stop the write process even if the
supplied power stays within specifications.
Read access
– serial: a hundred µs
– 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
– from 10 kilocycles to 1 000
kilocycles
– from 10 kilocycles to 100 kilocycles (the
use of many on-chip Flash memory pages
is equivalent to increasing the number of
write cycles) see Section 3.4: Cycling
capability
1.1
Difference in write access time
As Flash memories have a shorter write access time, critical parameters can be stored
faster in the emulated EEPROM than in an external serial EEPROM, thereby improving data
storage.
5/16
Embedded Flash memory versus EEPROM: main differences
AN2594
1.2
1.3
Difference in writing method
One of the major differences between external EEPROM and emulated EEPROM for
embedded applications is the writing method.
●
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.
●
Difference in erase time
The difference in erase time is the other major difference between a standalone EEPROM
and emulated EEPROM using embedded Flash memory. Unlike Flash memories,
EEPROMs do not require an erase operation to free up space before writing to them. This
means that some form of software management is required to store data in Flash memory.
Moreover, as the erase process of a block in the Flash memory takes a few milliseconds,
power shut-down and other spurious events that might interrupt the erase process (for
example a reset) should be considered when designing the Flash memory management
software. To design a robust Flash memory management software it is necessary to have a
deep understanding of the Flash memory erase process.
6/16
AN2594
2
2.1
Implementing EEPROM emulation
Implementing EEPROM emulation
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:
●
●
●
ERASED: the page is empty.
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. Header status switching between page0 and page1
Page0 Valid
Page1 Erased
Write Page0 data
Page0 Full
Page0 Valid
Page1 Receive
Copy Page0 data -> Page1
Erase Page0
Page0 Erased
Page1 Valid
Write Page1 data
Page1 Full
Page0 Receive
Page1 Valid
Copy Page1 data -> Page0
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/16
Implementing EEPROM emulation
AN2594
Figure 2. EEPROM variable format
Variable data (16 bits)
Variable virtual address (16 bits)
EEPROM variable element = 32-bit word
256 elements (1 Kbyte page)
for Medium-density devices
or
512 elements (2 Kbyte page)
for High-density devices
page0
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. Data update flow
Page0
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Add Var3
=1232h
Page0
12 32
77 77
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Add Var3
=1245h
Page0
12 32
77 77
12 45
77 77
FF FF
FF FF
FF FF
FF FF
Page1
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
FF FF
Page1
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
FF FF
FF FF
Active Page = Page0
FF FF
FF FF
Active Page = Page0
Add var2 =3434h
Page0
12 32
77 77
12 45
77 77
BC BC
55 55
34 34
66 66
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Active Page = Page0
Page0
12 32
77 77
32 32
77 77
22 45
66 66
BD BD
55 55
54 54
77 77
Page1
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
FF FF
Add Var2
=6464h
Page0
12 32
77 77
32 32
77 77
22 45
66 66
BD BD
55 55
54 54
77 77
Page1
64 64
66 66
54 54
77 77
BD BD
55 55
FF FF
FF FF
FF FF
Active Page = Page0
Active Page = Page1
Erase
Page0
Page0
FF FF
FF FF
FF FF
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
Active Page = Page1
2.1.2
EEPROM software description
This section describes the driver implemented for EEPROM emulation using the
STM32F10xxx Flash memory driver provided by STMicroelectronics.
A sample demonstration program is also supplied to demonstrate and test the EEPROM
emulation driver using the three variables Var1, Var2 and Var3 defined in the VirtAddVarTab
table declared in the software main.c file.
ai14609
8/16