A wear leveling method and storage device suitable for Nor flash data storage

By dividing the Nor Flash memory into active and spare pages, the number of erase/write cycles is reduced, solving the problem of short lifespan of Nor Flash memory and achieving a longer service life and data security.

CN113793634BActive Publication Date: 2025-12-19HAINA CLOUD IOT TECH CO LTD +1
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202110933343.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-14
Publication Date
2025-12-19
Estimated Expiration
2041-08-14

AI Technical Summary

Technical Problem

Nor Flash memory has slow write and erase speeds when data is frequently modified, resulting in a short lifespan and a tendency to develop bad blocks. Existing technologies cannot effectively extend its lifespan.

Method used

By employing a wear leveling method, the Nor Flash storage space is divided into two storage pages of the same size, one as the active page and the other as the spare page. Through data page switching and data verification mechanisms, the number of erase and write operations is reduced, ensuring data security and integrity.

Benefits of technology

By reducing the number of erase and write cycles of Nor Flash, the lifespan of the memory is extended, and data is not lost in the event of a power outage, thus improving the reliability and stability of data storage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113793634B_ABST
    Figure CN113793634B_ABST
Patent Text Reader

Abstract

The application relates to a wear leveling method suitable for NorFlash data storage, including a memory storage page allocation method, an initialization method and data writing and reading methods. The memory storage page allocation method is as follows: two storage pages are divided from a storage space; the initialization method includes: selecting one storage page as an active page and the other as a standby page, and updating the value of a global variable in the memory; the data writing method includes: judging whether the remaining storage space of the active page is not less than the space required by the data to be written; if yes, the data to be stored is written from a data offset address; if not, the standby page is switched to the active page, and the active page is written again; the data reading method is as follows: relevant parameters of the data to be read are acquired, data in the active page is searched from back to front, and the data first searched and meeting the relevant parameter requirements is read into the memory. The application also relates to a storage device and is used for executing the above method.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to a non-volatile flash memory technology, in particular to a wear leveling method and a storage device suitable for Nor flash data storage. BACKGROUND

[0002] Nor flash is a non-volatile flash memory technology, which is characterized by eXecute In Place (XIP) in the chip, so that the application program can run directly in the flash memory, without reading the code into the system RAM. The transmission efficiency of flash memory is very high, and it has high cost-effectiveness in small capacity of 1-4MB, but it is generally used to store small programs or data.

[0003] When writing data in Nor flash, the common practice is to write data at a fixed address, and when the data at this location needs to be changed, it needs to be erased and then written again. Data can only be written from 1 to 0, not from 0 to 1. After erasing, the Nor flash data becomes all 0xFF. When facing data that needs to be frequently changed, the low write and erase speed of Nor flash greatly affects its performance. The current general Nor flash memory has a program-erase endurance of about 100,000 times. When the number of times exceeds this limit, there is a possibility of bad blocks. At this time, the correctness of the stored data cannot be guaranteed.

[0004] In view of this, the present application is proposed. SUMMARY

[0005] One object of the present application is to overcome the shortcomings of the prior art and provide a wear leveling method suitable for Nor flash data storage, which reduces the number of erase-write times to delay the Nor flash memory from reaching the upper limit of the erase-write life, thereby prolonging the service life of the Nor flash memory.

[0006] Another object of the present application is to provide a storage device for executing the wear leveling method suitable for Nor flash data storage.

[0007] In order to achieve the first object of the present application, the present application adopts the following technical solutions:

[0008] A wear leveling method suitable for Nor flash data storage, including a method of storing page allocation, initialization method and data writing and reading method for Nor flash memory;

[0009] The method of storing page allocation includes: dividing the Nor flash storage space into at least two storage pages of the same size, and determining the starting address and size of the two storage pages;

[0010] The initialization method comprises: selecting one of the memory pages as an active page and the other as a standby page, and updating values of three global variables, namely, an active page address in the memory, a data offset address of the active page, and a page switching flag;

[0011] The data writing method comprises:

[0012] Step a1, obtaining data to be written;

[0013] Step a2, judging whether a remaining storage space of the active page is not smaller than a space required by the data to be written, if yes, writing the data to be stored from the data offset address of the active page, and if not, switching the standby page to the active page and then writing the data to be written into the new active page;

[0014] Step a3, updating the data offset address of the active page in the memory, and ending the writing;

[0015] The data reading method comprises:

[0016] Step b1, obtaining parameters of data to be read;

[0017] Step b2, according to the obtained parameters, searching the data to be read from the data offset address of the active page backwardly, and reading data found first and meeting the parameters into the memory.

[0018] Further, the memory page comprises a page attribute and a data unit;

[0019] The page attribute is used at least for defining the current memory page as the active page or the standby page;

[0020] The data unit is multiple, the data to be stored is stored in units of the data unit, the data unit is composed of a data body and a data header, the data body stores original data, and the data header has a certain length and is used for recording attribute information of the data unit.

[0021] Further, the page attribute is composed of a page cumulative number and a page check code, the page cumulative number is a variable and is used for distinguishing the current memory page as the active page or the standby page, and the page check code is a preset fixed value and is used for checking whether the page attribute is wrong.

[0022] Further, the data header comprises a data number, a data length, a data check value, a data unit offset, and a data header check code;

[0023] The data number is used for distinguishing different data bodies and is a preset value;

[0024] The data length is used to represent the size of the data body occupying the storage space;

[0025] The data check value is a check value calculated by a check algorithm according to the data body, and is used to check whether the data body is damaged;

[0026] The data unit offset is used to represent the offset address of the tail of the data unit in the storage page;

[0027] The data header check code is a preset fixed value, and is used to check whether the data header is damaged.

[0028] Further, the step a2 of switching the standby page to the active page comprises the following steps:

[0029] Step d1, erasing all the data of the standby page;

[0030] Step d2, reading the data headers from the back to the front one by one from the data offset address of the active page, and obtaining the data number, the data length, the data check value, the data unit offset and the data header check code stored in the data header;

[0031] Step d3, checking the data header check code, if the check is passed, executing step d4, and if the check is not passed, skipping the data header and executing step d5;

[0032] Step d4, judging whether the current data number is read, if yes, skipping the data header and executing step d5, and if no, reading out the data body according to the data unit offset and the data length, calculating the check value of the data body, and comparing the check value with the data check value in the data header, if they are equal, writing the data unit into the data offset address of the standby page, marking the data number as read, updating the data offset address of the standby page, and then executing step d5, and if they are not equal, abandoning the data body and executing step d5;

[0033] Step d5, judging whether there is a next data header in the active page, if yes, reading the next data header and returning to step d3, and if no, assigning the value of the page switch mark in the memory to the page accumulation number of the standby page after the page switch mark is increased by 1, assigning the page check code of the standby page to a fixed value, and writing the page attribute of the standby page;

[0034] Step d6, updating the global variables in the memory, the value of the active page address is the starting address of the storage page where the standby page is located, and the value of the data offset address of the active page is the data offset address of the standby page; completing the mutual switching of the standby page and the active page.

[0035] Further, in the step b1, the obtained parameter is the number of the data to be read out;

[0036] The step b2 comprises the following steps:

[0037] Step b2-1, read data headers one by one from the back to the front from the data offset address of the active page, and obtain the data number, data length, data check value, data unit offset and data header check code stored in the data header;

[0038] Step b2-2, check the data header check code, if the check is passed, execute the next step; if the check is not passed, switch the standby page to the active page, and return to step b2-1 after the switching is completed;

[0039] Step b2-3, compare the data number in the data header with the number of the data to be read out, if they are not consistent, execute the next step, if they are consistent, read out the data body according to the data unit offset and data length, calculate the check value of the data body, and compare it with the data check value in the data header, if they are equal, the data reading is successful, and the reading is ended; if they are not equal, discard the data body, and execute the next step;

[0040] Step b2-4, judge whether there is a next data header in the active page, if there is, read the next data header, and return to step b2-2, if there is not, return an error, and end the reading;

[0041] In the step b2-2, the method for switching the standby page to the active page is the same as that in the step a2.

[0042] Further, in the steps d5 and b2-4, the method for judging whether there is a next data header in the active page is as follows:

[0043] Step c1, calculate the data unit offset of the next data according to the current data unit offset, data length and data header length;

[0044] Step c2, judge whether the calculated data unit offset is greater than the page attribute length plus the data header length, if it is greater, it means that there is a next data header, if it is not greater, it means that there is no next data header.

[0045] Further, the initialization method comprises a first power-on initialization method and a non-first power-on initialization method.

[0046] The first power-on initialization method comprises the following steps:

[0047] S1, specify one of the storage pages as the active page, and the other as the standby page, and update the values of three global variables in the memory: the active page address is the start address of the active page, the data offset address of the active page is the offset address of the end of the page attribute in the active page, and the page switching flag is 0;

[0048] S2, erase both storage pages, preset the page check code as a fixed value, and the page accumulation number as 0, and write the page attributes of the active page, and complete the initialization;

[0049] The method of non-first power-on initialization comprises the following steps:

[0050] S'1, read the page attributes of the two storage pages, check the page check codes of the two page attributes, if both checks pass, execute S'2, if only one check passes, execute S'3, and if both checks fail, execute first power-on initialization;

[0051] S'2, select the storage page with a larger page accumulation number as the active page, find the end position of the active page data, and update the values of three global variables in the memory: the active page address is the start address of the storage page where the active page is located, the page switching flag is the page accumulation number of the active page, and the data offset address of the active page is the offset address of the data end position in the active page, and complete the initialization;

[0052] S'3, select the storage page that passes the check as the active page, find the end position of the active page data, and update the values of three global variables in the memory: the active page address is the start address of the storage page where the active page is located, the page switching flag is the page accumulation number of the active page, and the data offset address of the active page is the offset address of the data end position in the active page, and complete the initialization.

[0053] Further, during the writing of data, after writing data each time, the data offset address of the active page in the memory needs to be updated, and the updated data offset address of the active page = the data offset address of the current active page + data length + data header length.

[0054] In order to achieve the second object, the application adopts the following technical scheme:

[0055] A storage device configured to perform the wear leveling method for Nor Flash data storage described above.

[0056] After adopting the technical scheme of the application, the following beneficial effects are brought:

[0057] 1. The application reduces the number of erasing and writing of Nor Flash, and improves the service life of Nor Flash. Since the writing method adopted by the application always writes data at the data offset address of the active page each time, the previous data will not be erased, and the data of the active page will be erased only after the active page data is full, thereby greatly reducing the operation of erasing and writing Nor Flash, and improving the service life of Nor Flash.

[0058] 2、The application adopts double-area backup data to ensure that the data is not lost when power is cut off during the erasing process of the Nor Flash.

[0059] The application always writes at the data offset address of the active page when writing data into the Nor Flash, and does not erase the previous data. Even if the power is cut off during the data writing process of the Nor Flash, the data except the data being written can be ensured not to be lost. If the data is not completely written when the power is cut off, the data reading will fail at the next power-on, so that the active page switching operation is performed to move all the effective data to the new active page, thereby ensuring the safety of the data. During the active page switching process, the operation of erasing the old active page is performed after all the effective data is written into the new active page and the page attribute is written into the new active page. Therefore, even if the power is cut off during the active page switching process and the switching is unsuccessful, the data of the old active page still exists, and the old active page is treated as the active page at the next power-on, thereby ensuring that the data is not lost. BRIEF DESCRIPTION OF DRAWINGS

[0060] Figure 1 The application storage page structure diagram;

[0061] Figure 2 The application data header structure diagram;

[0062] Figure 3 The application data writing flowchart;

[0063] Figure 4 The application data reading flowchart;

[0064] Figure 5 The application active page and standby page mutual switching flowchart. DETAILED DESCRIPTION

[0065] The specific embodiments of the application will be further described in detail below with reference to the accompanying drawings.

[0066] Referring to FIG. Figures 1 to 5 A wear leveling method suitable for Nor Flash data storage includes a method for allocating storage pages to the Nor Flash memory, an initialization method, and data writing and reading methods.

[0067] The method for allocating storage pages includes: dividing the Nor Flash storage space into at least two storage pages of the same size, and determining the start address and size of the two storage pages.

[0068] Three global variables are set in the memory: the active page address (g_PgAcitve), the data offset address of the active page (g_PgOffset), and the page switching flag (g_PgCycle).

[0069] Specifically, when dividing the storage page, each storage page is set to consist of a page header and a data item.

[0070] The page header occupies the first eight bytes of the storage page, and includes a page count (Pg_Cnt) and a page magic code (Pg_Magic), each occupying four bytes. The page count (Pg_Cnt) is a variable, and the initial value is 0. The page magic code (Pg_Magic) is a preset fixed value, and is used to check whether the page header is incorrect.

[0071] The data item is a basic unit of data storage, and consists of a data body and a data header. The data body stores original data. The data header is used to record attribute information of the data item, and the data header length (header_len) is a fixed length value. When data needs to be stored, the data to be stored is encapsulated as a data item for storage.

[0072] As shown in Figure 2 The data header includes a data number (ID), a data length (len), a data item offset (hofs), a data header magic code (magic), and a data check value. The data number (ID) is used to distinguish different data bodies, and is a preset value. When applied to an access control device, for example, one access control device is assumed to support identification of 1000 cards, and numbers 1 to 1000 can be assigned, i.e., each card corresponds to a unique ID, and 0001 can be set to represent the first card, 0002 to represent the second card, and so on. The data length (len) is used to represent the size of the storage space occupied by the data body, e.g., 0010 is used to represent 10 bytes. The data item offset (hofs) is used to represent the offset address of the end position of the data item in the storage page. The data header magic code (magic) is a preset fixed value, and is used to check whether the data header is damaged. The data check value is a check value calculated according to the data body through a check algorithm, and is used to check whether the data body is correct when the data is read out. Preferably, the data check value is a crc8 check value, which is obtained by calculating the crc8 value of the data body.

[0073] The initialization method includes a first power-on initialization method and a non-first power-on initialization method.

[0074] Specifically, the first power-on initialization method includes the following steps:

[0075] S1, one of the storage pages is designated as an active page (active_page) and the other as a backup page (backup_page), and three global variables in the memory are updated: the active page address (g_PgAcitve) is the start address of the active page, the data offset address (g_PgOffset) of the active page is the offset address of the page attribute end address in the active page, and the page switching flag (g_PgCycle) is 0;

[0076] S2, both storage pages are erased, and then the page check code (Pg_Magic) is assigned a pre-set fixed value, the page cumulative number (Pg_Cnt) is 0, and the page attribute (pageHeader) of the active page is written, completing the first power-on initialization.

[0077] After the first power-on initialization, data can be written and read to and from the Nor Flash memory.

[0078] Specifically, the data writing method is as follows:

[0079] Step a1, obtaining the data to be written;

[0080] Step a2, judging whether the remaining storage space of the active page is not less than the space required by the data to be written, if yes, the data to be stored is encapsulated as a data unit (dataItem) and written to the active page from the data offset address (g_PgOffset) of the active page; if not, it means that the active page is full, and the backup page needs to be switched to the active page first, and then the data to be stored is encapsulated as a data unit (dataItem) and written to the active page from the data offset address (g_PgOffset) of the new active page.

[0081] Step a3, after writing the data, updating the data offset address (g_PgOffset) of the active page in the memory, and writing is completed. During the data writing process, the data offset address (g_PgOffset) of the active page in the memory needs to be updated after writing the data each time, to facilitate the next data writing and reading. The updated data offset address (g_PgOffset) of the active page = the current data offset address (g_PgOffset) of the active page + data length (len) + data header length (header_len), the data length (len) is the length of the current written data, and the data header length (header_len) is a certain value.

[0082] Specifically, when writing data, each parameter in the data header is confirmed by the following method:

[0083] Data ID = the number of data to be written,

[0084] Data length (len) = the length of data to be written,

[0085] Data body (data) = the original data to be stored,

[0086] Data unit offset (hofs) = data offset address of the active page (g_PgOffset) + data length (len) + data header length (header_len),

[0087] Data header check code (magic) = a fixed value set,

[0088] Data check value crc8 = the crc8 value calculated for the data body (data).

[0089] The address of the data header (header) after encapsulation is written to: active page address (g_PgAcitve) + data offset address of the active page (g_PgOffset) + data length (len).

[0090] The write address of the data body (data) is: active page address (g_PgAcitve) + data offset address of the active page (g_PgOffset).

[0091] In step a2, the method for determining whether the remaining storage space of the active page is not less than the space required for the data to be written is: substituting the storage capacity of the active page (active-page_len), the data offset address (g_PgOffset), the data length of the data to be written (len), and the data header length (header_len) into the following formula:

[0092] active-page_len≥g_PgOffset+len+header_len,

[0093] If the formula is true, the remaining storage space of the active page is not less than the space required for the data to be written, and if the formula is not true, the remaining storage space of the active page is less than the space required for the data to be written.

[0094] After the first power-on initialization, the remaining storage space of the active page is generally not less than the space required for the data to be written, so the operation of switching the standby page to the active page will not be performed.

[0095] Specifically, the method for switching the standby page to the active page is:

[0096] Step d1, erase all data of the standby page, and initialize the data offset address (b_PgOffset) of the standby page to the offset address of the end address of the page header (pageHeader) in the memory page.

[0097] Step d2, read the data header (header) one by one from the data offset address (g_PgOffset) of the active page, starting from the back to the front, to obtain the data number (ID), data length (len), data check value, data unit offset (hofs), and data header check code (magic) stored in the data header (header).

[0098] Step d3, check the current data header check code (magic), if the check is passed, execute step d4, if the check is not passed, skip the data header (header) and directly execute step d5;

[0099] Step d4, judge whether the current data number (ID) has been read, if yes, skip the data header (header) and execute step d5, if no, read the data body (data) according to the data unit offset (hofs) and the data length (len), calculate the check value of the data body (data), and compare it with the data check value in the data header (header), if they are equal, write the data unit (dataItem) to the data offset address (b_PgOffset) of the standby page, and mark the data number (ID) as read in the memory, then update the data offset address (b_PgOffset) of the standby page = the current data offset address (b_PgOffset) of the standby page + data length (len) + data header length (header_len), then execute step d5, if they are not equal, discard the data body (data) and continue to execute step d5;

[0100] It should be noted that when the data unit (dataItem) is written to the standby page, the data unit offset (hofs) in the data header (header) needs to be updated, and the updated data unit offset (hofs) = data offset address (b_PgOffset) of the standby page + data length (len) + data header length (header_len).

[0101] Step d5, judging whether there is a next data header in the active page, if yes, reading the next data header, and returning to step d3, if no, setting the page cycle flag (g_PgCycle) to be equal to the page count of the standby page (Pg_Cnt) plus 1, setting the page magic code of the standby page (Pg_Magic) to be equal to a defined fixed value, and writing the page header of the standby page;

[0102] Step d6, updating the global variables in the memory, the value of the active page address (g_PgAcitve) being equal to the start address of the standby page, and the value of the data offset address of the active page (g_PgOffset) being equal to the data offset address of the standby page (b_PgOffset), thus completing the mutual switching of the standby page and the active page;

[0103] Specifically, the data reading method is as follows:

[0104] Step b1, obtaining the parameters of the data to be read out;

[0105] Step b2, according to the obtained parameters, starting from the data offset address of the active page (g_PgOffset), searching the data to be read out from back to front, and reading the data found first and meeting the parameter requirements into the memory.

[0106] Specifically, in step b1, the obtained parameters are the number of the data to be read out.

[0107] The step b2 includes the following steps:

[0108] Step b2-1, starting from the data offset address of the active page (g_PgOffset), reading the data headers from back to front one by one, and obtaining the data number (ID), data length (len), data check value, data unit offset (hofs) and data header check code (magic) stored in the data header.

[0109] Step b2-2, checking the data header check code (magic), if the check is passed, executing the next step, if the check is not passed, switching the standby page to be the active page, and returning to step b2-1 after the switching is completed;

[0110] Step b2-3, compare the data ID in the header with the ID of the data to be read out, if not identical, then execute the next step, if identical, then read out the data body according to the data unit offset (hofs) and the data length (len), calculate the check value of the data body, and compare with the data check value in the header, if equal, then the data body is the data to be read out, the data reading is successful, and the reading is ended; if not equal, then discard the data body, and execute the next step;

[0111] Step b2-4, judge whether there is a next data header in the active page, if yes, then read the next data header, and return to step b2-2; if not, then return an error, and end the reading.

[0112] In step b2-2, the method for switching the spare page to the active page is the same as step a2.

[0113] Specifically, in step d5 and step b2-4, the method for judging whether there is a next data header in the active page is as follows:

[0114] Step c1, calculate the data unit offset (hofs) of the next data according to the current data unit offset (hofs), the data length (len) and the data header length (header_len); the data unit offset (hofs) of the next data = the current data unit offset (hofs) - the data length (len) - the data header length (header_len);

[0115] Step c2, judge whether the calculated data unit offset (hofs) is greater than the page attribute (pageHeader) length + the data header length (header_len), if yes, then there is the next data, if not, then there is no next data.

[0116] When the Nor Flash memory is powered on again or restarted after power off, the non-first power-on initialization needs to be executed, and the method for non-first power-on initialization includes the following steps:

[0117] S'1, read the page attributes (pageHeader) of two storage pages, and check the page check codes (Pg_Magic) of the two page attributes (pageHeader), if the checks are passed, then execute S'2, if only one check is passed, then execute S'3, if the checks are not passed, then execute the first power-on initialization;

[0118] S'2, select the storage page with large page count (Pg_Cnt) as active page, and find the end address of active page data, update the values of three global variables in memory: the value of active page address (g_PgAcitve) is the start address of the storage page where the active page is located, the value of page switching flag (g_PgCycle) is the page count (Pg_Cnt) of the active page, and the value of data offset address (g_PgOffset) of the active page is the offset address of the data end address in the active page, complete initialization;

[0119] S'3, select the storage page passed through the verification as active page, and find the end address of active page data, update the values of three global variables in memory: the value of active page address (g_PgAcitve) is the start address of the storage page where the active page is located, the value of page switching flag (g_PgCycle) is the page count of the active page, and the value of data offset address (g_PgOffset) of the active page is the offset address of the data end address in the active page, complete initialization.

[0120] In S'2 and S'3, the method for finding the end address of active page data is: starting from the last address of the active page, read the stored data byte by byte from back to front, and when the read data is not 0xFF, mark the address of the next byte of the position as the data end address.

[0121] The application always writes at the data offset address (g_PgOffset) of the active page when writing data to the Nor Flash, without erasing the previous data. Under normal circumstances, only when the active page data is full, the active page switching operation will erase the storage page once, which greatly reduces the operation of erasing and writing the Nor Flash, thereby improving the service life of the Nor Flash. In addition, when reading data, it is read from the data offset address (g_PgOffset) of the active page from back to front, so when the same data number (ID) data is written multiple times, the read data is the last written data, i.e. the latest data, and the previous written data becomes invalid data.

[0122] The application also relates to a storage device configured to perform the wear leveling method for Nor Flash data storage described above.

[0123] The above is the embodiment of the application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the principles of the application, which should also be considered as the protection scope of the application.

Claims

1. A wear leveling method suitable for Nor Flash data storage, characterized in that, The application relates to a method for storing page allocation of a Nor flash memory, an initialization method and a data writing and reading method. The method for storing page allocation comprises the following steps: dividing a Nor flash memory space into two same-size storing pages, determining the starting address and size of the two storing pages; The initialization method comprises the following steps: selecting one of the storing pages as an active page and the other as a standby page, and updating the values of three global variables, i.e. the active page address, the data offset address of the active page and the page switching mark in the memory; The data writing method comprises the following steps: Step a1: obtaining data to be stored; Step a2: judging whether the remaining storage space of the active page is not smaller than the space required by the data to be stored; if yes, writing the data to be stored from the data offset address of the active page; if not, switching the standby page to the active page, and then writing the data to be stored into the new active page; The switching of the standby page to the active page in step a2 comprises the following steps: Step d1: erasing all the data of the standby page; Step d2: reading the data header from the data offset address of the active page from back to front, obtaining the data number, data length, data check value, data unit offset and data header check code stored in the data header; the data unit is composed of a data body and a data header, the data body stores original data, and the length of the data header is a certain value, which is used for recording the attribute information of the data unit; Step d3: checking the data header check code; if the check is passed, step d4 is executed; if the check is not passed, the data header is skipped, and step d5 is executed; Step d4: judging whether the current data number has been read; if yes, the data header is skipped, and step d5 is executed; if not, the data body is read out according to the data unit offset and data length, the check value of the data body is calculated, and is compared with the data check value in the data header; if the two are equal, the data unit is written into the data offset address of the standby page, the data number is marked as having been read, the data offset address of the standby page is updated, and then step d5 is executed; if the two are not equal, the data body is abandoned, and step d5 is executed; When the data unit is written into the standby page, the data unit offset in the data header needs to be updated, and the updated data unit offset = data offset address of the standby page + data length + data header length; Step d5: judging whether there is a next data header in the active page; if yes, the next data header is read, and step d3 is returned; if not, the page switching mark in the memory is added by 1, and is assigned to the page accumulation number of the standby page; the page check code of the standby page is a defined fixed value, and is written into the page attribute of the standby page; Step d6: updating the global variables in the memory: the value of the active page address is the starting address of the storing page where the standby page is located, and the value of the data offset address of the active page is the data offset address of the standby page; the mutual switching of the standby page and the active page is completed; Step a3: updating the data offset address of the active page in the memory, and writing the data; The data reading method comprises the following steps: Step b1: obtaining the parameters of the data to be read; Step b2, according to the acquired parameters, finding the data to be read out from the rear to the front from the data offset address of the active page, and reading the data first found and meeting the parameter requirements into the memory; The initialization method comprises: a first power-on initialization method and a non-first power-on initialization method; the non-first power-on initialization method comprises the following steps: S'1, reading the page attributes of the two storage pages, checking the page check codes of the two page attributes, if the checks are passed, executing S'2, if the check is passed only one, executing S'3, if the checks are not passed, executing the first power-on initialization; S'2, selecting the storage page with the larger page accumulation number as the active page, and finding the end position of the active page data, updating the values of three global variables in the memory: the active page address is the start address of the active page, the page switching flag is the page accumulation number of the active page, and the data offset address of the active page is the offset address of the data end position in the active page, completing the initialization; S'3, selecting the storage page passed the check as the active page, and finding the end position of the active page data, updating the values of three global variables in the memory: the active page address is the start address of the active page, the page switching flag is the page accumulation number of the active page, and the data offset address of the active page is the offset address of the data end position in the active page, completing the initialization; In S'2 and S'3, the method for finding the end address of the active page data is: starting from the last address of the active page, reading the stored data from the rear to the front byte by byte, and when the read data is not 0xFF, marking the address of the next byte of the position as the data end address.

2. The wear leveling method for Nor Flash data storage as claimed in claim 1, wherein, The storage page comprises a page attribute and a data unit; The page attribute is used to define the current storage page as an active page or a standby page. The data unit is a plurality of units, and the data to be stored is stored in units of data units.

3. The wear leveling method for Nor Flash data storage as claimed in claim 2, wherein, The page attribute comprises a page accumulation number and a page check code, the page accumulation number is a variable, and is used to distinguish whether the current storage page is an active page or a standby page; and the page check code is a preset fixed value, and is used to check whether the page attribute is wrong.

4. The wear leveling method for Nor Flash data storage of claim 3, wherein, The data head comprises a data number, a data length, a data check value, a data unit offset, and a data head check code; The data number is used to distinguish different data bodies, and is a preset value; The data length is used to indicate the size of the storage space occupied by the data body; The data check value is a check value calculated according to the data body through a check algorithm, and is used to check whether the data body is damaged; The data unit offset is used to indicate the offset address of the tail of the data unit in the storage page; The data head check code is a preset fixed value, and is used to check whether the data head is damaged.

5. The wear leveling method for Nor Flash data storage as claimed in claim 1, wherein, In step b1, the acquired parameter is the number of data to be read out; Step b2 comprises the following steps: Step b2-1, starting from the data offset address of the active page, reading the data head from the rear to the front byte by byte, and obtaining the data number, the data length, the data check value, the data unit offset, and the data head check code stored in the data head; Step b2-2, comparing the data number in the data head with the acquired parameter, if the data number is equal to the acquired parameter, reading the data unit offset in the data head into the memory, and reading the data in the data unit offset into the memory; if the data number is not equal to the acquired parameter, repeating step b2-1. Step b2-2, check the data header check code, if the check is passed, then execute the next step; if the check is not passed, switch the standby page to the active page, and return to step b2-1 after the switching is completed; Step b2-3, compare the data number in the data header with the number of the data to be read out, if they are not consistent, execute the next step, if they are consistent, read out the data body according to the data unit offset and the data length, calculate the check value of the data body, and compare it with the data check value in the data header, if they are equal, the data reading is successful, and the reading is ended; if they are not equal, discard the data body, and execute the next step; Step b2-4, judge whether there is the next data header in the active page, if there is, read the next data header forward, and return to step b2-2, if there is not, return an error, and end the reading; In step b2-2, the method of switching the standby page to the active page is the same as step a2.

6. The wear leveling method for Nor Flash data storage as claimed in claim 5, wherein, In steps d5 and b2-4, the method of judging whether there is the next data header in the active page is as follows: Step c1, calculate the data unit offset of the next data according to the current data unit offset, the data length and the data header length; Step c2, judge whether the calculated data unit offset is greater than the page attribute length + the data header length, if it is greater, it means that there is the next data header, if it is not greater, it means that there is no next data header.

7. The wear leveling method for Nor Flash data storage as claimed in claim 4, wherein, The method of the first power-on initialization comprises the following steps: S1, specify one of the storage pages as the active page, and the other as the standby page, and update the values of three global variables in the memory: the active page address is the start address of the active page, the data offset address of the active page is the offset address of the page attribute end address in the active page, and the page switching flag is 0; S2, erase the two storage pages, then preset the page check code as a fixed value, the page cumulative number as 0, and write them into the page attribute of the active page, and complete the initialization.

8. The wear leveling method for Nor Flash data storage as claimed in claim 4, wherein, In the data writing process, after writing the data each time, the data offset address of the active page in the memory needs to be updated, and the updated data offset address of the active page = the current data offset address of the active page + the data length + the data header length.

9. A storage device, comprising: The storage device is configured to execute the wear leveling method suitable for Nor Flash data storage according to any one of claims 1-8.

Citation Information

Patent Citations

  • Data storage structure of Flash memory and data manipulation mode thereof

    CN102081577A

  • Data structure, storage method, reading method and transfer method of flash memory

    CN112162933A

  • Intelligent gas meter applying balanced life storage method and data storage method

    CN112525278A