Disclosure of Invention
In view of the above, it is necessary to provide a method for storing flash data in order to solve the above technical problems.
A method for storing flash data comprises the following steps:
a, B configuring two sectors, dividing each sector into n groups;
reading the effective mark values of the same group of data in the sectors A and B, and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors;
judging an invalid sector according to the valid mark value of the specific group, and erasing the data of the invalid sector;
new data is written by the next group of locations of the last data record.
In one embodiment, the method further comprises the following steps:
reading the data of the next group according to the position of the last data record;
judging whether the read group data exists or not;
if the data exists, representing that the data writing fails caused by the last power failure, and recording a last writing failure mark.
In an embodiment, the finding the position of the last data record according to the valid flag values of the same group of data in the two sectors specifically includes the following steps:
reading valid flag values of a first set of data of the A sector and the B sector;
and searching the position of the last data record according to the valid mark values of the two first groups of data.
In an embodiment, the finding the position of the last data record according to the valid flag values of the two first groups of data specifically includes the following steps:
the first group of data of the A sector is marked as A1, and the first group of data of the B sector is marked as B1;
reading the valid flag values in A1 and B1, wherein the valid flag value of A1 is marked as X, the valid flag value of B1 is marked as Y, and meanwhile, when the value of the valid flag value is 0xFF, the group of data is represented to be invalid, and when the value of the valid flag value is 0X7F, the group of data is represented to be valid;
if X, Y is equal to 0xFF, the data representing the A sector and the B sector are invalid, An or Bn is the position of the last data record by default, the data of An or Bn is read when the data is read next time, An is the last group of data of the A sector, and Bn is the last group of data of the B sector;
if X, Y is equal to 0x7F, then the valid flag value Z in An must be read again, and if Z is equal to 0xFF, then the last data record is located at A1; if Z is not equal to 0xFF, the position of the last data record is B1;
if X is equal to 0xFF and Y is equal to 0X7F, reading the last group of data of the B sector forwards, and searching group data Bt with a first effective mark value of 0X7F as the position of the last data record, wherein Bt is represented as the data of the t-th group of the B sector;
if X is equal to 0X7F and Y is equal to 0xFF, reading from the last group of data of the A sector and searching for the group data At with the first valid flag value of 0X7F as the position of the last data record, where At is the t-th group of data of the A sector.
In an embodiment, the determining, in combination with the valid flag value of the specific group, an invalid sector, and erasing data of the invalid sector specifically includes the following steps:
when X, Y is equal to 0x7F, the valid flag value Z in An needs to be read again, if Z is equal to 0xFF, all data in B sector is erased; if Z is not equal to 0xFF, all data in the A sector is erased.
In one embodiment, the method further comprises the following steps:
judging whether the previous power failure has sector erasing failure;
if the position of the last data record is in the A sector, reading the effective mark value of Bn; when the effective mark value of Bn is not equal to 0xFF, the B sector is erased again;
if the position of the last data recording is in the B sector, reading the effective mark value of An; when the valid flag value of An is not equal to 0xFF, the A sector is re-erased.
In one embodiment, the method further comprises the following steps:
when new data is written in, analyzing the value of the last write failure mark;
if the last write failure flag is characterized as failure, skipping the group of the last write operation failure;
and if the last write failure mark is characterized as successful, normally judging to write.
In an embodiment, if the last write failure flag is characterized as a failure, the group in which the last write operation failed needs to be skipped, specifically including the following steps:
if the read group is the last group of the sectors, erasing the data of the other sector, switching to the first group of the sectors which are just erased for writing during writing, and erasing the sectors which are fully written before the writing is finished;
if the read group is the penultimate group of the sector, switching to the first group of the other sector for writing during writing, and erasing the sector which is fully written before the writing is finished;
if the read group is any group except the two cases, writing the read group into the next group of the read group;
in addition to the above, if the last write failure flag is characterized as successful, then normally determining the write, specifically includes the following steps:
if the read group is the last group of the sectors, switching to the first group of another sector for writing during writing, and erasing the sectors which are fully written before the writing after the writing;
if the read group is any group that is not the last group, the next group of the read group is written.
A flash data saving system, comprising:
a configuration module, configured A, B two sectors, each sector being divided into n groups;
the searching module is used for reading the effective mark values of the same group of data in the A and B sectors and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors;
the erasing module is used for judging invalid sectors according to the valid mark values of the specific group and erasing the data of the invalid sectors; and
and the writing module is used for writing new data into the next group of the position recorded by the last data.
A computer device comprising a memory and a processor, the memory storing a computer program, wherein the processor implements the steps of a method for saving flash data when executing the computer program.
A computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the steps of a method for saving flash data.
The method adopts a flash memory space partition management method, in the memory, the storage space required to store specific data is divided into two sectors for management, each sector is divided into each group according to the size of the specific data, and the reading and writing of the flash are operated by taking the group as a unit; when writing operation is carried out, information in a sector is read firstly, the address written at this time is judged, and when data is written, a specific effective mark value is additionally arranged at the tail of the data to indicate whether the group of data is effective or not, so that power failure caused by half writing of the data is prevented, and the data is unreliable. And simultaneously, after the writing is finished, the written data is read immediately, the read data is compared with the written data, and if the read data is inconsistent with the written data, flash life exhaustion early warning is carried out.
An erase sector operation erases another sector only if the address where the data is written is the first group of the current sector. Therefore, the problem that the operation timeliness of the program is influenced because the sector where the data is located needs to be erased before the data is written every time is solved.
During reading operation, the information of each sector is read first, and the address read this time is judged. And the read data is subjected to identification judgment, so that the real validity of the data is ensured. If invalid, it is considered to take the last written data.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
The flash data storage method can be applied to the application environment shown in fig. 1. Wherein the terminal 102 communicates with the server 104 via a network. The terminal 102 may be, but not limited to, various personal computers, notebook computers, smart phones, tablet computers, and the like, and the server 104 may be implemented by an independent server or a server cluster formed by a plurality of servers.
In one embodiment, as shown in FIG. 2, a method for saving flash data is provided. The method is described as an example of the application to the terminal in fig. 1, and the method includes the following steps:
s100, configuring A, B two sectors, and dividing each sector into n groups.
S200, reading the effective mark values of the same group of data in the sectors A and B, and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors.
S300, judging an invalid sector by combining the valid mark values of the specific group, and erasing the data of the invalid sector;
s400, new data is written by the next group of the position recorded by the last data.
The specific content of step S100 includes the following steps:
and after power-on operation, setting the initial state of each variable value. A, B two sectors are configured for storing specific data, the first address of record A, B sector is Paddr1, Paddr2, valid flag value Fvalue2 of each group in A, B sector is 0x7F, sector flag fsegction is 0, group number Fgroup is 0, flash life alarm flag Ferror is 0, last write failure flag Fw _ fail is 0. Where Fsection is an a sector when Fsection is 1, a B sector when Fsection is 2, and Fvalue2 is 0xFF indicating data invalid (set to valid by default, Fvalue2 is 0x 7F).
In another embodiment, step S100 further includes the following steps:
when the initial state of each variable value is configured, an address Paddr0 is configured to store an enable flag value Fvalue1 ═ 0xAA 55.
After the initial state of each variable value is configured, first, whether a storage method of flash data is enabled or not is detected, two bytes of data are read from an address Paddr0 and are marked as T:
if T is equal to Fvalue1, the storage method of the flash data is enabled, and A, B sectors do not need to be erased;
if T is not equal to Fvalue1, indicating that the storage method of the flash data is not enabled, A, B sectors need to be erased, and then Fvalue1 is written into the address of Paddr 0.
Each sector is divided into n groups, the n groups of data of the a sector are respectively a1-An, the n groups of data of the B sector are respectively B1-Bn, the size of the group is the size of the specific data plus one byte (for putting a valid flag value), and therefore, the group number Fgroup is (1, 2, 3 … … n). And if the sector space has not enough space after the sharing, abandoning the use of the space.
The specific contents of step S200 and step S300 include the following steps:
the position of the last data record is found according to the valid flag values of the same group of data in the two sectors, and the same group of data can be found for the data of any one of the sectors, for example, the position of the last data record can be found through the valid flag values of A1 and B1, can be found through An and Bn, and can be found through At and Bt (At is the data of the t-th group in the A sector, Bt is the data of the t-th group in the B sector, and t is any number in 1-n).
In the present embodiment, a1 and B1 are exemplified for detailed description:
reading valid marker values in A1 and B1, wherein the valid marker value of A1 is marked as X, the valid marker value of B1 is marked as Y (X, Y only represents the marker values in each group, and has no other specific meanings, the same applies to Z), and meanwhile, when the value of the valid marker value is 0xFF, the group of data is represented to be invalid, and when the value of the valid marker value is 0X7F, the group of data is represented to be valid;
if X, Y is equal to 0xFF, the data representing the a sector and the B sector are invalid, An or Bn is the default position of the last data record, the data of An or Bn is read when the data is read next time, An is the last group of data of the a sector, Bn is the last group of data of the B sector, and at this time, the initial variable is fsegction-1 or fsegction-2, and Fgroup-n;
if X, Y is equal to 0x7F, the valid flag value Z in An needs to be read again, if Z is equal to 0xFF, the position of the last data record is a1, and at the same time, the initial variable Fsection is set to 1, and all data in the B sector are erased; if Z is not equal to 0xFF, the position of the last data record is B1, meanwhile, the initial variable Fsection is set to be 2, and all data in the A sector are erased;
if X is equal to 0xFF and Y is equal to 0X7F, reading from the last group of data in the B sector, and finding out group data Bt with a first valid flag value of 0X7F, which is the position of the last data record, where Bt is represented as the data in the t-th group of the B sector, at this time, setting an initial variable Fsection to 2, and Fgroup to t;
if X is equal to 0X7F and Y is equal to 0xFF, the last group of data in the a sector is read forward, and the group data At with the first valid flag value of 0X7F is the position of the last data record, where At represents the data in the t-th group of the a sector, and At this time, the initial variable Fsection is set to 1 and Fgroup is set to t.
Considering the accuracy of the method affected by the special situation, therefore, the determination S500 is performed to determine whether there is a sector erasure failure in the last power failure, and step S500 specifically includes the following steps:
if the position of the last data recording is in the A sector (Fsection is 1), reading a valid flag value of Bn; when the effective mark value of Bn is not equal to 0xFF, the B sector is erased again;
if the position of the last data recording is in the B sector (Fsection is 2), reading a valid flag value of An; when the valid flag value of An is not equal to 0xFF, the A sector is re-erased.
In another embodiment, before new data is written, a power-down writing determination step S600 is performed, where the step S600 specifically includes the following steps:
reading the data of the next group according to the position of the last data record;
judging whether the read group data has invalid data or not;
if invalid data exists (namely, data in the invalid data is not equal to 0xFF), the data write failure caused by last power failure is represented, and a last write failure mark is recorded, and at this time, a last write failure mark Fw _ fail is set to 1.
In the above process, the data of the next group of operation addresses is read, and the operation address calculation formula is as follows:
if Fsection is 1, the operation address Paddr1+ (Fgroup-1) × (data size + 1);
if Fsection is 2, the operation address Paddr2+ (Fgroup-1) × (data size + 1).
The specific content of step S400 includes the following steps:
when new data is written in, analyzing the value of the last write failure mark;
if the last write failure flag is characterized as failure (Fw _ fail ═ 1), skipping the group in which the last write operation failed;
if the last write failure flag is characterized as successful (Fw _ fail ═ 0), then the write is normally judged.
If Fw _ fail is 1, the method specifically includes the following steps:
if the read group is the last group of the sectors (i.e. Fgroup is n, which indicates that the previous writing in the 1 st group of another sector fails), erasing the data of another sector, when new data is written, switching to the first group of the sectors which are just erased for writing, erasing the sectors which are full before the writing is completed, setting the last writing failure flag Fw _ fail to be 0, simultaneously, after the writing is completed, reading the written data and comparing the written data with the target data, and if the read data is inconsistent with the target data, setting the flash life alarm flag Ferror to be 1;
if the read group is the penultimate group of the sectors (i.e. Fgroup +1 equals n, which indicates that the last group of the sectors failed to be written in at the time of the last time), when new data is written, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, setting the last writing failure flag Fw _ fail to be 0, simultaneously, after the writing is finished, reading the written data and comparing the written data with target data, and if the written data is not consistent with the target data, setting the flash life alarm flag Ferror equals 1;
if the read group is any one of the two cases (i.e., Fgroup +1< n), new data is written into the next group of the read group (i.e., Fgroup +1), and after the write is completed, the last write failure flag Fw _ fail is set to 0. Then reading the written data and comparing the data with the target data, and if the data is inconsistent with the target data, setting a flash life alarm mark Ferror to be 1;
if Fw _ fail is equal to 0, the method specifically includes the following steps:
if the read group is the last group of the sectors (that is, Fgroup is n), during writing, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, simultaneously, after the writing is finished, reading the written data and comparing the written data with the target data, and if the written data is not consistent with the target data, setting a flash life alarm mark Ferror to be 1;
and if the read group is any group which is not the last group (namely Fgroup < n), writing the next group of the read group, simultaneously, after the writing is finished, comparing the read and written data with the target data, and if the read and written data are inconsistent with the target data, setting a flash service life alarm mark Ferror to be 1.
Because the flash service life alarm mark Ferror is generated when data is written in, the flash data storage method can perform early warning, and the phenomenon that the read data is unreliable and the normal operation of the read data is influenced is avoided.
In another embodiment, the method for saving flash data of the present invention further includes the following steps:
during a read operation, whether the last data read is the data which is successfully written or the data which is failed to be written last time can be determined according to the value of Fw _ fail and the operation addresses represented by Fsection and Fgroup.
The flash data storage method solves the problem that the flash is prematurely damaged due to frequent writing because each read and write is carried out at a fixed address by partition and grouping storage, prolongs the service life of the flash, and can inquire the data written in history; the strategy that the erasing operation is executed only when the sector is fully written is adopted, so that the problem that the time consumption is large because the whole sector needs to be erased before data is written every time is solved, and the running timeliness is optimized; by comparing the effective mark values, the problem that the normal operation of the data is influenced because the data is not real when the power is turned on and started next time due to power failure in the data writing operation process is solved;
in one embodiment, as shown in fig. 3, a system for saving flash data further includes a configuration module 1, a search module 2, an erase module 3, and a write module 4. The configuration module 1 is used for configuring A, B two sectors, and dividing each sector into n groups; the searching module 2 is used for reading the effective mark values of the same group of data in the sectors A and B and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors; the erasing module 3 is used for judging an invalid sector according to the valid mark value of the specific group and erasing the data of the invalid sector; the write module 4 is used for writing new data from the next group of locations where the last data was recorded.
For the specific limitation of the above-mentioned flash data saving system, reference may be made to the above limitation on the flash data saving method, and details are not described herein again. The various modules in the above-described apparatus may be implemented in whole or in part by software, hardware, and combinations thereof. The modules can be embedded in a hardware form or independent from a processor in the computer device, and can also be stored in a memory in the computer device in a software form, so that the processor can call and execute operations corresponding to the modules.
In one embodiment, a computer device is provided, which may be a server, the internal structure of which may be as shown in fig. 4. The computer device includes a processor, a memory, a network interface, and a database connected by a system bus. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The database of the computer equipment is used for storing the data of the human face living body detection method based on the infrared image. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to realize a human face living body detection method based on infrared images.
Those skilled in the art will appreciate that the architecture shown in fig. 4 is merely a block diagram of some of the structures associated with the disclosed aspects and is not intended to limit the computing devices to which the disclosed aspects apply, as particular computing devices may include more or less components than those shown, or may combine certain components, or have a different arrangement of components.
In one embodiment, there is provided a computer device comprising a memory and a processor, the memory storing a computer program, the processor implementing the following steps when executing the computer program:
s100, configuring A, B two sectors, and dividing each sector into n groups.
S200, reading the effective mark values of the same group of data in the sectors A and B, and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors.
And S300, judging an invalid sector according to the valid mark value of the specific group, and erasing the data of the invalid sector.
S400, new data is written by the next group of the position recorded by the last data.
The specific content of step S100 includes the following steps:
and after power-on operation, setting the initial state of each variable value. A, B two sectors are configured for storing specific data, the first address of record A, B sector is Paddr1, Paddr2, valid flag value Fvalue2 of each group in A, B sector is 0x7F, sector flag fsegction is 0, group number Fgroup is 0, flash life alarm flag Ferror is 0, last write failure flag Fw _ fail is 0. Where Fsection is an a sector when Fsection is 1, a B sector when Fsection is 2, and Fvalue2 is 0xFF indicating data invalid (set to valid by default, Fvalue2 is 0x 7F).
In another embodiment, step S100 further includes the following steps:
when the initial state of each variable value is configured, an address Paddr0 is configured to store an enable flag value Fvalue1 ═ 0xAA 55.
After the initial state of each variable value is configured, first, whether a storage method of flash data is enabled or not is detected, two bytes of data are read from an address Paddr0 and are marked as T:
if T is equal to Fvalue1, the storage method of the flash data is enabled, and A, B sectors do not need to be erased;
if T is not equal to Fvalue1, indicating that the storage method of the flash data is not enabled, A, B sectors need to be erased, and then Fvalue1 is written into the address of Paddr 0.
Each sector is divided into n groups, the n groups of data of the a sector are respectively a1-An, the n groups of data of the B sector are respectively B1-Bn, the size of the group is the size of the specific data plus one byte (for putting a valid flag value), and therefore, the group number Fgroup is (1, 2, 3 … … n). And if the sector space has not enough space after the sharing, abandoning the use of the space.
The specific contents of step S200 and step S300 include the following steps:
the position of the last data record is found according to the valid flag values of the same group of data in the two sectors, and the same group of data can be found for the data of any one of the sectors, for example, the position of the last data record can be found through the valid flag values of A1 and B1, can be found through An and Bn, and can be found through At and Bt (At is the data of the t-th group in the A sector, Bt is the data of the t-th group in the B sector, and t is any number in 1-n).
In the present embodiment, a1 and B1 are exemplified for detailed description:
the effective marker values in A1 and B1 are read, the effective marker value in A1 is recorded as X, and the effective marker value in B1 is recorded as Y (X, Y only represents the marker values in each group, and has no other specific meanings, and Z is the same): meanwhile, when the value of the valid flag value is 0xFF, the group of data is represented to be invalid, and when the value of the valid flag value is 0x7F, the group of data is represented to be valid;
if X, Y is equal to 0xFF, the data representing the a sector and the B sector are invalid, An or Bn is the default position of the last data record, the data of An or Bn is read when the data is read next time, An is the last group of data of the a sector, Bn is the last group of data of the B sector, and at this time, the initial variable is fsegction-1 or fsegction-2, and Fgroup-n;
if X, Y is equal to 0x7F, the valid flag value Z in An needs to be read again, if Z is equal to 0xFF, the position of the last data record is a1, and at the same time, the initial variable Fsection is set to 1, and all data in the B sector are erased; if Z is not equal to 0xFF, the position of the last data record is B1, meanwhile, the initial variable Fsection is set to be 2, and all data in the A sector are erased;
if X is equal to 0xFF and Y is equal to 0X7F, reading from the last group of data in the B sector, and finding out group data Bt with a first valid flag value of 0X7F, which is the position of the last data record, where Bt is represented as the data in the t-th group of the B sector, at this time, setting an initial variable Fsection to 2, and Fgroup to t;
if X is equal to 0X7F and Y is equal to 0xFF, the last group of data in the a sector is read forward, and the group data At with the first valid flag value of 0X7F is the position of the last data record, where At represents the data in the t-th group of the a sector, and At this time, the initial variable Fsection is set to 1 and Fgroup is set to t.
Considering the accuracy of the method affected by the special situation, therefore, the determination S500 is performed to determine whether there is a sector erasure failure in the last power failure, and step S500 specifically includes the following steps:
if the position of the last data recording is in the A sector (Fsection is 1), reading a valid flag value of Bn; when the effective mark value of Bn is not equal to 0xFF, the B sector is erased again;
if the position of the last data recording is in the B sector (Fsection is 2), reading a valid flag value of An; when the valid flag value of An is not equal to 0xFF, the A sector is re-erased.
In another embodiment, before new data is written, a power-down writing determination step S600 is performed, where the step S600 specifically includes the following steps:
reading the data of the next group according to the position of the last data record;
judging whether the read group data has invalid data or not;
if invalid data exists (namely, data in the invalid data is not equal to 0xFF), the data write failure caused by last power failure is represented, and a last write failure mark is recorded, and at this time, a last write failure mark Fw _ fail is set to 1.
In the above process, the data of the next group of operation addresses is read, and the operation address calculation formula is as follows:
if Fsection is 1, the operation address Paddr1+ (Fgroup-1) × (data size + 1);
if Fsection is 2, the operation address Paddr2+ (Fgroup-1) × (data size + 1).
The specific content of step S400 includes the following steps:
when new data is written in, analyzing the value of the last write failure mark;
if the last write failure flag is characterized as failure (Fw _ fail ═ 1), skipping the group in which the last write operation failed;
if the last write failure flag is characterized as successful (Fw _ fail ═ 0), then the write is normally judged.
If Fw _ fail is 1, the method specifically includes the following steps:
if the read group is the last group of the sectors (i.e. Fgroup is n, which indicates that the previous writing in the 1 st group of another sector fails), erasing the data of another sector, when new data is written, switching to the first group of the sectors which are just erased for writing, erasing the sectors which are full before the writing is completed, setting the last writing failure flag Fw _ fail to be 0, simultaneously, after the writing is completed, reading the written data and comparing the written data with the target data, and if the read data is inconsistent with the target data, setting the flash life alarm flag Ferror to be 1;
if the read group is the penultimate group of the sectors (i.e. Fgroup +1 equals n, which indicates that the last group of the sectors failed to be written in at the time of the last time), when new data is written, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, setting the last writing failure flag Fw _ fail to be 0, meanwhile, after the writing is finished, reading the written data and comparing the written data with target data, and if the written data is inconsistent, setting the flash life alarm flag Ferror to be 1;
if the read group is any one of the two cases (i.e., Fgroup +1< n), new data is written into the next group of the read group (i.e., Fgroup +1), and after the write is completed, the last write failure flag Fw _ fail is set to 0. Subsequently, reading and writing data and comparing the data with target data, and if the data are inconsistent, setting a flash life alarm mark Ferror to be 1 and a last writing failure mark Fw _ fail to be 0;
if Fw _ fail is equal to 0, the method specifically includes the following steps:
if the read group is the last group of the sectors (that is, Fgroup is n), during writing, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, simultaneously, after the writing is finished, reading the written data and comparing the written data with the target data, and if the written data is not consistent with the target data, setting a flash life alarm mark Ferror to be 1;
and if the read group is any group which is not the last group (namely Fgroup < n), writing the next group of the read group, simultaneously, after the writing is finished, comparing the read and written data with the target data, and if the read and written data are inconsistent with the target data, setting a flash service life alarm mark Ferror to be 1.
Because the flash service life alarm mark Ferror is generated when data is written in, the flash data storage method can perform early warning, and the phenomenon that the read data is unreliable and the normal operation of the read data is influenced is avoided.
In another embodiment, the method for saving flash data of the present invention further includes the following steps:
during a read operation, whether the last data read is the data which is successfully written or the data which is failed to be written last time can be determined according to the value of Fw _ fail and the operation addresses represented by Fsection and Fgroup.
In one embodiment, a computer-readable storage medium is provided, having a computer program stored thereon, which when executed by a processor, performs the steps of:
s100, configuring A, B two sectors, and dividing each sector into n groups.
S200, reading the effective mark values of the same group of data in the sectors A and B, and searching the position of the last data record according to the effective mark values of the same group of data in the two sectors.
And S300, judging an invalid sector according to the valid mark value of the specific group, and erasing the data of the invalid sector.
S400, new data is written by the next group of the position recorded by the last data.
The specific content of step S100 includes the following steps:
and after power-on operation, setting the initial state of each variable value. A, B two sectors are configured for storing specific data, the first address of record A, B sector is Paddr1, Paddr2, valid flag value Fvalue2 of each group in A, B sector is 0x7F, sector flag fsegction is 0, group number Fgroup is 0, flash life alarm flag Ferror is 0, last write failure flag Fw _ fail is 0. Where Fsection is an a sector when Fsection is 1, a B sector when Fsection is 2, and Fvalue2 is 0xFF indicating data invalid (set to valid by default, Fvalue2 is 0x 7F).
In another embodiment, step S100 further includes the following steps:
when the initial state of each variable value is configured, an address Paddr0 is configured to store an enable flag value Fvalue1 ═ 0xAA 55.
After the initial state of each variable value is configured, first, whether a storage method of flash data is enabled or not is detected, two bytes of data are read from an address Paddr0 and are marked as T:
if T is equal to Fvalue1, the storage method of the flash data is enabled, and A, B sectors do not need to be erased;
if T is not equal to Fvalue1, indicating that the storage method of the flash data is not enabled, A, B sectors need to be erased, and then Fvalue1 is written into the address of Paddr 0.
Each sector is divided into n groups, the n groups of data of the a sector are respectively a1-An, the n groups of data of the B sector are respectively B1-Bn, the size of the group is the size of the specific data plus one byte (for putting a valid flag value), and therefore, the group number Fgroup is (1, 2, 3 … … n). And if the sector space has not enough space after the sharing, abandoning the use of the space.
The specific contents of step S200 and step S300 include the following steps:
the position of the last data record is found according to the valid flag values of the same group of data in the two sectors, and the same group of data can be found for the data of any one of the sectors, for example, the position of the last data record can be found through the valid flag values of A1 and B1, can be found through An and Bn, and can be found through At and Bt (At is the data of the t-th group in the A sector, Bt is the data of the t-th group in the B sector, and t is any number in 1-n).
In the present embodiment, a1 and B1 are exemplified for detailed description:
the effective marker values in A1 and B1 are read, the effective marker value in A1 is recorded as X, and the effective marker value in B1 is recorded as Y (X, Y only represents the marker values in each group, and has no other specific meanings, and Z is the same): meanwhile, when the value of the valid flag value is 0xFF, the group of data is represented to be invalid, and when the value of the valid flag value is 0x7F, the group of data is represented to be valid;
if X, Y is equal to 0xFF, the data representing the a sector and the B sector are invalid, An or Bn is the default position of the last data record, the data of An or Bn is read when the data is read next time, An is the last group of data of the a sector, Bn is the last group of data of the B sector, and at this time, the initial variable is fsegction-1 or fsegction-2, and Fgroup-n;
if X, Y is equal to 0x7F, the valid flag value Z in An needs to be read again, if Z is equal to 0xFF, the position of the last data record is a1, and at the same time, the initial variable Fsection is set to 1, and all data in the B sector are erased; if Z is not equal to 0xFF, the position of the last data record is B1, meanwhile, the initial variable Fsection is set to be 2, and all data in the A sector are erased;
if X is equal to 0xFF and Y is equal to 0X7F, reading from the last group of data in the B sector, and finding out group data Bt with a first valid flag value of 0X7F, which is the position of the last data record, where Bt is represented as the data in the t-th group of the B sector, at this time, setting an initial variable Fsection to 2, and Fgroup to t;
if X is equal to 0X7F and Y is equal to 0xFF, the last group of data in the a sector is read forward, and the group data At with the first valid flag value of 0X7F is the position of the last data record, where At represents the data in the t-th group of the a sector, and At this time, the initial variable Fsection is set to 1 and Fgroup is set to t.
Considering the accuracy of the method affected by the special situation, therefore, the determination S500 is performed to determine whether there is a sector erasure failure in the last power failure, and step S500 specifically includes the following steps:
if the position of the last data recording is in the A sector (Fsection is 1), reading a valid flag value of Bn; when the effective mark value of Bn is not equal to 0xFF, the B sector is erased again;
if the position of the last data recording is in the B sector (Fsection is 2), reading a valid flag value of An; when the valid flag value of An is not equal to 0xFF, the A sector is re-erased.
In another embodiment, before new data is written, a power-down writing determination step S600 is performed, where the step S600 specifically includes the following steps:
reading the data of the next group according to the position of the last data record;
judging whether the read group data has invalid data or not;
if invalid data exists (namely, data in the invalid data is not equal to 0xFF), the data write failure caused by last power failure is represented, and a last write failure mark is recorded, and at this time, a last write failure mark Fw _ fail is set to 1.
In the above process, the data of the next group of operation addresses is read, and the operation address calculation formula is as follows:
if Fsection is 1, the operation address Paddr1+ (Fgroup-1) × (data size + 1);
if Fsection is 2, the operation address Paddr2+ (Fgroup-1) × (data size + 1).
The specific content of step S400 includes the following steps:
when new data is written in, analyzing the value of the last write failure mark;
if the last write failure flag is characterized as failure (Fw _ fail ═ 1), skipping the group in which the last write operation failed;
if the last write failure flag is characterized as successful (Fw _ fail ═ 0), then the write is normally judged.
If Fw _ fail is 1, the method specifically includes the following steps:
if the read group is the last group of the sectors (i.e. Fgroup is n, which indicates that the previous writing in the 1 st group of another sector fails), erasing the data of another sector, when new data is written, switching to the first group of the sectors which are just erased for writing, erasing the sectors which are full before the writing is completed, setting the last writing failure flag Fw _ fail to be 0, simultaneously, after the writing is completed, reading the written data and comparing the written data with the target data, and if the read data is inconsistent with the target data, setting the flash life alarm flag Ferror to be 1;
if the read group is the penultimate group of the sectors (i.e. Fgroup +1 equals n, which indicates that the last group of the sectors failed to be written in at the time of the last time), when new data is written, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, setting the last writing failure flag Fw _ fail to be 0, meanwhile, after the writing is finished, reading the written data and comparing the written data with target data, and if the written data is inconsistent, setting the flash life alarm flag Ferror to be 1;
if the read group is any one of the two cases (i.e., Fgroup +1< n), new data is written into the next group of the read group (i.e., Fgroup +1), and after the write is completed, the last write failure flag Fw _ fail is set to 0. Then reading the written data and comparing the data with the target data, and if the data is inconsistent with the target data, setting a flash life alarm mark Ferror to be 1;
if Fw _ fail is equal to 0, the method specifically includes the following steps:
if the read group is the last group of the sectors (that is, Fgroup is n), during writing, switching to the first group of another sector for writing, erasing the sectors which are fully written before the writing is finished, simultaneously, after the writing is finished, reading the written data and comparing the written data with the target data, and if the written data is not consistent with the target data, setting a flash life alarm mark Ferror to be 1;
and if the read group is any group which is not the last group (namely Fgroup < n), writing the next group of the read group, simultaneously, after the writing is finished, comparing the read and written data with the target data, and if the read and written data are inconsistent with the target data, setting a flash service life alarm mark Ferror to be 1.
Because the flash service life alarm mark Ferror is generated when data is written in, the flash data storage method can perform early warning, and the phenomenon that the read data is unreliable and the normal operation of the read data is influenced is avoided.
In another embodiment, the method for saving flash data of the present invention further includes the following steps:
during a read operation, whether the last data read is the data which is successfully written or the data which is failed to be written last time can be determined according to the value of Fw _ fail and the operation addresses represented by Fsection and Fgroup.
The invention adopts a flash memory space partition management method, in a memory, a storage space for storing specific data is divided into two sectors for management, each sector is divided into each group according to the size of the specific data, and the read-write of the flash is operated by taking the group as a unit; when writing operation is carried out, information in a sector is read firstly, the address written at this time is judged, and when data is written, a specific effective mark value is additionally arranged at the tail of the data to indicate whether the group of data is effective or not, so that power failure caused by half writing of the data is prevented, and the data is unreliable. And simultaneously, after the writing is finished, the written data is read immediately, the read data is compared with the written data, and if the read data is inconsistent with the written data, flash life exhaustion early warning is carried out.
An erase sector operation erases another sector only if the address where the data is written is the first group of the current sector. Therefore, the problem that the operation timeliness of the program is influenced because the sector where the data is located needs to be erased before the data is written every time is solved.
During reading operation, the information of each sector is read first, and the address read this time is judged. And the read data is subjected to identification judgment, so that the real validity of the data is ensured. If invalid, it is considered to take the last written data.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by hardware instructions of a computer program, which can be stored in a non-volatile computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the embodiments provided herein may include non-volatile and/or volatile memory, among others. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above-mentioned embodiments only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.