Eeprom Emulation stm32
Eeprom Emulation stm32
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
Contents
4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
List of tables
List of figures
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 Table 1.
2.1 Principle
EEPROM emulation is performed in various ways, taking into consideration the Flash
memory limitations and product requirements. The approach detailed below requires at
least two Flash memory pages of an identical size allocated to non-volatile data: one that is
initially erased, the other that is ready to take over when the former page needs to be
garbage-collected. A header field that occupies the first half word (16-bit) of each page
indicates t.he page status. Each of these pages is called Page0 and Page1 in the rest of this
document
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 is completely transferred to the erased page.
Figure 1 shows how the page status changes.
Each variable element is defined by a virtual address and a value to be stored in the 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 up-to-date data value.
Variable data
(16 bits) 256 elements
(1-Kbyte page)
Variable virtual
address (16 bits)
page0 page1
ai14608d
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
Active Page = Page0 Active Page = Page0 Active Page = Page0 Active Page = Page0
ai14609
Page header corruption is possible in the event of power loss during data update or
page erase/transfer. In this case, the EE_Init() function will attempt to restore
EE_Init() the emulated EEPROM to a known good state. This function should be called prior
to accessing the emulated EEPROM after each power-down. It accepts no
parameters.
This function erases page0 and page1 and writes a VALID_PAGE header to
EE_Format()
page0.
This function reads both page headers and returns the valid page number. The
EE_FindValidPage() passed parameter indicates if the valid page is sought for a write or read operation
(READ_FROM_VALID_PAGE or WRITE_IN_VALID_PAGE).
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 case the active page is full, the PAGE_FULL value is returned. This
routine uses the parameters below:
EE_VerifyPageFullWrite
Virtual address: may be any of the three declared variables’ virtual addresses
Variable()
(Var1, Var2 or Var3)
Data: the value of the variable to be stored
This function returns FLASH_COMPLETE on success, PAGE_FULL if there is not
enough memory for a variable update, or a Flash memory error code to indicate an
operation failure (erase or program).
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
EE_ReadVariable()
found, the ReadStatus variable is returned with the value “1”, otherwise it is reset
to indicate that the variable has been found and the variable value is returned on
the Read_data variable.
It transfers the latest value of all variables (data with associated virtual address)
from the current page to the new active page. At the beginning, it determines the
active page, which is the page the data is to be transferred from. The new page
EE_PageTransfer() 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.
This function is called by the user application to update a variable. It uses the
EE_WriteVariable() EE_VerifyPageFullWriteVariable(), and EE_PageTransfer() routines
that have already been described.
Note: The following functions can be used to access the emulated EEPROM:
- EE_Init()
- EE_ReadVariable()
- EE_WriteVariable()
These functions are used in the application code delivered with this application note.
Figure 4 shows the procedure for updating a variable entry in the EEPROM.
EE_VerifyPageFullWriteVariable()
EE_FindValidPage()
Yes current No
active page
full
Copy all current elements by
Add new element at
reading the active page
the 1st empty element
from the bottom, taking
place in the current
into account the new
active page
EE_PageTransfer() updated element .
EE_ReadVariable()
Erase previous active page End
End
ai14610b
Key features
● User-configured emulated EEPROM size
● Increased Flash memory endurance: page erased only when it is full
● Non-volatile data variables can be updated infrequently
● Interrupt servicing during program/erase is possible
5. When the EEPROM mechanism is run for the 1st time or for an invalid status (see Table 1: Header status
switching between page0 and page1 for more details), the two pages are erased and the page used for
storage is marked as VALID_PAGE.
6. A typical EEPROM initialization is performed when a valid page exists (the EEPROM has been initialized at
least once). During a typical EEPROM initialization, one of the two pages is erased (see Table 1: Header
status switching between page0 and page1 for more details).
by Word(32-bit) FLASH_ProgramWord
by half word(16-bit) FLASH_ProgramHalfWord
12 32 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
ai14611
However, in an 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 STM32F0xx’s 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 depends on the amount/size of data that the user wants to handle. In
this example, two pages (of 1 Kbyte) are used and programmed with 16-bit data. Each
variable corresponds to a 16-bit virtual address. That is, each variable occupies a word of
storage space. A page can store 1 Kbyte multiplied by the Flash memory endurance of
10 000 cycles, giving a total of 10 000 Kbytes of data storage capacity for the lifetime of one
page in the emulated EEPROM memory. Consequently, 20 000 Kbytes 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.
4 Revision history
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 ST’s 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 ST’S 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 TWO AUTHORIZED ST REPRESENTATIVES, 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 USER’S 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.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.