USB - Insertion & Removal

06 Jan 2013

Hi all..

been trawling the forum for hours, (and I mean HOURS),.. and messing about with code.

I have a simple program that writes to a USB stick(Once a second) using MSCFileSystem, and it works.

I can pull the USB stick and the program doesn't stop working. When you try to open the file, it returns a NULL filehandle, and you just trap that and it's fine.

Where I am flummoxed is that I HAVE to have a USB stick on the USB socket on startup, otherwise the program hangs at whatever test I try to do to see if there is a stick there.

opendir fopen for read fopen for write fopen for append disk_status disk_initialise

They *all* hang if there is no stick in the socket.

However,.. if there is a stick in the socket on startup, I can merrily pull it after a few secs, and the program is fine.

I just can't find a way to test *something* is NULL if the stick isn't there.

There's a note on MSCFileSystem saying insertion/removal not implemented and you have to have a stick in on startup, but I thought there'd be *some* way of looking, but it hangs.

if anyone has any ideas.. I'd be most grateful.

cheers

Dave

06 Jan 2013

I decided to check the code a bit, and initialization didnt really look to be able to hang to me, so I made myself a very useful program. It uses Chris' MSCFileSystem library with FatFileSystemCpp. The code of this program is:

#include "mbed.h"
#include "MSCFileSystem.h"


MSCFileSystem msc("msc");
DigitalOut led(LED1);
 int main() {
    FILE *fp = fopen("/msc/myfile.txt", "w");
    if (fp!=NULL)
        error("Not supposed to happen");
     while(1) {
        led = !led;
        wait(0.2);
        }
 }

Since it is a bare mbed I am fairly certain there is no USB stick to be found ;)

And this code happily flashes led1, so it doesnt hang in anyway. In the terminal I get pretty much what I expected:

In Host_Init
Initializing Host Stack
Host Initialized
Connect a Mass Storage device
ERROR: In Host_EnumDev at Line 385 - rc = -1
Could not enumerate device: -1
06 Jan 2013

Many MANY thanks for the reply...

I'm using Chris's library too....

I'm off to see if I am going mad or not....

watch this space

07 Jan 2013

Well...

This is my code. lcdb is just a utility method to print things on the bottom line of the lcd. This hangs with "Timeinit" on the screen.

Bizarrely, if I *then* plug in a USB stick while it's sitting there waiting... it'll set off and continue and run !

(whether the time file is on the USB stick or not)

am I missing something ?

I'm using Chris's MSCFileSystem & Revision 53 of mbed libraries... I am using mbedRTOS, but this all happens in the main before I create any threads and kick them off.

int findTime()
{

    lcdb("TimeInit");
    struct tm  tma;
    time_t theTime;

    FILE *tfp = fopen("/fs/myfile.txt", "w");    <---HANGS ON THIS LINE, JUST WAITS FOR A STICK !
    if (tfp!=NULL) { //stick found
            fclose(tfp);

        lcdb("Opening T File");

        FILE *tf = fopen("/fs/time.cfg", "r");

        lcdb("T File tried");

        if (tf==NULL) {
            lcdb("No T File");
        }

        if (tf!=NULL) { // was able to find it.

            int result;
            char buffer[20];
            result = fread(buffer,1,20,tf);
            fclose(tf);

            //todo rename time file
            if (remove("/fs/time.cfg") == -1)  {
                printf("Remove failed, but thats fine \r\n");
            } else {
                //time file removed
            }

            int day, month, year, hour, min, sec;
            sscanf(buffer, "%i/%i/%i %i:%i:%i", &day, &month, &year,&hour,&min,&sec);
            lcd.locate(0,1);
            lcd.printf(buffer);
            Thread::wait(1000);

            tma.tm_year = year - 1900;
            tma.tm_mon = month  - 1;
            tma.tm_mday = day;
            tma.tm_hour = hour;
            tma.tm_min = min ;
            tma.tm_sec = sec;
            theTime = mktime(&tma);
            set_time(theTime);


        } else {
            lcdb("No T File");
            set_time(1);
        }

    } else {
        lcdb("No USB");
        set_time(1);

//no stick found
    }
    return 0;

}
07 Jan 2013

First guess: remove RTOS from your library and try it without. RTOS has irritating habbit of making stuff hang if I read a bit around the forum. (Personally for a project I pretty much decided to go for controlling time with a radio time module instead of internet time, simply because the new ethernet code uses RTOS afaik, and I dont want RTOS included).

07 Jan 2013

Hmm....

I'll have to try a recode with a round robin scheduler maybe. See if that helps !

I must admit I'm liking RTOS, but obviously you have to be careful how you use it. (no point in having a thread that's triggered once a second if it takes 2 secs to run, but that's true for all schedulers)

MCU programming for embedded systems isn't my forte tbh.. I'm a Java/Client/Server Enterprise Systems chap as my day job, so this is all new to me. So I'm still learning...

cheers

Dave