FLASH storage and reading method
By dividing the data into a data block content area and an auxiliary information area, using the feature value checksum rewrite mechanism, the RAM consumption and life problems during FLASH storage errors are solved, and efficient and secure data storage and reading operations are achieved.
Patent Information
- Application Number
- CN202411210729.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2024-12-03
AI Technical Summary
In the prior art, when storing data to FLASH, if the write operation is incorrect, it is necessary to read the entire sector into RAM and modify it and rewrite it, resulting in large RAM consumption and long running time, and repeated erasing will reduce the service life of FLASH.
Divide the data into the data block content area and the auxiliary information area. Write and read data through feature value verification. If errors are found, rewritten to avoid repeated erasures. Use specific byte values (such as 0xA5 or 0x5A) to mark the data. Validity, limit the number of consecutive write errors to prevent sector damage.
It realizes a method of correcting data errors without consuming a lot of RAM and affecting FLASH life, reducing the risk of storage errors and operating overhead, and improving storage efficiency and device reliability.
Smart Images

Figure CN119065610A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a FLASH storage and reading method. Background Art
[0002] Although the capacity of FLASH is much larger than that of EEPROM, the write operation is different from that of E2PROM. Generally, it needs to be erased before writing. Generally, erasing requires erasing a sector (usually 4096 bytes). As a result, if a write operation error is found, either it can only be ignored and the modification must be abandoned, or the data needs to be read into RAM, and then the sector needs to be erased after modification and the data needs to be rewritten to FLASH. This will require a large amount of RAM and a large amount of MCU running time. In addition, it may only be necessary to store a few bytes, but because the write data fails and needs to be rewritten, storing a large amount of data is more prone to storage errors when the power supply is unstable. In addition, because the number of erase times is also limited, repeated erasing will greatly reduce the service life of FLASH.
[0003] If an error occurs when storing data in FLASH, the traditional method is to read the data of the entire sector into RAM, and then erase the sector after modification and rewrite the data. This will consume a large amount of RAM (generally 4k RAM bytes) and a large amount of running time, which may cause MCU jams. In addition, it may only be necessary to store a few bytes, but because the write failure requires rewriting, it is easier to store errors when the power supply is unstable. In addition, because the number of erasures is also limited, repeated erasures will greatly reduce the service life of FLASH. The present invention designs a method that can correct erroneous data without requiring too much overhead and without affecting the service life of FLASH. Summary of the invention
[0004] The technical problem to be solved by the present invention is generally to provide a FLASH storage and reading method.
[0005] To solve the above problems, the technical solution adopted by the present invention is:
[0006] A method for storing and reading FLASH comprises the following steps:
[0007] S1, perform data storage operations:
[0008] S1.1, first, divide each piece of data into a data block content area and auxiliary information;
[0009] S1.2, select the characteristic value; when the flash is erased, the byte value has two cases: 0xff and 0x00; the characteristic value should be a value other than 0xff or 0x00;
[0010] S1.3, when a piece of data is written, the write pointer is offset; the write pointer is incremented by 1, and returns to 0 when the write pointer exceeds the maximum pointer range;
[0011] S1.4, first, after obtaining the write pointer, write the data in the data block content area into the data block content area corresponding to the write pointer in the FLASH and read it out; then, compare the written data content with the read data content, if it is found that the written data content is consistent with the read data content, write the selected feature value to the auxiliary information area of this data; if it is found that the written data content is inconsistent with the read data content, and read the value of the auxiliary information area of this data, it is used to store the flag of whether this data is valid;
[0012] If the values of the two auxiliary information areas are consistent, then the value opposite to the flash erase value in step S1.2 is written into the auxiliary information area of the data in the data block content area;
[0013] S1.5, read the auxiliary information area of the data in S1.4, and compare whether the read value is consistent with the characteristic value written in step S1.2;
[0014] If they are not consistent, the write pointer is shifted and the previous step S1.4 operation is repeated, that is, the data is rewritten to the back of the address of the last written data;
[0015] If the read value is consistent with the written characteristic value, the write pointer is offset and the write operation ends;
[0016] S2. Perform data reading operation;
[0017] S2.1, get the read pointer and offset;
[0018] S2.2, reading the auxiliary information area corresponding to the data block content area corresponding to the current read pointer in step S2.1, and determining whether the read data content is consistent with the written characteristic value;
[0019] If the read data content is inconsistent with the written characteristic value, the data block content area corresponding to the read pointer is considered to be invalid data, and the read pointer performs an offset operation, and then repeats the previous step S2.1 operation until the data in the auxiliary information area corresponding to the current pointer is consistent with the characteristic value, or the pointer traversal does not read a record in which the data in the auxiliary information area is consistent with the characteristic value, then the read fails and the read operation ends;
[0020] If the read data content is consistent with the written characteristic value, the data block content area is considered to be valid data, and the read operation ends after reading the data block content corresponding to the read pointer.
[0021] As a further improvement of the above technical solution:
[0022] In step S1.1, the data block content area is used to store data; the stored data includes load records, event records, freezing and settlement data;
[0023] Auxiliary information is used to record whether the data in the content area of the data block is valid; the auxiliary information includes a valid characteristic value and an invalid value; the characteristic value represents that the data stored in the corresponding data block content area is valid, and the invalid value represents that the data stored in the corresponding data block content area is invalid.
[0024] In step S1.2, the byte value is selected as 0xA5 or 0x5A.
[0025] In step S1.4, if the flash should be 0xff after erasing, write 0x00; if it should be 0x00 after erasing, write 0xFF.
[0026] Wherein, in step S1.5, it is set that if N consecutive write errors occur, the FLASH sector is considered to be damaged and the sector storage needs to be changed.
[0027] In step 2.1, the read pointer needs to be determined according to the actual application situation, that is, whether the oldest data needs to be read first or the latest data needs to be read first;
[0028] If you need to read the oldest data first, the pointer will initially point to the beginning of the data, so the pointer will increase by 1 when offsetting, and will return to 0 when the pointer reaches the write pointer;
[0029] If you need to read the latest data first, the pointer initially points to the value of the write pointer - 1 at the end of the data, so the pointer is decremented by 1 during the offset, and when the pointer is decremented to 0, it is assigned the value of the write pointer - 1.
[0030] When reading the latest data, first read the data of the most recent record, then read the two most recent records, reading from the most recent record upward; when reading the oldest data, first read the data of the first record, then read the two most recent records, reading from the oldest record downward.
[0031] The invention has reasonable design, low cost, durability, safety and reliability, simple operation, time and labor saving, money saving, compact structure and convenient use. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 It is a schematic diagram of the data storage process of the present invention.
[0033] Figure 2 It is a data reading flow chart of the present invention. DETAILED DESCRIPTION
[0034] like Figure 1 , this embodiment includes the following steps: 1. Performing data storage operation:
[0035] a) Divide each piece of data into two parts: data block content area + auxiliary information. The data block content area is used to store the data itself, such as load records, event records, freezing and settlement data, parameters, etc. The auxiliary information is used to record whether the data in the data block content area is valid. Selecting a characteristic value represents that the data stored in the data block content area is valid, and the remaining values represent that the data stored in the data block content area is invalid.
[0036] b) Selection of characteristic values: After flash is erased, the byte value is either 0xff or 0x00. The characteristic value should be a value other than 0xff or 0x00. It is recommended to select representative values such as 0xA5 and 0x5A.
[0037] c) After a piece of data is written, the write pointer is offset: the write pointer is incremented by 1 and returns to 0 when the write pointer exceeds the maximum pointer range.
[0038] d) After obtaining the write pointer, write the data to the data block content area corresponding to the write pointer in FLASH, and then read it out immediately, compare the written data content with the read data content, and if it is found that the written data content is consistent with the read data content, write the selected characteristic value to the auxiliary information area of this data; if it is found that the written data content is inconsistent with the read data content, and read the value and characteristic value of the auxiliary information area of this data, if the two values are consistent (possibly due to erase failure, etc.), write the value opposite to the flash erase value (if the flash should be 0xff after erasing, write 0x00; if it should be 0x00 after erasing, write 0xFF) and write it to the auxiliary information area of this data.
[0039] e) Read the auxiliary information area of this data, compare whether the read value is consistent with the written characteristic value, if not, the write pointer is offset, and repeat the previous step, that is, rewrite this data to the back of the last written data address. If it is wrong to write 5 times in a row, it can be limited to consider that this FLASH sector is damaged and the sector storage needs to be changed; if the read value is consistent with the written characteristic value, the write pointer is offset and the write operation is ended.
[0040] like Figure 2 , 2. Data reading operation:
[0041] a) Get the read pointer and offset: The read pointer needs to be determined based on whether the actual application needs to read the oldest data first or the latest data first. If the oldest data needs to be read first, the pointer must point to the beginning of the data at the beginning, so the pointer is incremented by 1 during the offset, and returns to 0 when the pointer reaches the write pointer. If the latest data needs to be read first, the pointer must point to the write pointer - 1 at the end of the data at the beginning, so the pointer is decremented by 1 during the offset, and when the pointer is decremented to 0, it is assigned the value of the write pointer - 1.
[0042] b) Get the read pointer;
[0043] c) Read the auxiliary information area corresponding to the data block content area corresponding to the current read pointer, and determine whether the read data content is consistent with the written characteristic value.
[0044] d) If the read data content is inconsistent with the written characteristic value, the data block content area corresponding to this read pointer is considered to be invalid data, and the read pointer performs an offset operation, and then repeats the previous step until the data in the auxiliary information area corresponding to the current pointer is consistent with the characteristic value, or the pointer traversal does not read a record in which the data in the auxiliary information area is consistent with the characteristic value, then the read fails and the read operation ends.
[0045] If the read data content is consistent with the written characteristic value, the data block content area is considered to be valid data, and the read operation ends after reading the data block content corresponding to the read pointer.
[0046] The present invention is fully described for a clearer disclosure, and the prior art is not listed one by one.
[0047] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some of the technical features therein with equivalents; it is obvious for those skilled in the art to combine multiple technical solutions of the present invention. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention. The technical contents not described in detail in the present invention are all known technologies.
Claims
1. A method for storing and reading FLASH, characterized in that: The steps include: S1, perform data storage operations: S1.1, first, divide each piece of data into a data block content area and auxiliary information; S1.2, select the characteristic value; when the flash is erased, the byte value has two cases: 0xff and 0x00; the characteristic value selects a value other than 0xff and 0x00; S1.3, when a piece of data is written, the write pointer is offset; the write pointer is incremented by 1, and returns to 0 when the write pointer exceeds the maximum pointer range; S1.4, first, after obtaining the write pointer, write the data in the data block content area into the data block content area corresponding to the write pointer in the FLASH and read it out; then, compare the written data content with the read data content, if it is found that the written data content is consistent with the read data content, write the selected feature value to the auxiliary information area of this data; if it is found that the written data content is inconsistent with the read data content, and read the value of the auxiliary information area of this data, it is used to store the flag of whether this data is valid; If the values of the two auxiliary information areas are consistent, then the value opposite to the flash erase value in step S1.2 is written into the auxiliary information area of the data in the data block content area; S1.5, read the auxiliary information area of the data in S1.4, and compare whether the read value is consistent with the characteristic value written in step S1.2; If they are not consistent, the write pointer is shifted and the previous step S1.4 operation is repeated, that is, the data is rewritten to the back of the address of the last written data; If the read value is consistent with the written characteristic value, the write pointer is offset and the write operation ends; S2. Perform data reading operation; S2.1, get the read pointer and offset; S2.2, reading the auxiliary information area corresponding to the data block content area corresponding to the current read pointer in step S2.1, and determining whether the read data content is consistent with the written characteristic value; If the read data content is inconsistent with the written characteristic value, the data block content area corresponding to the read pointer is considered to be invalid data, and the read pointer performs an offset operation, and then repeats S2.1 operation until the data in the auxiliary information area corresponding to the current pointer is consistent with the characteristic value, or the pointer traversal does not read a record in which the data in the auxiliary information area is consistent with the characteristic value, then the read fails and the read operation ends; If the read data content is consistent with the written characteristic value, the data block content area is considered to be valid data, and the read operation ends after reading the data block content corresponding to the read pointer.
2. The method for storing and reading FLASH according to claim 1, characterized in that: In step S1.1, the data block content area is used to store data; the stored data includes load records, event records, freezing and settlement data; Auxiliary information is used to record whether the data in the content area of the data block is valid; the auxiliary information includes a valid characteristic value and an invalid value; the characteristic value represents that the data stored in the corresponding data block content area is valid, and the invalid value represents that the data stored in the corresponding data block content area is invalid.
3. The FLASH storage and reading method according to claim 1, characterized in that: In step S1.2, the byte value is selected as 0xA5 or 0x5A.
4. The FLASH storage and reading method according to claim 1, characterized in that: In step S1.4, if the flash should be 0xff after erasing, write 0x00; if it should be 0x00 after erasing, write 0xFF.
5. The FLASH storage and reading method according to claim 1, characterized in that: in, In step S1.5, it is set that if N consecutive write errors occur, the FLASH sector is considered to be damaged and the sector storage needs to be changed.
6. The FLASH storage and reading method according to claim 1, characterized in that: In step 2.1, If you need to read the oldest data first, the pointer will initially point to the beginning of the data, so the pointer will increase by 1 when offsetting, and will return to 0 when the pointer reaches the write pointer; If you need to read the latest data first, the pointer initially points to the value of the write pointer - 1 at the end of the data, so the pointer is decremented by 1 during the offset, and when the pointer is decremented to 0, it is assigned the value of the write pointer - 1.
7. The FLASH storage and reading method according to claim 6, characterized in that: When reading the latest data, first read the data of the most recent record, then read the data of the two most recent records, and read from the most recent record upwards; When reading the oldest data, read the data of the first record first, then read the second, and read from the oldest record downward.
