Some understanding of stm32 flash emulation eeprom

table of Contents

1. Introduction

Second, the difference before using external eeprom and emulation eeprom

Three, realize the principle of eeprom simulation

4. Exception handling of data loss

Five, code and original document download address


 

This article refers to st official documents

AN4061 EEPROM emulation in STM32F0xx microcontrollers

AN2594 EEPROM emulation in STM32F10x microcontrollers

 

1. Introduction

EEPROM (Electrically Erasable Programmable Read-Only Memory) is often used in industrial applications to store updatable data. EEPROM is a permanent (non-volatile) memory used in complex systems (such as computers) and other electronic devices

Storage system, which can store and retain a small amount of data in the event of a power failure. In order to reduce costs, specific software algorithms can be used to replace external EEPROM with on-chip Flash

Second, the difference before using external eeprom and emulation eeprom

Three, realize the principle of eeprom simulation

 

First use 2 pages of FLASH space. If page 0 is full of data, copy the [valid data] in page 0 to page 1, if page 1 is full, copy [valid data] in page 1 to 0 Pages are recycled in this way. Of course, if you want to increase the service life, you can increase the multi-page cycle. The official example is only an example of 2 pages. The first 4 bytes of each page are reserved, and the first 2 bytes are the status flags of the page.

 

The following figure shows the data storage format in FLASH:

Each variable element is defined by a virtual address and value, which will be stored in Flash for subsequent retrieval

Or update (in the implemented software, the length of both the virtual address and the data is 16 bits). If the data is modified, the modified data associated with the previous virtual address will be stored in the new Flash location. Data retrieval will return the latest data value.

 

Figure 3 shows the process of data update:

1. Write data

Assuming that the virtual address of the saved data is 0X7777, then the program writes data from the top address of the current effective page to query the space where the virtual address location is 0XFFFF, if it is 0XFFFF, then the location can save the data; if not, then continue to find the next one If there is no 0XFFFF space on this page, it means that this page is full, then copy this page [valid data] to another page to continue to save the data. When the data of the same virtual address is saved twice, as shown in the second box in the first column of the above figure: from top to bottom, the data 1245 corresponding to the second virtual address is 0X7777 is valid. Knowing this point, then you will basically understand how to read data.

2. Read data

When reading data, it starts from the end address of the valid page to check whether it is valid data. If it is, it returns immediately. The program judges valid data through the virtual address. The data of the first matching virtual address is valid

3. Process data when the page is full

Having said that, I have seen a lot of problems with the unsuccessful use of example programs, so please pay attention to the following. Their error estimates are caused by the following reasons. When 1 page is full, there are actually a lot of invalid data in it, you only need to copy [valid data] to another page.

 

Simply put, it is to write from the beginning to the end when writing, and read from the end to the beginning when reading, to ensure that the latest data is obtained. . . .

4. Exception handling of data loss

In flash data erasing, if the power is unstable at the moment of startup, the chip may reset after data erasing, resulting in data loss. Or it is interrupted during flash erasing and writing, which may also cause flash data loss. So some mechanisms need to be made to prevent this problem.

The flowchart in the figure below uses two spaces for data storage, one location is used as the main storage area for data, and all data is read from this block of flash. The other location is used as a data backup area to restore the data in the main storage area when the data in the main storage area is abnormally erased.

 

 

Five, code and original document download address

https://www.stmcu.org.cn/document/detail/index/id-200203

 

Guess you like

Origin blog.csdn.net/zhuimeng_ruili/article/details/112630215