Summary of the invention
In order to improve the life-span of flash memory, the embodiment of the invention provides a kind of method and apparatus of revising data in memory device.Technical scheme is as follows:
A kind of method of in memory device, revising data, wherein said memory device comprises first storage medium and second storage medium, the data field of described memory device is arranged in described first storage medium, the recording areas of described memory device and index area are arranged in described second storage medium, when needs were revised data in the described memory device, described method comprised:
The available block that obtains erasable number of times minimum in the erasable meter digital in described data field is data block as an alternative;
New data is read and replaced with to data in the data block to be revised;
Described new data is write in the described replacement data piece;
Judge whether data block described to be revised is file header;
If, revise first concordance list in the described index area, from described first concordance list, read the index bit in the sign of data block described to be revised, and use the index bit of data block described to be revised to replace the index bit of described replacement data piece; Erasable meter digital in the sign of described replacement data piece is increased a step-length; To be revised as described replacement data piece with data block described to be revised as the file header of the file of file header in the described recording areas; Data block described to be revised is changed to available block;
If not, revise first concordance list in the described index area, the index bit in the sign of the previous data block of data block to be revised described in described first concordance list is revised as the piece number of described replacement data piece; Erasable meter digital in the sign of described replacement data piece is increased a step-length, and the piece of the next data block of data block described to be revised number is write in the index bit of sign of described replacement data piece; Data block described to be revised is changed to available block.
Described method also comprises: the preprocessing process of described memory device;
Described preprocessing process specifically comprises:
Described data field is divided into a plurality of data blocks, the equal and opposite in direction of described a plurality of data blocks, and have separately piece number;
In first concordance list of described index area, state to each data block in the described data field identifies respectively, described sign comprises index bit and erasable meter digital, described index bit is used for writing down the piece number of the next data block that the file be made up of a plurality of data blocks is connected with current data block or the state of current data block, and described erasable meter digital is used to write down current data block by erasable number of times;
The content of described first concordance list of backup in second concordance list of described index area.
The available block of erasable number of times minimum data block as an alternative in the erasable meter digital of described acquisition specifically comprises:
Search described first concordance list, the available block that obtains erasable meter digital minimum is data block as an alternative;
When having the available block of a plurality of erasable meter digital minimums, use in the available block of described a plurality of erasable meter digital minimums one data block as an alternative, specifically comprise:
The available block of selecting first the erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums is data block as an alternative;
Or,
The available block of selecting last the erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums is data block as an alternative;
Or,
The available block of selecting an erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums at random is data block as an alternative.
Described available block is: Blank data block or dirty;
Described Blank data block, writes described new data in the described Blank data block when described acquisition available block is Blank data block for not comprising the data block of data;
Described dirty for recorded data the invalid but data block that is not wiped free of, when the available block of described acquisition was dirty, the data of wiping in described dirty became Blank data block, and described new data is write in the described Blank data block.
Describedly judge that whether data block described to be revised is file header, specifically comprises:
Judge whether data block described to be revised is recorded in the described recording areas;
If data block then described to be revised is a file header;
If not, data block then described to be revised is not a file header.
Before first concordance list in the described index area of described modification, described method also comprises:
Described second concordance list is set to effectively.
Described method also comprises:
It is effective that first concordance list is set, in the content backup of described amended first concordance list second concordance list in the described index area.
A kind of device of in memory device, revising data, wherein said memory device comprises first storage medium and second storage medium, the data field of described memory device is arranged in described first storage medium, the recording areas of described memory device and index area are arranged in described second storage medium, and described device comprises:
Obtain module, the available block that is used for obtaining in described data field erasable number of times minimum in the erasable meter digital is data block as an alternative;
Replace module, be used for new data is read and replaced with to the data of data block to be revised;
Writing module is used for described new data is write described replacement data piece;
Judge module is used to judge whether data block described to be revised is file header;
First processing module, be used for when the judged result of described judge module when being, revise first concordance list in the described index area, from described first concordance list, read the index bit in the sign of data block described to be revised, and use the index bit of data block described to be revised to replace the index bit of described replacement data piece; Erasable meter digital in the sign of described replacement data piece is increased a step-length; To be revised as described replacement data piece with data block described to be revised as the file header of the file of file header in the described recording areas; Data block described to be revised is changed to available block;
Second processing module, be used for when the judged result of described judge module for not the time, revise first concordance list in the described index area, the index bit in the sign of the previous data block of data block to be revised described in described first concordance list is revised as the piece number of described replacement data piece; Erasable meter digital in the sign of described replacement data piece is increased a step-length, and the piece of the next data block of data block described to be revised number is write in the index bit of sign of described replacement data piece; Data block described to be revised is changed to available block;
Described device also comprises: pretreatment module specifically comprises:
Division unit is used for described data field is divided into a plurality of data blocks, the equal and opposite in direction of described a plurality of data blocks, and have separately piece number;
Identify unit, be used for first concordance list in described index area, state to each data block in the described data field identifies respectively, described sign comprises index bit and erasable meter digital, described index bit is used for writing down the piece number of the next data block that the file be made up of a plurality of data blocks is connected with current data block or the state of current data block, and described erasable meter digital is used to write down current data block by erasable number of times;
Backup units is used for the content at described first concordance list of second concordance list backup of described index area.
Described acquisition module specifically is used for:
Search described first concordance list, the available block that obtains erasable meter digital minimum is data block as an alternative;
When having the available block of a plurality of erasable meter digital minimums, use in the available block of described a plurality of erasable meter digital minimums one data block as an alternative, specifically comprise:
The available block of selecting first the erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums is data block as an alternative;
Or,
The available block of selecting last the erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums is data block as an alternative;
Or,
The available block of selecting an erasable meter digital minimum in the available block of described a plurality of erasable meter digital minimums at random is data block as an alternative.
Described available block is: Blank data block or dirty;
Described Blank data block, writes described new data in the described Blank data block when described available block is Blank data block for not comprising the data block of data;
Described dirty for recorded data the invalid but data block that is not wiped free of, when described available block was dirty, the data of wiping in described dirty became Blank data block, and described new data is write in the described Blank data block.
Described judge module specifically is used for:
Judge whether data block described to be revised is recorded in the described recording areas;
If data block then described to be revised is a file header;
If not, data block then described to be revised is not a file header.
Described device also comprises:
Module is set, is used for described second concordance list and is set to effectively.
Described device also comprises:
Backup module is used for content backup with described amended first concordance list in second concordance list of described index area.
The beneficial effect that the technical scheme that the embodiment of the invention provides is brought is:
By for the data block in the memory device is provided with erasable meter digital, thus make erasable each time can be with minimum erasable data block data block as an alternative, balance erasable number of times, prolonged life-span of flash memory;
Embodiment
For making the purpose, technical solutions and advantages of the present invention clearer, embodiment of the present invention is described further in detail below in conjunction with accompanying drawing.
Embodiment 1
Present embodiment provides a kind of method of revising data in memory device, and in the present embodiment, we preferably use in memory device the partial data to a file to carry out erasablely describing as example.Comprise first storage medium and second storage medium at the employed memory device of present embodiment, in the present embodiment with first storage medium with second storage medium is respectively EEPROM and Flash describes.Wherein, EEPROM and Flash are electrically-erasable memory, and EEPROM can make amendment to data in step-by-step, and Flash can only monoblock make amendment to data.
Introduce in memory device, revising before the method for data that the embodiment of the invention provides, introduce the initialization procedure of memory device at first in conjunction with the accompanying drawings.
Referring to Fig. 1, in whole memory device, except the code district, memory device is divided into three districts: index area, recording areas and data field.
(1) data field is deposited among the Flash of memory device
Wherein, can be a plurality of equal-sized with data separation, be called data block 1, data block 2, data block 3 etc.
When at file of data area stores, use one or more data blocks that file data is stored, the data block of using in storing process must be that integer is individual.File of being made up of a plurality of data blocks shows by the form of chained list, comprising file header, center section and end-of-file.File header is first data block of a file data, and end-of-file is last data block of a file data, store an employed all data blocks of file and can arrange in order, and also can be out of order arrangement, as shown in Figure 2.
(2) index area is deposited among the EEPROM of memory device
Wherein, the index area comprises first concordance list and second concordance list.State to all data blocks of data field in first concordance list identifies successively, and as shown in Figure 3, this sign comprises erasable meter digital and index bit, and the figure place of sign is according to the number decision of data block.Erasable meter digital has write down this data block by erasable number of times, and index bit has write down the piece number of the next data block that is connected with this data block or the state of this data block in a file of being made up of a plurality of data block.In the present embodiment, the number of supposing data block in the flash memory then preferably uses two bytes that each data block is identified less than 254.Wherein, the erasable number of times of first this data block of byte records, the state of second this data block of byte records or record be the piece number of the next data block of this data block hereof.The sign of all Blank data blocks in first concordance list all is set to 0xXX00, the end-of-file data block be designated 0xXXFF (wherein, XX represents the erasable number of times of this data block).For example when initialization, first concordance list all is set to 0x0000, file as shown in Figure 4, and data block 3 corresponding identification should be 0xXX05, and data block 6 corresponding identification should be 0xXXFF.Wherein, second concordance list is the backup of first concordance list, can prevent that reasons such as unexpected power down or misoperation from causing the first concordance list loss of data and cause and can not the file in the flash memory be read and write;
(3) recording areas is also deposited among the EEPROM of memory device
It has write down file header, file attribute, the size of file, the Time of Day of document creation of the All Files of storing etc. in flash memory.
So far, introduced the initialization procedure of flash memory after, be described in detail in conjunction with the method for the following examples erasable data in the flash memory.
In the present embodiment, with a data block revising a file is that example describes this method, set a file A, its memory state in flash memory as shown in Figure 5, take 4 data blocks of flash memory altogether, file header 2, data block 4, data block 7 and end-of-file 9, first concordance list of its correspondence as shown in Figure 6.With the data in the data block 4 are made amendment (being that data block 4 is data block to be revised) is that example describes, referring to Fig. 7.
Step 101: the available block of seeking erasable number of times minimum in the erasable meter digital in the data field is data block as an alternative;
In this step, seek erasable number of times minimum available block method can for: scan all signs in first concordance list, and the byte of searching the erasable number of times of recording data blocks, with the available block of the erasable number of times minimum that finds data block as an alternative.
Wherein, if there is the available block of a plurality of erasable number of times minimums, then can search: for example according to other rules, first available block in the available block of the erasable number of times minimum that selection finds is data block as an alternative, or select last available block data block as an alternative in the available block of the erasable number of times minimum find, or from the available block of erasable number of times minimum the method etc. of picked at random replacement data piece.In the present embodiment, the method for data block and the replacement data piece that finds are that data block 11 describes as an alternative with first available block in the available block of the erasable number of times minimum selecting to find.
Available block specifically can comprise Blank data block and dirty, and so-called Blank data block is not for comprising the data block of data, but so-called dirty is meant the invalid data block that is not wiped free of of recorded data.
Step 102: the content of data block 4 that will be to be revised reads and is updated to the new data of desiring in the writing data blocks 4 from data block 4;
Wherein, find the process of the data block of prepare revising 4 to be: in recording areas, to find the data block at the file header place that needs revise according to filename, read first index area and find data block 4, it is read according to side-play amount.
With the content in the data block 4 read and be updated to desire the new data in the writing data blocks 4 process can for: the content of storage in the data block 4 is read in internal memory, is new data with content update in internal memory.
Step 103: new data is write replacement data piece 11;
In this step, when the available block that obtains in the step 101 is Blank data block, when promptly replacement data piece 11 is Blank data block, can directly new data be write replacement data piece 11;
When the available block that obtains in the step 101 is dirty, when promptly replacement data piece 11 is dirty, need earlier replacement data piece 11 to be carried out erase operation, make it become Blank data block, again new data is write replacement data piece 11.
Step 104: judge whether the data block 4 that will revise is file header;
If, execution in step 105;
If not, execution in step 108.
Wherein, the judgment data piece whether be file header method can for: judge whether this data block is registered as file header in recording areas:
If then be file header;
Otherwise, be not file header.
Need to prove that before execution in step 105 or 108, can also comprise: it is effective that second concordance list is set, the purpose of this step mainly is the effect that prevents power down in order to play, even power down suddenly, second tables of data is effective, can not exert an influence, can recover legacy data overall data.
Step 105: first concordance list is made amendment, sign in first concordance list is read with data block 4 and data block 11, and the index bit in the sign of use data block 4 is replaced the index bit of the sign of above-mentioned data block 11, and the counting in the erasable meter digital of the sign of replacement data piece 11 increased a step-length, be set to increase by 1 in the present embodiment;
Wherein, in the present embodiment, aforesaid operations is specifically as follows:
Read the sign 0x0507 of data block 4 in first concordance list, the index bit of sign that will the index bit 07 replacement data piece 11 of sign among the 0x0507 adds 1 with the counting in the erasable meter digital of the sign of replacement data piece 11.Aforesaid operations obtains the new identification information 0x0507 of replacement data piece 11.
Step 106: the sign of data block 4 in first concordance list is set to available block;
In the present embodiment, sign that can data block 4 is set to 0x0500;
Step 107: it is effective that first concordance list is set, and in recording areas data block 4 is updated to data block 11, execution in step 110 as the file header of the file of file header;
In the present embodiment; if power down takes place suddenly in the process of execution in step 105 to 108; because second concordance list is effective; and the data in the data block 4 are not wiped free of as yet; when next time, flash memory powered on, compare the content of first concordance list and second concordance list, and the content of second concordance list is copied in first concordance list; and it is effective that first concordance list is set, and can effectively play the effect that prevents that power down, protected data from not losing.
Step 108: first concordance list is made amendment, the piece of replacement data piece No. 11 11 is written to the index bit of sign of the previous data block of data block to be revised 4, and with the step-length of erasable meter digital increase in the sign of replacement data piece, be that erasable meter digital is increased by 1 in the present embodiment, and the piece of the next piece of data block that will be to be revised No. 4 is that the sign of data block 4 is set to available block in the index bit of piece numbers 7 sign that writes replacement data piece 11;
In this enforcement, the previous data block of the data block of revising 4 is data blocks 2, the call number of its sign in first concordance list is 0x0504, because use data block 11 to replace data block 4, therefore the piece of data block No. 11 is written in the index bit of sign 0x0504, obtain the new sign 0x050B of data block 2, and sign that can data block 4 is set to 0x0500.
Step 109: it is effective that first concordance list is set;
Step 110: in second concordance list, file modification finishes with the content backup of first concordance list.
Wherein, the concordance list content after file modification is finished as shown in Figure 8.
In this step, if generation power down, because this moment, the content of first concordance list was correct, content the unknown of second concordance list, the content that compared first concordance list and second concordance list when next time, flash memory powered on, and the content of first concordance list copied in second concordance list, overall data is just upgraded for new data.
The embodiment of the invention is by for the data block in the memory device is provided with erasable meter digital, thus make erasable each time can be with minimum erasable data block data block as an alternative, balance erasable number of times, prolonged life-span of flash memory; Simultaneously, since first concordance list and the mutually redundant relation of second concordance list, contingent loss of data when effectively having prevented power down.
Embodiment 2
The embodiment of the invention provides a kind of device of revising data in memory device, and for the description of the method for revising data, referring to Fig. 9, this device comprises in the reference example 1:
Obtain module 201, be used for obtaining available block data block as an alternative in the data field;
Replace module 202, be used for new data is read and replaced with to the data of data block to be revised;
Writing module 203 is used for new data is write the replacement data piece;
Judge module 204 is used to judge whether data block to be revised is file header;
First processing module 205, be used for when the judged result of judge module when being, revise first concordance list in the index area, from first concordance list, read the index bit in the sign of data block to be revised, and use the index bit of data block to be revised to replace the index bit of replacement data piece; Erasable meter digital in the sign of replacement data piece is increased a step-length; To be revised as the replacement data piece with data block to be revised as the file header of the file of file header in the recording areas; Data block to be revised is changed to available block;
Second processing module 206, be used for when the judged result of judge module for not the time, revise first concordance list in the index area, index bit in the sign of the previous data block of data block to be revised in first concordance list is revised as the piece number of replacement data piece, erasable meter digital in the sign of replacement data piece is increased a step-length, and the piece that will wait to revise the next piece of data block number writes in the index bit of replacement data piece; Data block to be revised is changed to available block.
Further, this device also comprises: pretreatment module, and this module specifically comprises:
Division unit is used for the data field is divided into a plurality of data blocks, the equal and opposite in direction of a plurality of data blocks, and have separately piece number;
Identify unit, be used for first concordance list in the index area, state to each data block in the data field identifies respectively, sign comprises index bit and erasable meter digital, index bit is used for writing down the piece number of the next data block that the file be made up of a plurality of data blocks is connected with current data block or the state of current data block, and erasable meter digital is used to write down current data block by erasable number of times;
Backup units is used for the content at second concordance list backup, first concordance list of index area.
Obtaining module 201 specifically is used for:
Search first concordance list, the available block that obtains erasable meter digital minimum is data block as an alternative;
When having the available block of a plurality of erasable meter digital minimums, use in the available block of a plurality of erasable meter digital minimums one data block as an alternative, can comprise several different methods: for example, first available block in the available block of a plurality of erasable meter digital minimum that selection finds is data block as an alternative, or select last available block data block as an alternative in the available block of a plurality of erasable meter digital minimum find, or from the available block of a plurality of erasable meter digital minimums the method etc. of picked at random replacement data piece;
Need to prove that available block is: Blank data block or dirty;
Blank data block when the available block that obtains module 201 acquisitions is Blank data block, can directly write new data this Blank data block for not comprising the data block of data;
Dirty for recorded data the invalid but data block that is not wiped free of, when obtaining available block that module 201 obtains and be dirty, need earlier should dirty to be erased to Blank data block, again new data is write above-mentioned new Blank data block.
Wherein, judge module 204 specifically is used for:
Judge whether data block to be revised is recorded in the recording areas;
If data block then to be revised is a file header;
If not, data block then to be revised is not a file header.
Further, this device also comprises:
Module is set, is used for second concordance list and is set to effectively.
Further, this device also comprises:
Backup module is used for content backup with amended first concordance list in second concordance list of index area.
The embodiment of the invention is by for the data block in the memory device is provided with erasable meter digital, thus make erasable each time can be with minimum erasable data block data block as an alternative, balance erasable number of times, prolonged life-span of flash memory; Simultaneously, since first concordance list and the mutually redundant relation of second concordance list, contingent loss of data when effectively having prevented power down.
Below only be preferred embodiment of the present invention, or not within the spirit and principles in the present invention not all in order to restriction the present invention, any modification of being done, be equal to replacement, improvement etc., all should be included within protection scope of the present invention.