chipKIT® Development Platform

Inspired by Arduino™

USB MSD LOGGER

Created Mon, 12 Mar 2012 15:10:18 +0000 by freto


freto

Mon, 12 Mar 2012 15:10:18 +0000

I am playing with the USBMSDHost example by Keith Vogel from the Digilent Library; I probably missed something obvious but I cannot figure out how to write data to a USB stick at each loop().

The example works great to write one time to a file, but how about writing some data repetitively to the disk? I am thinking to open a file in Setup(), then read some sensors and write to the stick, flush every few seconds to make sure the data was written, etc...

The loop() in the example goes like this:

void loop() {
  // put your main code here, to run repeatedly: 
  
    //USB stack process function
    RunUSBTasks();

    //if thumbdrive is plugged in
    if(USBMSDHost.SCSIMediaDetect())
    {
        deviceAttached = TRUE;

        //now a device is attached
        //See if the device is attached and in the right format
        if(MDDFS.Init())
        {
            //Opening a file in mode "w" will create the file if it doesn't
            //  exist.  If the file does exist it will delete the old file
            //  and create a new one that is blank.
            myFile = MDDFS.fopen("test2.txt","w");

            //Write some data to the new file.
            MDDFS.fwrite("This is a test.",1,15,myFile);
                
            //Always make sure to close the file so that the data gets
            //  written to the drive.
            MDDFS.fclose(myFile);

            //Just sit here until the device is removed.
            while(deviceAttached == TRUE)
            {
                RunUSBTasks();
            }
        }
    }   
}

The obvious was to open a file in Setup and comment MSSFS.fclose(myFile) but that is not the solution, the file is written only once anyway.

Thanks for any advice you may have.