A memory data storage method and system
By dividing the target and candidate areas in the memory, switching to save data and erasing expired data in time, the data loss and life shortening of the FLASH memory when power is off is solved, and more efficient data storage and memory life extension are achieved.
Patent Information
- Application Number
- CN202210845553.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-19
- Publication Date
- 2025-08-12
- Estimated Expiration
- 2042-07-19
AI Technical Summary
The existing FLASH memory is prone to reduced life due to data loss and abnormality when power is off, and backup data needs to be frequently erased during system recovery, resulting in shortening memory life.
The storage space is divided into a target area and a candidate area. By judging the remaining space and data writing time sequence, the storage area is switched and the expired data is erased in time, reducing the number of single erases.
It improves the service life of the memory, ensures accurate data storage when power is off, and reduces the write loss and life loss of the memory.
Smart Images

Figure CN115202579B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer storage technology, and in particular to a method and system for storing memory data. Background Art
[0002] When using a microcontroller to save data, EEPROM (Electrically Erasable Programmable Read-Only Memory) and FLASH (Flash Memory) are commonly considered. Both EEPROM and FLASH are non-volatile memories, meaning they retain data even after power is removed.
[0003] However, microcontrollers generally lack EEPROM, so Flash memory must be used to emulate it. Before writing data to Flash memory, the corresponding erase location must be erased. A power outage during data recording can cause data loss or abnormalities in the Flash memory. Existing Flash memories typically use a backup method for power-off protection during data writes, meaning each data write is written twice to the Flash memory.
[0004] Regarding the above-mentioned related technologies, the erase unit corresponding to the FLASH memory is usually large, and the dynamic data generated by the system during actual use will gradually increase over time. After the system is powered on again to restore the data, the backed-up data must be erased from a fixed position each time. The large number of erases may easily reduce the life of the FLASH memory. Summary of the Invention
[0005] In order to reduce the number of single erases of a memory and increase the service life of the memory, the present application provides a memory data preservation method and system.
[0006] This application provides a method for preserving data in a memory device during power-off, which adopts the following technical solution:
[0007] A method for saving data in a memory after power failure, comprising the following steps:
[0008] Dividing a target area and a candidate area set based on a storage space of the memory, wherein the candidate area set includes at least one candidate area;
[0009] Writing storage data into the target area, wherein the storage data includes configuration data, dynamic data, and power-off save data;
[0010] Determine whether the remaining storage space size of the target area is less than a preset value, and if so, extract the last written storage data as valid data based on the order of the storage data writing time;
[0011] Updating any one of the candidate regions in the candidate region set as a new target region, and writing the valid data into the new target region;
[0012] The original target area is erased, the original target area is updated as the new candidate area, and dynamic data is continuously written into the new target area, wherein the original target area is the target area before the update.
[0013] By adopting the above technical solution, before writing stored data to the memory, the memory's storage space is divided into a target area and at least one candidate area. The target area is used to store data, while the candidate area is used for backup data storage. Since the program relies on configuration data to operate, the configuration data is stored in the memory to facilitate data access by the program. Dynamic data and power-off save data are data generated by the program during operation. When writing stored data, a preset value is set and compared with the remaining storage space in the target area to determine whether the target area can continue to write stored data. If the remaining storage space in the current target area is determined to be insufficient, the most recently written data (i.e., the latest data) is extracted from the current target area and then written to a new target area created by updating any candidate area. After the latest data is written to the new target area, the original target area is erased and updated to a new candidate area, which is placed in the candidate area set so that it can be reselected from the candidate area set and written to later.
[0014] Throughout the entire data writing and saving process, there's always reserved storage space for writing data. Furthermore, at least two storage spaces are used to switch between stored data. During this switching process, only the latest data is written, allowing for timely clearing of outdated data. This fully utilizes memory space and balances data writing losses. Furthermore, when restoring data after a power outage, there's no need to erase excessive amounts of data at once, reducing the risk of memory lifespan being shortened by excessive erase cycles.
[0015] Optionally, the preset value is the sum of the storage space occupied by the power-off saved data and the storage space occupied by the dynamic data.
[0016] By adopting the above technical solution, since power outages can occur at any time, to ensure sufficient storage space to store the power-off save data during a power outage, the preset value is set to the sum of the storage space occupied by the power-off save data and the storage space occupied by the dynamic data. After saving dynamic data and if the current storage space is just large enough to store the power-off save data, the remaining storage space in the current target area is minimized to prevent insufficient storage space from being available to store the power-off save data the next time dynamic data is stored.
[0017] Optionally, the specific method of writing storage data to the target area includes:
[0018] Writing the configuration data into the target area;
[0019] Dynamic data is written to the target area based on the size of the configuration data.
[0020] By adopting the above technical solution, the configuration data is the data that the program relies on for execution. Before the memory begins backing up and saving the stored data, the configuration data that the program itself relies on for execution must also be saved synchronously to minimize data loss caused by power outages. The size of the configuration data required by a program generally corresponds to the size of the program. Therefore, when the program writes the configuration data value, the size of the written data is used to determine whether the configuration data has been saved. After the configuration data is saved, the dynamic data is saved.
[0021] Optionally, in the specific method for determining whether the remaining storage space of the target area is less than a preset value, if the determination result is no, the method further includes:
[0022] Determine whether a power outage has occurred;
[0023] If yes, writing the power-off protection data into the target area;
[0024] If not, the power-off save data is periodically written to the target area.
[0025] By adopting the above technical solution, when the current target area has enough storage space to write the storage data, the current target area is detected to be powered off, so that the memory can write the storage data in time when the power is off.
[0026] Optionally, the specific method of periodically writing the power-off save data to the target area includes:
[0027] Determining whether the power-off saved data currently requiring periodic writing is the same as the power-off saved data written last time;
[0028] If yes, the power-off saved data that currently needs to be written periodically is not written;
[0029] If not, the power-off save data that needs to be written periodically is written, and it is further determined whether the remaining storage space size of the target area is less than a preset value.
[0030] By adopting the above technical solution, even when there is no power outage, the power-off saved data is periodically written to minimize the possibility that a sudden power outage may cause the power-off saved data to be incorrectly written at the time of the power outage. When the power-off saved data is periodically written to the memory, it is also checked for duplicates. If the data has not changed, the power-off saved data will not be written to the memory, thereby reducing the memory's write loss.
[0031] Optionally, before the step of dividing a target area and a candidate area set based on the storage space of the memory, the step includes: erasing the storage space of the memory.
[0032] By adopting the above technical solution, due to the characteristics of the FLASH memory itself, data can only be written to empty or erased cells, so the storage space needs to be erased before the write operation to better store data.
[0033] Optionally, before the step of writing storage data into the target area, the method includes: writing identification data into the target area, wherein the identification data is used to mark the target area.
[0034] By adopting the above technical solution, the program controls the writing of identification data to determine the target area, so that the data storage area can be accurately found and the stored data can be written when the program is running.
[0035] Optionally, the specific method of updating any one of the candidate regions in the candidate region set to be the new target region and writing the valid data into the new target region includes: writing the new identification data into any one of the candidate regions in the candidate region set.
[0036] By adopting the above technical solution, when any candidate area is enabled as a new target area, new identification data is written through the program so that the data storage area can be accurately found when the program is running.
[0037] The present application also provides a memory data preservation system, which includes at least one processor and at least one memory communicatively connected to the processor, wherein the memory stores instructions for execution by the processor, and the instructions are executed by at least one processor, and the processor is used to execute the memory power-off data preservation method recorded in the above scheme.
[0038] In summary, this application includes at least one of the following beneficial technical effects:
[0039] 1. During the entire data storage process, multiple storage areas are used for switching and saving, and expired and invalid data is erased in a timely manner. When power is cut off and data is restored and storage space is erased uniformly, the amount of erased data can be greatly reduced, which can extend the service life of the memory;
[0040] 2. In the event of a power outage, the configuration data, dynamic data and power-off save data can be saved promptly and effectively, improving the accuracy of data storage;
[0041] 3. When periodically saving data after power failure, invalid data can be checked for duplicates to reduce memory write loss. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 This is an overall flow chart of a memory data storage method according to an embodiment of the present application.
[0043] Figure 2 This is a schematic diagram of dividing storage space in a memory in a memory data storage method according to an embodiment of the present application.
[0044] Figure 3 This is a flow chart of a method for storing memory data in an embodiment of the present application before writing the stored data.
[0045] Figure 4 This is a flowchart of a specific method for writing storage data into a target area in a memory data storage method according to an embodiment of the present application.
[0046] Figure 5 This is a schematic diagram of the data storage sequence in a memory data storage method according to an embodiment of the present application.
[0047] Figure 6 This is a flow chart of a method for updating a candidate area to a new target area in a memory data storage method according to an embodiment of the present application.
[0048] Figure 7 This is a schematic diagram of extracting and transferring valid data for the first time in a memory data storage method according to an embodiment of the present application.
[0049] Figure 8 This is a schematic diagram of continuing to write dynamic data into a new target area after an update in a memory data storage method according to an embodiment of the present application.
[0050] Figure 9 This is a schematic diagram of extracting and transferring valid data for the second time in a memory data storage method according to an embodiment of the present application.
[0051] Figure 10 This is a schematic diagram of a memory data storage method according to an embodiment of the present application, in which power-off storage data is directly written before data transfer.
[0052] Figure 11 This is a schematic diagram of writing power-off saved data after transferring valid data for the first time in a memory data saving method according to an embodiment of the present application.
[0053] Figure 12 This is a schematic diagram of periodically writing power-off saved data or periodically writing power-off saved data in conjunction with real-time detection and writing power-off saved data in a memory data saving method in an embodiment of the present application.
[0054] Figures 13 and 14 This is a schematic diagram of transferring and writing stored data in at least two candidate areas in a memory data storage method according to an embodiment of the present application. DETAILED DESCRIPTION
[0055] The present application is further described in detail below with reference to the accompanying drawings.
[0056] When using a microcontroller to save data, EEPROM memory and FLASH memory are generally considered. EEPROM memory and FLASH memory are both non-volatile memories, that is, EEPROM memory and FLASH memory can still save data after power failure.
[0057] EEPROM memory is a type of electrically erasable programmable read-only memory (EPROM). It doesn't require erasing the memory cells before writing data, and can be rewritten one byte at a time. EEPROM memory writes and erases data in byte or word units, and erases and writes are independent and independent of other data, resulting in a long lifespan of up to millions of cycles. However, EEPROM memory has slow read and write speeds and a relatively small capacity. FLASH memory is a fast, electrically erasable and reprogrammable memory, also known as flash memory. FLASH memory requires erasing the memory cells before writing new data. FLASH memory writes are typically performed in word or double word units, while erasing is typically performed in memory blocks or pages. FLASH memory also has a relatively short lifespan, typically only around 100,000 cycles. Furthermore, FLASH memory blocks or pages are typically large, typically allowing only full page or block erasures; individual bytes cannot be erased.
[0058] EEPROM is often used to store critical data. However, microcontrollers generally lack EEPROM, so FLASH memory is used to emulate it. When using FLASH memory for data storage, backup data must be created to prevent data loss and abnormalities in the FLASH memory due to sudden system power outages. In practical applications, FLASH memory is used to store dynamic data generated by the system. This dynamic data accumulates over time, and the backup data needs to be erased after the system is powered back on. However, the amount of backup data is large, and erasing the backup data from a fixed location each time can easily shorten the lifespan of the FLASH memory.
[0059] The embodiment of the present application discloses a method for storing memory data. Figure 1 and Figure 2 , the memory data saving method includes the following steps.
[0060] Step S10: Divide a target area and a candidate area set based on the storage space of the memory.
[0061] The candidate area set includes at least one candidate area. The entire FLASH memory is primarily divided into a data area and a spare area. The spare area is generally used for error detection and correction in the data area. In this embodiment, the memory's storage space is the data area used for reading and writing data. Both the target area and the candidate area are areas within the FLASH memory that store data. FLASH memory is generally divided into blocks, sectors, pages, and bytes based on its physical structure.
[0062] FLASH memory has a minimum erase unit. The structures of FLASH memories produced by different manufacturers vary slightly. Some FLASH memories use blocks as the minimum erase unit, while others use pages. In this embodiment, the FLASH memory uses pages as the minimum erase unit. Both the target area and the candidate area are stored in a page structure within the FLASH memory.
[0063] Based on the storage space size of the FLASH memory, if there are multiple page storage structures in the FLASH memory, one of the pages is used as the target area, while the other page storage structures are used as candidate areas. These multiple candidate areas are aggregated to form a candidate area set. The candidate area set contains at least one candidate area. Therefore, when the target area is used as the main storage space for program write data, at least one candidate area in the candidate area set can be used as a backup storage space for program write data.
[0064] Reference Figure 3Before step S10, the method further includes step S1, erasing the storage space of the memory.
[0065] Based on the programming principle of FLASH memory, a 1 in a storage cell can only be written to a 0, not a 0 to a 1. The erase operation, on the other hand, changes all 0s in a storage cell to 1s. This means that due to the nature of FLASH memory, it is impossible to overwrite a single byte; write operations are only possible in erased areas. Therefore, erasing the storage space of the FLASH memory before use can reduce the risk of errors during subsequent data write operations. Erasing the storage space of the FLASH memory is generally performed by the manufacturer before the FLASH memory leaves the factory.
[0066] Before step S20, the method further includes step S19 of writing identification data into the target area.
[0067] Among them, the identification data is used to mark the target area. Although the target area and the candidate area are both areas for storing data, when the program controls the writing of data, the program needs to select and distinguish the area location where the data is written. In this embodiment, since the target area and the candidate area are both page structures, when writing the storage data, it is written according to the minimum erase unit. In order to enable the program to better find the area location of the data storage, by writing the identification data to the target area where the storage data currently needs to be written, the target area has an identification function relative to other candidate areas, thereby facilitating the program to better find the area where the data is currently stored.
[0068] Reference Figure 1 and Figure 3 , step S20, writing storage data to the target area.
[0069] The stored data includes configuration data, dynamic data and power-off saved data.
[0070] Configuration data contains program configuration parameters and initialization settings. When the system program is finalized, the size of the configuration data in FLASH memory is also determined. The configuration data is already written to the FLASH memory by the manufacturer before shipment. When using FLASH memory for data backup, the configuration data must also be backed up to ensure proper program operation after a power outage.
[0071] Dynamic data is real-time data written to the FLASH memory by the program during execution. Dynamic data typically changes over time. The amount of dynamic data stored in the FLASH memory is time-dependent; as the program runs longer, the amount of dynamic data stored increases. The byte size of a single dynamic data item in the FLASH memory is fixed, but continuously writing and saving dynamic data increases the amount of storage space occupied by the dynamic data, gradually reducing the remaining storage space in the FLASH memory.
[0072] Power-off save data is data needed by the program to maintain the pre-power-off state when the system is powered back on. This data can be saved immediately upon detecting a power outage, or it can be saved periodically in advance. Generally, this data includes configuration data, dynamically saved data, and user-configured data. In this embodiment, to address data saving speed issues during power outages, the data only includes user-configured data. The size of the FLASH memory occupied by this data is fixed.
[0073] Reference Figure 4 , in the specific method of step S20, the following steps are included.
[0074] Step S201: Write configuration data into the target area.
[0075] After the identification data is written into the target area, the target area is used as a storage space for currently writing stored data. Since the configuration data is the original data relied upon by the program, when the program just starts running, the configuration data is first written into the target area by the program control.
[0076] Step S202: Writing dynamic data into the target area based on the size of the configuration data.
[0077] The configuration data size is fixed and corresponds to the program. Therefore, when the program is determined, the required configuration data size is also determined. When writing configuration data, the byte size of the written data is obtained to determine whether the configuration data has been fully written. After confirming that the configuration data has been written, the dynamic data is written to the target area. During the actual dynamic data writing process, the frequency of dynamic data writing is controlled by the program.
[0078] Reference Figure 5 , Figure 5This shows how data is stored within the storage space. In this example, identification data, configuration data, and a single dynamic data entry each occupy one slot, while power-off save data occupies two slots. Dynamic data 1 through dynamic data N are real-time dynamic data written in chronological order under program control. The number of slots occupied by identification data, configuration data, dynamic data, and power-off save data is for illustration only and does not represent the actual space occupied.
[0079] Reference Figure 1 , step S30, determining whether the remaining storage space size of the target area is less than a preset value.
[0080] In step S30 , if the judgment result is yes, step S31 is executed to extract the last written storage data as valid data based on the order of storage data writing time.
[0081] The remaining storage space in the target area is the storage space where data has not yet been written. The preset value is the sum of the storage space occupied by the power-off save data and the storage space occupied by one dynamic data. That is, in this embodiment, the preset value is three grids of space. Since the storage space in the target area is also limited, and power outages can occur at any time, by setting the preset value to the sum of the storage space occupied by the power-off save data and the storage space occupied by one dynamic data, there is always storage space in the target area to store the power-off save data.
[0082] Valid data is the most recent data stored in the FLASH memory. Because the configuration data size is fixed, it is only written once to the current target area during backup. This means that the first configuration data written to the current target area is also the last configuration data written. Configuration data remains valid until the target area is erased.
[0083] Dynamic data, on the other hand, is stored in chronological order in the FLASH memory and is subject to change. Over time, the program triggers the writing of multiple values into the FLASH memory. For this dynamic data, the last written data is considered the most recent data until the next update. Data prior to the last write operation is considered outdated.
[0084] As for the power-off saved data, if there are multiple power-off saved data in the current target area, the power-off saved data written last is the latest data in the order of time writing, that is, the limited power-off saved data.
[0085] Step S40: Update any candidate region in the candidate region set as a new target region, and write valid data into the new target region.
[0086] Among them, when the remaining storage space in the current target area is less than a preset value, if the storage data continues to be written into the current target area, a problem of no storage space to save the power-off saved data may occur when a sudden power outage occurs.
[0087] Because storage space must be reserved for data preservation during power outages, a candidate region is needed as backup storage space to continue writing data when the current target region runs out of storage space. Dynamic data transfer is performed by updating any candidate region in the candidate region set to the new target region and writing the valid data in the current target region to the new target region. The transferred data is valid, reducing the need for operations on outdated dynamic data.
[0088] Reference Figure 6 In the specific method of step S40, it includes step S41, writing the new identification data to any one of the candidate areas in the candidate area set.
[0089] The target area is a data storage area. When the program controls the writing of data values, the program needs to quickly find the location of the target area. When the candidate area is updated to the new target area, new identification data is written to the candidate area to mark the candidate area, making it easier for the program to control the search.
[0090] In this embodiment, there is only one candidate region in the candidate region set. When the target region's storage space is insufficient, the candidate region is updated to become the new target region, and the valid data in the original target region is written to the new target region. After the valid data is written, the stored data in the original target region is erased, and the original target region is updated to become the candidate region. By writing and saving the stored data in both the target region and the candidate region, only valid data is saved during storage, eliminating excess invalid data. This reduces the problem of FLASH memory being easily degraded when excessive invalid data is erased.
[0091] Reference Figure 7 , Figure 7 This example demonstrates the process of updating the candidate region and transferring valid data from the stored data when there is only one candidate region in the memory and insufficient storage space in the target region. Configuration data and multiple real-time dynamic data are written to the storage space. In this embodiment, because power-off data saves two cells of space, when writing dynamic data 4, the remaining storage space is two cells, which is less than the preset three cells. Therefore, data transfer is required. The latest value of each type of data in the original target region is extracted and stored in the candidate region.
[0092] Specifically, if there is no power outage, after the configuration data is written to the target area, dynamic data will continue to be written. When writing dynamic data 4, the remaining storage space in the target area is two grids, which is less than the space corresponding to the preset value. To ensure that there is enough space in the FLASH memory to store the power-off save data in the event of a sudden power outage while saving the new dynamic data, the stored data needs to be immediately transferred to the candidate area.
[0093] By writing new identification data to the candidate area, the candidate area is updated as the new target area, reactivating the storage space originally corresponding to the candidate area. Before the storage data is transferred, the latest data (valid data) is extracted based on the data writing time. Compared to the latest data, the data written later is outdated and invalid.
[0094] In this example, only one set of configuration data is written, and this configuration data is fixed, so this configuration data is the most recent. Therefore, when transferring data, the configuration data is read and written to the new target area. In this example, dynamic data 4 is the last data written to the original target area, meaning it is valid data. Therefore, when transferring data, dynamic data 4 is read and written to the new target area. Dynamic data is arranged in the order of storage units in the storage space. In actual use, dynamic data can also be arranged in other orders according to program protocols.
[0095] After the valid data is written to the new target area, the original target area is erased to clear all data, making the original target area the backup storage space. The identification data is also erased, so the original target area is updated to the candidate area.
[0096] Reference Figure 1 and Figure 8 , step S50, erase the original target area, update the original target area to the new candidate area, and continue to write dynamic data into the new target area.
[0097] The original target area is the target area before the update. After the valid data in the original target area is transferred to the new target area, the storage space corresponding to the new target area is converted from the original standby storage space to the main storage space to continue writing storage data.
[0098] However, while the program is running and continuously writing stored data, a large amount of invalid dynamic data is stored in the original target area. This invalid dynamic data consumes a large amount of FLASH memory storage space. After the candidate area is updated to the new target area, the original target area cannot be written to any further data. Since the main storage space has been reselected, the original target area is erased, reducing the amount of stored data written to the FLASH memory. When power is restored and the storage space is erased uniformly, the amount of stored data that needs to be erased in a single FLASH memory erase can be reduced, thereby extending the FLASH memory's service life.
[0099] After erasing the original target area, the storage space under the original target area becomes writeable. Data generated under program control is written to the new target area. After erasing the original target area, it is updated to the candidate area, becoming the new backup storage space. If the remaining storage space in the new target area is insufficient for writing and saving stored data, a new candidate area can be selected from the candidate area set for writing and saving stored data. Figure 8 This section shows the process of continuously storing dynamic data in a new target area after the candidate area is updated.
[0100] Reference Figure 9 , Figure 9 This example demonstrates how, when insufficient storage space is available in the target region after the candidate region has been updated, data from the target region is transferred to the candidate region, changing the relative positions of the target and candidate regions. The data transfer process is identical to the previous transfer method and will not be further described here.
[0101] It's worth noting that, when there's only one candidate area, after the second data transfer is complete, the current data remains in the target area written to at the beginning of the program run. This means that data can be transferred and written back and forth between the target area and the candidate area, retaining only valid data and erasing invalid data, reducing data storage redundancy. When erasing all data simultaneously, the amount of data to be erased at a time is reduced, extending the lifespan of the FLASH memory.
[0102] Reference Figure 1 In step S30, it is determined whether the remaining storage space size of the target area is less than a preset value. If the determination result is no, the following steps are performed.
[0103] Step S32: Determine whether a power outage occurs.
[0104] Among them, power-off detection relies on the system hardware where the FLASH memory is located to perform detection, and the program judges, analyzes and processes the detection results.
[0105] In step S32, the following steps are included.
[0106] Step S321: If the judgment result is yes, write the power-off protection data into the target area.
[0107] Since there is always reserved storage space to save power-off data, the power-off data can be written and saved at any time. The writing time is simply the writing time of the storage unit of the FLASH memory, so the writing time requirement of the power-off data can be met during a power outage.
[0108] Reference Figure 10 , Figure 10 This example shows how power-off save data is written to the original target area during a power outage. Because storage space is always reserved for writing power-off save data before a power outage is detected, no further data can be written to the original target area after the power-off save data is written.
[0109] Reference Figure 11 , Figure 11 This example demonstrates how power-off save data is written to a new target area during a power outage. If the target area is a candidate area update, storage space is reserved for writing power-off save data before power-off detection. Power-off save data can still be written to the target area after a dynamic data transfer.
[0110] Step S322: If the judgment result is no, periodically write the power-off save data into the target area.
[0111] Among them, when writing power-off saved data during power-off, although the writing speed of power-off saved data is the writing time of the storage unit of the FLASH memory, considering the factors of the program itself running, the power-off saved data is periodically saved before power off. By saving in advance, it is ensured that there is always power-off saved data stored in the target area.
[0112] Reference Figure 12 , Figure 12 An example of a process for periodically writing data to save power during a power outage, or an example of a process for periodically writing data to save power during a power outage and then writing it in real time when a power outage is detected is shown.
[0113] When the power-off saving data 1 is the power-off saving data written periodically, the power-off saving data 2 is the data written in real time after the power is cut off. Figure 7 as well as Figure 8The display process is consistent. When periodically writing saved power-off data, valid data is also determined in the order of writing time. Before periodically writing power-off data, ensure that the power-off data to be written is inconsistent with the previously written power-off data to avoid data duplication.
[0114] In step S322, the specific method of periodically writing the power-off save data into the target area includes the following steps.
[0115] Step S3221: Determine whether the power-off save data that needs to be periodically written is the same as the power-off save data written last time.
[0116] Among them, periodic writing of power-off save data will occupy a large amount of storage space in the FLASH memory. Therefore, before writing the power-off save data, it is necessary to perform a duplicate data search on the power-off save data to confirm that the power-off save data to be written is new data. When writing the power-off save data in the first cycle, due to the previous
[0117] In step S3221, the following steps are included.
[0118] Step S32211: If the judgment result is yes, the power-off saved data that currently needs to be written periodically is not written.
[0119] Among them, when it is determined that the power-off save data that needs to be written periodically is the same as the power-off save data written last time, it indicates that the power-off save data at this time has not changed. That is, before the power-off save data needs to be written currently, the power-off save data written last time is the latest and valid data. Continuing to write the same data does not change the valid value of the data, but increases the write loss of the FLASH memory. Therefore, the power-off save data to be written is deduplicated, and the power-off save data that is the same as the power-off save data written last time will not be written to the FLASH memory, so as to reduce the amount of invalid data stored in the FLASH memory and reduce the problem of reduced lifespan caused by repeated erasure.
[0120] Step S32212: If the judgment result is no, write the power-off saved data that currently needs to be written periodically, and continue to write dynamic data into the target area.
[0121] When it is determined that the power-off saved data currently required to be written periodically is different from the power-off saved data written last time, it indicates that the power-off saved data required to be written periodically has changed, and the power-off saved data at this time is regarded as the latest data.
[0122] Because the storage space of the FLASH memory is greater than or equal to the sum of the storage space occupied by the power-off save data and the storage space occupied by one dynamic data before periodic writing of the power-off save data, it is still necessary to detect and determine whether the FLASH memory has enough space to store the power-off save data when periodically writing the power-off save data, so as to meet the storage space requirement for writing the power-off save data in the event of a sudden power outage in the system.
[0123] In other embodiments, the candidate region set may further include at least two candidate regions.
[0124] Reference Figure 13 and Figure 14 , Figure 13 and Figure 14 An example of the process of transferring and writing storage data in multiple candidate areas is shown. Different from the above embodiment, when the storage space of the original target area is insufficient, there are at least two options for updating the candidate area to the new target area. Any candidate area in the candidate area set can be updated to the new target area by writing identification data. Therefore, after dividing the candidate area set, an address index is established for each candidate area in the candidate area set. Before updating the candidate area to the new target area, the program selects the candidate area according to the address index. By establishing the address index, each candidate area can be updated to the new target area. In addition, the storage space location of the storage data transfer can be changed according to the address index to change the frequency of erasing and writing of blocks or pages that are used more frequently in the FLASH memory, so as to avoid the erased data always being at the front of the FLASH memory, and balance the write loss of the storage space at the front of the FLASH memory.
[0125] The implementation principle of the embodiment of the present application is as follows: before writing storage data into the memory, the storage space of the memory is divided into a target area and at least one candidate area, the target area is used to store data, and the candidate area is used for backup storage data. When writing storage data, a preset value is set and compared with the remaining storage space of the target area to determine whether the target area can continue to write storage data. If it is determined that the remaining storage space of the current target area is insufficient, the data written for the last time, that is, the latest data, is extracted from the current target area, and then the latest data is written to the new target area updated by any candidate area. After the latest data is written to the new target area, the original target area is erased and updated to a new candidate area, which is placed in the candidate area set so that the area can be reselected in the candidate area set and the storage data can be written.
[0126] When the storage space is insufficient, the saved valid data will be rewritten to the new storage area. At the same time, when the storage space is sufficient, the power-off save data will be written periodically and the power-off save data will be written when the power is detected to improve the accuracy of data storage.
[0127] Throughout the entire data writing and saving process, there's always reserved storage space for configuration data, dynamic data, and power-off data. At least two storage spaces are used for switching between storage, and only the latest data is written during the switching process, ensuring timely removal of outdated data. This fully utilizes memory space and balances data writing losses. Furthermore, when restoring data after a power outage, there's no need to erase excessive amounts of data at once, reducing the risk of memory lifespan shortened by excessive erase cycles.
[0128] The present application also discloses a memory data storage system. The memory data storage system includes at least one processor and at least one memory communicatively connected to the processor. The memory stores instructions for execution by the processor, and the instructions are executed by the at least one processor. Furthermore, the processor is configured to execute the memory data storage method described in the above embodiments.
[0129] The above are all preferred embodiments of the present application, and are not intended to limit the scope of protection of the present application. Therefore, any equivalent changes made based on the structure, shape, and principle of the present application should be included in the scope of protection of the present application.
Claims
1. A method for storing memory data, characterized in that: The following steps are involved: Dividing a target area and a candidate area set based on a storage space of the memory, wherein the candidate area set includes at least one candidate area; Writing storage data into the target area, wherein the storage data includes configuration data, dynamic data, and power-off save data; Determine whether the remaining storage space size of the target area is less than a preset value, and if so, extract the last written storage data as valid data based on the order of the storage data writing time; Updating any one of the candidate regions in the candidate region set as a new target region, and writing the valid data into the new target region; The original target area is erased, the original target area is updated as the new candidate area, and dynamic data is continuously written into the new target area, wherein the original target area is the target area before the update.
2. The memory data storage method according to claim 1, wherein: The preset value is the sum of the storage space occupied by the power-off saved data and the storage space occupied by the dynamic data.
3. The memory data storage method according to claim 1, wherein: The specific method of writing storage data to the target area includes: Writing the configuration data into the target area; Dynamic data is written to the target area based on the size of the configuration data.
4. The memory data storage method according to claim 1, wherein: In the specific method for determining whether the remaining storage space size of the target area is less than a preset value, if the determination result is no, the method further includes: Determine whether a power outage has occurred; If yes, writing the power-off protection data into the target area; If not, the power-off save data is periodically written to the target area.
5. The memory data storage method according to claim 4, wherein: The specific method of periodically writing the power-off save data to the target area includes: Determining whether the power-off saved data currently requiring periodic writing is the same as the power-off saved data written last time; If yes, the power-off saved data that currently needs to be written periodically is not written; If not, the power-off save data that needs to be written periodically is written, and it is further determined whether the remaining storage space size of the target area is less than a preset value.
6. The memory data storage method according to claim 1, wherein: Before the step of dividing a target area and a candidate area set based on the storage space of the memory, the method includes: erasing the storage space of the memory.
7. The memory data storage method according to claim 1, wherein: Before the step of writing storage data into the target area, the method includes: writing identification data into the target area, wherein the identification data is used to mark the target area.
8. The memory data storage method according to claim 7, wherein: The specific method of updating any one of the candidate regions in the candidate region set to be the new target region and writing the valid data into the new target region includes: writing the new identification data into any one of the candidate regions in the candidate region set.
9. A memory data storage system, characterized in that: The system includes at least one processor and at least one memory in communication with the processor, wherein the memory stores instructions for execution by the processor, and the instructions are executed by at least one processor, and the processor is used to execute the memory power-off data preservation method according to any one of claims 1-8.
Citation Information
Patent Citations
Data storage method and device, computer equipment and storage medium
CN112181303A
Erase Management in Memory Systems
US20150026386A1