n-bed testing USB HOST and a Flash disk
Fork of USBHostMSD_HelloWorld by
main.cpp@9:55d2e740be1c, 2015-05-21 (annotated)
- Committer:
- chalikias
- Date:
- Thu May 21 12:27:13 2015 +0000
- Revision:
- 9:55d2e740be1c
- Parent:
- 4:f8a5c8aa895a
COMMENTS AND CHANGED PROGRAM NAME
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chalikias | 9:55d2e740be1c | 1 | //n-bed SD card testing |
chalikias | 9:55d2e740be1c | 2 | //based on n-bed_USBHostMSD_HelloWorld |
chalikias | 9:55d2e740be1c | 3 | |
samux | 0:0d68fe822228 | 4 | #include "mbed.h" |
samux | 0:0d68fe822228 | 5 | #include "USBHostMSD.h" |
samux | 0:0d68fe822228 | 6 | |
samux | 0:0d68fe822228 | 7 | DigitalOut led(LED1); |
samux | 0:0d68fe822228 | 8 | |
samux | 0:0d68fe822228 | 9 | void msd_task(void const *) { |
samux | 0:0d68fe822228 | 10 | |
samux | 0:0d68fe822228 | 11 | USBHostMSD msd("usb"); |
samux | 0:0d68fe822228 | 12 | int i = 0; |
samux | 0:0d68fe822228 | 13 | |
samux | 0:0d68fe822228 | 14 | while(1) { |
samux | 0:0d68fe822228 | 15 | |
samux | 1:473f339c54c1 | 16 | // try to connect a MSD device |
samux | 1:473f339c54c1 | 17 | while(!msd.connect()) { |
samux | 1:473f339c54c1 | 18 | Thread::wait(500); |
samux | 1:473f339c54c1 | 19 | } |
samux | 1:473f339c54c1 | 20 | |
samux | 4:f8a5c8aa895a | 21 | // in a loop, append a file |
samux | 4:f8a5c8aa895a | 22 | // if the device is disconnected, we try to connect it again |
samux | 1:473f339c54c1 | 23 | while(1) { |
samux | 1:473f339c54c1 | 24 | |
samux | 4:f8a5c8aa895a | 25 | // append a file |
samux | 1:473f339c54c1 | 26 | FILE * fp = fopen("/usb/test1.txt", "a"); |
samux | 1:473f339c54c1 | 27 | |
samux | 1:473f339c54c1 | 28 | if (fp != NULL) { |
samux | 1:473f339c54c1 | 29 | fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); |
samux | 1:473f339c54c1 | 30 | printf("Goodbye World!\r\n"); |
samux | 1:473f339c54c1 | 31 | fclose(fp); |
samux | 1:473f339c54c1 | 32 | } else { |
samux | 1:473f339c54c1 | 33 | printf("FILE == NULL\r\n"); |
samux | 1:473f339c54c1 | 34 | } |
samux | 1:473f339c54c1 | 35 | |
samux | 0:0d68fe822228 | 36 | Thread::wait(500); |
samux | 0:0d68fe822228 | 37 | |
samux | 1:473f339c54c1 | 38 | // if device disconnected, try to connect again |
samux | 1:473f339c54c1 | 39 | if (!msd.connected()) |
samux | 1:473f339c54c1 | 40 | break; |
samux | 0:0d68fe822228 | 41 | } |
samux | 1:473f339c54c1 | 42 | |
samux | 0:0d68fe822228 | 43 | } |
samux | 0:0d68fe822228 | 44 | } |
samux | 0:0d68fe822228 | 45 | |
samux | 0:0d68fe822228 | 46 | |
samux | 0:0d68fe822228 | 47 | int main() { |
samux | 2:3e3df7a79050 | 48 | Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); |
samux | 0:0d68fe822228 | 49 | while(1) { |
samux | 0:0d68fe822228 | 50 | led=!led; |
samux | 0:0d68fe822228 | 51 | Thread::wait(500); |
samux | 0:0d68fe822228 | 52 | } |
samux | 0:0d68fe822228 | 53 | } |