Question

I am working on an embedded device that uses an SD-Card for storage and needs to allow a PC to access that SD-Card over USB.

The best approach I could come up with is virtualizing the disk and presenting that virtualized picture to the PC. That would allow the PC to read and write files, while my device is also reading and writing.

The problem comes up when you consider that the PC may have cached reads, so it becomes impossible for the PC user to see new files as the device creates them -- The PC will only see files that existed at the time it was connected.

Another drawback is that it is not safe to remove the flash card until the PC has been disconnected.

Are there any workarounds for this? How do modern cameras handle it? I believe that media transfer protocol was created for just this purpose, however it is not widely supported in a transparent manner. And I'm not dealing with "media" files, although I assume that doesn't matter.

Is there some commonly used clever workaround that I am missing?

Thank you

Was it helpful?

Solution

As far as I know, there is no way for two devices to cooperatively share a block device using common file systems such as FAT. (unless perhaps it's a read-only file system)

The solution is for only one device (i.e. the camera) to own the file system at the low level (block level). Then that device can share files using a higher-level protocol (higher than raw block operations). As you said, the USB Media Transfer Protocol is an example of that. Other examples of a higher-level file sharing protocol are NFS and SMB, although I don't think those would be applicable in this scenario.

Quoting from Media Transfer Protocol on Wikipedia:

A main reason for using MTP rather than for example the USB mass-storage device class (MSC) is that the latter operates at the granularity of a mass storage device block (usually in practice, a FAT block), rather than at the logical file level. In other words, the USB mass storage class is designed to give a host computer undifferentiated access to bulk mass storage, such as compact flash, rather than to a file system, which might be safely shared with the target device (except for specific files which the host might be modifying/accessing). In practice, therefore, when a USB host computer has mounted an MSC partition, it assumes absolute control of the storage, which then may not be safely modified by the device without risk of data corruption until the host computer has severed the connection.

OTHER TIPS

Build two different partitions on the same SD card - have your device use one and expose one to the PC. Since there will be no concurrent access to the same file system the problem is solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top