Disclosure of Invention
The invention aims to provide a data storage method to overcome the defects in the prior art.
In order to achieve the purpose, the technical scheme of the invention is as follows: a data storage method, provide a main memory area including multiple memory blocks, a backup memory area used for backing up the data of the said main memory area, an index area including at least one index unit, a data temporary storage area used for temporarily storing the data to be stored; the method is realized according to the following steps:
step S12: acquiring data to be stored, and writing the data into the data temporary storage area;
step S13: calculating a storage offset corresponding to the current time information;
step S14: calculating a write address of the data to be stored in the main storage area according to the following mode, and writing the data to be stored in the main storage area according to the write address:
the write address = the starting address of the main memory area + the starting offset stored in the current index unit + the storage offset;
step S15: calculating the backup address of the writing address mapping in the backup storage area according to the following mode, and writing the data to be stored into the backup storage area according to the backup address:
the backup address = the starting address of the backup storage area + the starting offset stored in the current index unit + the storage offset;
the index unit is configured to store a start offset of the storage block, and the current time information includes a storage tag used to tag a storage period and a time tag used to tag a sampling time sequence number corresponding to the data to be stored.
Further, before data storage is performed by initial power-on, initialization is performed according to the following steps S110 to S113:
step S110: erasing the main storage area and the backup storage area;
step S111: reading the initial address of a storage block of the main storage area, which is not written with data, and writing a storage mark of the current time information into the initial address;
step S112: calculating the initial offset of the initial address of the current storage block of the main storage area relative to the initial address of the main storage area, and writing the initial offset into the current index unit;
step S113: calculating the backup address of the backup storage area mapped by the starting address of the current storage block of the main storage area as follows, and writing the storage mark into the backup address:
backup address = the starting address of the backup storage area + the starting offset stored by the current index unit.
Further, a time buffer for storing time information is provided, and the step S110 further includes:
acquiring current time information and writing the current time information into a time temporary storage area;
after the step S15, the method further includes the following steps:
step S16: and acquiring the current time information and writing the current time information into a time temporary storage area.
Further, the step S12 specifically includes the following steps S121 to S122:
step S121: judging whether the storage flag of the current time information is consistent with the storage flag of the time information stored in the time temporary storage area, if so, executing step S122, otherwise, jumping to execute the steps S13 to S16, and executing the steps S111 to S113 after step S16;
step S122: judging whether the time stamp of the current time information is consistent with the time stamp of the time information stored in the time temporary storage area, if so, acquiring the data to be stored, writing the data into the data temporary storage area, and repeatedly executing the step S122; otherwise, jumping to execute the step S13 to the step S16.
Further, a main check area for storing the check code of the main storage area and a backup check area for storing the check code of the backup storage area are also provided;
in step S14, writing the data to be stored into the write address, or in step S111, writing the current storage flag into the start address, and further calculating a check code of the primary storage area; if the check is correct, writing the check code into the main check area, otherwise erasing the main storage area and the main check area, writing the data stored in the backup storage area into the corresponding address of the main storage area, and writing the check code of the backup check area into the main check area;
in step S15, writing the data to be stored into a backup address, or in step S113, writing a current storage flag into a backup address, and further calculating a check code of the backup storage area; if the check is correct, the check code is written into the backup check area, otherwise, the backup storage area and the backup check area are erased, the data stored in the main storage area is written into the corresponding address of the backup storage area, and the check code of the main check area is written into the backup check area.
Further, the index unit points to the next index unit in a circular circulation mode.
Further, in step S13, the method of calculating the storage offset corresponding to the current time information is as follows: storage offset = n1+ n2 (timestamp-1) +1, where n1 is the byte length of the storage marker, n2 is the byte length of the data to be stored, and n1, n2 are both positive integers.
Further, the time information includes year, month and date, the storage flag is year, and the time flag = month × 31+ date.
Further, the main storage area and the backup storage area adopt nor flash memories, and the index area, the data temporary storage area and the time temporary storage area adopt ferroelectric memories; and the main verification area and the backup verification area both adopt ferroelectric memories.
Further, when the storage process is interrupted unexpectedly, the data storage method after power-on is implemented according to the following steps S21 to S24:
step S21: reading the data stored in the main storage area, and recalculating the check code of the main storage area;
step S22: judging whether the recalculated check code is consistent with the check code stored in the main check area, if so, erasing the backup storage area and the backup check area, writing the data stored in the main storage area into the corresponding address of the backup storage area, writing the check code in the main check area into the backup check area, and skipping to execute the step S12 to continue to store the data; if not, go to step S23;
step S23: reading the data stored in the backup storage area, and recalculating the check code of the backup storage area;
step S24: judging whether the recalculated check code is consistent with the check code stored in the backup check area, if so, erasing the main storage area and the main check area, writing the data stored in the backup storage area into the corresponding address of the main storage area, writing the check code which is the backup check area into the main check area, skipping to execute the step S12, and continuing to store the data; if the data are inconsistent, the data cannot be recovered, and initialization is carried out according to the steps during initial power-on storage.
Compared with the prior art, the invention has the following beneficial effects:
(1) the storage address of the data in the main storage area or the backup storage area is closely related to the sampling time of the data, the sampling time corresponding to the data stored in any storage unit can be acquired by reverse-pushing according to the calculation rule of the storage offset, the written data does not contain specific sampling time, the storage space is saved, and meanwhile, the reading and the restoring of the data are not influenced. Taking time information including year, month and date as an example, storing all data in one year in the same storage block by taking the year as a storage mark, and writing the year information into the storage block; the data of every other day is transferred to the corresponding storage address of the main storage area or the backup storage area according to the month and the date of the current time, the storage address corresponding to the main storage area/the backup storage area in a certain day is fixed, when the data of the year is read, the storage block corresponding to the year is read in the main storage area, the month and the date corresponding to the data can be restored according to the storage address of the certain data, the specific month and date do not need to be written, the storage space is saved, and the later data restoration is not influenced.
(2) The invention adopts two identical but independent sectors of a main storage area and a backup storage area to store data in a time-sharing way, the data are firstly stored in the main storage area and then written into the backup storage area, the main storage area and the backup storage area form identical storage data which are mutually backed up, power is cut off at any time in the erasing and writing process of the sectors, the data stored in at least the main storage area or the backup storage area are ensured to be effective, one storage area can be used for carrying out data recovery on the other storage area, and a data structure that the main storage area and the backup storage area are mutually backed up is reestablished.
(3) The invention adopts the ferroelectric memory and the Nor Flash memory at the same time, utilizes the characteristics that the ferroelectric memory is erased and written for unlimited times and can be stored in a power-down mode to carry out real-time storage, makes up the defect that the Nor Flash memory is erased and written for limited times, utilizes the characteristics that the Nor Flash memory has large storage space and low cost to carry out large-data-volume storage, makes up the defect that the Nor Flash memory has high cost, and can simultaneously meet the requirements of real-time storage and large-data-volume storage by complementing the Nor Flash memory and the.
(4) And a check area is respectively configured for the main storage area and the backup storage area, and each write-in data is checked when being written into the main storage area or the backup storage area, so that each write-in data is correct, and meanwhile, whether the data stored in the main storage area or the backup storage area is wrong can be judged by using a check code stored in the check area.
Detailed Description
The technical scheme of the invention is specifically explained below with reference to the accompanying drawings.
In the present embodiment, the data storage device and the storage method proposed by the present invention are described in detail by taking the daily power generation amount of the photovoltaic system as an example, so as to make the inventive concept of the present invention more clearly and clearly understood by those skilled in the art.
The invention provides a data storage method and also provides a data storage device, as shown in fig. 1, comprising a main storage area, a backup storage area, an index area, a data temporary storage area, a time temporary storage area, a main check area and a backup check area. The main storage area and the backup storage area adopt a nor flash memory and are used for storing data which are acquired by a sensor or other modes and need to be stored for a long time. In this embodiment, the daily power generation amount is the daily power generation amount of the photovoltaic system; the index area, the data temporary storage area, the time temporary storage area, the main verification area and the backup verification area adopt ferroelectric memories which are used for storing data which needs to be continuously updated and repeatedly erased in the whole data acquisition period.
Further, in this embodiment, the main storage area includes a plurality of storage blocks with the same address length, the backup storage area and the main storage area form a storage block structure with the same address length, and the storage blocks are used for storing data to be stored in one storage cycle. In this embodiment, each storage block is used to store sampled data in a time period of year, and the address length of each storage block is greater than the maximum address length required by data to be stored in a year, so that the data block is not covered, and all data in the year can be completely reproduced.
Further, in this embodiment, the index area includes at least one index unit, the index unit is configured to store a start offset of a start address of a storage block of the main storage area relative to a start address of the main storage area, and the index unit points to a next index unit in a circular loop manner; in this embodiment, 20 index units are provided, which are respectively used for writing the start addresses of 20 memory blocks in the main memory area, and can overwrite the start addresses of different memory blocks.
Further, in this embodiment, the data temporary storage area is used for temporarily storing the data to be stored. In this embodiment, the data to be stored is the daily power generation amount accumulated at the current time.
Further, in this embodiment, the time buffer is used to store time information of the sampling time corresponding to the data to be stored. It should be noted that the sampling time may include information of year, month, date, hour, minute, second, etc., or even smaller time units according to actual needs, and the time information stored in the time buffer includes a storage flag for marking one storage period and a time flag for marking a sampling time serial number of the current data to be stored. In this embodiment, the storage period is one year, the storage flag is a corresponding year, the sampling period is 1 day, and the time flag is a month and a date, and more specifically, the current date may correspond to the number of days in the year. The sampling time sequence number represents a sequence number of a sampling period corresponding to the current data to be stored in the storage period, that is, represents that the current data to be stored is the data sampled for the second time.
Further, in this embodiment, the main verification area and the backup verification area are respectively used for storing the verification code of the main storage area and the verification code of the backup storage area.
Further, in this embodiment, the storage units that need to be erased repeatedly, such as the index area, the data temporary storage area, the time temporary storage area, the main verification area, the backup verification area, and the like, adopt a ferroelectric memory, the main storage area and the backup storage area are used for storing a large number of data storage units and adopt a Nor Flash memory, the characteristics that the ferroelectric memory is erased for unlimited times and can be stored in a power-down mode are used for real-time storage, the defect that the erasing times of the Nor Flash memory are limited is overcome, the characteristics that the Nor Flash memory is large in storage space and low in cost are used for large-volume data storage, the defect that the Nor Flash memory is high in cost is overcome, the advantages of the two are combined, the defects are complemented, and the cost is controlled on the premise.
Further, in this embodiment, the data storage method provided by the present invention, as shown in fig. 2, specifically includes the following steps:
it should be noted that the following time information includes a storage flag and a time flag, in this embodiment, the daily power generation amount of the photovoltaic system is used as a storage object, the time information includes a year, a month, and a date, where the year is the storage flag, and the time flag includes the month and the date, for example, the current date is 2016, 9, 20, and the storage flag is 2016, and the time flag is month 31+ date, specifically 9 × 31+20= 299. It should be noted that the default month is calculated by 31 days per month to simplify the calculation and ensure that each sampling time has a corresponding memory cell, ensuring data integrity.
Before data storage is carried out by initial power-on, initialization is carried out according to the following steps S110 to S113:
step S110: erasing the main storage area and the backup storage area; acquiring current time information and writing the current time information into a time temporary storage area;
step S111: reading the initial address of a storage block of the main storage area, wherein data is not written in the storage block, and writing the current storage mark, namely the current year, into the initial address; assuming that the memory block is the first data block of the main memory area, and the start address of the memory block is Ox0000, that is, the start address of the main memory area, the current year is written into the memory unit with the address of Ox 0000;
step S112: calculating the initial offset of the initial address of the current storage block of the main storage area relative to the initial address of the main storage area, and writing the initial offset into the current index unit; based on step S111, the calculated starting offset amount is 0;
step S113, mapping the initial address of the current storage block of the main storage area to the backup address of the backup storage area, wherein the backup address = the initial address of the backup storage area + the initial offset stored in the current index unit, and writing the storage mark into the backup address; assuming that the starting address of the backup storage area is Ox8000, the backup address is Ox 8000;
the initialization process of the initial power-on is completed.
Step S12: acquiring data to be stored, and writing the data into a data temporary storage area;
step S12 specifically includes the following steps S121 to S122:
step S121: judging whether the storage mark of the current time information is consistent with the storage mark of the time information stored in the time temporary storage area, if so, executing step S122, namely, reacquiring the data to be stored, writing the data into the data temporary storage area and acquiring the corresponding time information; otherwise, the jump is executed from step S13 to step S16 to complete the storage of the data to be stored, and after step S16, the jump is executed from step S111 to step S113 to re-read one of the storage blocks of the primary storage area where no data is written, complete the storage of the storage marks in the primary storage area and the backup storage area, and write the start offset in the next index unit to prepare for writing the data to be stored into the new storage block.
Step S122: judging whether the time mark of the current time information is consistent with the time mark of the time information stored in the time temporary storage area, if so, indicating that the next sampling time is not reached, in this embodiment, that is, the day is not finished, acquiring the data to be stored, writing the data into the data temporary storage area, and repeatedly executing the step S122; otherwise, the step S13 is skipped.
As in the above steps S111 to S113, writing of the storage mark to the start address of one of the storage blocks of the main/backup storage areas is realized, and in S121 to S122, it is ensured that the data having the same storage mark is stored in the same storage block, facilitating the reading and restoring of the data.
Step S13: calculating a storage offset corresponding to the current time information; preferably, the storage offset = n1+ n2 (month 31+ date-1) +1, where n1 is the byte length of the storage flag, n2 is the byte length of the data to be stored, and n1 and n2 are both positive integers.
The storage address of the data in the main storage area or the backup storage area is related to the sampling time of the data, the sampling time corresponding to the data stored in any storage unit can be acquired by reverse-pushing according to the calculation rule of the storage offset, the written data does not contain specific sampling time, the storage space is saved, and meanwhile, the reading and the restoring of the data are not influenced. Taking time information including year, month and date as an example, the year is a storage mark, the month and the date are used as time marks, all data in the same year are stored in the same storage block, the storage block only stores data of one year, and the year information is written into the storage block; the month and the date are used as time marks, the data of every other day are transferred to the main storage area or the backup storage area, the writing address of each data in the main storage area is related to the corresponding month and date, the storage address corresponding to the main storage area/the backup storage area in a certain day of the year is fixed, when the data of the year is read, the storage block corresponding to the year is read in the main storage area, the month and the date corresponding to the data can be restored according to the storage address of the certain data, the specific month and date do not need to be written, the storage space is saved, and the data restoration in the later period is not influenced.
Step S14: calculating a write address of the current data to be stored in the main storage area, wherein the write address = an initial address of the main storage area + an initial offset stored in the current index unit + a storage offset, and writing the data to be stored in the write address;
step S15: calculating a backup address stored in a backup storage area by the current data to be stored, wherein the backup address = an initial address of the backup storage area + an initial offset stored in a current index unit + a storage offset, and writing the data to be stored in the backup address;
step S16: acquiring the current time information, writing the current time information into the time temporary storage area, erasing the data temporary storage area, and jumping to execute step S12 to start storing the next data to be stored.
As in the above S11-S16, writing data into the primary storage area for storage and writing the data into the backup address of the backup storage area for data backup storage are realized. Before data storage, the main storage area and the backup storage area are two storage areas with completely same storage block structures and empty contents, and after data storage, the main storage area and the backup storage area can form two storage areas with same stored data contents.
As in S14, writing the data to be stored into the write address or writing the current storage flag into the start address in S111, calculating the check code of the main storage area, writing the check code into the main check area if the check is correct, otherwise erasing the main storage area and the main check area, writing the data content stored in the backup storage area into the corresponding address of the main storage area, so that the main storage area forms the data content completely consistent with the backup storage area, and writing the check code stored in the backup check area into the main check area. The specific correspondence relationship of the data content stored in the backup storage area at the corresponding address of the main storage area is as follows: the corresponding address of the main storage area = backup address of the backup storage area- (start address of backup storage area-start address of main storage area).
As in S15, writing the data to be stored into the backup address or writing the current storage flag into the backup address in S113, calculating the check code of the backup storage area, writing the check code into the backup check area if the check is correct, or erasing the backup storage area and the backup check area, writing the data content stored in the main storage area into the corresponding address of the backup storage area, so that the backup storage area forms a data structure completely consistent with the main storage area, and writing the check code stored in the main check area into the backup check area. The specific correspondence relationship of the data content stored in the main storage area at the corresponding address of the backup storage area is as follows: the corresponding address of the backup memory area = backup address + of the main memory area (starting address of the backup memory area-starting address of the main memory area).
When data is written in the main storage area/the backup storage area, the written data is verified every time, and it is guaranteed that each written data is correct. In this embodiment, the verification is performed by calculating the sum of the data in the main storage area and the data in the backup storage area. Taking the write address of the data to be stored written into the main storage area as an example, the verification method is as follows: before the data to be stored is written in, the check code in the check area is taken out, an original check code is calculated with the data to be stored for standby, and after the data to be stored is written in, the data in the main storage area is read out to calculate a new check code; if the two check codes are consistent, the check codes are correctly checked and written into the check area, otherwise, the check is wrong, namely, the data writing process is wrong. The verification mode when writing data in the backup storage area is the same as the above verification mode.
When the storage process is interrupted unexpectedly, the data storage method after power-up is realized according to the following steps S21 to S24:
step S21: reading data stored in the main storage area and recalculating the check code of the main storage area; the method for calculating the check code is consistent with the method for calculating the check code when data is written
Step S22: judging whether the recalculated check code is consistent with the check code stored in the main check area, if so, indicating that the data stored in the main memory area is valid, erasing the backup memory area and the backup check area, writing the data stored in the main memory area into the corresponding address of the backup memory area, writing the check code in the main check area into the backup check area, and skipping to execute the step S12 to continue to store the data; if the data in the main storage area is not consistent with the data in the main storage area, the step S23 is executed;
step S23: reading data stored in the backup storage area and recalculating the check code of the backup storage area;
step S24: judging whether the recalculated check code is consistent with the check code stored in the backup check area, if so, indicating that the data stored in the main storage area is valid, erasing the main storage area and the main check area, writing the data stored in the backup storage area into the corresponding address of the main storage area, writing the check code of the backup check area into the main check area, and skipping to execute the step S12 to continue to store the data; if the data in the main storage area and the data in the backup storage area are inconsistent, the data in the main storage area and the data in the backup storage area are both damaged, and the data cannot be recovered by adopting a software mode, but the occurrence probability of the situation is extremely low; if this occurs, initialization may be performed according to the procedure when the memory was first powered up.
In this embodiment, after the power is turned on again, the check code is recalculated in the same check mode as that in the case where the data is written into the main storage area or the backup storage area, and the check code is compared with the check code stored in the main check area or the backup check area, to verify whether the data is destroyed due to storage interruption, and perform data recovery of the storage area according to the data of the other storage area, and reestablish two data to complete the same storage area. And ensuring that the data of the main storage area and the backup storage area are consistent and then storing new data.
The above is a preferred embodiment of the present invention, and all changes made according to the technical scheme of the present invention, such as modifying the above-mentioned data stored every other day to be stored once in 1 hour or 2 days or in other time periods, or modifying the above-mentioned one storage block to be used for storing data of one year to be stored in other time periods, or using the data storage device or the data storage method of the present invention in other fields to store other parameters, belong to the protection scope of the present invention when the generated functional effects do not exceed the scope of the technical scheme of the present invention.