Example program of using USB mass storage device (USB flash disk) with STM32F407VET6 boards (compatible with Seed Arch Max). This is a fork of https://os.mbed.com/users/va009039/code/F401RE-USBHostMSD_HelloWorld/
Dependencies: mbed FATFileSystem USBHost-STM32F4
main.cpp@0:4ea663e6aa00, 2019-02-19 (annotated)
- Committer:
- hudakz
- Date:
- Tue Feb 19 21:35:40 2019 +0000
- Revision:
- 0:4ea663e6aa00
- Child:
- 1:f1ba83d3df51
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:4ea663e6aa00 | 1 | #include "USBHostMSD.h" |
hudakz | 0:4ea663e6aa00 | 2 | |
hudakz | 0:4ea663e6aa00 | 3 | DigitalOut led1(LED1); |
hudakz | 0:4ea663e6aa00 | 4 | |
hudakz | 0:4ea663e6aa00 | 5 | int main() |
hudakz | 0:4ea663e6aa00 | 6 | { |
hudakz | 0:4ea663e6aa00 | 7 | printf("Starting..\r\n"); |
hudakz | 0:4ea663e6aa00 | 8 | |
hudakz | 0:4ea663e6aa00 | 9 | USBHostMSD msd("usb"); |
hudakz | 0:4ea663e6aa00 | 10 | |
hudakz | 0:4ea663e6aa00 | 11 | printf("Connecting the device\r\n"); |
hudakz | 0:4ea663e6aa00 | 12 | |
hudakz | 0:4ea663e6aa00 | 13 | if (!msd.connect()) { |
hudakz | 0:4ea663e6aa00 | 14 | error("USB mass storage device not found.\n"); |
hudakz | 0:4ea663e6aa00 | 15 | } |
hudakz | 0:4ea663e6aa00 | 16 | |
hudakz | 0:4ea663e6aa00 | 17 | FILE* fp = fopen("/usb/test1.txt", "a"); |
hudakz | 0:4ea663e6aa00 | 18 | if (fp) { |
hudakz | 0:4ea663e6aa00 | 19 | fprintf(fp, "Hello from mbed.\n"); |
hudakz | 0:4ea663e6aa00 | 20 | for (int i = 0; i < 21; i++) { |
hudakz | 0:4ea663e6aa00 | 21 | fprintf(fp, " %d", i); |
hudakz | 0:4ea663e6aa00 | 22 | led1 = !led1; |
hudakz | 0:4ea663e6aa00 | 23 | } |
hudakz | 0:4ea663e6aa00 | 24 | |
hudakz | 0:4ea663e6aa00 | 25 | fprintf(fp, "\n"); |
hudakz | 0:4ea663e6aa00 | 26 | fclose(fp); |
hudakz | 0:4ea663e6aa00 | 27 | } |
hudakz | 0:4ea663e6aa00 | 28 | |
hudakz | 0:4ea663e6aa00 | 29 | fp = fopen("/usb/test1.txt", "r"); |
hudakz | 0:4ea663e6aa00 | 30 | if (fp) { |
hudakz | 0:4ea663e6aa00 | 31 | int n = 0; |
hudakz | 0:4ea663e6aa00 | 32 | while (1) { |
hudakz | 0:4ea663e6aa00 | 33 | int c = fgetc(fp); |
hudakz | 0:4ea663e6aa00 | 34 | if (c == EOF) { |
hudakz | 0:4ea663e6aa00 | 35 | break; |
hudakz | 0:4ea663e6aa00 | 36 | } |
hudakz | 0:4ea663e6aa00 | 37 | |
hudakz | 0:4ea663e6aa00 | 38 | printf("%c", c); |
hudakz | 0:4ea663e6aa00 | 39 | n++; |
hudakz | 0:4ea663e6aa00 | 40 | led1 = !led1; |
hudakz | 0:4ea663e6aa00 | 41 | } |
hudakz | 0:4ea663e6aa00 | 42 | |
hudakz | 0:4ea663e6aa00 | 43 | fclose(fp); |
hudakz | 0:4ea663e6aa00 | 44 | printf("%d bytes\n", n); |
hudakz | 0:4ea663e6aa00 | 45 | } |
hudakz | 0:4ea663e6aa00 | 46 | |
hudakz | 0:4ea663e6aa00 | 47 | while (1) { |
hudakz | 0:4ea663e6aa00 | 48 | led1 = !led1; |
hudakz | 0:4ea663e6aa00 | 49 | wait_ms(200); |
hudakz | 0:4ea663e6aa00 | 50 | } |
hudakz | 0:4ea663e6aa00 | 51 | } |