Formatting the Flash Disk

Discussed in this section: fd.format, fd.formatj.

Before the flash disk can be used, it must be formatted. Formatting allocates and initializes the "housekeeping" area of the disk. This consists of two boot sectors plus a certain number of sectors for the file record table (FRT), the file allocation table (FAT), and possibly the transaction journal if you want one. For details, see Disk Area Allocation Details.

Formatting is performed using the fd.format or fd.formatj methods. These methods accept, as arguments, two parameters: the total number of sectors to be occupied by the disk and the maximum number of files that you wish to be able to store on the disk. fd.formatj additionally has the third argument: the number of sectors that you want to allocate to the transaction journal. fd.format does not give any sectors to the latter, so it is like setting the journal size to 0.

A tip note icon.Be generous when allocating the journal area. A size of 50-100 sectors would be recommended. Journal sectors get written to all the time, and having many of them prolongs the life of your flash memory.

The total number of sectors cannot exceed the size of the data area. That is, the maximum "gross" disk size is fd.availableflashspace sectors. The flash disk needs a number of sectors for its internal housekeeping, so actual useful capacity of the disk will be less. The Checking Disk Vitals topic explains how to find out current disk capacity, as well as get other useful info. There is also a minimum limit that will be accepted for the total disk size. The minimum exists because the number of sectors occupied by the disk must at least be enough for the "housekeeping" data.

A tip note icon.When debugging your application on devices with shared flash memory, use only a portion of the data area and leave some part of this area unoccupied. This will create a gap of unused sectors between the data and the firmware/application areas of the flash. This way, your application will (mostly) be able to grow without corrupting the data area.

The fd. object stores up to 64 files on the disk, in a single "root" directory. You can choose to have less files if you don't need so many. This will reduce the number of "housekeeing" sectors required for the disk. The economy is not dramatic, but you can still save some space. Each file record occupies 64 bytes, so one sector of the FRT table can store four file records. For this reason, the number of files you specify will always be adjusted to be a multiple of four. For example:  

'format a disk to occupy 3/4th of available space and store at least 10 files
If fd.formatj((fd.availableflashspace/4)*3,10,100)<>PL_FD_STATUS_OK Then
   'some problem...
End If
'actual maximum number of files will be 12 (adjusted up from 10 to be a multiple of 4) 

After the formatting, the disk will be in the dismounted state and will need to be mounted before any disk-related activity can be successfully performed.