A multi-partition cyclic data storage method resistant to abnormal power-off and readable storage medium
Patent Information
- Application Number
- CN202611452841.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-18
- Publication Date
- 2026-10-30
AI Technical Summary
[0004]为此,本发明所要解决的技术问题在于克服现有技术中现有存储系统在突发异常断电时存在数据存储不可靠、擦除过程风险集中以及恢复过程易出现数据不一致的问题
本发明所述的一种抗异常断电的多分区循环数据存储方法及可读存储介质,针对现有存储系统突发异常断电易出现数据丢失、记录失真的缺陷,通过优化掉电检测机制与数据存储策略,显著提升控制器实时存储数据的可靠性与准确性,有效降低数据误差。无论设备处于怠速、加速、减速等任意运行工况,或是钥匙非正常关断、蓄电池意外断开、线束突然脱落等异常操作、突发掉电场景,均可保障控制器运行关键数据完整保存,避免供电中断造成关键参数遗失。本方案实现简洁,无需大幅增加硬件成本,通用性强,具备良好工程应用价值。
Smart Images

Figure CN123011152A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of non-volatile memory data storage technology, and in particular to a multi-partition cyclic data storage method and readable storage medium that is resistant to abnormal power outages. Background Technology
[0002] Equipment controllers typically need to collect and periodically save operating parameters such as equipment runtime, idling speed, acceleration, and deceleration, as well as other key operational data, in order to monitor operating status, perform parameter statistics, and trace faults. Traditional equipment controllers can save data before power-off by delaying power-off after the key is turned off. This method can meet the needs of normal power-off scenarios. However, when there are sudden abnormal power outages such as abnormal key turn-off, accidental battery disconnection, or sudden detachment of the power supply harness, the controller may lose power during data writing, resulting in data loss or recording distortion.
[0003] Among existing emergency power failure protection schemes, one type uses EEPROM or adds a backup power supply to extend the write time through hardware redundancy, but this increases hardware costs; another type uses a single-partition cyclic overwrite method, which carries the risk of losing all partition data when a power failure occurs during FLASH erasure; and yet another type uses page-level write optimization and relies on complex page index management, which may result in inconsistencies between the index and data during abnormal power failures, leading to a break in the data chain. Summary of the Invention
[0004] Therefore, the technical problem this invention aims to solve is to overcome the problems of unreliable data storage, concentrated risks during the erasure process, and data inconsistency during the recovery process in existing storage systems during sudden abnormal power outages. This invention, without increasing hardware costs, utilizes the built-in FLASH storage resources of the controller chip to reduce errors in real-time data storage by the controller; it ensures that the data collected and stored by the controller in real time can be completely and reliably preserved under any operating conditions and various abnormal operation scenarios, avoiding data loss and recording distortion caused by power supply abnormalities.
[0005] To address the aforementioned technical problems, this invention provides a multi-partition cyclic data storage method resistant to abnormal power outages, comprising: The non-volatile storage area of the on-chip FLASH is divided into at least two independent physical storage partitions. An identity flag is configured at the storage start position of each physical storage partition. Based on the identity flag, each physical storage partition is dynamically divided into the current primary partition, the first backup partition, and the secondary backup partition, and a partition priority queue is established. Data initialization is completed based on the determined current primary partition. After the data initialization is completed, data frames are formed and written to the current primary partition at a preset time interval. The data frame includes a data header, a device operation data body, and a checksum at the end. The data header includes a fixed identifier and an incrementing number of records. Based on the number of records, it is determined whether the current primary partition has reached a preset storage limit. When the number of records reaches a preset maximum value, the last valid data in the current primary partition is obtained, and the partition flag used to identify the primary partition and the last valid data are written to the first backup partition, so that the original first backup partition is converted into a new current primary partition; the original current primary partition is erased, and the partition flag used to identify the first backup partition and the last valid data are written to the erased original current primary partition, so that the original current primary partition is converted into a new first backup partition; based on the converted new current primary partition and the new first backup partition, the number of records is reset to 0, and the periodic writing of the data frame continues based on the last valid data to perform the next round of cyclic storage; When the system experiences a power outage and is powered on again during the circular storage process, it reads the identity flags of each physical storage partition. Based on the read identity flags, it redetermines the current primary partition, the first backup partition, and the secondary backup partition. Then, it sequentially performs hardware status detection and data verification on each physical storage partition according to the partition priority queue. Based on the hardware status detection results and data verification results, it determines the last valid data from the current primary partition, the first backup partition, or the secondary backup partition. The determined last valid data is loaded into RAM and synchronized to the redetermined current primary partition and the first backup partition, so that the circular storage continues to execute after power-on based on the last valid data.
[0006] In one embodiment of the present invention, the data initialization includes: Read the identity flag bits of each physical storage partition, and identify the current primary partition, the first backup partition, and the secondary backup partition based on the identity flag bits; Hardware status detection and data frame verification are performed sequentially on the current primary partition, the first backup partition, and the secondary backup partition to determine the partition status of each physical storage partition. The valid data to be loaded into RAM is determined based on the partition status of each physical storage partition, and the data synchronization between the current primary partition and the first backup partition is completed based on the valid data.
[0007] In one embodiment of the present invention, when both the current primary partition and the first backup partition are in a normal state, the last valid data in the current primary partition is read, the last valid data is loaded into RAM, and the last valid data is synchronized to the first backup partition, so that the secondary backup partition remains in a dormant standby state.
[0008] In one embodiment of the present invention, when the current primary partition is in a normal state and the first backup partition is in an abnormal state, the last valid data in the current primary partition is read, the highest priority normal partition is determined from the secondary backup partitions in the partition priority queue, the partition flag used to characterize the identity of the first backup partition and the last valid data are written into the determined normal partition, so that the determined normal partition is converted into a new first backup partition, and the original first backup partition in the abnormal state is erased.
[0009] In one embodiment of the present invention, when the current primary partition is in an abnormal state and the first backup partition is in a normal state, the last valid data in the first backup partition is read, the highest priority normal partition is determined from the secondary backup partitions in the partition priority queue, the partition flag used to identify the primary partition and the last valid data are written into the determined normal partition, so that the determined normal partition is converted into a new current primary partition, and the original current primary partition in the abnormal state is erased.
[0010] In one embodiment of the present invention, when both the current primary partition and the first backup partition are in an abnormal state, the secondary backup partitions are traversed sequentially according to the partition priority queue, two physical storage partitions are determined from the secondary backup partitions in a normal state, and the two determined physical storage partitions are respectively configured as the new current primary partition and the new first backup partition. The initial business data in RAM is used as the starting data for cyclic storage, and the number of records is reset to 0. The original current primary partition and the original first backup partition that are in an abnormal state are erased.
[0011] In one embodiment of the present invention, when an abnormal power outage occurs during the process of writing the data frame to the current primary partition, after the system is powered on again, the data frame is verified according to the data header and end check value of the data frame. Based on the verification result, the data frame that was not written before the abnormal power outage is identified, and the last successfully written valid data in the first backup partition before the abnormal power outage is obtained. The valid data is used as the recovery data after the abnormal power outage.
[0012] In one embodiment of the present invention, after obtaining the recovery data, the recovery data is loaded into RAM and synchronized to the redefined current primary partition so that the valid data in the current primary partition and the first backup partition are consistent, and the periodic writing of the data frame continues based on the recovery data.
[0013] In one embodiment of the present invention, when the system is powered on again after a normal power-down, after determining that there are no hardware faults in each of the physical storage partitions and that the data frame verification is passed, the data header of each data frame in the current primary partition is parsed, the last valid data in the current primary partition is determined according to the number of records in the data header, the last valid data is synchronized to the first backup partition and loaded into RAM, and the number of records is continuously incremented based on the number of records corresponding to the last valid data.
[0014] The present invention also provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the aforementioned multi-partition cyclic data storage method for resisting abnormal power outages.
[0015] The technical solution of the present invention has the following advantages compared with the prior art: This invention discloses a multi-partition cyclic data storage method and readable storage medium resistant to abnormal power outages. Addressing the shortcomings of existing storage systems prone to data loss and recording distortion during sudden power outages, this invention significantly improves the reliability and accuracy of real-time data storage by optimizing the power outage detection mechanism and data storage strategy, effectively reducing data errors. Regardless of the device's operating conditions (idling, acceleration, deceleration, etc.), or abnormal operations such as abnormal key disconnection, unexpected battery disconnection, or sudden wiring harness detachment, the invention ensures the complete preservation of critical controller data, preventing the loss of key parameters due to power interruption. This solution is simple to implement, requires no significant increase in hardware costs, is highly versatile, and has good engineering application value. Attached Figure Description
[0016] To make the content of this invention easier to understand, the invention will be further described in detail below with reference to specific embodiments and accompanying drawings.
[0017] Figure 1 This is a flowchart of the multi-partition cyclic data storage method for resisting abnormal power outages according to the present invention. Detailed Implementation
[0018] The present invention will be further described below with reference to the accompanying drawings and specific embodiments, so that those skilled in the art can better understand and implement the present invention. However, the embodiments described are not intended to limit the present invention.
[0019] In this invention, "several" means one or more, "multiple" means two or more, "greater than," "less than," "exceeding," etc., are understood to exclude the stated number; "above," "below," "within," etc., are understood to include the stated number. In the description of this invention, the terms "first" and "second" are used only to distinguish technical features and should not be construed as indicating or implying relative importance, or implicitly indicating the number of indicated technical features, or implicitly indicating the order of the indicated technical features.
[0020] Example 1 Reference Figure 1 As shown, this embodiment provides a multi-partition cyclic data storage method to resist abnormal power outages. This method can be applied to controllers with on-chip FLASH. Without adding additional hardware such as EEPROM or backup power supply, the controller's own non-volatile storage resources are used to periodically store data generated during device operation. Through dynamic rotation between multiple physical storage partitions, data backup, and effective data recovery after power restoration, the integrity and reliability of data storage under abnormal power outage conditions are improved.
[0021] In this embodiment, the on-chip FLASH is used to store business data that needs to be retained for a long time during device operation, and the RAM is used to load currently valid data during system operation for the control program to read, update, and use. Device operation data may include device runtime, operating condition parameters, and other data that needs to be retained after the system is powered on again. This embodiment does not limit the specific type of device operation data.
[0022] Specifically, the method in this embodiment includes the following steps: S101. Perform physical storage partitioning on the on-chip FLASH and determine the working identity of each physical storage partition.
[0023] In this embodiment, the non-volatile storage area in the on-chip FLASH is divided into at least two independent physical storage partitions. Each physical storage partition occupies an independent FLASH address range, ensuring that erase or write operations in one physical storage partition will not directly overwrite data already stored in other physical storage partitions.
[0024] An identity flag is configured at the beginning of storage on each physical storage partition. The identity flag indicates the current working role of the corresponding physical storage partition, with different working roles corresponding to different identity flags. By reading the identity flag, the system can identify whether the corresponding physical storage partition is currently the primary partition, the first standby partition, or the secondary standby partition.
[0025] When there are two physical storage partitions, each physical storage partition can be configured as the current primary partition and the first backup partition. When there are more than two physical storage partitions, one physical storage partition can be configured as the current primary partition, the other physical storage partition can be configured as the first backup partition, and the remaining physical storage partitions can be configured as secondary backup partitions.
[0026] The primary partition is used to receive periodically written data frames during normal system operation; the first backup partition is used to store valid data that can be used for data recovery, and participates in partition identity switching when the primary partition reaches the preset storage limit or an anomaly occurs; the secondary backup partition remains in standby state when the primary partition and the first backup partition are normal, and is used to replace the abnormal partition according to a predetermined priority when the primary partition or the first backup partition is abnormal.
[0027] After determining the working identity of each physical storage partition, a partition priority queue is established based on the working identity and preset priority order of each physical storage partition. The partition priority queue is used to determine the selection order of each secondary spare partition when it is necessary to call the secondary spare partition. The original scheme used the method of identifying the primary partition, the first spare partition, and the secondary spare partition by identity flag bits and establishing the partition priority queue.
[0028] S102. Complete data initialization based on the current primary partition, and perform periodic data storage after initialization.
[0029] After the system is powered on, it first reads the identity flag bit of the storage start position of each physical storage partition, identifies the current primary partition, the first backup partition, and each secondary backup partition based on each identity flag bit, and completes data initialization in conjunction with the established partition priority queue.
[0030] After data initialization, the system periodically collects or organizes the device operation data to be saved according to a preset time interval, and forms data frames based on the device operation data corresponding to each storage cycle. The formed data frames are then written sequentially to the current main partition. In this way, the device operation data is continuously saved to the on-chip FLASH at fixed time intervals.
[0031] In this embodiment, each data frame sequentially includes a data header, a device operation data body, and a checksum at the end. The data header includes at least a fixed identifier and a record count. The fixed identifier identifies whether the corresponding stored content belongs to a predetermined data frame, and the record count characterizes the write order of the data frame in the current storage cycle. The record count increments after each valid data frame write. The device operation data body carries the actual device operation data that needs to be saved in this cycle. The checksum at the end is used to verify the integrity of the data frame during subsequent reads. Upon power-on, the fixed identifier, record count, and checksum in the data header can be combined to determine whether the corresponding data frame has been validly written, thus avoiding the misclassification of data frames that were only partially written during an abnormal power outage as valid data.
[0032] S103. Determine whether the current primary partition has reached the preset storage limit based on the number of records, and perform primary and backup partition rotation after reaching the preset storage limit.
[0033] As data frames are continuously written to the current primary partition, the record count continues to increase. The system determines whether the number of data frames already saved in the current primary partition has reached a preset maximum based on the record count.
[0034] If the number of records has not yet reached the preset maximum value, new data frames will continue to be formed according to the preset duration and written sequentially to the current primary partition.
[0035] When the number of records reaches the preset maximum value, it indicates that the current primary partition has reached the preset storage limit corresponding to this round of storage. At this time, the system does not directly erase the current primary partition, but first retrieves the last valid data in the current primary partition.
[0036] The last valid data is the data corresponding to the latest complete data frame in the current primary partition, determined by the record count and data verification results. Since this data represents the latest valid device operating status at the end of the current circular storage process, it is used as the starting data for the next round of circular storage.
[0037] Subsequently, the partition flag used to identify the primary partition and the last valid data entry are written to the original first backup partition. After the write is complete, the original first backup partition acquires the status of a primary partition and is converted into the new current primary partition.
[0038] After confirming that the primary partition flag and the last valid data have been written to the new current primary partition, the original current primary partition is then erased. After the original current primary partition is erased, the partition flag used to identify the first backup partition and the aforementioned last valid data are written to the erased original current primary partition, so that the original current primary partition acquires the identity of the first backup partition and is converted into the new first backup partition.
[0039] Therefore, during the partition rotation process, the last valid data is first saved to the original first backup partition. Even if an abnormal power outage occurs during the subsequent erasure of the original current primary partition, the new current primary partition will still retain the last valid data corresponding to the current cycle storage.
[0040] After completing the identity conversion between the current primary partition and the first backup partition, the record count is reset to 0, and the last valid data is used as the data basis for a new round of circular storage. The new current primary partition continues to receive the data frames formed subsequently.
[0041] By taking the above steps and dynamically rotating the identity between the current primary partition and the first backup partition, we can avoid repeatedly erasing and writing to the same FLASH area. On the other hand, we can ensure that there is a valid copy of the data before the partition erasure operation is performed, thereby reducing the risk of losing all valid data when an abnormal power outage occurs during the FLASH erasure stage.
[0042] S104. When a power outage occurs and power is restored during the circular storage process, each physical storage partition is tested, and valid data is restored based on the test results.
[0043] After the system is powered on again, the identification flag bit of the storage start position of each physical storage partition is read again.
[0044] Since the primary partition and the first backup partition can be dynamically rotated during operation, the system does not rely on fixed physical addresses to directly identify the primary and backup partitions after power-on. Instead, it uses the newly read identity flags as a basis to redetermine the current primary partition, the first backup partition, and each secondary backup partition.
[0045] After re-identifying the partition, hardware status checks and data verifications are performed on each physical storage partition in sequence according to the partition priority queue.
[0046] Hardware status detection is used to determine whether a FLASH storage anomaly has occurred in the physical storage partition, affecting normal reading or writing; data verification is used to determine whether the data frames stored in the physical storage partition are complete and valid. Data verification can be performed at least based on the fixed identifier in the data header and the checksum at the end to identify fully written data frames and incomplete data frames caused by abnormal power outages.
[0047] It should be noted that the specific FLASH hardware status detection method in this embodiment can be determined based on the FLASH access mechanism of the controller used.
[0048] In this step, based on the test results, data initialization and recovery can be completed according to the following situations.
[0049] (i) When both the current primary partition and the first backup partition are in normal condition, read each valid data frame in the current primary partition, determine the last valid data according to the number of records in the data header, load the last valid data into RAM, and synchronize the last valid data to the first backup partition.
[0050] After synchronization is complete, the current primary partition is used to continue writing subsequent periodic data frames, the first backup partition stores the valid data for recovery, and each secondary backup partition remains in a dormant standby state.
[0051] (ii) When the current primary partition is in a normal state, but the first backup partition experiences a hardware failure or its stored data is corrupted, read the last valid data in the current primary partition.
[0052] Subsequently, the secondary spare partitions are traversed according to the partition priority queue, and the highest priority normal partition is determined from the secondary spare partitions in a normal state. The partition flag used to identify the first spare partition and the last valid data read from the current primary partition are written to this normal partition, thus converting the normal partition into the new first spare partition.
[0053] After the new first backup partition is created, the original first backup partition that malfunctioned is erased. By creating the new first backup partition first and then processing the original malfunctioning partition, the loss of the unique and valid data copy due to another power outage during partition recovery can be avoided.
[0054] (iii) When the current primary partition is in an abnormal state, while the first backup partition is in a normal state, read the last valid data in the first backup partition.
[0055] Subsequently, following the same partition priority queue, the highest priority normal partition is determined from the secondary spare partitions. The partition flag used to identify the primary partition and the last valid data obtained from the first spare partition are written to this normal partition, thus converting the normal partition into the new current primary partition.
[0056] After the new primary partition is created, the original primary partition that malfunctioned is erased. Therefore, even if the data on the original primary partition is corrupted, the valid data stored in the first spare partition can be used to rebuild the primary partition.
[0057] (iv) When both the primary partition and the first standby partition are in an abnormal state, it means that the normal primary and standby data recovery paths cannot be used directly.
[0058] At this point, the secondary spare partitions are traversed sequentially according to the partition priority queue. Two physical storage partitions are selected from the secondary spare partitions in normal state and configured as the new current primary partition and the new first spare partition, respectively.
[0059] Since neither the original primary partition nor the original first backup partition can provide valid data that meets the requirements, the pre-set initial business data is retrieved from RAM and used as the starting data for restarting the loop storage. At the same time, the record count is reset to 0.
[0060] After configuring the new current primary partition and the first backup partition, erase the original current primary partition and the original first backup partition that malfunctioned.
[0061] Furthermore, in this embodiment, data recovery is also performed in the case where an abnormal power outage occurs during the data frame writing process.
[0062] When an abnormal power failure occurs while the system is writing a data frame to the current primary partition, the writing process of the on-chip FLASH is suddenly stopped. The data frame being written at the time of the abnormal power failure may have only completed the data header writing, only completed the writing of part of the device operation data body, or not yet completed the writing of the end check value.
[0063] Therefore, after the system is powered on again, it reads the data frames in the current main partition and performs data verification by combining the data header and end checksum.
[0064] For data frames whose fixed identifiers do not meet the preset requirements, whose end checksums are missing, or whose data verification results determined based on the end checksums do not meet the requirements, the data frame is determined to be a data frame that was not written before the abnormal power outage, and the data frame that was not written is not used as valid business data.
[0065] After identifying a data frame that has not been written, the system does not use the incomplete data in the data frame to continue operation. Instead, it retrieves the last valid data that was successfully saved before the abnormal power outage from the first backup partition and identifies this valid data as the recovery data after the abnormal power outage.
[0066] After obtaining the recovery data, the recovery data is loaded into RAM, enabling the control program to regain the valid business status that was confirmed before the abnormal power outage; at the same time, the recovery data is synchronized to the newly determined current primary partition, so that the valid data in the newly determined current primary partition and the first backup partition are restored to consistency.
[0067] After the primary and backup data synchronization is completed, the recovered data is used as the data basis to restart the periodic storage, and subsequent data frames are formed and written to the current primary partition according to the preset time interval.
[0068] By following the steps above, data frames that are not completed during an abnormal power outage will not participate in system recovery. The system can roll back to the last valid data that was successfully saved before the abnormal power outage, thereby avoiding record distortion or subsequent data chain anomalies caused by the incorrect loading of partially written data.
[0069] Furthermore, this embodiment also includes a data recovery process when the system is powered on again after a normal power-down.
[0070] When the system is powered on again after a normal power-down, it reads the identity flag bits of each physical storage partition and performs hardware status detection and data frame verification on each physical storage partition.
[0071] After confirming that there are no hardware faults affecting data reading in the current primary partition, the first backup partition, and related physical storage partitions, and that the data frame verification in the current primary partition has passed, the data header of each data frame in the current primary partition is parsed.
[0072] The order of valid data frames is determined based on the number of records in each data header, and the valid data frame with the highest number of records is identified as the last valid data in the current primary partition.
[0073] Subsequently, the last valid data is loaded into RAM and simultaneously synchronized to the first backup partition, so that the current primary partition and the first backup partition after power-on use the same valid data as the data basis for subsequent operation.
[0074] Furthermore, the record count corresponding to the last valid data is read, and the record count for subsequent records is incremented based on this count. For example, if the record count for the last valid data before normal power-down is n, then after power-on and data recovery, the record count for the next data frame can continue to increment from n. This process maintains the continuity of the record count for each data frame in the same round of storage and facilitates the determination of the latest valid data based on the record count.
[0075] Using the above method, during normal device operation, periodic data writing is performed on the current primary partition. When the preset storage limit is reached, a backup-then-erase approach is adopted to dynamically rotate between the primary partition and the first backup partition. Upon power-up, the available partition and the last valid data are determined using the identity flag, partition priority queue, hardware status detection results, and data verification results. When the primary partition or the first backup partition fails, the secondary backup partition can be called to complete the partition replacement. When an abnormal power outage occurs during data frame writing, the incomplete data frame can be discarded and the system can roll back to the successfully saved valid data, thereby reducing the risk of loss or distortion of critical operational data when FLASH erasure, data writing, and abnormal power outages overlap.
[0076] Example 2 Based on the same inventive concept, this embodiment provides a computer-readable storage medium storing a computer program thereon. When the computer program is executed by a processor, it causes the processor to execute the multi-partition cyclic data storage method for resisting abnormal power outages described in Embodiment 1. The computer-readable storage medium can be disposed in a device controller or used as a storage medium for loading corresponding control programs.
[0077] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0078] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0079] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0080] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0081] Finally, it should be noted that the above specific embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to examples, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.
Claims
1. A method for storing multi-partition cyclic data to resist abnormal power outages, characterized in that, include: The non-volatile storage area of the on-chip FLASH is divided into at least two independent physical storage partitions. An identity flag is configured at the storage start position of each physical storage partition. Based on the identity flag, each physical storage partition is dynamically divided into the current primary partition, the first backup partition, and the secondary backup partition, and a partition priority queue is established. Data initialization is completed based on the determined current primary partition. After the data initialization is completed, data frames are formed and written to the current primary partition at a preset time interval. The data frame includes a data header, a device operation data body, and a checksum at the end. The data header includes a fixed identifier and an incrementing number of records. Based on the number of records, it is determined whether the current primary partition has reached a preset storage limit. When the number of records reaches a preset maximum value, the last valid data in the current primary partition is obtained, and the partition flag used to identify the primary partition and the last valid data are written to the first backup partition, so that the original first backup partition is converted into a new current primary partition; the original current primary partition is erased, and the partition flag used to identify the first backup partition and the last valid data are written to the erased original current primary partition, so that the original current primary partition is converted into a new first backup partition; based on the converted new current primary partition and the new first backup partition, the number of records is reset to 0, and the periodic writing of the data frame continues based on the last valid data to perform the next round of cyclic storage; When the system experiences a power outage and is powered on again during the circular storage process, it reads the identity flags of each physical storage partition. Based on the read identity flags, it redetermines the current primary partition, the first backup partition, and the secondary backup partition. Then, it sequentially performs hardware status detection and data verification on each physical storage partition according to the partition priority queue. Based on the hardware status detection results and data verification results, it determines the last valid data from the current primary partition, the first backup partition, or the secondary backup partition. The determined last valid data is loaded into RAM and synchronized to the redetermined current primary partition and the first backup partition, so that the circular storage continues to execute after power-on based on the last valid data.
2. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, The data initialization includes: Read the identity flag bits of each physical storage partition, and identify the current primary partition, the first backup partition, and the secondary backup partition based on the identity flag bits; Hardware status detection and data frame verification are performed sequentially on the current primary partition, the first backup partition, and the secondary backup partition to determine the partition status of each physical storage partition. The valid data to be loaded into RAM is determined based on the partition status of each physical storage partition, and the data synchronization between the current primary partition and the first backup partition is completed based on the valid data.
3. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When both the current primary partition and the first backup partition are in a normal state, the last valid data in the current primary partition is read, the last valid data is loaded into RAM, and the last valid data is synchronized to the first backup partition, so that the secondary backup partition remains in a dormant standby state.
4. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When the current primary partition is in a normal state and the first backup partition is in an abnormal state, the last valid data in the current primary partition is read, the highest priority normal partition is determined from the secondary backup partitions in the partition priority queue, the partition flag used to identify the identity of the first backup partition and the last valid data are written into the determined normal partition, so that the determined normal partition is converted into a new first backup partition, and the original first backup partition in the abnormal state is erased.
5. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When the current primary partition is in an abnormal state and the first backup partition is in a normal state, the last valid data in the first backup partition is read, the highest priority normal partition is determined from the secondary backup partitions in the partition priority queue, the partition flag used to identify the primary partition and the last valid data are written into the determined normal partition, so that the determined normal partition is converted into a new current primary partition, and the original current primary partition in an abnormal state is erased.
6. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When both the current primary partition and the first backup partition are in an abnormal state, the secondary backup partitions are traversed sequentially according to the partition priority queue. Two physical storage partitions are determined from the secondary backup partitions in a normal state, and the two determined physical storage partitions are configured as the new current primary partition and the new first backup partition, respectively. The initial business data in RAM is used as the starting data for cyclic storage, and the number of records is reset to 0. The original current primary partition and the original first backup partition that are in an abnormal state are erased.
7. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When an abnormal power outage occurs during the process of writing the data frame to the current primary partition, after the system is powered on again, the data frame is verified according to the data header and end checksum of the data frame. Based on the verification result, the data frame that was not written before the abnormal power outage is identified, and the last valid data successfully written before the abnormal power outage in the first backup partition is obtained. The valid data is used as the recovery data after the abnormal power outage.
8. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 7, characterized in that, After obtaining the recovery data, the recovery data is loaded into RAM and synchronized to the newly determined current primary partition to ensure that the valid data in the current primary partition and the first backup partition are consistent. The periodic writing of the data frame continues based on the recovery data.
9. The multi-partition cyclic data storage method for resisting abnormal power outages according to claim 1, characterized in that, When the system is powered on again after a normal power-down, after confirming that there are no hardware faults in each of the physical storage partitions and that the data frame verification is successful, the data header of each data frame in the current primary partition is parsed, the last valid data in the current primary partition is determined according to the number of records in the data header, the last valid data is synchronized to the first backup partition and loaded into RAM, and the number of records is incremented based on the number of records corresponding to the last valid data.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the multi-partition cyclic data storage method for resisting abnormal power outages as described in any one of claims 1 to 9.
