First-in first-out FLASH data storage method, system and terminal
Technical Field
The invention relates to the technical field of automobile electronics and Internet of vehicles, in particular to a first-in first-out FLASH data storage method, a system and a terminal.
Background
In the automotive electronics and networking fields, TBOX box devices report generated data to a remote server in real time. However, when the wireless network is not smooth, the data generated during the period needs to be temporarily stored on a FLASH memory chip on the device in a cyclic coverage manner. FLASH has a limited number of erasures, which requires a reasonable strategy to use FLASH, otherwise, the FLASH may cause a whole page fault due to premature failure of part of the page.
In the prior art, strategies for FLASH management comprise file systems such as FAT and the like; they fail to make the FLASH memory blocks uniformly usable, resulting in partial blocks premature senility; in addition, the content of the management part occupies too much space and occupies too much space for data storage.
In order to solve the problems, the invention provides a first-in first-out FLASH data storage method, a system and a terminal.
Disclosure of Invention
The invention solves the technical problem of providing a first-in first-out FLASH data storage method, a system and a terminal. The first-in first-out FLASH data storage method, the first-out FLASH data storage system and the terminal can enable all blocks of the FLASH memory to be used uniformly, so that partial blocks are premature in failure.
In order to solve the technical problems, the technical scheme provided by the invention is as follows:
a first-in first-out FLASH data storage method includes:
defining a variable-length data block;
searching a blank area, writing a data block into the forefront end of the blank area, and marking the data block as valid;
after the writing is completed, the read-back check is performed to determine whether the writing is successful;
if not, rewriting the whole data area to 0;
if successful, the re-input data is written into the next blank area in the following way.
Preferably, the composition of the data block is: the beginning of a specified content, the middle contains the total length of the data block, the valid flag of the data block, the data content, and the data check code ends. Defining a data block has the advantage that insertion of data is facilitated, and by searching the data block, the search process is facilitated.
Preferably, the data storage method further includes a data reading process, and the data reading process includes:
searching the data block which is written first and marked as valid;
marking it as invalid and checking whether the modification is successful;
if the change is not successful, rewriting the whole data area to 0;
if the marking is successful, continuing to read the following valid data.
Further preferably, the writing and reading of the data both include a searching process, and the searching process specifically includes determining the current condition of the FLASH memory:
reading a part of data of the head of the FLASH memory, and recording as Dh; the end of the memory reads a part of data forward, which is marked as Dt; and judging the service conditions of Dh and Dt. Wherein the length of the part of data is the maximum effective length of one data block.
Further preferably, the use cases of the judgment Dh and Dt specifically are:
if Dh is unused and Dt is unused, writing the data into the head, and no readable data exists;
if Dh is not used and Dt is used, writing the data into the head, and searching the first block of valid data from beginning to end during reading;
if Dh is used and Dt is not used, searching for blank area writing from the head, and reading valid data from the head;
if Dh is used and Dt is used, writing and reading from the head to search for a blank area, judging whether a first data block mark is valid, searching for valid data from tail to head if yes, and searching for valid data from head to tail if no. The system can realize reasonable and uniform use of the FLASH memory by the searching method, and prevent premature senility of partial blocks.
It is further preferred that the entire area is rewritten to 0 when a bad block is encountered during the seek.
Further preferably, after the searching, after determining the writing point and the reading point, the writing point and the next reading point are saved to the memory variable, and are directly used when writing or reading is performed again. This is directly used and adjusted when writing or searching next time, avoiding searching again.
A first-in first-out FLASH data storage system comprising:
the definition module: the definition module is used for defining a variable-length data block;
and (3) a searching module: the searching module is used for searching the blank area, writing the data block into the forefront end of the blank area and marking the data block as valid;
and a judging module: the judging module is used for reading back to check whether the writing is successful after the writing is completed; if not, rewriting the whole data area to 0; if successful, the re-input data is written into the next blank area in the following way.
Preferably, the definition module is configured to define a variable-length data block, where the data block is composed of: the beginning of a specified content, the middle contains the total length of the data block, the valid flag of the data block, the data content, and the data check code ends.
Preferably, the storage system further includes a data reading module, and the reading process of the data reading module includes:
searching the data block which is written first and marked as valid;
marking it as invalid and checking whether the modification is successful;
if the change is not successful, rewriting the whole data area to 0;
if the marking is successful, continuing to read the following valid data.
Further preferably, the data system further includes a search module, and the search process of the search module is specifically determining the current situation of the FLASH memory:
reading a part of data of the head part of the memory, and recording as Dh; the end of the memory reads a part of data forward, which is marked as Dt; and judging the service conditions of Dh and Dt.
Further preferably, the use cases of the judgment Dh and Dt specifically are:
if Dh is unused and Dt is unused, writing the data into the head, and no readable data exists;
if Dh is not used and Dt is used, writing the data into the head, and searching the first block of valid data from beginning to end during reading;
if Dh is used and Dt is not used, searching for blank area writing from the head, and reading valid data from the head;
if Dh is used and Dt is used, writing and reading from the head to search for a blank area, judging whether a first data block mark is valid, searching for valid data from tail to head if yes, and searching for valid data from head to tail if no. The searching method can uniformly use all the blocks of the memory, thereby preventing the memory from premature senility.
Further preferably, the searching module rewrites the whole area to 0 when a bad block is encountered in the searching process.
Further preferably, after the searching, after determining the writing point and the reading point, the next writing point and the next reading point are saved to the memory variable, and are directly used when writing or reading is performed again.
A computer readable storage medium storing computer program instructions adapted to be loaded by a processor and to perform a first-in first-out FLASH data storage method.
A mobile terminal comprises a processor and a memory, wherein the processor is used for executing a program stored in the memory so as to realize a first-in first-out FLASH data storage method.
Compared with the prior art, the invention has the following beneficial effects: the method of the invention can well lead each block of the FLASH memory to be used uniformly, prevent partial blocks from premature senility, simultaneously prevent the space occupied by the content of the management part from excessively large proportion, and squeeze the space occupied by the data storage. The device can make each block of the whole memory be erased and used uniformly through the algorithm, and bad blocks can be skipped effectively, so that the safety of data is ensured.
Drawings
The invention will be further described with reference to the drawings and examples.
Fig. 1 is a flow chart of a first-in first-out FLASH data storage method according to the present invention;
fig. 2 is a block diagram of a first-in first-out FLASH data storage system according to the present invention.
Detailed description of the preferred embodiments
The invention will now be described in further detail with reference to the accompanying drawings. The figures are simplified schematic representations which illustrate the basic flow diagrams of the invention by way of illustration only, and which therefore show only the flow associated with the invention.
Example 1
As shown in fig. 1, the present invention is a method for storing FLASH data in first out, which specifically includes:
s1, defining a variable-length data block;
s2, searching a blank area, writing the data block into the forefront end of the blank area, and marking the data block as valid;
s3, after the writing is completed, the read-back check is performed to determine whether the writing is successful;
s4, if the data area is unsuccessful, rewriting the whole data area to be 0;
s5, if successful, the re-input data is written into the next blank area in a downstream mode.
The data storage process comprises the following steps:
step S1: defining a variable length data block, said data block having the composition: the beginning of a specified content, the middle contains the total length of the data block, the valid flag of the data block, the data content, and the data check code ends.
Step S2: and searching a blank area, writing the data block into the forefront end of the blank area, marking the data block as valid, and searching the whole FLASH memory before writing.
Step S3: after the writing is completed, the read-back check is performed to determine whether the writing is successful;
step S4: if not, rewriting the whole data area to 0;
step S5: if successful, the re-input data is written into the next blank area in the following way.
Example 2
The first-in first-out FLASH data storage method also comprises a data reading process, wherein the data reading process comprises the following steps:
searching the data block which is written first and marked as valid;
marking it as invalid and checking whether the modification is successful;
if the change is not successful, rewriting the whole data area to 0;
if the marking is successful, continuing to read the following valid data.
The writing and reading of the data comprise searching processes, and the searching processes specifically comprise determining the current condition of FLASH:
reading a part of data of the head part of the memory, and recording as Dh; the end of the memory reads a part of data forward, which is marked as Dt; and judging the use condition of Dh and Dt, wherein the length defined by a part of data is the maximum effective length of one data block.
The use cases of the judgment Dh and the judgment Dt can be divided into four types, and the use cases are specifically as follows:
(1) If Dh is unused and Dt is unused, it means that the memory is empty, i.e., data has not been written. In this case, the header is written when the data is written, and no readable data is read when the data is read.
(2) If Dh is not used and Dt is used, it means that the front part of the memory is empty and the rear part is full of data. In this case, when writing data, the head is written, and then if the blank area is used up, the next block area is erased to be the blank area, and the first block of valid data is searched from the beginning to the end when reading for the next writing.
(3) If Dh is used and Dt is not used, it means that the front part of the memory is full of data and the rear part is empty. For this case, when writing data, searching a blank area from tail to head during writing, writing the data into a start area of the blank area after finding, and then erasing the next block as the blank area for next writing if the blank area is used up; reading the valid data from the head at the time of reading;
(4) If Dh is used and Dt is used, it means that the front part of the memory is full, the middle part is empty, and the rear part is full. For this case, the blank area is searched from beginning to end during writing, the data is written into the initial area of the blank area after finding, and then if the blank area is used up, the next block is erased to be the blank area for the next writing; when the bad block is encountered in the searching process of checking the first block number of the memory, the whole area is rewritten into 0. Searching and reading the first block of valid data from beginning to end according to whether the mark is valid or not and if not; if so, searching for "valid" data from tail to head, and encountering invalid data, i.e., blank area or data marked as not "valid", is stopped, and the first block of "valid" data following the invalid data is the data that should be read.
By the searching method, all the blocks of the FLASH memory can be well and evenly used, partial premature senility of the blocks is avoided, the storage space is reasonably managed, and writing and searching of data are facilitated.
And when the searching module encounters a bad block in the searching process, the whole area is rewritten into 0.
After the writing point and the reading point are determined after the searching, the writing point and the next reading point are saved to the memory variable, and are directly used when writing or reading is performed again.
Example 3
As shown in fig. 2, the present invention provides a FLASH data storage system of first in first out:
definition module 1: the definition module is used for defining a variable-length data block;
the searching module 2: the searching module is used for searching the blank area, writing the data block into the forefront end of the blank area and marking the data block as valid;
and a judging module 3: the judging module is used for reading back to check whether the writing is successful after the writing is completed; if not, rewriting the whole data area to 0; if successful, the re-input data is written into the next blank area in the following way.
The system for data also comprises a searching module, wherein the searching process of the searching module is specifically to determine the current condition of the FLASH memory:
reading a part of data of the head part of the memory, and recording as Dh; the end of the memory reads a part of data forward, which is marked as Dt; and judging the service conditions of Dh and Dt.
Further preferably, the use cases of the judgment Dh and Dt specifically are:
if Dh is unused and Dt is unused, writing the data into the head, and no readable data exists;
if Dh is not used and Dt is used, writing the data into the head, and searching the first block of valid data from beginning to end during reading;
if Dh is used and Dt is not used, searching for blank area writing from the head, and reading valid data from the head;
if Dh is used and Dt is used, writing and reading from the head to search for a blank area, judging whether a first data block mark is valid, searching for valid data from tail to head if yes, and searching for valid data from head to tail if no.
And when the searching module encounters a bad block in the searching process, the whole area is rewritten into 0.
The invention also includes a computer readable storage medium having stored thereon computer program instructions adapted to be loaded by a processor and to perform a first-in first-out FLASH data storage method.
The invention also comprises a mobile terminal which comprises a processor and a memory, wherein the processor is used for executing a program stored in the memory so as to realize a first-in first-out FLASH data storage method.
The foregoing detailed description is directed to embodiments of the invention, and is not intended to limit the scope of the invention, but rather to cover all modifications and variations within the scope of the invention.