bool DoTestUSBHost; /* flag set by attached event to trigger test code */ int main(void) { SetupHardware(); while (1) { if (DoTestUSBHost) { DEBUGOUT("Mass Storage Host Demo running.\r\n"); USB_ReadWriteFile(); DEBUGOUT("Example completed.\r\n"); DoTestUSBHost = false; } } } void EVENT_USB_Host_DeviceAttached(const uint8_t corenum) { DEBUGOUT(("Device Attached on port %d\r\n"), corenum); DoTestUSBHost = true; } |
/* Read sectors */ int FSUSB_DiskReadSectors(DISK_HANDLE_T *hDisk, void *buff, uint32_t secStart, uint32_t numSec) { if (MS_Host_ReadDeviceBlocks(hDisk, 0, secStart, numSec, DiskCapacity.BlockSize, buff)) { DEBUGOUT("Error reading device block.\r\n"); USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0); return 0; } return 1; } |
/* Disk Status */ static volatile DSTATUS Stat = STA_NOINIT; /* Initialize Disk Drive */ DSTATUS disk_initialize(BYTE drv) { if (drv) { return STA_NOINIT; /* Supports only single drive */ } /* if (Stat & STA_NODISK) return Stat; *//* No card in the socket */ if (Stat != STA_NOINIT) { return Stat; /* card is already enumerated */ } #if !_FS_READONLY FSUSB_InitRealTimeClock(); #endif /* Initialize the Card Data Strucutre */ hDisk = FSUSB_DiskInit(); /* Reset */ Stat = STA_NOINIT; FSUSB_DiskInsertWait(hDisk); /* Wait for card to be inserted */ /* Enumerate the card once detected. Note this function may block for a little while. */ if (!FSUSB_DiskAcquire(hDisk)) { DEBUGOUT("Disk Enumeration failed...\r\n"); return Stat; } Stat &= ~STA_NOINIT; return Stat; } |
if (Stat != STA_NOINIT) { return Stat; /* card is already enumerated */ } |
bool USBHostWaitForReady(void) { FSUSB_DiskInsertWait(&FlashDisk_MS_Interface); return (USB_HostState[FlashDisk_MS_Interface.Config.PortNumber] == HOST_STATE_Configured); } |
/* Function to do the read/write to USB Disk */ static void USB_ReadWriteFile(void) { ...snip... if (!USBHostWaitForReady()) /* FIX for disk_initialize(): STA_NOINIT is never reset! */ { DEBUGOUT("\rUSBHostWaitForReady failed"); return; } f_mount(0, &fatFS); /* Register volume work area (never fails) */ ... |
/* Get the disk data structure */ DISK_HANDLE_T *FSUSB_DiskInit(void) { return &FlashDisk_MS_Interface; } |
/** LPCUSBlib Mass Storage Class driver interface configuration and state information. This structure is * passed to all Mass Storage Class driver functions, so that multiple instances of the same class * within a device can be differentiated from one another. */ static USB_ClassInfo_MS_Host_t FlashDisk_MS_Interface = { .Config = { .DataINPipeNumber = 1, .DataINPipeDoubleBank = false, .DataOUTPipeNumber = 2, .DataOUTPipeDoubleBank = false, .PortNumber = 1, }, }; |
I am using #LPC1769, I faced same problem.
I have added following function to fs_usb.c
void diskDeinitialize(){
Stat = STA_NOINIT;
}
I called diskDeinitialize function in EVENT_USB_Host_DeviceUnattached
void EVENT_USB_Host_DeviceUnattached(const uint8_t corenum) {
DEBUGOUT(("\r\nDevice Unattached on port %d\r\n"), corenum);
diskDeinitialize();
}
For me it's serves the purpose.
Is this the correct way to fix the problem ??
Hi Nataraj,
Your workaround is correct, that way the Stat variable is set back to STA_NOINT each time the device is unattached. The problem is that the USB Mass storage Host example was developed to read the files from USB mass storage device only the first time it is connected to the USB host port and then print the results to the UART console, it was not tested in the scenario when this is done multiple times.
Hope it helps!
Best Regards,
Carlos Mendoza
Technical Support Engineer