Flash memory data reading and writing method and flash memory data reading and writing device
By dividing the flash block into multiple data recording areas and erasing the blocks when there is no empty recording area, the problems of long erasing time and wear of the flash block are solved, and the rapidity of data updates and data availability after power failure are achieved.
Patent Information
- Application Number
- CN202010893456.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-08-31
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2040-08-31
AI Technical Summary
In the prior art, the entire block needs to be erased every time data is written to the flash block, resulting in a long erase time. Frequent writing will cause wear of the flash block and data loss when the device is powered down or restarted.
Divide the flash block into multiple data recording areas, erase the block only when there is no empty recording area, and increment the identification value when writing data, and use multiple flash blocks in rotation to reduce wear.
Reduces the delay in data updates, reduces wear of flash blocks, and ensures data availability when the device is powered down or restarted.
Smart Images

Figure CN114115700B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to a flash memory data reading and writing method and a flash memory data reading and writing device. Background Art
[0002] Embedded devices typically use data flash or embedded flash to store information such as user configurations and system operation event logs. This information must remain valid even after a power outage or a device restart due to an abnormality. Currently, this information is stored in designated locations within the data flash or embedded flash, specifically designated flash blocks. This information must first be erased per flash block before it can be written to. Summary of the Invention
[0003] The present disclosure relates to a flash memory data reading and writing method and a flash memory data reading and writing device, which divide a flash memory block into multiple data recording areas for writing and reading data, and only perform an erasing operation on the flash memory block when there is no empty data recording area for writing data, without having to erase the flash memory block every time data is written. As a result, the data update delay is shorter, the wear of the flash memory block is reduced, and in some embodiments, data writing anomalies during device power failure or restart can be better handled to ensure data availability.
[0004] According to one aspect of the present disclosure, a flash memory data reading and writing method is provided. The flash memory data reading and writing method includes: determining a data recording area to be written in an empty data recording area of a flash memory, wherein the flash memory includes one or more flash memory blocks, each flash memory block is divided into multiple data recording areas, and each data recording area is used to write a data record; if there is no empty data recording area in the flash memory to be used as the data recording area to be written, erasing at least one flash memory block of the flash memory and determining the data recording area to be written in the erased flash memory block; and writing the data to be written into the data recording area to be written in the form of a data record, wherein the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written.
[0005] According to another aspect of the present disclosure, a flash memory data reading and writing device is provided. The flash memory data reading and writing device includes: a determination module for determining a data recording area to be written in an empty data recording area of a flash memory, wherein the flash memory includes one or more flash memory blocks, each flash memory block is divided into multiple data recording areas, and each data recording area is used to write a data record; an erasing module for erasing at least one flash memory block of the flash memory if there is no empty data recording area in the flash memory to be used as the data recording area to be written, in which case the determination module determines the data recording area to be written in the erased flash memory block; and a writing module for writing the data to be written into the data recording area to be written in the form of a data record, wherein the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written.
[0006] According to another aspect of the present disclosure, a flash memory data reading and writing device is provided. The flash memory data reading and writing device includes a memory and a processor. The memory is used to store program code. The processor is used to perform the following operations when executing the program code: determining a data recording area to be written in an empty data recording area of the flash memory, the flash memory including one or more flash memory blocks, each flash memory block being divided into multiple data recording areas, each data recording area being used to write a data record; if there is no empty data recording area in the flash memory to be used as the data recording area to be written, erasing at least one flash memory block of the flash memory and determining the data recording area to be written in the erased flash memory block; and writing the data to be written in the data recording area to be written in the form of a data record, the data record including a payload field and an identification field for setting an identification value, the identification value of the newly written data record being incremented by a predetermined value based on the identification value of the last data record written.
[0007] According to the embodiments of the present disclosure, there is no need to erase the flash memory block every time data is written. As a result, the delay of data update is shorter, the wear of the flash memory block is reduced, and in some embodiments, data writing anomalies when the device loses power or restarts can be better handled to ensure data availability. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] The aspects, features and advantages of the present disclosure will become clearer and easier to understand through the following description of the embodiments in conjunction with the accompanying drawings, in which:
[0009] Figure 1 is a flow chart of a flash memory data reading and writing method according to an embodiment of the present disclosure;
[0010] Figure 2 is an example of data recording according to an embodiment of the present disclosure;
[0011] Figure 3 is a schematic diagram of determining a data recording area to be read and a data recording area to be written according to an embodiment of the present disclosure;
[0012] Figure 4 is a schematic diagram of determining a data recording area to be written when a flash memory includes three flash memory blocks according to an embodiment of the present disclosure;
[0013] Figure 5 is a flow chart of a flash memory data reading and writing method according to an embodiment of the present disclosure;
[0014] Figure 6 This is a flowchart of initialization of the flash memory data reading and writing method when the system is powered on according to an embodiment of the present disclosure;
[0015] Figure 7 is a schematic module diagram of a flash memory data reading and writing device according to an embodiment of the present disclosure;
[0016] Figure 8 4 is a schematic block diagram of another flash memory data reading and writing device according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0017] The present disclosure will be described in detail below with reference to exemplary embodiments of the present disclosure. However, the present disclosure is not limited to the embodiments described herein and may be implemented in many different forms. The described embodiments are intended only to make the present disclosure thorough and complete and to fully convey the concepts of the present disclosure to those skilled in the art. The features of the various described embodiments may be combined or replaced with each other unless expressly excluded or should be excluded based on the context.
[0018] As mentioned above, the current method for storing data in flash memory is to utilize designated flash blocks within the flash memory. When data needs to be written or updated, the corresponding flash blocks need to be erased. Since the entire flash block must be erased for each write operation, the erase process takes a long time, resulting in a longer delay in data updates. Furthermore, if a certain type of data is frequently written or updated, the flash blocks corresponding to that type of data will be frequently erased. However, each flash block has a limited number of erase times. Once the erase times exceed a threshold, the flash block becomes invalid and can no longer store data. Furthermore, if the device loses power or restarts while erasing or writing a flash block, the data on that flash block may have already been erased, while the data to be written has not been written, resulting in data loss on that flash block.
[0019] Embodiments of the present disclosure provide a flash memory data reading and writing method and a flash memory data reading and writing device, which divide a flash memory block into multiple data recording areas for writing and reading data, and perform an erase operation on the flash memory block only when there is no empty data recording area for writing data. There is no need to erase the flash memory block every time data is written. As a result, the data update delay is shorter, the wear of the flash memory block is reduced, and in some embodiments, data writing anomalies during device power failure or restart can be better handled to ensure data availability.
[0020] Figure 1 FIG. 1 is a flowchart of a flash memory data reading and writing method 100 according to an embodiment of the present disclosure. Figure 1 Steps S110-S130 correspond to the process of writing data. In order to write data, it is first necessary to determine where in the flash memory the data is to be written. As described above, the flash memory may include one or more flash memory blocks, which are used to store data and are the minimum operation units of the erase operation. In the flash memory data reading and writing method 100 according to the embodiment of the present disclosure, instead of using the flash memory block as the unit for writing and reading data, each flash memory block is divided into multiple data recording areas, and each data recording area is used as the unit for writing and reading data, and each data recording area is used to write and store a data record. Each of the above-mentioned data recording areas can be an area of fixed length in the flash memory block for storing data. In this way, for example, each flash memory block is divided into 10 data recording areas, and the data will be erased only once at least 10 data updates are performed, thereby reducing the delay of data updates and reducing the wear of the flash memory block. On the basis of using the data recording area as the data writing and reading unit, if there is an empty data recording area in the flash memory, the data will be written into the empty data recording area. Therefore, if Figure 1 As shown, in step S110 of the flash memory data reading and writing method 100, a data recording area to be written is determined in the empty data recording area of the flash memory. However, if there is no empty data recording area in the flash memory that can be used as the data recording area to be written, then it is necessary to perform an erase operation on at least one flash memory block so that the flash memory has an empty data recording area again. Therefore, Figure 1As shown, when there is no empty data recording area in the flash memory to be used as the data recording area to be written, in step S120 of the flash memory data reading and writing method 100, it is necessary to erase at least one flash memory block of the flash memory and determine the data recording area to be written in the erased flash memory block. After the data recording area to be written is determined in step S110 or S120, in step S130, the data to be written is written to the data recording area to be written in the form of a data record. After the data is written, the latest data is already stored in the data recording area to be written. Therefore, optionally, in order to facilitate subsequent read operations, the data recording area to be written can be directly determined as the data recording area to be read, and the data in the data recording area to be read can be read when performing a read operation.
[0021] According to an embodiment of the present disclosure, the data to be written is written in the form of data records. Figure 2 is an example of a data record 200 according to an embodiment of the present disclosure. The data record 200 may include a payload field and an identification field for setting an identification value (ID). The payload field corresponds to the data to be written. When each data is written, an identification value is also set for it. The identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written, for example, by 1. For example, if the identification value of the last data record written is 5, the identification value of the newly written data record is 5+the predetermined value (if the predetermined value is 1, the identification value of the newly written data record is 6).
[0022] The identification value can provide a reference for determining a data recording area to be read or a data recording area to be written when performing a subsequent data reading or writing operation. Figure 3 It is a schematic diagram of determining the data recording area to be read and the data recording area to be written according to an embodiment of the present disclosure. The maximum identification value corresponds to the most recently written data (i.e., the data written for the last time). Therefore, the non-empty data recording area storing the data record with the maximum identification value is the data recording area where the last write operation was performed. Such a data recording area can be used as the data recording area to be read. Therefore, the data recording area to be read can be determined in the non-empty data recording area of the flash memory, and the data recording area to be read is the data recording area storing the data record with the maximum identification value. For example, in Figure 3 In FIG, a flash memory block is divided into multiple data recording areas, including a non-empty data recording area and an empty data recording area. Assuming that the data record with the largest identification value is stored in the non-empty data recording area 310, then the data recording area 310 is the data recording area where the last write operation was performed, and can be used as the data recording area to be read.
[0023] The next data write operation can be performed in an empty data recording area that is logically continuous (i.e., immediately adjacent to) the data recording area 310, i.e., the data recording area 320. Therefore, such an empty data recording area is determined as the data recording area to be written. Thus, in one embodiment, if there is an empty data recording area in the flash memory, determining the data recording area to be written in the above step S110 may include determining the data recording area to be written as an empty data recording area (e.g., data recording area 320) that is logically continuous with the non-empty data recording area (e.g., data recording area 310) in the flash memory that stores the data record with the maximum identification value. In this specification, the terms "logically continuous", "logically immediately adjacent", or "logically adjacent" mean that the order in which two data recording areas are accessed is consecutive, and they may or may not be adjacent in physical location.
[0024] In another embodiment, if there are empty data recording areas but no non-empty data recording areas in the flash memory, that is, the flash memory is entirely composed of empty data recording areas, then determining the data recording area to be written in the above step S110 may include determining the first data recording area in the flash memory as the data recording area to be written.
[0025] Optionally, the data recording area to be read and the data recording area to be written may each be indicated by a pointer pointing to the corresponding data recording area.
[0026] To further reduce the wear of the flash memory blocks and reduce data loss, a flash memory having multiple flash memory blocks can be used. In other words, the flash memory can include at least two flash memory blocks for storing data. In this way, after all data recording areas in one flash memory block are filled, the next write operation can be performed in another different flash memory block. Therefore, even if an erase operation is required for the next write operation, that is, when there are no empty data recording areas in other flash memory blocks, other flash memory blocks can be erased instead of the current flash memory block (that is, the flash memory block storing the last written data). Therefore, compared to the method of using only one flash memory block, using multiple flash memory blocks means that the current flash memory block does not need to be erased immediately after it is filled, thereby reducing the wear of the flash memory blocks. In addition, because the last written data and the data to be written currently correspond to data recording areas in different flash memory blocks, the last written data will not be erased. Therefore, even if a data write anomaly occurs due to a power outage or restart of the device, the last written data can be read, reducing the risk of data loss and ensuring data availability. Therefore, in one embodiment, the flash memory can include at least two flash memory blocks. If there is no empty data recording area in the at least two flash memory blocks that can be used as a data recording area to be written, at least one flash memory block that does not store a data record with a maximum identification value may be erased. Figure 4FIG. 1 is a schematic diagram of determining a data recording area to be written when a flash memory includes three flash memory blocks according to an embodiment of the present disclosure. Figure 4 In the embodiment, flash memory blocks 1, 2, and 3 are respectively divided into multiple data recording areas. Assuming that the data record with the maximum identification value is stored in the non-empty data recording area 410 in flash memory 1, the data recording area 410 is the data recording area where the last write operation was performed, and thus it is determined as the data recording area to be read. All data recording areas in flash memory block 1 are already full, so it is necessary to determine the data recording area to be written in flash memory block 2 and flash memory block 3. In one example, if flash memory block 2 has not yet been written to data and flash memory block 3 is already full, then the empty data recording area 420 in flash memory block 2 can be determined as the data recording area to be written. In another example, if both flash memory block 2 and flash memory block 3 are already full, then an erase operation can be performed on at least one of flash memory block 2 and flash memory block 3, and the data recording area to be written can be determined in the erased flash memory block. For example, an erase operation is performed on flash memory block 2, and the empty data recording area 420 in flash memory block 2 formed after erasure is determined as the data recording area to be written; or an erase operation is performed on both flash memory blocks 2 and 3, and the empty data recording area 420 in flash memory block 2 formed after erasure is determined as the data recording area to be written. In this way, the wear of flash memory block 1 is reduced, and even if the device loses power or restarts, since flash memory block 1 is not erased, the availability of the data on flash memory block 1, especially the data last written to flash memory block 1 (written in data recording area 410 of flash memory block 1), is guaranteed. That is, the historical record can be effectively preserved.
[0027] In the case where the flash memory includes at least two flash memory blocks, in order to further reduce the wear of the flash memory blocks, the erase operation can be further limited to the next flash memory block adjacent to the currently full flash memory block. In other words, the flash memory block to be written and the flash memory block to be erased can be determined in sequence according to the logical order of the flash memory blocks. Therefore, in one embodiment, the data recording area to be written is determined in sequence from at least two flash memory blocks of the flash memory. For example, referring to Figure 4 In the example, if flash block 1 is not full, the data recording area to be written will continue to be determined in flash block 1. If flash block 1 is full, the data recording area to be written will be determined in flash block 2; if flash block 2 is also full, the data recording area to be written will be determined in flash block 3; then, if flash block 3 is also full, the data recording area to be written will be determined in flash block 1. Further, if Figure 4If the data record with the maximum identification value is stored in the data recording area 410 of flash memory block 1, and there is no empty data recording area in flash memory blocks 1, 2, and 3 to be used as the data recording area to be written, then the next flash memory block (i.e., flash memory block 2) of the flash memory block (i.e., flash memory block 1) storing the data record with the maximum identification value (i.e., the data record stored in data recording area 410) is erased, and the data recording area to be written is determined in the next flash memory block (i.e., flash memory block 2). As a result, for a flash memory block, it will not be erased until all other flash memory blocks have been erased once, thereby further reducing the wear of the flash memory block. For example, if the flash memory includes 10 flash memory blocks, each of which is divided into 10 data recording areas, flash memory block 1 will not be erased until flash memory blocks 2-9 have been erased once. In other words, flash memory block 1 will be erased once every 100 data updates, thereby reducing the wear of flash memory block 1. The same is true for the other flash memory blocks 2-9.
[0028] During the erase and write operation, if the device suddenly loses power or restarts, it may cause the corresponding data record area to fail to write or fail to write the data record correctly. Therefore, when reading data, it is necessary to detect whether the read data is the correctly stored data. To this end, return to the reference Figure 2 In one embodiment, the data record 200 may further include a checksum field for storing a checksum code. The checksum code may be generated, for example, using a checksum algorithm based on the data to be written into the payload field and the identifier value in the identifier field. Various known checksum algorithms may be used to generate the checksum code, such as a checksum, an XOR check, a cyclic redundancy check (e.g., CRC-16), and the like.
[0029] In the case where the data record includes a check field, when reading the data in the data record, a check code stored therein can be used to check whether the data in the payload field read is the correctly stored data. Figure 1 The flash memory data reading and writing method described may also include Figure 5 The process shown. Figure 5 is a flow chart of a flash memory data reading and writing method 500 according to an embodiment of the present disclosure, wherein steps S510-S550 correspond to the process of reading data. In order to read data, the location of the data to be read must be determined first. For this purpose, as already mentioned above, Figure 3 As discussed, in step S510, a data recording area to be read may be determined in a non-empty data recording area of the flash memory, where the data recording area to be read is a data recording area storing a data record having a maximum identification value.
[0030] In step S520, a data record is read from the determined data record area to be read and verified using the checksum in the data record. For example, when a CRC-16 algorithm is used, the checksums of the payload field and the ID field in the read data record can be calculated using the CRC-16 algorithm and compared with the actual checksums in the read data record. If they are equal, the verification is considered successful; otherwise, the verification is considered unsuccessful.
[0031] In step S530, it is determined whether the verification is successful. If the verification is successful, then in step S540, the data record read is determined to be a valid data record. If the verification is unsuccessful, then in step S550, the data record area to be read is updated based on the identification value of the data record, and the previous non-empty data record area logically adjacent to the currently read data record area is read to find a valid data record. That is, in step S550, the data record area to be read is determined to be a data record area that stores data records with identification values that are decremented by the predetermined value compared to the identification value of the read data record, and returns to step S520 to read and verify the data records based on the updated data record area to be read until a valid data record is found. For example, in Figure 3 Initially, the non-empty data recording area 310 is determined as the data recording area to be read because it stores the data record with the largest identification value. However, if verification based on the check code in the data record stored in the non-empty data recording area 310 fails, the data recording area to be read needs to be further updated to a data recording area that stores a data record with an identification value (e.g., 5-1=4) obtained by decrementing the identification value (e.g., 5) of the data record stored in the data recording area 310 by a predetermined value (e.g., 1), i.e., the non-empty data recording area 330. If verification based on the check code in the data record stored in the data recording area 330 still fails, the data recording area to be read needs to be further updated to the data recording area 340 until verification succeeds and a valid data record is found.
[0032] Figure 6 FIG6 is a flowchart 600 of the initialization of the flash memory data reading and writing method when the system is powered on according to an embodiment of the present disclosure. Figure 6As shown, when the system loaded with flash memory is powered on, the flash memory data reading and writing method according to the embodiment of the present disclosure also includes steps S610-S630. In step S610, it is necessary to determine whether all data recording areas of the flash memory are empty data recording areas or non-empty data recording areas. The data recording area to be read must be located in the non-empty data recording area, and the data recording area to be written must be located in the empty data recording area. In step S620, in order to determine which of the non-empty data recording areas is the data recording area to be read, it is possible to first determine whether the data record is a valid data record based on the check code in the data record stored in each non-empty data recording area. This can exclude non-empty data recording areas that have data records but are invalid, such as those data recording areas that were not correctly written due to a power outage or restart of the device during a write operation. The process of determining whether a data record is a valid record based on the check code has been described above with reference to step S520 and will not be repeated here. In step S630, the data recording area to be read and the data recording area to be written are determined. The data recording area storing the valid data record with the largest identification value is determined as the data recording area to be read, and the data recording area to be written is determined to be the empty data recording area that is logically closest to the determined data recording area to be read. As described above, the valid data record with the largest identification value is the most recent record written, so the data recording area storing such a data record is determined as the data recording area to be read. Furthermore, the next data write operation should logically continue from the data recording area to be read, that is, it should be the empty data recording area that is logically closest to the data recording area to be read, and therefore, such an empty data recording area is determined as the data recording area to be written.
[0033] Return to reference Figure 2 To facilitate and expedite data record area detection, in one embodiment, data record 200 may further include an encapsulation header field to identify the beginning of the data record. The encapsulation header field may contain special characters such as 0xAA or 0x55, which facilitate detection. This allows data to be read and written only in the data record area corresponding to the data record with the encapsulation header field, saving time searching for the data record area in memory.
[0034] Therefore, the flash memory data reading and writing method according to the embodiment of the present disclosure reduces the delay of data update, reduces the wear of the flash memory block, and in some embodiments, can better handle data writing anomalies when the device is powered off or restarted, thereby ensuring data availability.
[0035] Figure 7 The flash memory data reading and writing device 700 according to the embodiment of the present disclosure is a schematic block diagram. The flash memory data reading and writing device 700 can be used to implement reference Figure 1-6 The flash memory data reading and writing method described above. The flash memory data reading and writing device 700 may include a determination module 710, an erasing module 720 and a writing module 730.
[0036] The determination module 710 may determine a data recording area to be written in an empty data recording area of the flash memory. The flash memory includes one or more flash blocks. Each flash block is divided into multiple data recording areas. Each data recording area is used to write one data record.
[0037] If there is no empty data recording area in the flash memory as the data recording area to be written, the erasing module 720 can erase at least one flash memory block of the flash memory. In this case, the determining module 710 can determine the data recording area to be written in the erased flash memory block.
[0038] The writing module 730 can write the data to be written into the data record area to be written in the form of a data record, where the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written.
[0039] In one embodiment, the determination module 710 may determine the data recording area to be written as an empty data recording area in the flash memory that logically follows the non-empty data recording area that stores the data record with the largest identification value. If there is no non-empty data recording area in the flash memory, the determination module 710 may determine the data recording area to be written as any first empty data recording area in the flash memory.
[0040] In one embodiment, the flash memory may include at least two flash memory blocks. If there is no empty data recording area in at least two flash memory blocks as the data recording area to be written, the erasing module 720 may erase at least one flash memory block that does not store the data record with the maximum identification value.
[0041] In one embodiment, the determination module 710 may determine the data recording area to be written from the at least two flash memory blocks in sequence when determining the data recording area to be written. If there is no empty data recording area in the at least two flash memory blocks to be used as the data recording area to be written, the erasing module 720 may erase the flash memory block next to the flash memory block storing the data record with the largest identification value, and the determination module 710 may determine the data recording area to be written in the next flash memory block.
[0042] In one embodiment, the data recording area may further include a check field for storing a check code.
[0043] In one embodiment, Figure 7As shown, the determination module 710 may further include a reading and verification module 740. The determination module 710 may determine a data record area to be read in a non-empty data record area of the flash memory, where the data record area to be read is a data record area storing a data record with a maximum identification value. The reading and verification module 740 may read a data record from the data record area to be read and verify it using a verification code. If the verification is successful, the reading and verification module 740 may determine the read data record as a valid data record; if the verification is unsuccessful, the determination module 710 may determine the data record area to be read as a data record area storing a data record with an identification value that is decremented by a predetermined value from the identification value of the read data record, and the reading and verification module 740 may read and verify data records based on the updated data record area to be read until a valid data record is found.
[0044] In one embodiment, when a system equipped with a flash memory is powered on, the determination module 710 can determine whether all data recording areas of the flash memory are empty data recording areas or non-empty data recording areas. Next, the read and verify module 740 can determine whether the data records stored in the non-empty data recording areas are valid data records based on the verification code. The determination module 710 can then determine the data recording area storing the valid data record with the largest identification value as the data recording area to be read, and determine the data recording area to be written to as the empty data recording area logically closest to the data recording area to be read.
[0045] In one embodiment, after the writing module 730 completes writing, the determining module 710 may determine the data recording area to be written as the data recording area to be read.
[0046] In one embodiment, the data record may further include an encapsulation header field for identifying the start of the data record.
[0047] The flash memory data reading and writing device according to the embodiment of the present disclosure reduces the delay of data update, reduces the wear of the flash memory block, and can better handle data writing anomalies when the device is powered off or restarted, thereby ensuring data availability.
[0048] Figure 8 FIG1 is a schematic block diagram of another flash memory data reading and writing device 800 according to an embodiment of the present disclosure. The flash memory data reading and writing device 800 can be any device used for reading and writing flash memory data, such as a notebook computer, a desktop computer, a tablet computer, etc. The type of device 800 does not constitute a limitation to the present disclosure. Figure 8As shown, the flash memory data reading and writing device 800 may include a memory 810 and a processor 820. The memory 810 is used to store program code. The processor 820 is used to perform the following operations when executing the program code stored in the memory 810: determining a data recording area to be written in an empty data recording area of the flash memory, the flash memory includes one or more flash memory blocks, each flash memory block is divided into multiple data recording areas, and each data recording area is used to write a data record. If there is no empty data recording area in the flash memory to be used as the data recording area to be written, then erase at least one flash memory block of the flash memory, and determine the data recording area to be written in the erased flash memory block. The data to be written is written into the data recording area to be written in the form of a data record, the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written.
[0049] In one embodiment, when executing the program code, the processor 820 further performs the following operations: determining the data recording area to be written to be an empty data recording area in the flash memory that is logically consecutive to the non-empty data recording area that stores the data record with the largest identification value. If there is no non-empty data recording area in the flash memory, determining the data recording area to be written to be any first empty data recording area in the flash memory.
[0050] In one embodiment, the flash memory includes at least two flash memory blocks, and the processor 820 also performs the following operations when executing the program code: if there is no empty data recording area in at least two flash memory blocks to be used as the data recording area to be written, then erase at least one flash memory block that does not store the data record with the maximum identification value.
[0051] In one embodiment, the processor 820 also performs the following operations when executing the program code: when determining the data recording area to be written, the data recording area to be written is determined in sequence from at least two flash memory blocks; if there is no empty data recording area in at least two flash memory blocks to be used as the data recording area to be written, then the next flash memory block of the flash memory block that stores the data record with the largest identification value is erased, and the data recording area to be written is determined in the next flash memory block.
[0052] In one embodiment, the data recording area further includes a check field for storing a check code.
[0053] In one embodiment, when executing the program code, the processor 820 further performs the following operations: determining a data record area to be read in a non-empty data record area of the flash memory, wherein the data record area to be read is a data record area storing a data record with a maximum identification value. Reading a data record from the data record area to be read and verifying it using a checksum. If the check succeeds, determining the read data record as a valid data record. If the check fails, determining the data record area to be read as a data record area storing a data record with an identification value that is decremented by a predetermined value from the identification value of the read data record, and reading and verifying data records based on the updated data record area to be read until a valid data record is found.
[0054] In one embodiment, when a system equipped with a flash memory is powered on, the processor 820, while executing program code, further performs the following operations: determining whether all data recording areas of the flash memory are empty data recording areas or non-empty data recording areas; determining whether data records stored in non-empty data recording areas are valid data records based on a checksum; determining a data recording area storing a valid data record with a maximum identification value as a data recording area to be read, and determining a data recording area to be written to as an empty data recording area that is logically closest to the data recording area to be read.
[0055] In one embodiment, when executing the program code, the processor 820 further performs the following operation: after completing writing, determining the data recording area to be written as the data recording area to be read.
[0056] In one embodiment, the data record further includes an encapsulation header field for identifying the start of the data record.
[0057] The flash memory data reading and writing device according to the embodiment of the present disclosure reduces the delay of data update, reduces the wear of the flash memory block, and can better handle data writing anomalies when the device is powered off or restarted, thereby ensuring data availability.
[0058] The block diagrams of the devices, equipment, and systems described in this disclosure are intended to be illustrative examples only and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As will be appreciated by those skilled in the art, these circuits, devices, equipment, and systems may be connected, arranged, or configured in any manner as long as the desired purpose is achieved.
[0059] Those skilled in the art should understand that the above-mentioned specific embodiments are merely examples and not limitations, and that various modifications, combinations, partial combinations and replacements may be made to the embodiments of the present disclosure according to design requirements and other factors. As long as they are within the scope of the attached claims or their equivalents, they fall within the scope of rights to be protected by the present disclosure.
Claims
1. A method for reading and writing flash memory data, comprising: determining a data recording area to be written in an empty data recording area of a flash memory, wherein the flash memory includes at least two flash memory blocks, each flash memory block is divided into a plurality of data recording areas, and each data recording area is used to write one data record; Writing the data to be written into the data recording area to be written in the form of a data record, wherein the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written; as well as If there is no empty data recording area in the at least two flash memory blocks to be used as the data recording area to be written, then erasing the next flash memory block of the flash memory block storing the data record with the largest identification value, and determining the data recording area to be written in the next flash memory block, Wherein, when determining the data recording area to be written, the data recording area to be written is determined from the at least two flash memory blocks in sequence, The data recording area to be written is determined to be an empty data recording area in the flash memory that logically continues to a non-empty data recording area in which a data record with a maximum identification value is stored.
2. The flash memory data reading and writing method according to claim 1, wherein: The data recording area further includes a check field for storing a check code.
3. The flash memory data reading and writing method according to claim 2, further comprising: determining a data recording area to be read in a non-empty data recording area of the flash memory, wherein the data recording area to be read is a data recording area storing a data record having a maximum identification value; Reading data records from the data record area to be read and verifying them using the verification code; If the verification is successful, the read data record is determined to be a valid data record; If the verification is unsuccessful, the data record area to be read is determined to be a data record area that stores data records with identification values that are decremented by the predetermined value from the identification value of the read data record, and data records are read and verified based on the updated data record area to be read until a valid data record is found.
4. The flash memory data reading and writing method according to claim 2, further comprising: determining whether all data recording areas of the flash memory are empty data recording areas or non-empty data recording areas; determining whether the data record stored in the non-empty data record area is a valid data record based on the check code; as well as The data recording area storing the valid data record having the largest identification value is determined as the data recording area to be read, and the data recording area to be written is determined as an empty data recording area logically closest to the data recording area to be read.
5. The flash memory data reading and writing method according to claim 1 , further comprising: After the writing is completed, the data recording area to be written is determined as the data recording area to be read.
6. The flash memory data reading and writing method according to claim 1, wherein: The data record further includes an encapsulation header field, which is used to identify the start of the data record.
7. A flash memory data reading and writing device, comprising: a determination module, configured to determine a data recording area to be written in an empty data recording area of a flash memory, wherein the flash memory includes at least two flash memory blocks, each flash memory block is divided into a plurality of data recording areas, and each data recording area is used to write one data record; a writing module, configured to write the data to be written into the data recording area to be written in the form of a data record, wherein the data record includes a payload field and an identification field for setting an identification value, wherein the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written; as well as an erasing module, wherein if there is no empty data recording area in the at least two flash memory blocks to be used as the data recording area to be written, the erasing module erases the next flash memory block of the flash memory block storing the data record with the largest identification value, and the determining module determines the data recording area to be written in the next flash memory block, Wherein, when determining the data recording area to be written, the determining module sequentially determines the data recording area to be written from the at least two flash memory blocks. The determining module determines the data recording area to be written as an empty data recording area in the flash memory that logically continues to store the data record with the largest identification value.
8. The flash memory data reading and writing device according to claim 7, wherein: The data recording area further includes a check field for storing a check code.
9. The flash memory data reading and writing device according to claim 8, wherein: The determining module further determines a data recording area to be read in a non-empty data recording area of the flash memory, wherein the data recording area to be read is a data recording area storing a data record with a maximum identification value. The flash memory data reading and writing device further includes a reading and verification module, which reads data records from the data recording area to be read and verifies the data using the verification code, wherein: If the verification is successful, the reading and verification module determines the read data record as a valid data record; If the verification is unsuccessful, the determination module determines the data record area to be read as a data record area that stores data records with identification values that are decremented by the predetermined value from the identification value of the read data record, and the reading and verification module reads data records and verifies based on the updated data record area to be read until a valid data record is found.
10. The flash memory data reading and writing device according to claim 9, wherein: When the system with the flash memory loaded is powered on: The determining module determines whether all data recording areas of the flash memory are empty data recording areas or non-empty data recording areas; The reading and verification module determines whether the data record stored in the non-empty data record area is a valid data record based on the verification code; as well as The determination module determines the data recording area storing the valid data record with the maximum identification value as the data recording area to be read, and determines the data recording area to be written as an empty data recording area logically closest to the data recording area to be read.
11. The flash memory data reading and writing device according to claim 7, further comprising: After the writing module completes the writing, the determining module determines the data recording area to be written as the data recording area to be read.
12. The flash memory data reading and writing device according to claim 7, wherein: The data record further includes an encapsulation header field (0xAA), which is used to identify the start of the data record.
13. A flash memory data reading and writing device, comprising a memory and a processor, wherein: The memory is used to store program code; The processor is configured to perform the following operations when executing the program code: determining a data recording area to be written in an empty data recording area of a flash memory, wherein the flash memory includes at least two flash memory blocks, each flash memory block is divided into a plurality of data recording areas, and each data recording area is used to write one data record; Writing the data to be written into the data recording area to be written in the form of a data record, wherein the data record includes a payload field and an identification field for setting an identification value, and the identification value of the newly written data record is incremented by a predetermined value based on the identification value of the last data record written; as well as If there is no empty data recording area in the at least two flash memory blocks to be used as the data recording area to be written, then erasing the next flash memory block of the flash memory block storing the data record with the largest identification value, and determining the data recording area to be written in the next flash memory block, Wherein, when determining the data recording area to be written, the data recording area to be written is determined from the at least two flash memory blocks in sequence, The data recording area to be written is determined to be an empty data recording area in the flash memory that logically continues to a non-empty data recording area in which a data record with a maximum identification value is stored.
Citation Information
Patent Citations
Block-based flash memory and its operation method
CN101923516A
Flash memory management method and flash memory device
CN102841851A