Re-mount SD card in "mass storage" mode

I configured USB as “mass storage” and the storage location to the SD card (as described here).

When I plug the USB cable to the PC, the PC can see the files contained in the SD card.

If my VF61 device doesn’t try to access the SD card while the USB cable is connected, everything works fine and when the USB cable is unplugged the device is able to re-mount the SD card and see its content.

If the VF61 device tries to access the SD card while the USB cable is plugged, something goes wrong and the SD card isn’t re-mlunted after the USB cable is unplugged.

I searched inside this forum and on the web, finding some different suggestions; which is the recommended approach from Toradex?

Is there a way (hardware pin, function, …) for VF61 to check if the USB cable is plugged and/or id the SD card can be accessed?

Is there a robust way to re-mount it after the device is notified that the USB cable has been unplugged?

Dear @vix

The USB mass storage approach brings this severe disadvantage that you cannot access the drive while a USB cable is connected, therefore not too many customers are using it. We were not aware about the problem with re-mounting the drive.

Detecting mass storage connection

To detect whether a USB cable is connected, you can sense the 5V USB voltage which is provided by the USB host (PC). On the Colibri Evaluation Board, this signal USB_C_DET is routed to SODIMM 137.
Another option is verifying the [HKLM\Drivers\Active] key in the registry to see whether the mass storage driver is loaded.
However, there is always the chance that the cable was (un)plugged between your check and the actual SD access, so this will never be fully safe.

Alternatives

If your data is not too big, you can install a ram drive which you make available as a mass storage device. This way you keep your SD card accessible from your application at all times.

You could also use different file transfer models, such as using an FTP server, or building an RNDIS network connection over USB. Of course this would require some additional software implementation to make it work.

I would appreciate a feedback how urgent the SD issue is for your project.

Regards, Andy

I add some information from my side.

First of all I was partially wrong: after the USB cable is unplugged, the SD card is re-mounted (I double checked this saving a dummy batch file inside a folder named AutoRun on the SD card so that it’s executed when the SD card is re-mounted).

The problem is that if I call some functions like FindFirstFile() and CreateDirectory() while SD card is unmounted, some big issue happens under the hood.

Afterwards, when the SD card is re-mounted, calling the same functions FindFirstFile() and CreateDirectory() doens’t work and they can’t see the files on the SD card anymore.

I thought to your suggestions, but I was not so satisfied about them (the only one that could work is sensing the level of USB_C_DET pin, but I decided not to implement it).

I was able to fix the issue using OpenStore(), GetStoreInfo() and the storeInfo.dwMountCount flag.