mbed-os storage access example.
Dependencies: SDBlockDevice_GR_PEACH USBHost_custom
This is a sample for using USB memory and SD card in mbed-os 5.4 or later version.
Import program
00001 #include "mbed.h" 00002 #include "FATFileSystem.h" 00003 #include "SDBlockDevice_GR_PEACH.h" 00004 #include "USBHostMSD.h" 00005 00006 int main() { 00007 int i = 0; 00008 int storage_type = 0; 00009 char storage_str[16]; 00010 00011 FATFileSystem fs("storage"); 00012 SDBlockDevice_GR_PEACH sd; 00013 USBHostMSD usb; 00014 00015 while(1) { 00016 // try to connect a storage device 00017 while (1) { 00018 if (sd.connect()) { 00019 storage_type = 0; // SD 00020 strcpy(storage_str, "SD card"); 00021 fs.mount(&sd); 00022 break; 00023 } 00024 if (usb.connect()) { 00025 storage_type = 1; // USB 00026 strcpy(storage_str, "USB memory"); 00027 fs.mount(&usb); 00028 break; 00029 } 00030 Thread::wait(500); 00031 } 00032 00033 // in a loop, append a file 00034 // if the device is disconnected, we try to connect it again 00035 while(1) { 00036 // append a file 00037 FILE * fp = fopen("/storage/test1.txt", "a"); 00038 00039 if (fp != NULL) { 00040 fprintf(fp, "Hello %s World: %d!\r\n", storage_str, i++); 00041 printf("Goodbye World!\r\n"); 00042 fclose(fp); 00043 } else { 00044 printf("FILE == NULL\r\n"); 00045 } 00046 Thread::wait(500); 00047 00048 // if device disconnected, try to connect again 00049 if (storage_type == 0) { 00050 if (!sd.connected()) break; 00051 } else { 00052 if (!usb.connected()) break; 00053 } 00054 } 00055 fs.unmount(); 00056 } 00057 }
main.cpp@0:392df152119c, 2017-03-16 (annotated)
- Committer:
- dkato
- Date:
- Thu Mar 16 09:28:46 2017 +0000
- Revision:
- 0:392df152119c
- Child:
- 1:c1b5260c1491
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:392df152119c | 1 | #include "mbed.h" |
dkato | 0:392df152119c | 2 | #include "FATFileSystem.h" |
dkato | 0:392df152119c | 3 | #include "SDBlockDevice_GR_PEACH.h" |
dkato | 0:392df152119c | 4 | #include "USBHostMSD.h" |
dkato | 0:392df152119c | 5 | |
dkato | 0:392df152119c | 6 | int main() { |
dkato | 0:392df152119c | 7 | int i = 0; |
dkato | 0:392df152119c | 8 | int strage_type = 0; |
dkato | 0:392df152119c | 9 | char strage_str[16]; |
dkato | 0:392df152119c | 10 | |
dkato | 0:392df152119c | 11 | FATFileSystem fs("storage"); |
dkato | 0:392df152119c | 12 | SDBlockDevice_GR_PEACH sd; |
dkato | 0:392df152119c | 13 | USBHostMSD usb; |
dkato | 0:392df152119c | 14 | |
dkato | 0:392df152119c | 15 | while(1) { |
dkato | 0:392df152119c | 16 | // try to connect a storage device |
dkato | 0:392df152119c | 17 | fs.unmount(); |
dkato | 0:392df152119c | 18 | while (1) { |
dkato | 0:392df152119c | 19 | if (sd.connect()) { |
dkato | 0:392df152119c | 20 | strage_type = 0; // SD |
dkato | 0:392df152119c | 21 | strcpy(strage_str, "SD card"); |
dkato | 0:392df152119c | 22 | fs.mount(&sd); |
dkato | 0:392df152119c | 23 | break; |
dkato | 0:392df152119c | 24 | } |
dkato | 0:392df152119c | 25 | if (usb.connect()) { |
dkato | 0:392df152119c | 26 | strage_type = 1; // USB |
dkato | 0:392df152119c | 27 | strcpy(strage_str, "USB memory"); |
dkato | 0:392df152119c | 28 | fs.mount(&usb); |
dkato | 0:392df152119c | 29 | break; |
dkato | 0:392df152119c | 30 | } |
dkato | 0:392df152119c | 31 | Thread::wait(500); |
dkato | 0:392df152119c | 32 | } |
dkato | 0:392df152119c | 33 | |
dkato | 0:392df152119c | 34 | // in a loop, append a file |
dkato | 0:392df152119c | 35 | // if the device is disconnected, we try to connect it again |
dkato | 0:392df152119c | 36 | while(1) { |
dkato | 0:392df152119c | 37 | // append a file |
dkato | 0:392df152119c | 38 | FILE * fp = fopen("/storage/test1.txt", "a"); |
dkato | 0:392df152119c | 39 | |
dkato | 0:392df152119c | 40 | if (fp != NULL) { |
dkato | 0:392df152119c | 41 | fprintf(fp, "Hello %s World: %d!\r\n", strage_str, i++); |
dkato | 0:392df152119c | 42 | printf("Goodbye World!\r\n"); |
dkato | 0:392df152119c | 43 | fclose(fp); |
dkato | 0:392df152119c | 44 | } else { |
dkato | 0:392df152119c | 45 | printf("FILE == NULL\r\n"); |
dkato | 0:392df152119c | 46 | } |
dkato | 0:392df152119c | 47 | Thread::wait(500); |
dkato | 0:392df152119c | 48 | |
dkato | 0:392df152119c | 49 | // if device disconnected, try to connect again |
dkato | 0:392df152119c | 50 | if (strage_type == 0) { |
dkato | 0:392df152119c | 51 | if (!sd.connected()) break; |
dkato | 0:392df152119c | 52 | } else { |
dkato | 0:392df152119c | 53 | if (!usb.connected()) break; |
dkato | 0:392df152119c | 54 | } |
dkato | 0:392df152119c | 55 | } |
dkato | 0:392df152119c | 56 | } |
dkato | 0:392df152119c | 57 | } |