455
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
losing the old and the new contents of the sector.
For many
, 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
this way, too.
Secondly, try limiting rewrites in the FRT and FAT -- these most sensitive areas of
the disk.
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
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.tof)
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
(
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
R/O property always tells you the number of a sector
corresponding to the current pointer position (
). 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.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).
436
453
453
437
380
478
476