Data storage methods, devices, and sensors
By employing a structure of N storage sectors and M storage blocks in the FLASH memory, and combining data status and write count to locate the previous storage block position, the problems of inaccurate data management and frequent erasure in FLASH memory are solved, achieving higher data reliability and system stability.
Patent Information
- Application Number
- CN202511056006.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-30
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2045-07-30
AI Technical Summary
In existing embedded systems, especially sensor-related intelligent devices, the segmented and block management of FLASH memory suffers from problems such as insufficient data identification accuracy, insufficient coordination between sector state switching and data migration, and insufficient storage efficiency and reliability due to fixed erase trigger conditions.
A data storage method is adopted, which configures the FLASH memory into a structure of N storage sectors and each sector is equally divided into M storage blocks. Each time data is written, it is written sequentially in a loop. The position of the previous storage block is located by combining the data status and the number of writes, so as to avoid frequent erasure and ensure the continuity and accuracy of data writing.
It extends the lifespan of FLASH memory, enhances data storage reliability and system fault tolerance, and solves the problems of easy data loss and poor anomaly recovery in traditional solutions.
Smart Images

Figure CN120560592B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data storage technology, specifically to a data storage method, apparatus, and sensor. Background Technology
[0002] In embedded systems, especially in sensor-related intelligent devices, microcontrollers (MCUs) often rely on on-chip FLASH memory to store non-volatile data such as calibration parameters and configuration information to ensure data integrity after power failure. FLASH memory is widely used due to its high integration and non-volatility; however, its physical characteristics limit its lifespan, requiring the sector to be erased before data modification. Frequent erasures shorten its lifespan. Therefore, segmented block storage technology has become an important direction for optimizing data management.
[0003] Existing technologies have implemented partitioned and block-based management of FLASH memory. For example, patent application CN102063379A discloses such a scheme, which typically divides the memory into multiple sectors, each sector into multiple data storage blocks, and performs data reading and writing on the basis of storage blocks. The sector status is managed by identifying the data storage blocks. After a sector is filled, the process switches to the next one. When the last empty sector remains, the erase and write frequency is reduced by backing up the valid data of the earliest written sector, erasing the earliest sector, and reusing it in a cyclical manner.
[0004] However, while existing solutions offer improvements in multi-sector circular storage, they still have limitations in practical applications: First, the identification of valid data relies solely on a simple comparison of the write order, which is insufficient in accuracy; second, when facing sector state switching and data migration, the coordination between identifier updates and read / write erasure is inadequate, and errors or data loss can easily occur due to mismatches between the state and the data in abnormal situations; finally, the triggering conditions for sector erasure are fixed, relying on the filling state without dynamically adjusting in conjunction with data validity, which affects storage efficiency optimization and leaves room for improvement in both reliability and efficiency. Summary of the Invention
[0005] In view of the above problems, this application provides a data storage method, apparatus, and sensor to solve the above technical problems.
[0006] In a first aspect, this application provides a data storage method applied to a FLASH memory, wherein the FLASH memory is configured to have N sectors designated as storage sectors for storing data, and each storage sector is equally divided into M storage blocks; the data storage method includes:
[0007] Each time data is written, the data is written to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector. When N storage sectors are filled, the next time data is written, the writing operation will return to the first storage block of the first storage sector.
[0008] Each time data is written, it includes stored data, verification parameters of the stored data, and the number of times the stored data is written. The number of times the data is written is incremented by 1 when data is written for the next time. When the number of times the data is written reaches a preset maximum value, the number of times the data is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of each storage block.
[0009] The step of writing the data into a storage block according to the arrangement order of the storage sectors and the arrangement order of the storage blocks in their respective storage sectors includes:
[0010] The position of the previous storage block can be located based on the data status of the data stored in each of the storage blocks, or based on the data status and write count of the data stored in each of the storage blocks.
[0011] Based on the previous storage block, determine the location of the target storage block for the data to be written, and write the data to the target storage block;
[0012] The data status includes valid and invalid. When the verification parameter is correct, the data status is valid; otherwise, the data status is invalid. The previous storage block is the storage block used when the data was written last time.
[0013] Secondly, this application provides a data storage device, comprising:
[0014] The data writing module is used to write the data to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector each time data is written. When N storage sectors are filled, the next time data is written, the module returns to the first storage block of the first storage sector for writing operation.
[0015] Each time data is written, it includes stored data, verification parameters of the stored data, and the number of times the stored data is written. The number of times the data is written is incremented by 1 when data is written for the next time. When the number of times the data is written reaches a preset maximum value, the number of times the data is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of each storage block.
[0016] The step of writing the data into a storage block according to the arrangement order of the storage sectors and the arrangement order of the storage blocks in their respective storage sectors includes:
[0017] The position of the previous storage block can be located based on the data status of the data stored in each of the storage blocks, or based on the data status and write count of the data stored in each of the storage blocks.
[0018] Based on the previous storage block, determine the location of the target storage block for the data to be written, and write the data to the target storage block;
[0019] The data status includes valid and invalid. When the verification parameter is correct, the data status is valid; otherwise, the data status is invalid. The previous storage block is the storage block used when the data was written last time.
[0020] Thirdly, this application provides a sensor that stores data using the data storage method described in the first aspect.
[0021] The data storage method, apparatus, and sensor provided in this application are applied to FLASH memory. The FLASH memory is configured with N storage sectors and M equally divided storage blocks within each sector. Each time data is written, the location of the previously written storage block is determined based on the data status (valid or invalid) of the data in each storage block, or by combining the data status with the number of writes. The target storage block to be written to is then determined based on this previous block. Specifically, this method eliminates the need for erasing FLASH sectors during each write operation. Instead, data is written sequentially between storage blocks in different sectors, avoiding wear caused by frequent erasures. Simultaneously, by judging the data status or combining the number of writes, the location of the last successful write can be accurately identified, thus finding the storage block to be written to. Furthermore, after a system restart, the system can quickly restore to the previous valid data state, thereby improving data reliability and system fault tolerance. The data storage method provided in this application solves the problems of short FLASH lifespan, easy data loss, and poor fault recovery capability caused by traditional single-sector centralized storage, and has the beneficial effects of improving FLASH lifespan and enhancing data storage reliability and fault tolerance.
[0022] These or other aspects of this application will become more apparent in the following description of the embodiments. Attached Figure Description
[0023] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0024] Figure 1 A flowchart of a data storage method provided in an embodiment of this application is shown.
[0025] Figure 2 Another flowchart of the data storage method provided in the embodiments of this application is shown.
[0026] Figure 3 This paper illustrates a possible state diagram of the data storage status of N storage sectors in the data storage method provided in this application embodiment.
[0027] Figure 4 This paper illustrates another possible state diagram of the data state of the stored data in N storage sectors of the data storage method provided in the embodiments of this application.
[0028] Figure 5 This diagram illustrates another possible state of the data storage status of N storage sectors in the data storage method provided in this application embodiment.
[0029] Figure 6 This paper illustrates another flowchart of a data storage method provided in an embodiment of the present application.
[0030] Figure 7 This paper illustrates another flowchart of a data storage method provided in an embodiment of the present application.
[0031] Figure 8 This paper illustrates another flowchart of a data storage method provided in an embodiment of the present application.
[0032] Figure 9 A schematic diagram of a data storage device provided in an embodiment of this application is shown.
[0033] Figure 10 A schematic diagram of the sensor provided in an embodiment of this application is shown.
[0034] Figure 11 A schematic diagram of an electronic device provided in an embodiment of this application is shown.
[0035] Figure 12 A schematic diagram of a computer storage medium provided in an embodiment of this application is shown. Detailed Implementation
[0036] To enable those skilled in the art to better understand the solutions of this application, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the scope of protection of this application.
[0037] In the embodiments of this application, it should be noted that, in this document, relational terms such as first and second are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations.
[0038] Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0039] Furthermore, in the embodiments of this application, "multiple" refers to two or more. Therefore, in the embodiments of this application, "multiple" can also be understood as "at least two". "At least one" can be understood as one or more, such as one, two, or more. For example, including at least one means including one, two, or more, and is not limited to which ones are included. For example, including at least one of A, B, and C, then it could include A, B, C, A and B, A and C, B and C, or A and B and C.
[0040] This application provides a data storage method applied to a FLASH memory. The FLASH memory is configured to use N sectors as storage sectors for data storage, and each storage sector is equally divided into M storage blocks as basic data writing units. Optionally, the N storage sectors can be set as N consecutive sectors in the FLASH memory, or as N non-consecutive sectors, with each storage block having the same size. In practical applications, the above partitioning method can be flexibly set according to the specific FLASH model, system requirements, and data update frequency. For example, N can be 2 or a larger integer, and M is reasonably selected according to the parameter size to achieve an optimized balance between space utilization and storage efficiency.
[0041] The data storage method provided in this application includes:
[0042] Each time data is written, the data is written to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector. When N storage sectors are filled, the next time data is written, the loop returns to the first storage block of the first storage sector to perform the write operation.
[0043] Each write operation includes the stored data, the verification parameters of the stored data, and the number of writes. The write count is incremented by 1 with each subsequent write operation. When the write count reaches a preset maximum value, it is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of storage blocks. Optionally, the initial value is the starting value for the write count, which can be set to any integer. Typically, this starting value is set to 0 for easy counting and management. That is, the maximum write count only needs to be greater than the sum of the number of storage blocks. When the initial value is set to 0, the maximum write count is determined by the data type. For example, when set to an 8-bit data type, the maximum value is 2^8 = 255. When set to a 16-bit data type, the maximum value is 2^16 - 1 = 65535 (65536 values between 0 and 65535). When the initial value is not 0, the difference between the maximum write count and the initial value is expressed by the data type. For example, when set to an 8-bit data type, the difference is 2^8 - 1 = 255. When set to a 16-bit data type, the difference is 2^16 - 1 = 65535.
[0044] Specifically, each time data is written, it starts from the first block of the first storage sector and writes sequentially until all blocks of the current storage sector are full. Then, it automatically switches to the first block of the next storage sector to continue writing, and so on, until all N storage sectors are full. Finally, it loops back to the first block of the first storage sector to continue writing, thus forming a continuous and cyclical data writing pattern. This method avoids the lifespan degradation problem caused by frequent erasure of the same sector in traditional FLASH storage solutions, significantly extending the lifespan of the FLASH memory.
[0045] Figure 1 A flowchart of a data storage method provided in an embodiment of this application is shown, such as... Figure 1 As shown in the embodiments of this application, the step of writing data to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector specifically includes:
[0046] The location of the previous storage block can be determined based on the data status of the data stored in each storage block, or based on the data status and the number of writes to the data stored in each storage block. The data status includes valid and invalid; a valid data status indicates correct verification parameters, while an invalid data status indicates invalid data. The previous storage block is the storage block used during the previous data write.
[0047] Optionally, the data storage method provided in this application, during the first write operation after system power-on initialization, locates the previously written storage block (i.e., the previous storage block) by the data status of the data stored in the storage block, or by combining the data status of the data stored in the storage block with the number of writes. Specifically, the data stored in the storage block includes two types: stored data, i.e., data written each time, and non-stored data, which may correspond to storage block failure, memory erasure state, etc. Therefore, by detecting whether the data stored in different storage blocks is valid and combining the number of writes of that data to determine whether it is the most recently written stored data, the location of the previous storage block is used as the position of the previous storage block, thereby determining the target storage block to be written to this time. In subsequent write operations during system operation, since the information of the previously written storage block can be remembered, the target storage block to be written can be directly determined based on the information of the previously written storage block, without repeatedly scanning the storage area, thus improving write efficiency and response speed.
[0048] Optionally, the verification parameter settings can be implemented through CRC check, checksum verification, or other verification algorithms that can be used to verify data integrity. These algorithms are used to determine whether the data is complete and valid each time it is read or recovered. Specifically, the verification parameters are different each time the stored data is written. The verification parameters are obtained by using the stored data as input parameters to the verification algorithm (such as CRC check or checksum verification) and the algorithm outputs the results. The verification parameters are written to the storage block along with the stored data when the data is written. When it is necessary to verify whether the data is valid, it is only necessary to determine whether the verification parameters of the data are correct. If the verification parameters are correct, it means that the data stored in the storage block is valid. If the verification is incorrect or there are no verification parameters for the stored data, it means that the data stored in the storage block is invalid. The absence of verification parameters for the stored data may correspond to conditions such as storage block failure or the memory being in an erased state.
[0049] The location of the target storage block for the data to be written is determined based on the previous storage block, and the data is written to the target storage block. Optionally, if the location of the previous storage block is known, then the next storage block after the previous storage block is the target storage block. For example, if the previous storage block is the first storage block of the first storage sector, then the target storage block is the second storage block of the first storage sector; if the previous storage block is the Mth storage block of the first storage sector, then the first storage sector is already full, and the target storage block is the first storage block of the second storage sector.
[0050] The data storage method provided in this application is applied to a FLASH memory. The FLASH memory is configured with N storage sectors and M equally divided storage blocks within each sector. Each time data is written, the location of the previously written storage block is determined based on the data status (valid or invalid) of the data in each storage block, or by combining the data status with the number of writes. The target storage block to be written to is then determined based on this previous block. Specifically, this method does not require erasing the FLASH sectors during each write operation. Instead, it sequentially writes data between storage blocks in different storage sectors, avoiding wear caused by frequent erasures. Simultaneously, by judging the data status or combining the number of writes, the location of the last successful write can be accurately identified, thus finding the storage block to be written to. Furthermore, after a system restart, it can quickly restore to the previous valid data state, thereby improving data reliability and system fault tolerance. The data storage method provided in this application can solve the problems of short FLASH lifespan, easy data loss, and poor fault recovery capability caused by traditional single-sector centralized storage, and has the beneficial effects of improving FLASH lifespan and enhancing data storage reliability and fault tolerance.
[0051] In some embodiments, Figure 2 Another flowchart of the data storage method provided in an embodiment of this application is shown, such as... Figure 2 As shown in the embodiments of this application, the step of locating the position of the previous storage block based on the data status of the data stored in each storage block, or locating the position of the previous storage block based on the data status and write count of the data stored in each storage block, includes:
[0052] Traverse N storage sectors and obtain the first and second storage block indices for each sector. For each sector, the first storage block index is the index of the first storage block with valid data, and the second storage block index is the index of the last storage block with valid data. Optionally, the storage block index refers to the number or address used to identify and locate each storage block within the FLASH memory. The specific implementation depends on the design and implementation details of the FLASH memory. Each storage block has its specific size and a unique identifier, i.e., the storage block index. It is understood that the definition of storage block index is a known term in the field of data storage, therefore, this embodiment of the application will not provide further explanation here.
[0053] The data status of the stored data in N storage sectors is determined based on the first storage block index and the second storage block index. Optionally, in the step of determining the data status of the stored data in N storage sectors based on the first storage block index and the second storage block index, the data status of the stored data in the N storage sectors can have the following three possibilities: Figures 3 to 5 Each of these three cases is shown as a possible scenario, such as Figures 3 to 5 As shown, these three scenarios are: 1. All data in N storage sectors are invalid. 2. Only one storage sector contains valid data. 3. At least two storage sectors contain valid data.
[0054] When the data stored in N memory sectors is all invalid, this situation can only occur after the FLASH memory is initialized and powered on, or when the FLASH memory fails. In this case, N memory sectors are erased, preset data is written to the first block of the first memory sector, and the first block of the first memory sector is set as the target memory block, ending the current write operation. The target memory block at this point becomes the previous memory block for the next data write. The preset data here is usually a default parameter defined by the designer to configure the memory sectors of the FLASH memory.
[0055] When only one storage sector contains valid data, the storage block corresponding to the second storage block index is marked as the previous storage block. Because only one storage sector contains valid stored data, the index of the storage block whose last data status is valid, i.e., the second storage block index, can be directly determined to be the storage block used when the data was written previously, i.e., the previous storage block.
[0056] When the data status of at least two storage sectors is valid, the storage sector to which the previous storage block belongs and its location within that sector are determined based on a comparison of the difference between the first and second write counts between any two adjacent storage sectors. This situation indicates that multiple storage sectors are already full, therefore, it is necessary to locate the previous storage block by comparing the differences between the first and second write counts of different sectors.
[0057] Wherein, the first write count difference is the difference between the write count of the storage block corresponding to the first storage block index of the j-th storage sector and the write count of the storage block corresponding to the second storage block index of the (j+1)-th storage sector, and the second write count difference is the difference between the write count of the storage block corresponding to the first storage block index of the (j+1)-th storage sector and the write count of the storage block corresponding to the second storage block index of the j-th storage sector. The value of j ranges from 1 to P-1, and P is the number of valid storage sectors in the data state.
[0058] The data storage method provided in this application solves the technical problem of data management chaos and reliability degradation caused by the difficulty in accurately determining the previous write position in a multi-sector scenario of FLASH memory by dynamically locating the previous storage block based on the data status and write count of the storage block. It achieves the technical effects of efficiently and accurately locating the previous storage block, improving the continuity of data writing and the stability of the storage system, and helping to extend the service life of FLASH memory.
[0059] In some embodiments of this application, the step of determining the data status of the stored data of N storage sectors based on the first storage block index and the second storage block index specifically includes the following three cases:
[0060] When neither the first nor the second storage block index is obtained, the data status of the data stored in the marked storage sector is invalid. Optionally, if neither the first nor the second storage block index exists in the storage sector, it means that the storage sector does not store any storage data, or even if the storage sector stores storage data, the stored data is invalid.
[0061] When the storage block corresponding to the second storage block index is the Mth storage block of its respective storage sector, the data status of the data stored in the marked storage sector is valid. Optionally, a storage sector has a total of M storage blocks. When the last storage block with a valid data status is the Mth storage block of that storage sector, it obviously indicates that all storage blocks in that storage sector store valid storage data.
[0062] When the storage block corresponding to the second storage block index is not the Mth storage block of its storage sector, the data status of the data stored in the marked storage sector includes both valid and invalid data. Optionally, contrary to the previous case, when the last storage block with a valid data status is not the Mth storage block of the storage sector, it obviously indicates that the storage blocks before this storage block all store valid storage data, and the storage blocks after this storage block store invalid data.
[0063] In some embodiments of this application, when only one storage sector stores data with a valid data status, the step of determining the location of the target storage block for the current data to be written based on the previous storage block and writing data to the target storage block includes the following three cases:
[0064] When the current storage block is the i-th storage block in its storage sector, the (i+1)-th storage block in the storage sector is set as the target storage block, and a write operation is performed on the target storage block. The value of i ranges from 1 to M-1. Specifically, if the current storage block and the target storage block belong to the same storage sector, data can be written directly to the target storage block.
[0065] When the current storage block is the Mth storage block of its storage sector, set the first storage block of the next storage sector as the target storage block, and determine whether the next storage sector is in an erasure state.
[0066] If the next storage sector is in an erase state, then a write operation is performed on the target storage block;
[0067] If the next storage sector is not in the erase state, then erase the next storage sector and perform a write operation to the target storage block.
[0068] Specifically, when the current storage block and the target storage block do not belong to the same storage sector, meaning the current write operation involves writing data across sectors, it is first determined whether the next storage sector needs to be erased. If erasure is required, the storage sector is erased first, and then the write operation is performed. If erasure is not required, the write operation is performed directly. In the general rules of FLASH memory, if a storage sector is in an erased state, all data stored in the storage sector is FF.
[0069] The data storage method provided in this application solves the technical problem of write failure or data inconsistency caused by improper handling of sector erase status during continuous writing of FLASH memory by dynamically deciding whether to perform an erase operation based on whether the next storage sector is in an erase state when writing across sectors. This achieves the technical effect of ensuring the correctness and integrity of each write operation, improving data writing efficiency and system stability, and reducing unnecessary erase operations to extend the service life of FLASH memory.
[0070] In some embodiments, Figure 6 This illustrates yet another flowchart of the data storage method provided in an embodiment of this application, such as... Figure 6 As shown in this embodiment, when the data status of data stored in at least two of the storage sectors is valid, the step of determining the storage sector to which the previous storage block belongs and the position of the previous storage block within the belonging storage sector based on the comparison result of the first write count difference and the second write count difference between each pair of adjacent storage sectors includes:
[0071] Get the difference between the first write count and the second write count between the j-th storage sector and the (j+1)-th storage sector.
[0072] The negative values in the difference between the first and second write counts are corrected using a modulo operation based on the preset maximum value.
[0073] Compare the difference in the first number of writes after modulo operation correction with the difference in the second number of writes after modulo operation correction.
[0074] If the difference in the first number of writes after modulo operation correction does not exceed the difference in the second number of writes after modulo operation correction, then the storage block corresponding to the first storage block index of the j-th storage sector is the previous storage block.
[0075] If the difference in the first number of writes after modulo operation correction is greater than the difference in the second number of writes after modulo operation correction, then determine whether j+1 is equal to P.
[0076] If J+1 is not equal to P, then increment the value of j by 1 and loop back to step: get the difference between the first and second write counts between the j-th and (j+1)-th storage sectors;
[0077] If j+1 equals P, then the storage block corresponding to the second storage block index of the j+1th storage sector is the previous storage block.
[0078] The initial value of j is 1.
[0079] For example, taking the data status of four storage sectors A, B, C, and D in sequence as valid as an example, i.e., P=4. In this case, based on the comparison result of the first write count difference and the second write count difference between every two adjacent storage sectors, the steps to determine the storage sector to which the previous storage block belongs and the position of the previous storage block within that storage sector are as follows:
[0080] First, obtain the difference between the first and second write counts between storage sector A and storage sector B. Specifically, assume that the first and second block indices of storage sector A are Sector_Index_Head_A and Sector_Index_Tail_A, respectively; the first and second block indices of storage sector B are Sector_Index_Head_B and Sector_Index_Tail_B, respectively; the write counts of the blocks corresponding to the first and second block indices of storage sector A are Block_Data_CNT_Head_A and Block_Data_CNT_Tail_A, respectively; and the write counts of the blocks corresponding to the first and second block indices of storage sector B are Block_Data_CNT_Head_B and Block_Data_CNT_Tail_B, respectively. Then, the difference in the first write count is:
[0081] A_H_B_T = Block_Data_CNT_Head_A - Block_Data_CNT_Tail_B.
[0082] The difference in the second write count is:
[0083] B_H_A_T = Block_Data_CNT_Head_B - Block_Data_CNT_Tail_A.
[0084] After obtaining the difference between the first and second write counts, the negative values in the first and second write count differences are then corrected using a modulo operation based on the preset maximum value. Specifically, assuming the initial value is 0 and the preset maximum value is 2^ 16 -1 is equivalent to 65535. There are 65536 numbers between the initial value and the preset value. Therefore, the following conditional operator can be applied to the difference in the first number of writes:
[0085] A_H_B_T = A_H_B_T<0?(A_H_B_T + 65536):A_H_B_T.
[0086] The following conditional operator is applied to the difference in the number of writes:
[0087] B_H_A_T = B_H_A_T<0?(B_H_A_T + 65536):B_H_A_T.
[0088] Where, ? is a ternary operator in programming languages, also known as the conditional operator. A_H_B_T = A_H_B_T<0?(A_H_B_T + 65536): A_H_B_T means: if the value of A_H_B_T is less than 0, then convert it to an equivalent 16-bit unsigned integer (i.e., add 65536); otherwise, leave the original value unchanged. B_H_A_T = B_H_A_T<0?(B_H_A_T + 65536): B_H_A_T means: if the value of B_H_A_T is less than 0, then convert it to an equivalent 16-bit unsigned integer (i.e., add 65536); otherwise, leave the original value unchanged.
[0089] After performing modulo operation correction on the difference between the first and second write counts, the first write count difference after modulo operation correction and the second write count difference after modulo operation correction are compared. If B_H_A_T ≥ A_H_B_T, that is, the difference between the first and second write counts does not exceed the difference between the second and third write counts, it means that the previous storage block is located in storage sector A and the specific index is: Sector_Index_Tail_A, that is, the second storage block index of storage sector A.
[0090] Conversely, if B_H_A_T < A_H_B_T, meaning the difference in the first write count is greater than the difference in the second write count, it indicates that the previous storage block is not located in storage sector A. In this case, the difference in the first write count and the difference in the second write count between storage sector B and storage sector C are compared again. If the comparison result indicates that the previous storage block is located in storage sector B, the method ends. Otherwise, if the comparison result indicates that the previous storage block is not located in storage sector B, the comparison between storage sector C and storage sector D is continued. Storage sector D is the last stored data and contains valid storage sectors. Therefore, this comparison is the last one. At this point, the previous storage block can be determined to be located in storage sector C or storage sector D based on the comparison result.
[0091] It is understandable that the comparison process between two adjacent memory sectors, such as the comparison process between memory sector B and memory sector C, and the comparison process between memory sector C and memory sector D, is the same as the comparison process between memory sector A and memory sector D. Therefore, it will not be repeated here.
[0092] The data storage method provided in this application, by introducing a modulo operation correction and comparison mechanism based on the difference in the number of writes between adjacent sectors when the data states of multiple storage sectors are all valid, dynamically determines the sector to which the previous storage block belongs and its location. This solves the technical problem that FLASH memory cannot accurately locate the previous write position in multi-sector continuous write scenarios, thus causing data chaos or management errors. It achieves the technical effects of accurately and efficiently identifying the location of the previous storage block, improving the continuity of data writing and the reliability of the storage system, and extending the service life of the FLASH memory.
[0093] In some embodiments, Figure 7 This illustrates yet another flowchart of the data storage method provided in an embodiment of this application, such as... Figure 7 As shown in the embodiments of this application, the step of locating the position of the previous storage block based on the data status of the data stored in each storage block, or locating the position of the previous storage block based on the data status and write count of the data stored in each storage block, further includes:
[0094] When an abnormal power outage occurs during the data writing process, the data written before the power outage is cleared upon the next power-on, and the previous storage block located when the data was written before the power outage is set as the current previous storage block. This embodiment of the application achieves data rollback through this mechanism, further improving data operation security.
[0095] In some embodiments of this application, the data written each time includes, in addition to the stored data and the verification parameters of the stored data, a formatting flag parameter. Each time data is read, if the read formatting flag parameter differs from the set editable parameter, an operation to erase N storage sectors is performed. Specifically, each time data is written, the editable parameter is configured as a first parameter, the formatting flag parameter is configured as a first parameter, and each time data is read, the editable parameter is configured as either the first parameter or the second parameter.
[0096] Optionally, in this embodiment, when the editable parameter and the formatting flag parameter are the same, no operation is performed on the storage sector; when the editable parameter and the formatting flag parameter are different, all storage sectors are erased. Specifically, when writing data, the programmable parameter is the same as the formatting flag parameter; when reading data, the programmable parameter is configured according to the need for formatting. This editable parameter is a parameter that can be manually customized, thereby enabling active control of the formatting operation of the storage sector by defining the editable parameter, reducing the difficulty of subsequent data maintenance, and improving the convenience of data maintenance.
[0097] Furthermore, when each written data includes both formatting flag parameters and verification parameters, the data status determination condition can be further set as follows: when the verification parameter is correct and the formatting flag parameter is the first parameter, the data status is valid; otherwise, the data status is invalid.
[0098] In some embodiments, Figure 8 This illustrates yet another flowchart of the data storage method provided in an embodiment of this application, such as... Figure 8 As shown in the embodiment of this application, after determining the location of the target storage block for the data to be written based on the previous storage block and writing data to the target storage block, the method further includes:
[0099] Read back the data written to the target storage block and compare whether the data after writing is consistent with the data before writing.
[0100] If the data written is the same as the data before writing, then the write is marked as successful.
[0101] If the data after writing is inconsistent with the data before writing, continue writing the current data to the next storage block until the data after writing is consistent with the data before writing.
[0102] If the data after writing is inconsistent with the data before writing, and the number of times the execution steps continue to write the current data to the next storage block exceeds the total number of storage blocks, then a FLASH storage abnormality is marked. This indicates that the FLASH memory is faulty and needs to be repaired.
[0103] This application's embodiments employ a write-after-read comparison mechanism to ensure the authenticity and reliability of each write operation, avoiding "false writes" caused by physical damage to the FLASH memory or voltage fluctuations. Furthermore, upon detecting a write failure, it automatically switches to the next storage block to continue attempting to write, improving the system's fault tolerance and continuous operation capabilities. When multiple attempts fail, the system promptly reports FLASH abnormalities, helping to detect hardware failures early and prevent subsequent data loss or system crashes.
[0104] It is understood that in the embodiments of this application, the data storage method is embodied in the form of executable instructions, which are called and executed by a hardware processor (such as a microcontroller, microprocessor, etc.) to control the FLASH memory to complete the corresponding data storage operations. Therefore, the understanding of each step should not be limited to its specific implementation in the program.
[0105] Based on the above data storage method, this application provides a data storage device. Figure 9 A schematic diagram of a data storage device provided in an embodiment of this application is shown, such as... Figure 9 As shown, the data storage device provided in this application embodiment includes:
[0106] The data writing module is used to write data to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector each time data is written. When N storage sectors are filled, the next time data is written, it will return to the first storage block of the first storage sector for writing operation.
[0107] Each write operation includes the stored data, the verification parameters of the stored data, and the number of times the stored data is written. The number of writes is incremented by 1 for each subsequent write operation. When the number of writes reaches a preset maximum value, the number of writes is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of storage blocks.
[0108] Data is written to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block within its respective storage sector, including:
[0109] The position of the previous storage block can be located based on the data status of the data stored in each storage block, or based on the data status and write count of the data stored in each storage block.
[0110] Based on the previous storage block, determine the location of the target storage block for the data to be written, and then write the data to the target storage block.
[0111] The data status includes valid and invalid. When the verification parameters are correct, the data status is valid; otherwise, the data status is invalid. The previous storage block is the storage block used when the data was written last time.
[0112] For other details regarding the implementation of the above technical solution by each module in the above data storage device, please refer to the description in the data storage method provided in the above embodiments of the invention, which will not be repeated here.
[0113] Based on the above data storage method, this application embodiment also provides a sensor 10. Figure 10 A schematic diagram of the sensor provided in an embodiment of this application is shown, as follows: Figure 10 As shown, sensor 10 stores data using the data storage method described in the above embodiments.
[0114] Based on the above data storage method, this application also provides an electronic device. Figure 11 A schematic diagram of an electronic device provided in an embodiment of this application is shown, such as... Figure 11 As shown, the electronic device 110 provided in this embodiment includes a processor 111 and a memory 112 coupled to the processor 111. The memory 112 stores a computer program, which, when executed by the processor 91, causes the processor 111 to perform the steps of the data storage method in the above embodiment.
[0115] For other details regarding the implementation of the above technical solution by the processor 111 in the above electronic device, please refer to the description of the data storage method provided in the above embodiments of the invention, which will not be repeated here.
[0116] The processor 111 can also be called a CPU (Central Processing Unit). The processor 111 may be an integrated circuit chip with signal processing capabilities. The processor 111 may also be a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The general-purpose processor may be a microprocessor, or the processor 111 may be any conventional processor.
[0117] Based on the above data storage method, embodiments of this application also provide a computer-readable storage medium. Figure 12 A schematic diagram of a computer storage medium provided in an embodiment of this application is shown, such as... Figure 12As shown, this application embodiment also provides a computer-readable storage medium 120, on which a readable computer program 121 is stored. The computer program 121 can be stored in the storage medium in the form of a software product, including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, magnetic disks or optical disks, ROM (Read-Only Memory), RAM (Random Access Memory), or terminal devices such as computers, servers, mobile phones, and tablets.
[0118] The above description, in conjunction with specific embodiments, provides a further detailed explanation of this application and should not be construed as limiting the specific implementation of this application to these descriptions. For those skilled in the art, various simple deductions or substitutions can be made without departing from the concept of this application, and all such modifications and substitutions should be considered within the scope of protection of this application.
Claims
1. A data storage method, characterized in that, Applied to FLASH memory, the FLASH memory is configured to set N sectors as storage sectors for storing data, and each storage sector is equally divided into M storage blocks; The data storage method includes: Each time data is written, the data is written to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector. When N storage sectors are filled, the next time data is written, the writing operation will return to the first storage block of the first storage sector. Each time data is written, it includes stored data, verification parameters of the stored data, and the number of times the stored data is written. The number of times the data is written is incremented by 1 when data is written for the next time. When the number of times the data is written reaches a preset maximum value, the number of times the data is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of each storage block. The step of writing the data into a storage block according to the arrangement order of the storage sectors and the arrangement order of the storage blocks in their respective storage sectors includes: The position of the previous storage block can be located based on the data status of the data stored in each of the storage blocks, or based on the data status and write count of the data stored in each of the storage blocks. Based on the previous storage block, determine the location of the target storage block for the data to be written, and write the data to the target storage block; The data status includes valid and invalid. When the verification parameter is correct, the data status is valid; otherwise, the data status is invalid. The previous storage block is the storage block used when the data was written last time. After the step of determining the location of the target storage block for the current data to be written based on the previous storage block and writing data to the target storage block, the method further includes: Read back the data written to the target storage block and compare whether the data after writing is consistent with the data before writing. If the data after writing is the same as the data before writing, then the write is marked as successful; If the data after writing is inconsistent with the data before writing, continue writing the current data to the next storage block until the data after writing is consistent with the data before writing; If the data after writing is inconsistent with the data before writing, and the number of times the execution steps continue to write the current data to the next storage block exceeds the total number of storage blocks, then the FLASH memory is marked as abnormal.
2. The data storage method as described in claim 1, characterized in that, The step of locating the position of the previous storage block based on the data status of the data stored in each of the storage blocks, or locating the position of the previous storage block based on the data status and write count of the data stored in each of the storage blocks, includes: Traverse N storage sectors and obtain the first storage block index and the second storage block index for each storage sector. The first storage block index is the index of the first storage block with valid data status, and the second storage block index is the index of the last storage block with valid data status. The data status of the stored data in the N storage sectors is determined based on the first storage block index and the second storage block index; When the data status of the data stored in N storage sectors is invalid, erase N storage sectors, write preset data to the first storage sector, set the first storage block of the first storage sector as the target storage block, and end the current write operation. When the data status of data stored in only one of the storage sectors is valid, the storage block corresponding to the second storage block index is marked as the previous storage block; When the data status of data stored in at least two of the storage sectors is valid, the storage sector to which the previous storage block belongs and the position of the previous storage block in the storage sector are determined based on the comparison result of the first write count difference and the second write count difference between each pair of adjacent storage sectors. Wherein, the first write count difference is the difference between the write count of the storage block corresponding to the first storage block index of the j-th storage sector and the write count of the storage block corresponding to the second storage block index of the (j+1)-th storage sector, and the second write count difference is the difference between the write count of the storage block corresponding to the first storage block index of the (j+1)-th storage sector and the write count of the storage block corresponding to the second storage block index of the j-th storage sector, where j ranges from 1 to P-1, and P is the number of valid storage sectors in the data state.
3. The data storage method as described in claim 2, characterized in that, Determining the data status of the N storage sectors based on the first storage block index and the second storage block index includes: When the first storage block index and the second storage block index are not obtained, the data status of the data stored in the storage sector is marked as invalid. When the storage block corresponding to the second storage block index is the Mth storage block of the storage sector, the data status of the data stored in the storage sector is marked as valid. When the storage block corresponding to the second storage block index is not the Mth storage block of the storage sector, the data status of the data stored in the storage sector is marked as both valid and invalid.
4. The data storage method as described in claim 2, characterized in that, When only one of the storage sectors contains valid data, the step of determining the location of the target storage block for the current data to be written based on the previous storage block and writing data to the target storage block includes: When the preceding storage block is the i-th storage block of the storage sector, the (i+1)-th storage block of the storage sector is set as the target storage block, and a write operation is performed on the target storage block. The value of i ranges from 1 to M-1. When the previous storage block is the Mth storage block of its storage sector, the first storage block of the next storage sector is set as the target storage block, and it is determined whether the next storage sector is in an erasure state. If the next storage sector is in an erase state, then a write operation is performed on the target storage block; If the next storage sector is not in an erased state, then erase the next storage sector and perform a write operation to the target storage block.
5. The data storage method as described in claim 2, characterized in that, When the data status of data stored in at least two of the storage sectors is valid, determining the storage sector to which the previous storage block belongs and the location of the previous storage block within that sector, based on a comparison of the difference between the first and second write counts between any two adjacent storage sectors, includes: Obtain the difference between the first write count and the second write count between the j-th storage sector and the (j+1)-th storage sector; The negative values in the difference between the first and second write counts are corrected using a modulo operation based on the preset maximum value; Compare the difference in the first number of writes after modulo operation correction with the difference in the second number of writes after modulo operation correction. If the difference in the first number of writes after modulo operation correction does not exceed the difference in the second number of writes after modulo operation correction, then the storage block corresponding to the second storage block index of the j-th storage sector is the previous storage block; If the difference in the first number of writes after modulo operation correction is greater than the difference in the second number of writes after modulo operation correction, then determine whether j+1 is equal to P. If j+1 is not equal to P, then increment the value of j by 1 and return to step: obtain the first write count difference and the second write count difference between the j-th storage sector and the (j+1)-th storage sector; If j+1 equals P, then the storage block corresponding to the second storage block index of the j+1th storage sector is the previous storage block; The initial value of j is 1.
6. The data storage method as described in claim 1, characterized in that, The method of locating the position of the previous storage block based on the data status of the data stored in each storage block, or based on the data status and write count of the data stored in each storage block, further includes: When an abnormal power failure occurs during the data writing process, the data written before the power failure is cleared upon the next power-on, and the previous storage block located when the data was written before the power failure is set as the current previous storage block.
7. The data storage method as described in claim 1, characterized in that, Each written data also includes: a formatting flag parameter. Each time data is read, if the read formatting flag parameter is different from the set editable parameter, an operation to erase N storage sectors is performed. Specifically, each time data is written, the editable parameter is configured as the first parameter, the formatting flag parameter is configured as the first parameter, and each time data is read, the editable parameter is configured as either the first parameter or the second parameter. In the step of locating the position of the previous storage block based on the data status of the data stored in each storage block, or based on the data status and write count of the data stored in each storage block, The data status is valid when the verification parameter is correct and the formatting flag parameter is the first parameter; otherwise, the data status is invalid.
8. A data storage device, characterized in that, Applied to a FLASH memory, the FLASH memory is configured to use N sectors as storage sectors for storing data, and each storage sector is equally divided into M storage blocks. The data storage device includes: The data writing module is used to write the data to a storage block according to the arrangement order of each storage sector and the arrangement order of each storage block in its respective storage sector each time data is written. When N storage sectors are filled, the next time data is written, the module returns to the first storage block of the first storage sector for writing operation. Each time data is written, it includes stored data, verification parameters of the stored data, and the number of times the stored data is written. The number of times the data is written is incremented by 1 when data is written for the next time. When the number of times the data is written reaches a preset maximum value, the number of times the data is reset to a preset initial value. The difference between the preset maximum value and the initial value is greater than the sum of the number of each storage block. The step of writing the data into a storage block according to the arrangement order of the storage sectors and the arrangement order of the storage blocks in their respective storage sectors includes: The position of the previous storage block can be located based on the data status of the data stored in each of the storage blocks, or based on the data status and write count of the data stored in each of the storage blocks. Based on the previous storage block, determine the location of the target storage block for the data to be written, and write the data to the target storage block; The data status includes valid and invalid. When the verification parameter is correct, the data status is valid; otherwise, the data status is invalid. The previous storage block is the storage block used when the data was written last time. After the step of determining the location of the target storage block for the current data to be written based on the previous storage block and writing data to the target storage block, the method further includes: Read back the data written to the target storage block and compare whether the data after writing is consistent with the data before writing. If the data after writing is the same as the data before writing, then the write is marked as successful; If the data after writing is inconsistent with the data before writing, continue writing the current data to the next storage block until the data after writing is consistent with the data before writing; If the data after writing is inconsistent with the data before writing, and the number of times the execution steps continue to write the current data to the next storage block exceeds the total number of storage blocks, then the FLASH memory is marked as abnormal.
9. A sensor, characterized in that, The sensor stores data using the data storage method described in any one of claims 1 to 7.
Citation Information
Patent Citations
Data storage method of FLASH memory
CN102063379A
Method for prolonging service life of electrically erasable storage element
CN107797765A