AN2540
Application note
EEPROM emulation in
STR91xFxx devices
Introduction
Replacing external EEPROM with emulated EEPROM from the embedded-Flash memory of
the microcontroller is a complex development. This application note is aimed at readers that
are already familiar with the techniques used to secure the content of evolutive information
in the external EEPROM of embedded applications.This application note explains the
differences between external/internal EEPROMs and embedded-Flash memory. It also
gives advice on how to replace external EEPROM with emulated-EEPROM using the
on-chip Flash memory of STR91xFxx devices.
This document also focuses on some embedded aspects in emulated-EEPROM data
storage, that are assumed to be known by the reader.
Overview
Electrically erasable and programmable read-only memory (EEPROM) is a key component
in many embedded applications requiring non-volatile storage of data that are updated at a
byte, half-word or word granularity during run time.
On the other hand, the microcontrollers used in those systems are more and more based on
embedded-Flash memory. To eliminate components, save silicon area and reduce system
cost, the STR91xFxx Flash memory could eventually replace the external EEPROM for
simultaneous code and data storage.
However unlike Flash memory, external EEPROM does not require a block erase operation
to free up space before data can be rewritten. A special software management is required to
store data into Flash memory.
Obviously the emulation software scheme depends on many factors including the EEPROM
reliability, Flash memory architecture and product requirements. Two approaches to
implementation are described in detail in this application note using the on-chip Flash
memory of the STR91xFxx microcontrollers.
April 2013
Rev 2
1/25
www.st.com
Contents
AN2540 - Application note
Contents
1
Embedded Flash memory vs. EEPROM: main differences . . . . . . . . . . 5
1.1
1.2
1.3
Difference in write access time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Difference in writing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Difference in erase time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2
Appropriate solution for Emulated EEPROM in the STR91xFxx . . . . . . 7
2.1
2.2
STR91xFxx on-chip Flash memory features . . . . . . . . . . . . . . . . . . . . . . . 7
STR91xFxx Flash memory library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3
Implementing the EEPROM emulation . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1
3.2
Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1st method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.1
3.2.2
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3
2nd method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3.1
3.3.2
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
EEPROM software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4
Program execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4
Embedded application aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1
Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.1
4.1.2
Programming on a word-by-word basis . . . . . . . . . . . . . . . . . . . . . . . . . 18
Programming on a byte-by-byte basis . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2
4.3
4.4
Wear-leveling: Flash endurance improvement . . . . . . . . . . . . . . . . . . . . . 19
4.2.1
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Sector header recovery in case of power loss . . . . . . . . . . . . . . . . . . . . . 20
Emulated EEPROM parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.4.1
4.4.2
Program/Erase parameter cycling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Program timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5
6
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2/25
AN2540 - Application note
List of tables
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8.
Table 9.
Table 10.
Differences between Embedded Flash memory and EEPROM . . . . . . . . . . . . . . . . . . . . . . 5
EepromFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
FindValidSector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
ReadVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
WriteVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
WriteVerifyVariableFull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
EepromSectorTransfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Status combinations and actions to be taken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Write time related to the current implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3/25
List of figures
AN2540 - Application note
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Header field status switching between sector0 and 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Sector swap scheme: Sector1 erased . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Sector swap scheme: Sector0 erased . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1st method: WriteVariable flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Data storage procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Data update flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2nd method: WriteVariable flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
WriteOnebyte function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Sector swap scheme with four sectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4/25
AN2540 - Application note
Embedded Flash memory vs. EEPROM: main differences
1
Embedded Flash memory vs. EEPROM: main
differences
Before describing the proposed concept for EEPROM emulation, it is important to
remember the main differences between the embedded Flash memory of a microcontroller
and serial external EEPROMs. These differences are the same for any microcontroller (that
is they are not specific to STR91xFxx products). They are summarized in
Table 1.
Table 1.
Feature
Differences between Embedded Flash memory and EEPROM
External EEPROM
A few ms
– random byte: 5 to 10 ms
– page: equivalent to a hundred µs per
word (5 to 10 ms per page)
N/A
Emulated EEPROM using on-chip Flash
memory
Write time
A few µs
(e.g.: 20 µs per 16-bit word)
seconds (e.g.: 1.5 s)
Once started, is CPU-dependent: a CPU
reset will stop the write process even if the
supply stays within specifications.
Parallel: a hundred ns
very few CPU cycles per 16-bit word.
Erase time
Once started, is not CPU-dependent,
Write method
needs only proper supply.
Serial: a hundred µs
Read access random word: 92 µs
page: 22.5 µs/byte
1.1
Difference in write access time
As the Flash memory has a shorter write access time, critical parameters can be stored
faster into the emulated EEPROM than into an external serial EEPROM. The use of the
Flash memory therefore improves the system robustness.
1.2
Difference in writing method
One of the major differences between external 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 a power supply failure can interrupt the writing
process, so properly sizing the decoupling capacitors can secure the write process to a
standalone EEPROM.
Emulated EEPROM from an embedded Flash memory: once started by the CPU, the
writing 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) in their
applications, and to determine a proper method to handle them.
●
5/25