The RAM example loses its changes at reset. Use your own storage code when data must survive power loss. Also allow for the PC keeping cached copies of files.
Add the request's file offset to the storage address where that file begins. For example, a file starting at address 65536 with a request at offset 128 needs bytes starting at 65664. Use a number type large enough for that address. The small RAM example changes its offset to a Word only after checking that it fits the small array.
A file must support reads starting anywhere, including repeated reads of the same bytes. For generated text, prepare one stable snapshot first. For a larger log, store the text or use a repeatable way to find the character at each byte position.
For writable flash, account for its page-program boundaries and erase blocks. A 64-byte USB chunk may cover only part of an erase block. Your storage code may need to read the block, preserve unaffected bytes, apply the change in RAM, erase, and rewrite it. Never assume the USB chunk size is the flash write size. Internal program-memory operations also depend on the selected PIC and compiler; the MSC layer does not supply a firmware updater or protect a flash layout for you.
Decide when your storage code promises a write is complete. If you retain accepted writes in a staging buffer, keep them coherent for later reads and genuinely commit them when flushing. A USB_MSC_FLUSH request uses file index 255: finish pending work for all writable files, then call USB_MSC_Complete(USB_OK). If the promised storage operation fails, report failure. The RAM example can complete immediately because every accepted byte is already in its intended RAM location.
Keep a slow request in the same main-program handler until it finishes. During a polling wait, you may call ordinary USB_Service(); in interrupt mode, let the USB handler run. Do not call USB_MSCService() again while the request is unfinished. Do not pass the completion to an unrelated background task: the completion call has no request number to match a late reply.
Writes can span several requests. A failure in a later chunk does not roll back earlier chunks. For settings that must change together, stage the data, check its version, length and checksum, and apply the complete valid update as one application operation. Repeated writes must not accidentally repeat an irreversible action.
The PC may keep a cached copy of file contents, read ahead or repeat a read. Opening STATUS.CSV may therefore read the PC's copy without asking the PIC again. Do not start a new measurement just because a file is opened. Keep one stable snapshot while the drive is mounted, so a file read in several parts cannot mix different readings. Use HID, CDC or browser requests for fresh readings on demand.
Close files and safely eject/unmount the drive before unplugging it or deliberately changing the published snapshot and reconnecting. Continue servicing USB and completing any flush while ejection is in progress. A successful flush must reflect the storage code's real completion, not merely that firmware has noticed the request.
An immediate reread after a write may come from the computer's cache. To verify the PIC's RAM contents, safely eject and establish a new connection while keeping the board powered. If unplugging USB removes its only supply, the next startup restores the example's defaults; use persistent storage when changes must survive that.
Created with the Personal Edition of HelpNDoc: Transform Your Help Documentation Process with a Help Authoring Tool