ST AN4061 Application note

AN4061
Application note
EEPROM emulation in STM32F0xx microcontrollers
Introduction
EEPROMs (Electrically Erasable Programmable Read-Only Memory) are often used in industrial applications to store updateable data. An EEPROM is a type of permanent (non­volatile) memory storage system used in complex systems (such as computers) and other electronic devices to store and retain small amounts of data in the event of power failure.
For low-cost purposes, an external EEPROM can be replaced by an on-chip Flash, with a specific software algorithm.
This application note describes the software solution for substituting a standalone EEPROM by emulating the EEPROM mechanism using the on-chip Flash of STM32F0xx devices.
The emulation is achieved by employing at least two pages in the Flash. The EEPROM emulation code swaps data between the pages as they become filled, in a manner that is transparent to the user.
The EEPROM emulation driver supplied with this application note meets the following requirements:
Lightweight implementations offering a simple API that consists of three functions for
initialization, read data and write data, and reduced footprint.
Simple and easily updateable code model
Clean-up and internal data management transparent to the user
Background page erase
At least two Flash memory pages to be used, more if possible for wear leveling
May 2012 Doc ID 022893 Rev 1 1/19
www.st.com
Contents AN4061
Contents
1 Main differences between external and emulated EEPROM . . . . . . . . . 5
1.1 Difference in write access time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Difference in erase time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Similarity in writing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Implementing EEPROM emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Case of use: application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 EEPROM emulation software description . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 EEPROM emulation memory footprint . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.5 EEPROM emulation timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Embedded application aspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.1 Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Wear-leveling: Flash memory endurance improvement . . . . . . . . . . . . . . 14
3.2.1 Wear-leveling implementation example . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Page header recovery in case of power loss . . . . . . . . . . . . . . . . . . . . . . 15
3.4 Cycling capability and page allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.4.1 Cycling capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.4.2 Flash page allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.5 Real-time consideration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2/19 Doc ID 022893 Rev 1
AN4061 List of tables
List of tables
Table 1. Differences between external and emulated EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Table 2. API definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 3. Memory footprint for EEPROM emulation mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Table 4. EEPROM emulation timings with a 48 MHz system clock . . . . . . . . . . . . . . . . . . . . . . . . . 12
Table 5. Flash program functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 6. Application design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Table 7. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Doc ID 022893 Rev 1 3/19
List of figures AN4061
List of figures
Figure 1. Header status switching between page0 and page1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 2. EEPROM variable format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 3. Data update flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 4. WriteVariable flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Figure 5. Page swap scheme with four pages (wear-leveling). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4/19 Doc ID 022893 Rev 1

AN4061 Main differences between external and emulated EEPROM

1 Main differences between external and emulated
EEPROM
The EEPROM is a key component of many embedded applications that require non-volatile storage of data updated with byte or word granularity during run time.
Microcontrollers used in these systems are more often based on embedded Flash memory. To eliminate components, save PCB space and reduce system cost, the STM32F0xx Flash memory may be used instead of an external EEPROM for simultaneous code and data storage.
Unlike Flash memory, however, the external EEPROM does not require an erase operation to free up space before data can be rewritten. Special software management is required to store data in an embedded Flash memory.
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 an embedded Flash memory and an external serial EEPROM are the same for any microcontroller that uses the same Flash memory technology (it is not specific to the STM32F0xx family products). The major differences are summarized in Ta bl e 1 .

Table 1. Differences between external and emulated EEPROM

External EEPROM
Feature
Write time
Erase time N/A Page Erase time: from 20 ms to 40 ms
Write method
Read access
Write/Erase cycles 1 million Write cycles
(for example, M24C64:
I²C serial access EEPROM)
– Random byte Write within 5 ms.
Word program time = 20 ms
– Page (32 bytes) Write within 5
ms. Word program time = 625 µs
– Once started, is not CPU-
dependent
– Only needs proper supply
– Serial: a hundred µs – Random word: 92 µs – Page: 22.5 µs per byte
Half-word program time: from 124 µs to 26 ms
Once started, is CPU-dependent. If a Write operation is interrupted by software reset,
the EEPROM Emulation algorithm is stopped, but current Flash write operation is not interrupted by a software reset.
Can be accessed as half words (16 bits) or full words (32 bits).
Parallel: (at 48 MHz) the access time by half-word is from 3.8 µs to 110 µs
10 kilocycles by page. Using multiple on-chip Flash memory pages is equivalent to increasing the number of write cycles. See Section 3.4: Cycling
capability and page allocation.
Emulated EEPROM using
on-chip Flash memory
(2)
(1)
(2
1. For further detail, refer to Chapter 2.5: EEPROM emulation timing.
2. For further detail, refer to “Memory characteristics” in STM32F051xx Datasheet.
Doc ID 022893 Rev 1 5/19
Main differences between external and emulated EEPROM AN4061

1.1 Difference in write access time

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

1.2 Difference in erase time

The difference in erase time is the other major difference between a standalone EEPROM and an 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 a Flash memory. Moreover, as the erase process of a block in the Flash memory does not take long, power shutdown and other spurious events that might interrupt the erase process (a reset, for example) should be considered when designing the Flash memory management software. To design robust Flash memory management software, a thorough understanding of the Flash memory erase process is necessary.
Note: In case of a software reset, ongoing page erase or mass erase operations on the
STM32F0xx embedded Flash are not interrupted.

1.3 Similarity in writing method

One of the similarities between external EEPROM and emulated EEPROM with the STM32F0xx embedded Flash is the writing method.
Standalone external EEPROM: once started by the CPU, the writing of a word cannot
be interrupted by a software reset. Only a 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 embedded Flash memory: once started by the CPU, the
write process can be interrupted by a power failure. In case of a software reset, ongoing word write operation on the STM32F0xx embedded Flash is not interrupted. The EEPROM algorithm is stopped, but the current Flash word write operation is not interrupted by a software reset.
6/19 Doc ID 022893 Rev 1
Loading...
+ 13 hidden pages