Method for reading and writing flash in electric power SOE data single-chip microcomputer

By introducing cache and pointer management mechanisms into the power SOE data microcontroller, the number of read and write times of flash memory is reduced, the problems of data loss and low efficiency are solved, and the persistent storage and efficient operation of data are achieved.

CN119200984APending Publication Date: 2024-12-27SHANGHAI SUNRISE POWER TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411286153.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-13
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

In existing power SOE data microcontrollers, frequent flash memory reading and writing will reduce its lifespan, affect the microcontroller efficiency, and have a high risk of data loss.

Method used

Using predefined cache and pointer management mechanisms, the SOE data to be written temporarily is stored by cache A, and write or read it to flash memory at an appropriate time, reducing the number of direct flash read and write times, using Fwrie and Fread The Flashwrite function performs data management to ensure that data is not lost when power is off.

Benefits of technology

It effectively reduces the number of read and write times of flash memory, reduces the risk of data loss, improves the operating efficiency of microcontrollers, and realizes persistent data storage.

✦ Generated by Eureka AI based on patent content.
Patent Text Reader

Abstract

The invention discloses a method for reading and writing flash in an electric power SOE data single-chip microcomputer, and relates to the technical field of electric power systems. According to the method, a section of continuous cache A is set for storing SOE data to be written into a flash memory, and a flash writing instruction is dynamically triggered according to an SOE data reading instruction of a single chip microcomputer and a cache writing position, so that the writing frequency and the SOE loss risk caused by power failure can be reduced, and the read data is certainly stored and is not lost in the power failure.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technology of power system, in particular to the technology of a method for reading and writing flash in a power SOE data single chip microcomputer. Background Art

[0002] A large amount of power SOE (Sequence of Events) data is important data for power monitoring and needs to be saved in flash memory so that it will not be lost even if the power is off.

[0003] Flash memory has the advantages of large capacity and low price. However, due to process reasons, the number of read and write times of the flash memory of the microcontroller is limited. Frequent reading and writing will reduce the life of the flash memory, and the speed of reading and writing the flash memory is also lower than the speed of the internal memory.

[0004] A large amount of flash storage data will occupy a large amount of CPU and affect the efficiency of the microcontroller. If the number of flash reads and writes can be reduced without affecting the flash's ability to store large amounts of data, the use of flash can be expanded and the operating efficiency of the microcontroller can be improved. Summary of the invention

[0005] In view of the defects existing in the above-mentioned prior art, the technical problem to be solved by the present invention is to provide a method for reading and writing flash in an electric SOE data microcontroller, which can ensure that the read data is stored and not lost during power failure, reduce the number of flash memory read and write times, and improve the operating efficiency of the microcontroller.

[0006] In order to solve the above technical problems, the present invention provides a method for reading and writing flash in a power SOE data microcontroller, which is characterized by comprising a pre-defined step and a flash reading and writing step;

[0007] 1) Predefined steps

[0008] Set up a continuous buffer A to store the SOE data to be written into the flash memory, and define a read pointer Pread and a write pointer Pwrite;

[0009] Define a write cache function Fwrie, which is used to write SOE data into the blank area of ​​cache A;

[0010] Define a flash read function Fread, which is used to return the corresponding SOE data;

[0011] Define a flash write function Flashwrite, which is used to write SOE data into the flash memory;

[0012] Define an array SA, which is used to store the SOE number and the storage capacity of SOE data. The initial value of the SOE number is set to 1, and the initial positions of the read pointer Pread and the write pointer Pwrite both point to the beginning of the cache A;

[0013] 2) Flash reading and writing steps

[0014] Set a time period T, and check the SOE data reading and writing status of the MCU every T time interval;

[0015] If the MCU generates new SOE data to be written into the flash memory, the Fwrie function is called;

[0016] If the MCU does not generate new SOE data, and the write pointer Pwrite is located at the last 50% storage area of ​​cache A, the Flashwrite function is called to write all the data of cache A into the flash memory, and the storage capacity value of cache A is defined as Aflashsize;

[0017] If the MCU sends out a request to read the SOE data of the specified SOE number, the Fread function is called;

[0018] 2.1) The processing flow of the Fwrie function is as follows:

[0019] The first step is to define the SOE data to be written into the flash memory as the target SOE data, and the storage capacity required by the target SOE data is defined as Asize, and the storage capacity of Asize is the length of the target SOE data plus 6 bytes, and the 6 bytes are used to store the sequence number of the target SOE data and the storage capacity value of Asize;

[0020] The second step is, if the storage capacity of the blank area of ​​cache A is greater than or equal to Asize, the target SOE data is stored at the beginning of the blank area of ​​cache A, and the storage capacity of the blank area of ​​cache A is subtracted from the value of Asize;

[0021] If the blank area storage capacity of cache A is less than Asize, the storage capacity occupied by all data in cache A is recorded as CSize, and the Flashwrite function is called to write all data in cache A into the flash memory, and the blank area storage capacity of cache A is reset to Aflashsize, and then the target SOE is stored at the starting end of cache A, and the blank area storage capacity value of cache A is updated to: the blank area storage capacity value of cache A is reduced by Asize;

[0022] The third step is to save the current SOE number and Asize into the array SA, move the write pointer Pwrite to the beginning of the blank area of ​​the buffer A, and then increase the SOE number by 1;

[0023] 2.2) The processing flow of the Flashwrite function is as follows:

[0024] If the blank area capacity of the flash memory is greater than or equal to Csize, the specified data of cache A are written one by one from the starting position of the blank area of ​​the flash memory, and then Csize is subtracted from the blank area capacity of the flash memory;

[0025] If the blank area capacity of the flash memory is less than Csize, the specified data of cache A are written one by one from the starting position of the storage area of ​​the flash memory, and then the blank area capacity value of the flash memory is updated to: the storage capacity value of the flash memory minus Csize;

[0026] 2.3) The processing flow of the Fread function is as follows:

[0027] If there is SOE data with the specified SOE number in cache A, then the SOE data with the specified SOE number is directly read from cache A, and then the Flashwrite function is called to write the data from the starting end to the SOE data with the specified SOE number in cache A into the flash memory, and the storage capacity occupied by this segment of data is recorded as Bsize, and then the remaining data in cache A is moved as a whole to the starting position of cache A, and the storage capacity value of the blank area of ​​cache A is increased by Bsize, and the read pointer Pread and the write pointer Pwrite are both pointed to the starting end of the blank area of ​​cache A;

[0028] If the SOE data with the specified SOE number does not exist in the array SA, the Flashread function is called;

[0029] 2.4) The processing flow of the Flashread function is as follows:

[0030] The storage capacity of the SOE data with the specified SOE number is found from the array SA, and the location of the SOE data in the flash memory is calculated according to the storage capacity value of the SOE data found, and then the data is directly read from the flash memory.

[0031] The method for reading and writing flash in a power SOE data single-chip microcomputer provided by the present invention opens a cache to temporarily store SOE data, and dynamically triggers a flash write instruction according to the single-chip microcomputer SOE read instruction and the cache write position, thereby reducing the number of write times and the risk of SOE data loss caused by power failure, ensuring that the read data is stored and not lost during power failure, and realizing load balancing at the same time, reducing the heavy tasks caused by multiple single SOE storages, and improving the operating efficiency of the single-chip microcomputer. DETAILED DESCRIPTION

[0032] The technical solution of the present invention is further described in detail below in conjunction with specific embodiments, but this embodiment is not intended to limit the present invention. All similar structures and similar variations of the present invention should be included in the protection scope of the present invention. The commas in the present invention represent the relationship of and, and the English letters in the present invention are case-sensitive.

[0033] A method for reading and writing flash in a power SOE data microcontroller provided by an embodiment of the present invention is characterized by comprising a pre-defined step and a flash reading and writing step;

[0034] 1) Predefined steps

[0035] Set up a continuous buffer A to store the SOE data to be written into the flash memory, and define a read pointer Pread and a write pointer Pwrite;

[0036] Define a write cache function Fwrie, which is used to write SOE data into the blank area of ​​cache A;

[0037] Define a flash read function Fread, which is used to return the corresponding SOE data;

[0038] Define a flash write function Flashwrite, which is used to write SOE data into the flash memory;

[0039] Define an array SA, which is used to store the SOE number and the storage capacity of SOE data. The initial value of the SOE number is set to 1, and the initial positions of the read pointer Pread and the write pointer Pwrite both point to the beginning of the cache A;

[0040] 2) Flash reading and writing steps

[0041] Set a time period T, and check the SOE data reading and writing status of the MCU every T time interval;

[0042] If the MCU generates new SOE data to be written into the flash memory, the Fwrie function is called;

[0043] If the MCU does not generate new SOE data, and the write pointer Pwrite is located at the last 50% storage area of ​​cache A, the Flashwrite function is called to write all the data of cache A into the flash memory, and the storage capacity value of cache A is defined as Aflashsize;

[0044] If the MCU sends out a request to read the SOE data of the specified SOE number, the Fread function is called;

[0045] 2.1) The processing flow of the Fwrie function is as follows:

[0046] The first step is to define the SOE data to be written into the flash memory as the target SOE data, and the storage capacity required by the target SOE data is defined as Asize, and the storage capacity of Asize is the length of the target SOE data plus 6 bytes, and the 6 bytes are used to store the sequence number of the target SOE data and the storage capacity value of Asize;

[0047] The second step is, if the storage capacity of the blank area of ​​cache A is greater than or equal to Asize, the target SOE data is stored at the beginning of the blank area of ​​cache A, and the storage capacity of the blank area of ​​cache A is subtracted from the value of Asize;

[0048] If the blank area storage capacity of cache A is less than Asize, the storage capacity occupied by all data in cache A is recorded as CSize, and the Flashwrite function is called to write all data in cache A into the flash memory, and the blank area storage capacity of cache A is reset to Aflashsize, and then the target SOE is stored at the starting end of cache A, and the blank area storage capacity value of cache A is updated to: the blank area storage capacity value of cache A is reduced by Asize;

[0049] The third step is to save the current SOE number and Asize into the array SA, move the write pointer Pwrite to the beginning of the blank area of ​​the buffer A, and then increase the SOE number by 1;

[0050] 2.2) The processing flow of the Flashwrite function is as follows:

[0051] If the blank area capacity of the flash memory is greater than or equal to Csize, the specified data of cache A are written one by one from the starting position of the blank area of ​​the flash memory, and then Csize is subtracted from the blank area capacity of the flash memory;

[0052] If the blank area capacity of the flash memory is less than Csize, the specified data of cache A are written one by one from the starting position of the storage area of ​​the flash memory, and then the blank area capacity value of the flash memory is updated to: the storage capacity value of the flash memory minus Csize;

[0053] 2.3) The processing flow of the Fread function is as follows:

[0054] If there is SOE data with a specified SOE number in cache A, then the SOE data with a specified SOE number is directly read from cache A, and then the Flashwrite function is called to write the data from the starting end to the SOE data with a specified SOE number in cache A into the flash memory, and the storage capacity occupied by this segment of data (the data from the starting end of cache A to the SOE data with a specified SOE number) is recorded as Bsize, and then the remaining data in cache A is moved as a whole to the starting position of cache A, and the storage capacity value of the blank area of ​​cache A is increased by Bsize, and the read pointer Pread and the write pointer Pwrite are both pointed to the starting end of the blank area of ​​cache A;

[0055] If the SOE data with the specified SOE number does not exist in the array SA, the Flashread function is called;

[0056] 2.4) The processing flow of the Flashread function is as follows:

[0057] The storage capacity of the SOE data with the specified SOE number is found from the array SA, and the location of the SOE data in the flash memory is calculated according to the storage capacity value of the SOE data found, and then the data is directly read from the flash memory.

Claims

1. A method for reading and writing flash in a power SOE data microcontroller, characterized in that: Includes predefined steps and flash reading and writing steps; 1) Predefined steps Set up a continuous buffer A to store the SOE data to be written into the flash memory, and define a read pointer Pread and a write pointer Pwrite; Define a write cache function Fwrie, which is used to write SOE data into the blank area of ​​cache A; Define a flash read function Fread, which is used to return the corresponding SOE data; Define a flash write function Flashwrite, which is used to write SOE data into the flash memory; Define an array SA, which is used to store the SOE number and the storage capacity of SOE data. The initial value of the SOE number is set to 1, and the initial positions of the read pointer Pread and the write pointer Pwrite both point to the beginning of the cache A; 2) Flash read and write steps Set a time period T, and check the SOE data reading and writing status of the MCU every T time interval; If the MCU generates new SOE data to be written into the flash memory, the Fwrie function is called; If the MCU does not generate new SOE data, and the write pointer Pwrite is located at the last 50% storage area of ​​cache A, the Flashwrite function is called to write all the data of cache A into the flash memory, and the storage capacity value of cache A is defined as Aflashsize; If the MCU issues a request to read the SOE data of the specified SOE number, the Fread function is called; 2.1) The processing flow of the Fwrie function is as follows: The first step is to define the SOE data to be written into the flash memory as the target SOE data, and the storage capacity required by the target SOE data is defined as Asize, and the storage capacity of Asize is the length of the target SOE data plus 6 bytes, and the 6 bytes are used to store the sequence number of the target SOE data and the storage capacity value of Asize; The second step is, if the storage capacity of the blank area of ​​cache A is greater than or equal to Asize, the target SOE data is stored at the beginning of the blank area of ​​cache A, and the storage capacity of the blank area of ​​cache A is subtracted from the value of Asize; If the blank area storage capacity of cache A is less than Asize, the storage capacity occupied by all data in cache A is recorded as CSize, and the Flashwrite function is called to write all data in cache A into the flash memory, and the blank area storage capacity of cache A is reset to Aflashsize, and then the target SOE is stored at the starting end of cache A, and the blank area storage capacity value of cache A is updated to: the blank area storage capacity value of cache A is reduced by Asize; The third step is to save the current SOE number and Asize into the array SA, move the write pointer Pwrite to the beginning of the blank area of ​​the buffer A, and then increase the SOE number by 1; 2.2) The processing flow of the Flashwrite function is as follows: If the blank area capacity of the flash memory is greater than or equal to Csize, the specified data of cache A are written one by one from the starting position of the blank area of ​​the flash memory, and then Csize is subtracted from the blank area capacity of the flash memory; If the blank area capacity of the flash memory is less than Csize, the specified data of cache A are written one by one from the starting position of the storage area of ​​the flash memory, and then the blank area capacity value of the flash memory is updated to: the storage capacity value of the flash memory minus Csize; 2.3) The processing flow of the Fread function is as follows: If there is SOE data with the specified SOE number in cache A, then the SOE data with the specified SOE number is directly read from cache A, and then the Flashwrite function is called to write the data from the starting end to the SOE data with the specified SOE number in cache A into the flash memory, and the storage capacity occupied by this segment of data is recorded as Bsize, and then the remaining data in cache A is moved as a whole to the starting position of cache A, and the storage capacity value of the blank area of ​​cache A is increased by Bsize, and the read pointer Pread and the write pointer Pwrite are both pointed to the starting end of the blank area of ​​cache A; If the SOE data with the specified SOE number does not exist in the array SA, the Flashread function is called; 2.4) The processing flow of the Flashread function is as follows: The storage capacity of the SOE data with the specified SOE number is found from the array SA, and the location of the SOE data in the flash memory is calculated according to the storage capacity value of the SOE data found, and then the data is directly read from the flash memory.