Data storage method for simulating EEPROM by using PFLASH software
Technical Field
The application belongs to the technical field of automobile electronic embedded software, and particularly relates to a data storage method for simulating EEPROM (electrically erasable programmable read-Only memory) by using PFLASH software.
Background
With the continuous progress of semiconductor technology (according to moore's law), the logic function peripherals integrated inside the MCU are more and more, and the memory is also larger and larger; on the other hand, the requirements of consumers on energy conservation, comfort, interconnection and safety of automobiles are higher and higher, and particularly, the development of automobile electronic technology is greatly accelerated in the rising of new energy electric vehicles, internet of vehicles and automatic driving technologies in recent years. The functions of the electronic ECU (Electronic Control Unit-electronic control unit) of the automobile are increasingly complex, and in order to cope with the needs of software remote (on-line) function upgrading (adding new functions) and bug repairing, the needs of bootLoader are increasingly more and more.
The ECU consists of a singlechip and various complex circuits, and the NVM of the ECU generally comprises an EEPROM or a Data-Flash integrated in an MCU chip for storing Data, a Code-Flash/Program-Flash for storing Program codes/Data and an off-chip NOR Flash or NAND-Flash expanded by an MPU.
EEPROM (Electrically Erasable Programmable read only memory) is a charged erasable programmable read-only memory, which is a memory chip with no data loss after power failure.
The Program-Flash is PFLASH, which is used for playing codes or data, namely the main Flash. In the prior art, a plurality of PFLASH are mainly used, and a fixed storage data length and a corresponding PFLASH address are set during initialization. The main problems are: abnormal power failure makes it difficult to find data which is effectively stored last time, and PFLASH space is wasted when the fixed length is much longer than the length of the data which is actually stored.
Disclosure of Invention
The application aims to solve the problems of abnormal power failure, how to find the data which are effectively stored last time and low FLASH space utilization rate when the power is on next time, and provides a data storage method for simulating EEPROM by using PFLASH software.
The application realizes the above purpose through the following technical scheme:
a data storage method for simulating an EEPROM using PFLASH software, comprising the steps of:
s1, constructing a data structure based on PFLASH software, wherein the data structure stores two bytes of parameters, a byte of sequence number and a byte of CRC (cyclic redundancy check) for processing singlechip storage data read by the PFLASH software to obtain first data;
s2, setting a data cache list and a default value list based on PFLASH software, wherein the data cache list is used for storing parameters in the first data, and the default value list is used for storing default values of the parameters in the first data;
s3, powering up and initializing the singlechip, judging whether the singlechip is powered up for the first time and whether the singlechip never stores data, and if so, storing default values of parameters in first data in a default value list into a data cache list; if not, finding a PFLASH page stored by the parameters of the current first data, and storing the parameters of the current first data into a data cache list;
and S4, when the current FLASH page is full of page replacement, writing the value in the current data cache list into the PFLASH page after page replacement.
As a further optimization scheme of the present application, in step S2, one data cache list and one default value list are set, which are lists for storing two byte parameters, respectively.
In step S3, when the parameters of the current first data are stored in the data cache list, whether the parameters are the same as the latest historical parameters cached in the data cache list or not is judged through CRC check, if so, the storage is canceled, and if not, the parameters of the current first data are stored in the data cache list.
As a further optimization scheme of the application, the method for finding the latest historical parameters corresponding to the parameters in the current first data comprises the following steps: after PFLASH of the parameter storage of the current first data is found, the latest historical parameter of each first data parameter is found out from back to front according to the serial number in the current first data.
As a further optimization scheme of the application, in the step S3, after finding the PFLASH stored currently, find each latest parameter from back to front according to the storage sequence number, and then judge the parameter value in the step S4.
As a further optimization scheme of the present application, the method for finding the PFLASH page stored by the parameters of the current first data is as follows: the PFLASH software simulates the EEPROM to set a start address on the PFLASH page of each singlechip for storing data, and after the singlechips are electrified, the PFLASH software reads the start address data and judges whether the current PFLASH page is the PFLASH page of the parameter storage of the current first data.
As a further preferred embodiment of the present application, in step S4, when writing the value in the current data cache list into the PFLASH page after the page change, the state of the current PFLASH is updated at the start address.
The application uses the minimum unit of the PFLASH writing operation, and can ensure that the parameter writing fails as little as possible when the power-down is abnormal.
If the last written data is not checked, the effective value of the last correct written data can be automatically found back, so that the problem that some key data are lost due to abnormal power-down is solved.
When the page is replaced by the stored data, the application does not adopt fixed-length integral transportation, only writes the current latest data cache list into a new page, and the application has no problem of PFLASH space waste with fixed length.
The application has the beneficial effects that:
1) The application constructs a data structure by using the minimum operation unit of 4 bytes stored by PFLASH, so that the failure of parameter writing can be ensured to be as few as possible when power-down is abnormal;
2) The application electrically reads the data of each PFLASH initial address used for simulating the EEPROM, judges whether the current PFLASH is the PFLASH stored currently, finds the PFLASH stored currently, and finds the latest data of each parameter from back to front;
3) When the singlechip is abnormally powered off, if the last written data is not checked, the valid value which is correctly written last time can be automatically found back, so that the problem that some key data are lost due to abnormal power-off is solved;
4) When the page is replaced by the stored data, the application does not adopt fixed-length integral transportation, only writes the current latest data cache list into a new page, and the application has no problem of PFLASH space waste with fixed length.
Detailed Description
The following detailed description of the application is provided to illustrate the application and should not be construed as limiting the scope of the application since it is intended that the following detailed description is given for the purpose of illustration only, and that certain non-essential modifications and adaptations of the application may occur to those skilled in the art in light of the foregoing disclosure.
Example 1
A data storage method for simulating an EEPROM using PFLASH software, comprising the steps of:
s1, constructing a data structure based on PFLASH software, wherein the data structure stores two bytes of parameters, a byte of sequence number and a byte of CRC (cyclic redundancy check) for processing singlechip storage data read by the PFLASH software to obtain first data;
s2, setting a data cache list and a default value list based on PFLASH software, wherein the data cache list and the default value list are respectively used for storing two byte parameters, the data cache list is used for storing parameters in the first data, and the default value list is used for storing default values of the parameters in the first data;
s3, powering up and initializing the singlechip, and judging whether the singlechip is powered up for the first time or not and whether data are never stored;
s301, when the singlechip is powered on for the first time, data are never stored, and default values of parameters in first data in a default value list are stored in a data cache list;
s302, when the singlechip is not powered on for the first time, and when the latest first data is stored, finding a PFLASH page stored by parameters of the current first data, and storing the parameters of the current first data into a data cache list;
specifically, when abnormal power failure occurs, the latest CRC verification of one parameter is certainly problematic, when the parameter of the current first data is stored in the data cache list, whether the parameter is the same as the latest historical parameter cached in the data cache list or not is judged through CRC verification, if so, the storage is canceled, and if not, the parameter of the current first data is stored in the data cache list;
the method for finding the latest historical parameters corresponding to the parameters in the current first data comprises the following steps: after PFLASH of the parameter storage of the current first data is found, according to the serial number in the current first data, finding out the latest historical parameter of each first data parameter from back to front;
the method for finding the PFLASH page stored by the parameters of the current first data comprises the following steps: the PFLASH software simulates the EEPROM to set a starting address on the PFLASH page of each singlechip for storing data, and after the singlechips are electrified, the PFLASH software reads the starting address data and judges whether the current PFLASH page is the PFLASH page for storing the parameters of the current first data;
and S4, when the current FLASH page is full of page replacement, writing the value in the current data cache list into the PFLASH page after page replacement, and updating the state of the current PFLASH at the starting address.
The application constructs a data structure with the minimum operation unit of 4 bytes stored by PFLASH and with sequence numbers, parameters and verification information, can ensure that the data loss caused by abnormal power failure is as little as possible, and can find the latest data upwards; when the current FLASH page is full and needs to be changed, the dynamic data cache list is adopted for writing, so that the space resources of the PFLASH are effectively utilized. The application is used in the high-power chip domestic BMS platform, and has excellent test verification performance.
The foregoing examples illustrate only a few embodiments of the application and are described in detail herein without thereby limiting the scope of the application. It should be noted that it will be apparent to those skilled in the art that several variations and modifications can be made without departing from the spirit of the application, which are all within the scope of the application.