File-Based and Direct Sector Access Coexistence

The file-based and direct sector access methods can be used at the same time, as long as you understand how direct access may affect (and possibly screw up) the flash disk.

Direct Sector Access explains that working with flash sectors is done through two RAM buffers numbered #0 and #1. Flash disk operation depends on these buffers as well.

Buffer #0 is used for processing housekeeping data and stores no valuable content that must be preserved past the end of a single disk-related method execution. That is, once a method such as fd.setdata has executed, buffer #0 has no valuable data in it.

Buffer #1 is used to load and store the contents of the most recently loaded (and possibly changed) data sector. So, if fd.setdata was recently called, this buffer may still hold new data, which may not be saved to the flash memory yet. Corrupting this unsaved data would have unpleasant consequences for the file. To prevent this, the fd. object automatically dismounts the disk (sets fd.ready = 0 — NO) if your application does fd.setbuffer or fd.getsector while fd.buffernum = 1 and while this buffer was loaded with new and as-yet-unsaved sector data.

Preventing disk dismounting is easy. You can opt to work on RAM buffer #0 (set fd.buffernum = 0) or flush the unsaved data by invoking fd.flush or fd.close.

The fd.setsector method will cause disk dismounting if the destination was within the disk area (sectors 0 through fd.totalsize - 1). Writing outside the disk area will not interfere with the flash disk operation.

The fd.getbuffer method does not cause any interference with the flash disk, so it can be used freely.

The fd.checksum method will write to the RAM buffer (when in the "PL_FD_CSUM_MODE_CALCULATE" mode), but the checksum calculation method is the same as the one used by the disk itself, so setting this checksum will never be wrong.

Finally, note that any file-related method will affect the value of fd.buffernum. Never assume that you know to what this property is set. Always set it explicitly and each time before performing direct sector access.