n-bed testing USB HOST and a Flash disk

Dependencies:   USBHost mbed

Fork of USBHostMSD_HelloWorld by Samuel Mokrani

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //n-bed SD card testing
00002 //based on n-bed_USBHostMSD_HelloWorld
00003 
00004 #include "mbed.h"
00005 #include "USBHostMSD.h"
00006 
00007 DigitalOut led(LED1);
00008 
00009 void msd_task(void const *) {
00010     
00011     USBHostMSD msd("usb");
00012     int i = 0;
00013     
00014     while(1) {
00015         
00016         // try to connect a MSD device
00017         while(!msd.connect()) {
00018             Thread::wait(500);
00019         }
00020         
00021         // in a loop, append a file
00022         // if the device is disconnected, we try to connect it again
00023         while(1) {
00024             
00025             // append a file
00026             FILE * fp = fopen("/usb/test1.txt", "a");
00027         
00028             if (fp != NULL) {
00029                 fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
00030                 printf("Goodbye World!\r\n");
00031                 fclose(fp);
00032             } else {
00033                 printf("FILE == NULL\r\n");
00034             }
00035             
00036             Thread::wait(500);
00037         
00038             // if device disconnected, try to connect again
00039             if (!msd.connected())
00040                 break;
00041         }
00042             
00043     }
00044 }
00045 
00046 
00047 int main() {
00048     Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
00049     while(1) {
00050         led=!led;
00051         Thread::wait(500);
00052     }
00053 }