an. How to enable file system support on a USB MSD device?
Microchip Harmony v1.05 NVM Driver setup sample
5. Open APP.c file and add code to mount the data drive and read and write files ... See Code sample 2 for more information.
4. Now, open APP.h header file and add additional application states for mounting data drives, opening files, reading files, etc.. The use of application states is a must to prevent blocking during wait states as file system operations are carried out.
3. Open NVM or other device driver that you need to provide raw data storage and select "Register with file system" option. Then click "Generate" to run Harmony Configurator's program code generator and alter the source code appropriately.
2. Open USB MSD example for a PIC32 starter kit or other PIC32 microcontroller circuit in MPLABX 3.05. This enables a basic USB MSD functionality "out of a box".
1. Install Microchip Harmony library v1.05 or later and Microchip Harmony Configuratior v1.05 or later, if they are not already installed.
A recipe to use PIC32 and Microchip Harmony v1.05 or later file system supoort togeher with USB Device support:
3. Recompile the project.
2. Enable file system support in System services. Check "Use file system service" and expand it. Set the appropriate size of flash RAM page media buffer. The size should match the actual page size specified in the underlying device driver.
2, Set Number of (NVM) Driver Clients to at least 2, because the driver will be used by MSD and the file system.
1. Enable register with file system option on the supporting device driver. Optionally, also configure the device driver, if it was not already configured in the USB MSD device sample. You may use other programming samples provided by Microchip to obtain configuration samples. A common mistake with NVM driver when changing the microcontroller family or type is not updating flash row and page sizes. Consider your target microcontroller datasheet to obtain the data. Harmony Configurator expects flash row and page sizes to be in bytes.
How to make a working configuration in Harmony Configurator for a memory drive:
FAT file system support is alost a must, if a microcontroller should make use of data on its data drives like flash RAM drive, RAM drive, SDCARD, etc.USB MSD (mass storage device) protocol supports various file systems and various data drives.
Code sample 1: Additional application states besides "INIT" and "RUNNING" in app.h file::
typedef enum
{
APP_STATE_INIT=0, /* Application's state machine's initial state. */
APP_STATE_RUNNING,
APP_STATE_MOUNT_DISK,
APP_STATE_OPEN_FILE,
APP_STATE_READ_FILE
} APP_STATES;
Code sample 2: New APP_tasks routine for APP.c file:
void APP_Tasks(void){
const SYS_FS_FUNCTIONS FatFsFunctions ={
.mount = f_mount,
.unmount = f_unmount,
.open = f_open,
.read = f_read,
.write = f_write,
.close = f_close,
.seek = f_lseek,
.tell = f_tell,
.eof = f_eof,
.size = f_size,
.fstat = f_stat,
};
const SYS_FS_REGISTRATION_TABLE sysFSInit [ SYS_FS_MAX_FILE_SYSTEM_TYPE ] ={
{
.nativeFileSystemType = FAT,
.nativeFileSystemFunctions = &FatFsFunctions
}
};
const uint8_t codeData[4] = "1234";
/* Check the application's current state. */
switch ( appData.state )
{
case APP_STATE_INIT: // initial state
{
appData.usbDeviceHandle = USB_DEVICE_Open(USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE);
if(appData.usbDeviceHandle != USB_DEVICE_HANDLE_INVALID)
{
/* Set the Event Handler.*/
USB_DEVICE_EventHandlerSet(appData.usbDeviceHandle, APP_USBDeviceEventHandler, (uintptr_t)&appData);
appData.state = APP_STATE_RUNNING; /* Set the next state */
}
break;
}
case APP_STATE_MOUNT_DISK:
if(SYS_FS_Mount("/dev/nvma1", "/mnt/myDrive1", FAT, 0, NULL) == SYS_FS_RES_SUCCESS){
appData.state = APP_STATE_OPEN_FILE;
BSP_LEDOn ( APP_USB_LED_2 );
}
break;
case APP_STATE_OPEN_FILE:
appData.fileHandle = SYS_FS_FileOpen("/mnt/myDrive1/FILE.TXT",(SYS_FS_FILE_OPEN_READ));
if(appData.fileHandle != SYS_FS_HANDLE_INVALID){ // File open is successful
BSP_LEDOn ( APP_USB_LED_1 );
appData.state = APP_STATE_READ_FILE;
}
break;
case APP_STATE_READ_FILE:
if(SYS_FS_FileRead(appData.fileHandle, (void *)appData.data, 4) == -1){
appData.state = APP_STATE_RUNNING; // Skip further operations
} else {
if(memcmp(appData.data, codeData, 4) != 0){
BSP_LEDOff ( APP_USB_LED_1 );
BSP_LEDOn ( APP_USB_LED_2 );
} else {
BSP_LEDOn ( APP_USB_LED_1 );
BSP_LEDOff ( APP_USB_LED_2 );
}
SYS_FS_FileClose(appData.fileHandle);
appData.state = APP_STATE_OPEN_FILE;
break;
}
break;
case APP_STATE_RUNNING:
if(CAN_MOUNT_DISK){
CAN_MOUNT_DISK=0;
appData.state = APP_STATE_MOUNT_DISK;
}
break;
default:
break;
}
Microchip Harmony v1.05 File System setup sample