|
Ensuring Disk Data Integrity |
Top Previous Next |
Maintaining the data integrity is a very important task, and the one where the flash memory needs a lot of help from your smart Tibbo Basic application. The biggest source of potential trouble is a sudden loss of power right in the middle of writing to the flash IC. This can cause devastation on two levels:
| • | The data in flash sectors is changed by first erasing the sector (a process in which all sector locations return to the value of &hFF), and then writing the new data. Should the power fail right in the middle of this process, you may end up losing the old and the new contents of the sector. |
| • | For many flash disk operations, a single operation requires making changes to several sectors. For example, creating a file requires to change data both in the file record table (FRT) and the file allocation table (FAT). Power failure in the middle of this process can leave the disk with the FRT already changed, and the FAT not yet changed -- a "loss of sync" situation that renders the disk unusable. |
Here is how you can address potential integrity issues:
File-based access
First and foremost -- the less you change the data, the more secure your data is. Your flash memory life is prolonged this way, too.
Secondly, try limiting rewrites in the FRT and FAT -- these most sensitive areas of the disk. Prolonging Flash Memory Life explains how creating all the files and setting their size once helps keep your flash memory healthy. The same approach also makes your system more reliable -- if the FAT/FRT does not get changed, then the fatal data corruption in FRT and/or FAT will not happen. If, however, you do need to create or delete the files, you can backup the entire service area of the disk first. Should the power fault occur in the middle of making changes to the FAT/FRT, you can simply restore them to their previous state.
To use backups, make your flash disk occupy a bit less space than available -- just enough for the backup of the entire "housekeeping" area of your flash disk to fit. The calculation for the housekeeping area size can be found in the Disk Area Allocation Details topic.
Before creating/deleting files, make a backup copy of the FAT/FRT data:
Dim f As Byte ... 'backup disk housekeeping data fd.buffernum=0 For f=0 To fd.numservicesectors-1 fd.getsector(f) fd.setsector(fd.totalsize+f) Next f
|
Should the need arise, you can copy the data back. Of course, you will need to store the number of sectors to copy back somewhere, for example, in the EEPROM (.stor object). You will also probably have a "transaction flag". If your device boots up and the flag is set, then the power failure has occurred in the middle of an important disk "transaction" and the previous state must be restored.
The power disaster may also strike when you are changing the data in the file itself. The fd.sector R/O property always tells you the number of a sector corresponding to the current pointer position (fd.pointer). This way you can backup this sector first and, should the power failure occur right in the middle of the data alteration, restore the original sector contents.
Note, of course, that writing to a file can span two sectors. Depending on the pointer position, your write could begin in one sector and end in the next. The next sector is also not simply the fd.sector+1, because the sectors belonging to the same file may actually be scattered across the disk. So, to make sure your sector backup works, make sure the sector boundaries are never crossed during writing. This can be easily achieved if you are dealing with "data table records" and record sizes are limited to the power of 2 (i.e. 1, 2, 4, 8, 16, 32, 64, 128 bytes per record).
Direct sector access
Again, you are your own master. Do whatever you want with the flash memory. Just remember to always plan for potential power failure. It can happen at any time, and you better figure out how your application will recover the data.