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:
Sat Feb 24 18:20:03 2018 +0000
Revision:
10:404433ce329d
Parent:
9:6ada4f89915c
Added SD card option for block device.

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