An4894 Eeprom Emulation Techniques and Software For stm32 Microcontrollers Stmicroelectronics
An4894 Eeprom Emulation Techniques and Software For stm32 Microcontrollers Stmicroelectronics
Application note
EEPROM emulation techniques and software for
STM32 microcontrollers
Introduction
EEPROMs (electrically erasable programmable read-only memories) are used for non-
volatile storage of updatable application data, or to retain small amounts of data in the event
of power failure in complex systems. To reduce cost, an external EEPROM can be replaced
by on-chip Flash memory, provided that a specific software algorithm is used.
This application note describes the software solution (X-CUBE-EEPROM) for substituting a
standalone EEPROM by emulating the EEPROM mechanism using the on-chip Flash
memory available on the STM32 Series products listed in Table 1: Applicable products. X-
CUBE-EEPROM also provides a firmware package including examples showing how to
exploit this EEPROM emulation driver (see Section 5: API and application examples).
For STM32WB Series products only, an example that maintains a Bluetooth® Low-Energy
connection and communication while performing EEPROM operations is provided.
The emulation method uses at least two Flash memory pages, between which the EEPROM
emulation code swaps data as they become filled. This is transparent to the user. The
EEPROM emulation driver supplied with this application note has the following features:
• Lightweight implementation and reduced footprint
• Simple API consisting of a few functions to format, initialize, read and write data, and
clean up Flash memory pages
• At least two Flash memory pages to be used for internal data management
• Clean-up simplified for the user (background page erase)
• Wear-leveling algorithm to increase emulated EEPROM cycling capability
• Robust against asynchronous resets and power failures
• Optional protection implementation for Flash-memory sharing between cores in multi-
core STM32 devices (for example STM32WB Series)
• Maintenance of cache coherency.
The EEPROM size to be emulated is flexible and only limited by the Flash memory size
allocated to that purpose.
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1 Reference documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 Advanced features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1 Data granularity management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2 Wear leveling algorithm and Flash page allocation . . . . . . . . . . . . . . . . . 18
4.3 Guard pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.4 Cycling capability: EEPROM endurance improvement . . . . . . . . . . . . . . 19
4.5 Computing the required size of Flash for EEPROM emulation . . . . . . . . 21
4.6 EEPROM emulation robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.6.1 Data recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.6.2 Page header recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.7 Real-time considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.7.1 Devices embedding Flash memory with RWW (Read While Write)
capability 23
4.7.2 Running the critical processes from the internal RAM . . . . . . . . . . . . . . 24
4.7.3 Dual core considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.8 Cleaning up the Flash memory in Interrupt or Polling mode . . . . . . . . . . 25
4.9 Cache coherency maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
List of tables
List of figures
1 General information
This document scopes STM32 microcontrollers that are based on an Arm®(a) core.
Application notes:
– STM32F0 Series: EEPROM emulation in STM32F0xx microcontrollers (AN4061)
– STM32F1 Series: EEPROM emulation in STM32F10x microcontrollers (AN2594)
[1] – STM32F2 Series: EEPROM emulation in STM32F2xx microcontrollers (AN3390)
– STM32F3 Series: EEPROM emulation in STM32F3xx microcontrollers (AN4046) / EEPROM
emulation in STM32F30x/STM32F31x STM32F37x/STM32F38x microcontrollers (AN4056)
– STM32F4 Series: EEPROM emulation in STM32F40x/STM32F41x microcontrollers (AN3969)
[2] Building wireless applications with STM32WB Series microcontrollers, application note (AN5289)
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
4 million cycles at 25 °C 10 kcycles per page @ 105 °C(2). Using multiple on-
chip Flash memory pages is equivalent to increasing
Endurance 1.2 million cycles at 85 °C
the number of write cycles. See Section 4.4: Cycling
600 kilocycles at 125 °C capability: EEPROM endurance improvement.
7 years @ 125 °C
(2) 50 years at 125 °C
Retention 15 years @ 105 °C
100 years at 25 °C
30 years @ 85 °C
1. For further details, refer to Chapter 5.3: EEPROM emulation timing.
2. Example data for STM32L4 Series. Refer to the datasheet of your STM32 product.
3.1 Principle
EEPROM emulation can be performed in various ways, taking into consideration the Flash
memory characteristics and final product requirements. The approach detailed below
requires two sets of Flash memory pages allocated to non-volatile data.
The first set of pages is initially erased and used to store new data and Flash programming
operations are done sequentially in increasing order of Flash addresses. Once the first set
of pages is full of data, it needs to be garbage-collected.
The second set of pages collects only the valid data from the first set of pages and the
remaining area can be used to store new data. Once the transfer of valid data to the second
set of pages is completed, the first set of pages can be erased.
Each set of pages can be made up of one or several Flash pages. For most STM32 Series
covered by this document, a header field that occupies the first four 64-bit words (32 bytes)
of each page indicates its status, the exception being STM32 U5 Series, where the header
occupies the first four 128-bit words (64 bytes). Each page has five possible states:
• ERASED: the page is empty (initial state)
• RECEIVE: the page used during data transfer to receive data from other full pages.
• ACTIVE: the page is used to store new data
• VALID: the page is full. This state does not change until all valid data is completely
transferred to the receiving page.
• ERASING: valid data in this page has been transferred. The page is ready to be
erased.
Figure 1 shows the page status evolution in the case where each set of pages is made of
two pages.
MSv45025V2
Transfer DATA
ERASED
Write DATA
MSv45026V1
The minimal write width in Flash memory is 64-bits (128-bits for STM32U5 Series) due to its
ECC (Error Correcting Code) that cannot be switched off; only zeroes (0x0000 0000 0000
0000, or 0X0000 0000 0000 0000 0000 0000 0000 0000 for STM32U5 Series) can be
written to an already programmed non-null Flash line. As the first four words are used by the
header, a Flash page can store up to 252 variable elements when the page size is 2 Kbytes,
and up to 508 variable elements when the page size is 4 Kbytes.
The possible states of a Flash page are coded by writing 0xAAAA AAAA AAAA AAAA (or
0xAAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA for STM32U5 Series) into the page
header. It is possible to determine the page state using the following procedure:
• The page is in ERASING state if its fourth line is not erased
• The page is in VALID state if the third line is not erased and the fourth line is erased
• The page is in ACTIVE state if the second line is not erased and the third and fourth
lines are erased
• The page is in RECEIVE state if the first line is not erased and the second, third and
fourth lines are erased
• The page is in ERASED state if the first four lines are erased.
This algorithm allows the coding of all states and transitions described in Section 3.2: Page
status valid transitions.
Each variable element is defined by a virtual address and a data value to be stored in Flash
memory for subsequent retrieval or update. In the implemented software, the virtual address
is 16 bits long and the data value is either 8 bits, 16 bits or 32 bits long. The driver requires
the virtual address values to be between 0x0001 (0x0000 corresponds to an EEPROM
element invalidated by the driver), and the maximum number of EEPROM variables
required. Also, since virtual addresses are 16-bits wide, the maximum number of EEPROM
variables cannot exceed 0xFFFE (0xFFFF corresponds to an erased Flash line). Moreover,
the number of variables is limited by the size of the product Flash memory (see Section 4.5:
Computing the required size of Flash for EEPROM emulation).
Each element also contains a 16-bit CRC that is used to check the element integrity. When
data is modified, the modified data associated with the same virtual address is stored in a
new Flash memory location. Data retrieval returns the up-to-date data value.
Figure 3. Flash page and EEPROM variable format (except STM32U5 Series)
Flash Page
RECEIVE
ACTIVE
Page Header 0xFFFFFFFFFFFFFFFF
(4 * 8 bytes) VALID
for ERASED page
ERASING
Variable Element
16-bit
Virtual 16-bit 8, 16 or 32-bit
Address CRC Data value
Page Data
(Number of variable
elements per page
* 8 bytes)
MSv45027V2
Variable Element
16-bit
Virtual 16-bit 96-bit
Address CRC Data value
Page Data
(Number of variable
elements per page
* 16 bytes)
MSv47646V1.
For this example, only two Flash pages are necessary: Page 0 and Page 1. In Figure 5:
Data update flow RCRC represents the 16-bit CRC value for this variable element (refer to
Section 4.6: EEPROM emulation robustness for more details about CRC).
Figure 5: Data update flow shows only the write commands which are done sequentially in
increasing order of Flash addresses.
Page 1
Format EEPROM
4 Advanced features
The EEPROM emulation firmware is designed to fulfill most of the requirements for an
embedded application in terms of non-volatile storage. This section covers these
requirements in detail. Other embedded application requirements may be needed in
particular cases and are addressed in section Section 6: Embedded application aspects.
Knowing the requested size of emulated EEPROM and the targeted endurance, it is possible
to compute the Flash memory size to be used for that purpose. The Flash memory size is
also a function of the data width of stored variables.
STM32L4
STM32L4+
Note: To get the same information for other STM32 Series you can apply the formula presented in
Section 4.5: Computing the required size of Flash for EEPROM emulation.
Note: Values of ‘Flash memory endurance values (kcycles)’ for applicable STM32 Series are
listed in Table 6: Flash memory endurance.
The formula is decomposed into the following steps:
1. Knowing the number of elements a Flash page can store (refer to Table 4: Flash
memory properties) compute the number of Flash pages needed to store the EEPROM
elements. (The calculated Flash memory size is independent of the element data
width).
2. Multiply the number of pages by the targeted integer cycling ratio (for instance 1 for the
standard 10 kcycles endurance or 10 to achieve 100 kcycles endurance when the
Flash memory endurance is 10 kcycles). The Flash-memory endurance of the product
(indicated in Table 6: Flash memory endurance), must be taken into account.
3. Multiply the number of Flash pages by 2 in order to take into account the data transfer
mechanism.
4. Add the number of guard pages (an even number) to get the final number of Flash
pages.
5. Knowing the Flash page size for your product (refer to Table 4: Flash memory
properties), compute the Flash memory size required by the EEPROM emulation.
STM32L4 calculation example
In order to store 4000 individual bytes, and knowing that each page can store up to 252
elements, a set of pages must comprise 16 Flash pages. A second set of pages, of the
same size, is required to transfer data when the first one is full. If we assume that we use 2
guard pages, 34 Flash pages are necessary.
Note: This calculation is a slightly conservative estimation.
If, despite the cleanup phase, another NMI is triggered during the EEPROM emulation,
nothing is done. The fact that the NMI is raised is considered to invalidate the line. The
cleanup phase eliminates the majority of lines corrupted by an asynchronous reset or power
down. Thus, after the cleanup phase, the number of remaining NMIs should, in most cases,
be 0.
If, during the first write to a variable element with a given address, the write is interrupted by
a power failure or an asynchronous reset, the driver considers that there is no data at this
address. In all other cases the EEPROM emulation software always returns the latest valid
data value by finding the previous value for this data stored in Flash memory.
4.7.1 Devices embedding Flash memory with RWW (Read While Write)
capability
With STM32 devices embedding a dual-bank Flash memory, it is recommended to put the
critical routines (Vector table, critical interrupt service routines) in one Bank and the area
used for EEPROM emulation in the other bank. The Flash area in the bank used for
EEPROM emulation can still be used, but with execution delays during Flash Write and
Erase operations.
Note: In order to know whether your STM32 device supports Flash Read While Write capability,
please refer to the product datasheet.
For STM32WB Series products integrating a Bluetooth® Low-Energy (BLE) stack, a ‘Flash
access management’ mechanism, based principally on the hardware semaphore feature, is
described in an application note [2].
For the P-NUCLEO-WB55 Nucleo board, two examples are provided in the X-CUBE-
EEPROM firmware package (see Section 5.1.2: STM32Cube expansion software (X-CUBE-
EEPROM)):
• An example that simply exploits the EEPROM emulation driver (similar to the examples
for other Series).
• An example that maintains a BLE connection and communication while processing
EEPROM operations. It follows the Flash access timing rules in AN5289 [2], and can
be used as a reference for implementing this mechanism.
To determine the availability of the above mechanisms for other ST MCUs with RF stacks,
please refer to their datasheets and other related documentation.
A sample demonstration project using the EEPROM emulation driver is also supplied in
order to demonstrate how to manage 1000 non-volatile variables. Sample projects are
provided for the boards listed in Table 8. They are located in the
Projects\STM32xx\EEPROM_Emul package folders, and can easily be tailored to other
boards using the same STM32 MCU Series.
One more example is provided for the P-NUCLEO-WB55.Nucleo board. It is located at
Projects\STM32WB\BLE_HeartRate_EEPROM_Emul\P-NUCLEO-WB55.Nucleo. The
Nucleo board maintains a BLE connection and communication in addition to processing
EEPROM operations. By using the DUALCORE_FLASH_SHARING preprocessor-defines
symbol, this example follows the Flash sharing mechanism and timing rules described in
Section 4.7.3: Dual core considerations.
• flash_interface.c: contains the functions needed to handle the STM32 Flash specific
features.
• main.c: this application program is an example using the described routines in order to
configure, write to and read from the emulated EEPROM.
• stm32xxxx_it.c: shows an example of interrupt service file using the
EE_DeleteCorruptedFlashAddress function in the NMI handler.
For the BLE STM32WB Series example, BLE_HeartRate_EEPROM_Emul, the code
illustrating the driver exploitation can be found in the Core/Src/app_entry.c file. In particular,
the functions EEPROM_Emul_Init and EEPROM_Emul_Operation are examples of
sequencer tasks for EEPROM driver initialization and exploitation (read and write
operations).
a. 100 variables are used in the STM32C0 series example to have consistency with the available flash
size.
Erases all Flash pages used for EEPROM emulation and writes an ACTIVE
EE_Format
header to the first page.
Configures the EEPROM emulation variables to their initial state and restores
the Flash pages to a known good state in case of asynchronous reset or
power loss during Flash write or erase operation.
Erases the Flash pages that need to be erased (for instance pages not fully
erased in ERASE state, pages in ERASING state). The EE_FORCE_ERASE
EE_Init parameter has to be used by default. If the application guarantees that no
asynchronous reset nor power loss can happen during Flash write or erase
operations, then the EE_CONDITIONAL_ERASE parameter can be used; refer
to Section 4.6.2: Page header recovery.
After reset, this function should be systematically called prior to accessing the
emulated EEPROM.
This function updates the data value corresponding to the virtual address
passed as a parameter. Only the last stored element is read.
It returns a Status equal to EE_OK unless it cannot find any data at the given
EE_ReadVariableXXbits
virtual address.
Three functions are implemented to deal with all supported data sizes (XX = 8,
16, 32 or 64).
This function updates the EEPROM at a given virtual address with the data
value passed as parameters.
If the set of pages used for EEPROM emulation is full, it triggers a Flash page
transfer and returns a Status equal to EE_CLEANUP_REQUIRED. In the
EE_WriteVariableXXbits case of the BLE application for the NUCLEO-WB55 Nucleo board
(DUALCORE_FLASH_SHARING is defined), it can return EE_FLASH_USED
when the Flash is used by CPU2(1)
Three functions are implemented to deal with all supported data sizes (XX = 8,
16, 32 or 64).
Erases the set of pages in ERASING state. The application program can call
this function when real-time constraints are low. The Flash cleanup is done in
EE_CleanUp
polling mode so this function does not return before one set of Flash pages
used for EEPROM emulation is erased.
Erases the set of pages in ERASING state. The Flash cleanup is started in
EE_CleanUp_IT interrupt mode so this function returns immediately and following page erase
is done by subsequent interrupt service routines.
This function can be called under NMI to delete a corrupted Flash Address
FI_DeleteCorruptedFlashAd
found by the Flash ECCD in order to clear this Flash interface Fault
dress
Section 4.6.1: Data recovery.
1. EE_WriteVariableXX bits can return EE_FLASH_USED only in the case of direct EEPROM variable writing and not
in the case of a page transfer, a page header update or the initialization process. Also, when this value is returned, the
driver can activate the HSEM interrupt so that an interrupt is raised when the CPU2 releases the semaphore associated to
the Flash device being used. The user program can then do something else while waiting for the semaphore release.
Table 10 does not take into account the Flash size used for EEPROM emulation itself. Refer
to the formula given in Section 4.5: Computing the required size of Flash for EEPROM
emulation to calculate the corresponding Flash memory size.
Note also that the application program (main.c) implements the VarDataTab[]variable to
store the EEPROM element values. Its size is 4000 bytes for 1000 32-bit elements.
For flexibility, this variable is placed in RAM in the provided example. Moreover
VarDataTab[] can easily be removed as most user applications do not need to have a
RAM copy of the elements stored in Flash memory.
Note: These timing measurements are provided in order to give the reader an idea of the
emulation firmware performance for different STM32 Series. They are based on 100
operations for each type of operation, thus they are not an absolute reference. It is possible
to find different minimums, maximums and even means (especially if the measurement
conditions are modified).
7 Conclusion
The STM32 devices’ internal Flash memory can be used advantageously in many
applications to emulate an EEPROM. Even though the memory characteristics are quite
different, this application note has shown that emulated EEPROM competes with real
EEPROM in terms of:
• Memory size
• Read and write access times
• Driver usage simplicity and flexibility
• Memory endurance
• Data retention
• Robustness to asynchronous resets and power failures
• Reliability
and mainly:
• cost by removing the need for an external component.
8 Revision history
c
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgment.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other
product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.