USB Mass Storage Device demo with SD card

Dependencies:   USBDevice USBMSD_SD max32630fthr

Fork of FTHR_USBMSD_Demo by Greg Steiert

This example implements a micro SD card reader allowing access to a card inserted into the micro SD connector through the micro USB connector using the standard USB-MSD interface so that it appears just like a USB drive to your system.

Committer:
switches
Date:
Fri Feb 23 23:26:43 2018 +0000
Revision:
8:8c31b3ba5371
Parent:
6:1f64f4cf7cc7
Child:
9:6ada4f89915c
MAX32630FTHR USBMSD Block Device Example;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:60a522ae2e35 1 #include "mbed.h"
switches 1:464c8b3634dc 2 #include "max32630fthr.h"
switches 8:8c31b3ba5371 3 #include "USBMSD_BD.h"
switches 8:8c31b3ba5371 4 #include "HeapBlockDevice.h"
switches 8:8c31b3ba5371 5 #include "FATFileSystem.h"
switches 8:8c31b3ba5371 6
switches 8:8c31b3ba5371 7 #define BLOCK_SIZE 512
switches 0:60a522ae2e35 8
switches 6:1f64f4cf7cc7 9 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
switches 3:7264c8044625 10
switches 1:464c8b3634dc 11 DigitalOut rLED(LED1);
switches 1:464c8b3634dc 12 DigitalOut gLED(LED2);
switches 8:8c31b3ba5371 13 DigitalOut bLED(LED3);
switches 8:8c31b3ba5371 14
switches 8:8c31b3ba5371 15 // Physical block device, can be any device that supports the BlockDevice API
switches 8:8c31b3ba5371 16 HeapBlockDevice bd(512*BLOCK_SIZE, BLOCK_SIZE);
switches 8:8c31b3ba5371 17
switches 8:8c31b3ba5371 18 // File system declaration
switches 8:8c31b3ba5371 19 FATFileSystem fs("fs");
switches 8:8c31b3ba5371 20
switches 8:8c31b3ba5371 21 // USB MSD
switches 8:8c31b3ba5371 22 USBMSD_BD msd(&bd, BLOCK_SIZE);
switches 8:8c31b3ba5371 23
switches 0:60a522ae2e35 24
switches 0:60a522ae2e35 25 // main() runs in its own thread in the OS
switches 0:60a522ae2e35 26 // (note the calls to Thread::wait below for delays)
switches 0:60a522ae2e35 27 int main()
switches 0:60a522ae2e35 28 {
switches 8:8c31b3ba5371 29 printf("--- Mbed OS filesystem example ---\n");
switches 8:8c31b3ba5371 30 rLED = LED_ON;
switches 6:1f64f4cf7cc7 31 gLED = LED_ON;
switches 8:8c31b3ba5371 32 bLED = LED_OFF;
switches 1:464c8b3634dc 33
switches 1:464c8b3634dc 34 Thread::wait(100);
switches 1:464c8b3634dc 35
switches 8:8c31b3ba5371 36 // Try to mount the filesystem
switches 8:8c31b3ba5371 37 printf("Mounting the filesystem... ");
switches 8:8c31b3ba5371 38 fflush(stdout);
switches 8:8c31b3ba5371 39 int err = fs.mount(&bd);
switches 8:8c31b3ba5371 40 printf("%s\n", (err ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 41 if (err) {
switches 8:8c31b3ba5371 42 // Reformat if we can't mount the filesystem
switches 8:8c31b3ba5371 43 // this should only happen on the first boot
switches 8:8c31b3ba5371 44 printf("No filesystem found, formatting... ");
switches 8:8c31b3ba5371 45 fflush(stdout);
switches 8:8c31b3ba5371 46 err = fs.reformat(&bd);
switches 8:8c31b3ba5371 47 printf("%s\n", (err ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 48 }
switches 8:8c31b3ba5371 49
switches 1:464c8b3634dc 50 rLED = LED_OFF;
switches 0:60a522ae2e35 51
switches 8:8c31b3ba5371 52 // Open the numbers file
switches 8:8c31b3ba5371 53 printf("Opening \"/fs/numbers.txt\"... ");
switches 8:8c31b3ba5371 54 fflush(stdout);
switches 8:8c31b3ba5371 55 FILE *f = fopen("/fs/numbers.txt", "r+");
switches 8:8c31b3ba5371 56 printf("%s\n", (!f ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 57 if (!f) {
switches 8:8c31b3ba5371 58 // Create the numbers file if it doesn't exist
switches 8:8c31b3ba5371 59 printf("No file found, creating a new file... ");
switches 8:8c31b3ba5371 60 fflush(stdout);
switches 8:8c31b3ba5371 61 f = fopen("/fs/numbers.txt", "w+");
switches 8:8c31b3ba5371 62 printf("%s\n", (!f ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 63
switches 8:8c31b3ba5371 64 for (int i = 0; i < 10; i++) {
switches 8:8c31b3ba5371 65 printf("\rWriting numbers (%d/%d)... ", i, 10);
switches 8:8c31b3ba5371 66 fflush(stdout);
switches 8:8c31b3ba5371 67 err = fprintf(f, " %d\n", i);
switches 8:8c31b3ba5371 68 if (err < 0) {
switches 8:8c31b3ba5371 69 printf("Fail :(\n");
switches 8:8c31b3ba5371 70 }
switches 8:8c31b3ba5371 71 }
switches 8:8c31b3ba5371 72 printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
switches 8:8c31b3ba5371 73
switches 8:8c31b3ba5371 74 printf("Seeking file... ");
switches 8:8c31b3ba5371 75 fflush(stdout);
switches 8:8c31b3ba5371 76 err = fseek(f, 0, SEEK_SET);
switches 8:8c31b3ba5371 77 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 78 }
switches 8:8c31b3ba5371 79
switches 8:8c31b3ba5371 80 // Go through and increment the numbers
switches 8:8c31b3ba5371 81 for (int i = 0; i < 10; i++) {
switches 8:8c31b3ba5371 82 printf("\rIncrementing numbers (%d/%d)... ", i, 10);
switches 8:8c31b3ba5371 83 fflush(stdout);
switches 8:8c31b3ba5371 84
switches 8:8c31b3ba5371 85 // Get current stream position
switches 8:8c31b3ba5371 86 long pos = ftell(f);
switches 8:8c31b3ba5371 87
switches 8:8c31b3ba5371 88 // Parse out the number and increment
switches 8:8c31b3ba5371 89 int32_t number;
switches 8:8c31b3ba5371 90 fscanf(f, "%d", &number);
switches 8:8c31b3ba5371 91 number += 1;
switches 8:8c31b3ba5371 92
switches 8:8c31b3ba5371 93 // Seek to beginning of number
switches 8:8c31b3ba5371 94 fseek(f, pos, SEEK_SET);
switches 8:8c31b3ba5371 95
switches 8:8c31b3ba5371 96 // Store number
switches 8:8c31b3ba5371 97 fprintf(f, " %d\n", number);
switches 8:8c31b3ba5371 98 }
switches 8:8c31b3ba5371 99 printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
switches 8:8c31b3ba5371 100
switches 8:8c31b3ba5371 101 // Close the file which also flushes any cached writes
switches 8:8c31b3ba5371 102 printf("Closing \"/fs/numbers.txt\"... ");
switches 8:8c31b3ba5371 103 fflush(stdout);
switches 8:8c31b3ba5371 104 err = fclose(f);
switches 8:8c31b3ba5371 105 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 106
switches 8:8c31b3ba5371 107 // Display the root directory
switches 8:8c31b3ba5371 108 printf("Opening the root directory... ");
switches 8:8c31b3ba5371 109 fflush(stdout);
switches 8:8c31b3ba5371 110 DIR *d = opendir("/fs/");
switches 8:8c31b3ba5371 111 printf("%s\n", (!d ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 112
switches 8:8c31b3ba5371 113 printf("root directory:\n");
switches 0:60a522ae2e35 114 while (true) {
switches 8:8c31b3ba5371 115 struct dirent *e = readdir(d);
switches 8:8c31b3ba5371 116 if (!e) {
switches 8:8c31b3ba5371 117 break;
switches 8:8c31b3ba5371 118 }
switches 8:8c31b3ba5371 119 printf(" %s\n", e->d_name);
switches 8:8c31b3ba5371 120 }
switches 8:8c31b3ba5371 121
switches 8:8c31b3ba5371 122 printf("Closing the root directory... ");
switches 8:8c31b3ba5371 123 fflush(stdout);
switches 8:8c31b3ba5371 124 err = closedir(d);
switches 8:8c31b3ba5371 125 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 126
switches 8:8c31b3ba5371 127 // Display the numbers file
switches 8:8c31b3ba5371 128 printf("Opening \"/fs/numbers.txt\"... ");
switches 8:8c31b3ba5371 129 fflush(stdout);
switches 8:8c31b3ba5371 130 f = fopen("/fs/numbers.txt", "r");
switches 8:8c31b3ba5371 131 printf("%s\n", (!f ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 132
switches 8:8c31b3ba5371 133 printf("numbers:\n");
switches 8:8c31b3ba5371 134 while (!feof(f)) {
switches 8:8c31b3ba5371 135 int c = fgetc(f);
switches 8:8c31b3ba5371 136 printf("%c", c);
switches 8:8c31b3ba5371 137 }
switches 8:8c31b3ba5371 138
switches 8:8c31b3ba5371 139 printf("\rClosing \"/fs/numbers.txt\"... ");
switches 8:8c31b3ba5371 140 fflush(stdout);
switches 8:8c31b3ba5371 141 err = fclose(f);
switches 8:8c31b3ba5371 142 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 143
switches 8:8c31b3ba5371 144
switches 8:8c31b3ba5371 145
switches 8:8c31b3ba5371 146
switches 8:8c31b3ba5371 147 // Switch to MSD
switches 8:8c31b3ba5371 148 // printf("Unmounting... ");
switches 8:8c31b3ba5371 149 // fflush(stdout);
switches 8:8c31b3ba5371 150 // err = fs.unmount();
switches 8:8c31b3ba5371 151 // printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 152
switches 8:8c31b3ba5371 153
switches 8:8c31b3ba5371 154 printf("Starting MSD... ");
switches 8:8c31b3ba5371 155 msd.disk_initialize();
switches 8:8c31b3ba5371 156 err = msd.connect();
switches 8:8c31b3ba5371 157 bLED = LED_ON;
switches 8:8c31b3ba5371 158 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
switches 8:8c31b3ba5371 159
switches 8:8c31b3ba5371 160 while (true) {
switches 8:8c31b3ba5371 161 wait_ms(500);
switches 8:8c31b3ba5371 162 printf(".");
switches 1:464c8b3634dc 163 gLED = !gLED;
switches 0:60a522ae2e35 164 }
switches 0:60a522ae2e35 165 }
switches 0:60a522ae2e35 166