Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of ​ flash simulating eeprom to store logs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the principle of flash simulating eeprom log storage". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Flash simulates the principle of storing logs in eeprom

Purpose of the article

Advantages of using flash to simulate eeprom

Scheme design

Matters needing attention

Purpose of the article

When doing mcu development, when it comes to data storage, we often consider using flash, eeprom, or ferroelectric memory. From a data storage point of view, the most secure must be FRAM, and then you will consider using EEPROM, for general data storage, flash is sufficient. Considering that a normal MCU does not carry EEPROM, the simplest solution can use flash to simulate eeprom and implement basic journaling file system operations. Here is a description of the basic principles.

Advantages of using flash to simulate eeprom

Generally do mcu development, will store data, if there is a file system, you can write to the file, but generally do not use the file system, then directly operate the flash read and write. According to the operational characteristics of flash, on the one hand, flash can only be changed from 1 to 0, not from 0 to 1. Only when a page is erased can the data on that page be changed from 0 to 1. In terms of a page size of 4K, if you save dozens of bytes at a time and repeat the operation many times, you need to erase one page at a time before writing data.

If the user data is a series of state information, only one state, or one byte, will be changed at a time, then each operation will read all the user data on the flash into memory, then change the state bits of the data in memory, then erase the page, and then write the data in memory to the page of the flash.

Because the read and write life of flash is limited, it is about 10w, and it takes a long time to erase page. So the above solution is flawed, so how can we make flash read and write like eeprom without having to erase the flash every time?

Scheme design

To make it very easy to use flash as an eeprom, you need to ensure two conditions

1. The amount of data read and written is at least less than the size of a page data amount of a flash of 1 hand 2.

two。 Prepare at least two pages

Then you can start to design the handling of the data.

The data structure of each page can be designed as shown in the figure above.

First of all, the header of each pages will have a flag that describes the current state of the pages, which is uninitialized, valid, and invalid. The specific scenario is that when the flash is not initialized, its flags is 0xff because of its electrical characteristics. At this time, you need to initialize the page and write to its flag bit. Then store the data down in turn, and at this time, because you know the amount of data written each time, you only need to determine its offset.

This will also lead to a problem, that is, how to ensure that the offset of the current write data can be known every time the power is turned on after a power outage?

To solve this problem, you can traverse the page0 every time you power on, make sure that the offset of the current data is obtained by reading the flag bits of the specific user data, and then record it in memory and then execute it.

When the data is full in a page0, it is time to transfer the data to the page1. This process can be well decomposed:

Because it is a log data type, there must be cases where the data is deleted or updated, so there will be two user data in page0 that are both index=0. And it's the latest one in the back.

Once the program writes data on the pages, checking that the remaining pages space is insufficient, it will set the state of the page0 to invalid, then start to set the flags of the page1 to a valid state, and start to transfer the data in the pages0 to the page1. Note that when transferring, you need to check the index in the page0 and replace the old data with the latest data. Finally, erase the page0.

The storage of log data operation can be realized by transporting the data in the above order.

Matters needing attention

The use of this scheme can be operated well in many cases, but once the amount of user data is very large, close to the amount of page data of 1 and 2, the problem of frequently erasing and writing flash will also exist.

With the increase of the number of page, the number of erasures can also be reduced, and the problem is that the program design becomes more complex.

In the process of actual use, the flag flag of each pages may not only have a valid and invalid state, for example, in the transfer process, the power is suddenly cut off, in order to ensure that the data is not lost, we must consider the state of each page abnormal situation, and be able to recover the data. Reach the safe state of power loss.

With these background management mechanisms, when writing logs, the upper layer can directly use read-write functions to operate, regardless of whether the underlying layer is flash or eeprom.

This is the end of the content of "what is the principle of flash simulating eeprom log storage". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report