8 years, 2 months ago.

Is there a newer version of USBHostMSD that works with the USBHostMSD constructor at the module?

This demo compiles and runs. It does have some extraneous debug printf's but those could be easily fixed.

However, as it exists, the access to the file system can only done from the thread.

void msd_task(void const *) {
    USBHostMSD msd("usb");
    ...

I guess every thread could probably have their own instance, but that would seem wasteful at least.

I moved the constructor out, so it was main.cpp global.

USBHostMSD msd("usb");

void msd_task(void const *) {
    ...

and it still compiles, but will not run. Interestingly, it won't even emit any crash/error information on the serial port and the LEDs are not on the blue-flash-of-death.

mbed libmbed-rtosFATFileSystem vUSBHostMSD constructorResults
v61v9v2inside msd_taskWorks
v61v9v2module levelFails, 66 Warns, not even a debug print
v115v106v7inside msd_taskWorks
v115v106v7module levelFails, 66 Warns, not even a debug print

I have a goal, that seems far away at the moment. That is to be able to access both the USB storage and the SD storage from one application. Does anybody have that working?

Question relating to:

1 Answer

8 years, 2 months ago.

Hi David,

I did some poking around and found some interesting things and a workaround! (feel free to ignore the interesting things and skip to the workaround)

Interesting things

The reason I think it fails to run when you move the constructor of USBHostMSD to the module level is it attempts to spin off a thread that handles USB activity. Since main hasn't started at this point, the RTOS probably isn't happy at this point and things just break.

The interesting bit of code that is doing that is located here: https://github.com/mbedmicro/mbed/blob/master/libraries/USBHost/USBHost/USBHost.cpp#L254

So whenever you construct USBHostMSD, it tries to spin off the thread immediately. This isn't a problem inside the msd_task thread, and its also not a problem in main, which leads me to the workaround.

Workaround

At the module level declare a pointer to a USBHostMSD instance. Then,construct the instance in main and set the pointer to that object. You can then access that in any thread.

Some example code:

USBHostMSD *global_msd;

void msd_task(void const *) {
    ...
    global_msd->connect();
   ...
}

void main() {
    USBHostMSD msd("usb");
    global_msd = &msd;
    
    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);

    while(1) {
        ...
    }
}

Hope that helps!

Brian

Accepted Answer

Thanks Brian. That would explain it. David.

[Update 13 Mar 2016] I successfully put together a combination that does what I was looking for. It supports both an SD and a USB memory device from within a single application. It uses libraries that others have created/maintained, with most changes in the FatFileSystem.cpp/.h files.

FileSys-MultiFileSystem

posted by David Smart 10 Mar 2016