Handle a file request

… visits
Parent Previous Next

For each request, first find which file and byte range the PC wants. Then fill or store those bytes and complete the request once.


Keep calling USB_MSCService() from your main program, including while the drive first appears on the PC. It gives your code one file request at a time. Handle the request and mark it complete before calling USB_MSCService() again.

API or field

Meaning and how to use it

USB_MSCService()

Progress storage commands and make a file request available. Always call from the main program, including in ISR mode.

USB_MSC_Request

0: no request; USB_MSC_READ (1): supply bytes; USB_MSC_WRITE (2): accept bytes; USB_MSC_FLUSH (3): finish storage work.

USB_MSC_File

File index, starting at 0. A flush uses 255 for the whole writable volume.

USB_MSC_Offset

A Dword byte offset within the selected file. Offset 0 is its first byte.

USB_MSC_Length

Number of bytes to handle: 1–64 for reads and writes. A flush has length 0 and offset 0.

USB_MSC_Data[64]

For a read, fill the first Length bytes. For a write, take the first Length bytes from this array.

USB_MSC_Complete(USB_OK)

Complete the current request successfully, after handling its bytes or flush.

USB_MSC_Complete(error)

Fail the request. For example, use USB_NOT_SUPPORTED for an operation your storage code cannot perform.

Read and write are named from the PC side. For a read, put the requested file bytes into USB_MSC_Data. For a write, take the incoming bytes from that array and store them.

Call the completion command once per request, after handling all its bytes. Passing USB_BUSY or another value other than USB_OK reports a failure; it does not ask the PC to wait. Then check USB_Result to see whether the library accepted the completion. A disconnect or reset may have cancelled the request.


The PC may start in the middle of a file, repeat a read, or switch files. For every request, use its File, Offset and Length. Do not just continue where the last request ended. Your handler only deals with file contents; the library handles the FAT and directory information.


MSC owns the data endpoints and TXReport/RXReport. Do not use USB_Read, USB_Write or their wait variants for additional packets in this personality.

See also

Created with the Personal Edition of HelpNDoc: Simplify Your Help Documentation Process with a Help Authoring Tool