|
Direct Sector Access |
Top Previous Next |
Direct sector access allows you to work with physical sectors of the flash chip directly, without the need to create and manage any files. You work with sectors through two identical 264-byte RAM buffers numbered #0 and #1 (each flash sector contains 264 bytes of data). The fd.buffernum property selects one of the buffers as the source/destination for the data (see the drawing below).

Reading data from a sector is a two-step process. First, you use fd.getsector to load all 264 bytes from desired sector into the currently selected RAM buffer. Next, you use fd.getbuffer to read the data from the selected buffer. This method allows you to read up to 255 bytes beginning from any offset in the buffer (offsets are counted from 0):
Dim s As String ... 'we want bytes 20-29 from logical sector #3 fd.buffernum=0 'select RAM buffer #0 If fd.getsector(3)<>PL_FD_STATUS_OK Then 'flash failure End If s=fd.getbuffer(20,10) 'now s contains desired data
|
Since the sector (and RAM buffer) size exceeds 255 bytes (maximum length of string variables), you can't actually read the whole sector contents in one portion. At least two fd.getbuffer reads are necessary for that.
To modify the data in the selected RAM buffer, use the fd.setbuffer method. The fd.setbuffer allows you to write new data at any offset of the selected RAM buffer. To store the contents of the RAM buffer back to the flash memory, use the fd.setsector method:
Dim s As String ... 'modify first 3 bytes of logical sector #5 If fd.getsector(3)<>PL_FD_STATUS_OK Then 'flash failure End If fd.setbuffer("ABC",0) 'write "ABC" to the buffer at offset 0 If fd.setsector(3)<>PL_FD_STATUS_OK Then 'flash failure End If
|
Since there are two identical RAM buffers, you can load the contents of two different sectors and work with the data concurrently, by switching between the buffers.
As covered under Sharing Flash Between Your Application and Data, logical sector numbers for fd.getsector and fd.setsector are not actual physical sector numbers of the flash IC. Logical sector #0 corresponds to the topmost physical sector of the flash IC, so logical numbering is "in reverse".
You can only write to sectors that reside within the data area of your flash chip (that is, logical sector numbers from 0 to fd.availableflashspace-1). This is done to prevent your application from inadvertently damaging its own code or the firmware. Trespassing the data area boundary will result in the 1- PL_FD_STATUS_FAIL status code. If you want to alter the data in the firmware/application area, see Upgrading the Firmware/Application topic.
If you are using direct sector access and file-based access at the same time, be sure to read about ensuring their proper coexistence.