|
1 | 1 | #include "mbed.h"
|
2 | 2 | #include "USBHostMSD.h"
|
3 | 3 | DigitalOut led(LED1);
|
4 |
| -void msd_task(void const *) { |
5 |
| - printf("init msd\n"); |
6 |
| - USBHostMSD msd("usb"); |
7 |
| - int i = 0; |
8 |
| - printf("wait for usb memory stick insertion\n"); |
9 |
| - while(1) { |
10 |
| - |
11 |
| - // try to connect a MSD device |
12 |
| - while(!msd.connect()) { |
13 |
| - Thread::wait(500); |
14 |
| - } |
15 |
| - |
16 |
| - // in a loop, append a file |
17 |
| - // if the device is disconnected, we try to connect it again |
18 |
| - |
19 |
| - // append a file |
20 |
| - FILE * fp = fopen("/usb/test1.txt", "a"); |
21 |
| - |
22 |
| - if (fp != NULL) { |
23 |
| - fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); |
24 |
| - printf("Goodbye World!\r\n"); |
25 |
| - fclose(fp); |
26 |
| - } else { |
27 |
| - printf("FILE == NULL\r\n"); |
28 |
| - } |
29 |
| - Thread::wait(500); |
30 |
| - printf("again\n"); |
31 |
| - // if device disconnected, try to connect again |
32 |
| - while (msd.connected()) { |
33 |
| - Thread::wait(500); |
34 |
| - } |
35 |
| - } |
| 4 | +#include "FATFileSystem.h" |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | + |
| 8 | +void msd_task(void const *) |
| 9 | +{ |
| 10 | + |
| 11 | + USBHostMSD msd; |
| 12 | + int i = 0; |
| 13 | + FATFileSystem fs("usb"); |
| 14 | + int err; |
| 15 | + printf("wait for usb memory stick insertion\n"); |
| 16 | + while(1) { |
| 17 | + |
| 18 | + // try to connect a MSD device |
| 19 | + while(!msd.connect()) { |
| 20 | + Thread::wait(500); |
| 21 | + } |
| 22 | + if (fs.mount(&msd) != 0) { |
| 23 | + continue; |
| 24 | + } else { |
| 25 | + printf("file system mounted\n"); |
| 26 | + } |
| 27 | + |
| 28 | + if (!msd.connect()) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + // in a loop, append a file |
| 33 | + // if the device is disconnected, we try to connect it again |
| 34 | + |
| 35 | + // append a file |
| 36 | + File file; |
| 37 | + err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND); |
| 38 | + |
| 39 | + if (err == 0) { |
| 40 | + char tmp[100]; |
| 41 | + sprintf(tmp,"Hello fun USB stick World: %d!\r\n", i++); |
| 42 | + file.write(tmp,strlen(tmp)); |
| 43 | + sprintf(tmp,"Goodbye World!\r\n"); |
| 44 | + file.write(tmp,strlen(tmp)); |
| 45 | + file.close(); |
| 46 | + } else { |
| 47 | + printf("FILE == NULL\r\n"); |
| 48 | + } |
| 49 | + Thread::wait(500); |
| 50 | + printf("again\n"); |
| 51 | + // if device disconnected, try to connect again |
| 52 | + while (msd.connected()) { |
| 53 | + Thread::wait(500); |
| 54 | + } |
| 55 | + while (fs.unmount() < 0) { |
| 56 | + Thread::wait(500); |
| 57 | + printf("unmount\n"); |
| 58 | + } |
| 59 | + } |
36 | 60 | }
|
37 | 61 |
|
38 |
| -int main() { |
| 62 | +int main() |
| 63 | +{ |
39 | 64 | Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
|
| 65 | + |
40 | 66 | while(1) {
|
41 | 67 | led=!led;
|
42 | 68 | Thread::wait(500);
|
|
0 commit comments