SLAU533D September 2013 – April 2017
mscProcessBuffer() is the first function to run after waking from LPM0. It checks the "operation" field of the RWbuf_info structure to see if the USB API is waiting for the application to handle any READ or WRITE buffer operations from the USB host.
If a READ or WRITE is pending, mscProcessBuffer accesses the media to perform the read or write. It does this by issuing low-level commands to the FatFs file system library, disk_read() and disk_write().
The block of code within mscProcessBuffer() for performing reads is:
while (RWbuf_info->operation == kUSBMSC_READ)
{
RWbuf_info->returnCode = disk_read(0,
RWbuf_info->bufferAddr,
RWbuf_info->lba,
RWbuf_info->lbCount); // Fetch a block from the medium
USBMSC_bufferProcessed(); // Close the buffer operation
}
In a more typical mass storage application, the media would probably be an SD-card or external SPI flash. In this application, the media is located inside the MSP430 internal flash memory. This is only approximately 60KB, but it is big enough for what this demo needs, and saves the cost and complexity of external media.
FatFs is commonly used to interface with FAT-formatted memory cards, but for this example it is been customized for internal MSP430 flash. Most of this was done in disk_read() and disk_write().
Whereas the high-level FatFs calls ask FatFs to open or read files, and leave it to FatFs to parse the volume and locate the files itself, the low-level calls bypass this, asking FatFs to read/write specific locations in the volume. These locations are measured in blocks or sectors. The address of a block is called a logical block address, or LBA, which is a parameter passed to disk_read().
The demo application implements the volume as an array called storageVol[]. The C-code contents of storageVol[] contain the files that were seen in Step 3 of Section 1.3. storageVol[] occupies all of the F5529 upper on-chip flash memory, from address 0x10000 to the end of the map, 0x243FF. To ensure the linker does not place any code or other data there, a special linker segment has been set up: MYDRIVE. This segment is defined in the auxiliary linker file \USB_app\F5529LP_UE.cmd. storageVolume.c then contains code to locate storageVol[] there.
The volume within storageVol[] is formatted as FAT. It was generated using a procedure that is described in the comments in storageVol.c. You can use this same procedure to create your own storage volumes represented in C code, with your own file/directory sets.
If you are interested in using FatFs for SD cards, download the USB Developers Package and look in the USB examples for MSC interfaces. One of the examples is a USB SD-card reader, written to run on the F5529 Experimenter's Board (MSP-EXP430F5529) which has an SD-card socket on it. This example has a version of FatFs set up for this purpose.