Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDBlockDevice_GR_PEACH USBHost_custom
main.cpp@2:30f86ef60767, 2017-03-23 (annotated)
- Committer:
- dkato
- Date:
- Thu Mar 23 08:40:49 2017 +0000
- Revision:
- 2:30f86ef60767
- Parent:
- 1:c1b5260c1491
Changed sd-driver to high-speed access version.
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 | 1:c1b5260c1491 | 8 | int storage_type = 0; |
| dkato | 1:c1b5260c1491 | 9 | char storage_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 | while (1) { |
| dkato | 0:392df152119c | 18 | if (sd.connect()) { |
| dkato | 1:c1b5260c1491 | 19 | storage_type = 0; // SD |
| dkato | 1:c1b5260c1491 | 20 | strcpy(storage_str, "SD card"); |
| dkato | 0:392df152119c | 21 | fs.mount(&sd); |
| dkato | 0:392df152119c | 22 | break; |
| dkato | 0:392df152119c | 23 | } |
| dkato | 0:392df152119c | 24 | if (usb.connect()) { |
| dkato | 1:c1b5260c1491 | 25 | storage_type = 1; // USB |
| dkato | 1:c1b5260c1491 | 26 | strcpy(storage_str, "USB memory"); |
| dkato | 0:392df152119c | 27 | fs.mount(&usb); |
| dkato | 0:392df152119c | 28 | break; |
| dkato | 0:392df152119c | 29 | } |
| dkato | 0:392df152119c | 30 | Thread::wait(500); |
| dkato | 0:392df152119c | 31 | } |
| dkato | 0:392df152119c | 32 | |
| dkato | 0:392df152119c | 33 | // in a loop, append a file |
| dkato | 0:392df152119c | 34 | // if the device is disconnected, we try to connect it again |
| dkato | 0:392df152119c | 35 | while(1) { |
| dkato | 0:392df152119c | 36 | // append a file |
| dkato | 0:392df152119c | 37 | FILE * fp = fopen("/storage/test1.txt", "a"); |
| dkato | 0:392df152119c | 38 | |
| dkato | 0:392df152119c | 39 | if (fp != NULL) { |
| dkato | 1:c1b5260c1491 | 40 | fprintf(fp, "Hello %s World: %d!\r\n", storage_str, i++); |
| dkato | 0:392df152119c | 41 | printf("Goodbye World!\r\n"); |
| dkato | 0:392df152119c | 42 | fclose(fp); |
| dkato | 0:392df152119c | 43 | } else { |
| dkato | 0:392df152119c | 44 | printf("FILE == NULL\r\n"); |
| dkato | 0:392df152119c | 45 | } |
| dkato | 0:392df152119c | 46 | Thread::wait(500); |
| dkato | 0:392df152119c | 47 | |
| dkato | 0:392df152119c | 48 | // if device disconnected, try to connect again |
| dkato | 1:c1b5260c1491 | 49 | if (storage_type == 0) { |
| dkato | 0:392df152119c | 50 | if (!sd.connected()) break; |
| dkato | 0:392df152119c | 51 | } else { |
| dkato | 0:392df152119c | 52 | if (!usb.connected()) break; |
| dkato | 0:392df152119c | 53 | } |
| dkato | 0:392df152119c | 54 | } |
| dkato | 1:c1b5260c1491 | 55 | fs.unmount(); |
| dkato | 0:392df152119c | 56 | } |
| dkato | 0:392df152119c | 57 | } |