Question

I'm currently developing an application for an ST32 (STM32F103xC) using Micrium OS-II. So far it's all gone smoothly, but I've hit a block now and can't proceed.

The board is configured to act as a USB storage device using the SD card so that the host PC can copy files saved out on the device. This works fine, inasmuch as can see the files currently on the SD card in a Windows Explorer.

However, when I try to use FATFS to create a file in my program, f_open() returns FR_NOT_READY. In order to investigate further, I built some sample code that writes files to SD and it works fine. So I moved the writing code around in my program, and I found that if I called f_open before I started the OS multitasking tick, it worked correctly - I could open a file and write to it. Putting the code immediately after the enabling of the tick (but before tasks have been created) goes back to getting FR_NOT_READY.

I am not sure I know what this means. Are there limitations on where I can write to files? Do I have to disable multitasking when writing? (Ouch, surely not?) Is it simply that I can't write to an SD card I am currently using as a USB device?

Was it helpful?

Solution

Indeed, you cannot expose the SD card as a mass storage device to an external USB host, and also mount it locally as a file system at the same time.

To clarify a bit further - actually you could attempt to do this with simple arbitration of access to the hardware, but you'd soon incur inconsistency if not data corruption, as each piece of file system code would assume that it had freedom to modify the file system, no requirement to immediately commit changes in a consistent manner and safety to assume that any filesystem data it has cached in memory is still valid, because nobody else could change the storage blocks without telling it.

If you want two hosts two access the storage volume, you would need to implement something with the multiple-client consistency protections common to a network file system - something which FAT is definitely not an example of.

Enumerating your options, the following should be possible:

A) Interface SD card and USB, expose SD card as a mass storage device without local access

B) Interface SD card and use it with local filesystem code; also have a USB interface for some other purpose without mass storage mode.

C) Interface SD card and USB, manage SD card as a local filesystem and grant the PC access via a network file sharing type mechanism which manages consistency

D) Support more than one of the above, but as independent modes which cannot be active at the same time and require substantial effort to switch between.

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