Maxim Integrated / Mbed OS FTHR_USBMSD_Demo

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.

Files at this revision

API Documentation at this revision

Comitter:
switches
Date:
Fri Feb 23 23:26:43 2018 +0000
Parent:
7:6e8c5f7d6d0e
Child:
9:6ada4f89915c
Commit message:
MAX32630FTHR USBMSD Block Device Example;

Changed in this revision

USBMSD_BD.lib Show annotated file Show diff for this revision Revisions of this file
USBMSD_SD.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBMSD_BD.lib	Fri Feb 23 23:26:43 2018 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/switches/code/USBMSD_SD/#6b2f29de40d3
--- a/USBMSD_SD.lib	Thu Oct 19 15:50:23 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/switches/code/USBMSD_SD/#cce1e689c548
--- a/main.cpp	Thu Oct 19 15:50:23 2017 +0000
+++ b/main.cpp	Fri Feb 23 23:26:43 2018 +0000
@@ -1,27 +1,166 @@
 #include "mbed.h"
 #include "max32630fthr.h"
-#include "USBMSD_SD.h"
+#include "USBMSD_BD.h"
+#include "HeapBlockDevice.h"
+#include "FATFileSystem.h"
+
+#define BLOCK_SIZE   512
 
 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
 
 DigitalOut rLED(LED1);
 DigitalOut gLED(LED2);
+DigitalOut bLED(LED3);
+
+// Physical block device, can be any device that supports the BlockDevice API
+HeapBlockDevice bd(512*BLOCK_SIZE, BLOCK_SIZE);
+
+// File system declaration
+FATFileSystem fs("fs");
+
+// USB MSD 
+USBMSD_BD msd(&bd, BLOCK_SIZE);  
+
 
 // main() runs in its own thread in the OS
 // (note the calls to Thread::wait below for delays)
 int main()
 {
+    printf("--- Mbed OS filesystem example ---\n");
+    rLED = LED_ON;
     gLED = LED_ON;
-    rLED = LED_ON;
+    bLED = LED_OFF;
 
     Thread::wait(100);
 
-    USBMSD_SD sd(P0_5, P0_6, P0_4, P0_7);  // mosi, miso, sclk, cs
+    // Try to mount the filesystem
+    printf("Mounting the filesystem... ");
+    fflush(stdout);
+    int err = fs.mount(&bd);
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        // Reformat if we can't mount the filesystem
+        // this should only happen on the first boot
+        printf("No filesystem found, formatting... ");
+        fflush(stdout);
+        err = fs.reformat(&bd);
+        printf("%s\n", (err ? "Fail :(" : "OK"));
+    }
+
     rLED = LED_OFF;
 
+    // Open the numbers file
+    printf("Opening \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    FILE *f = fopen("/fs/numbers.txt", "r+");
+    printf("%s\n", (!f ? "Fail :(" : "OK"));
+    if (!f) {
+        // Create the numbers file if it doesn't exist
+        printf("No file found, creating a new file... ");
+        fflush(stdout);
+        f = fopen("/fs/numbers.txt", "w+");
+        printf("%s\n", (!f ? "Fail :(" : "OK"));
+
+        for (int i = 0; i < 10; i++) {
+            printf("\rWriting numbers (%d/%d)... ", i, 10);
+            fflush(stdout);
+            err = fprintf(f, "    %d\n", i);
+            if (err < 0) {
+                printf("Fail :(\n");
+            }
+        }
+        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
+
+        printf("Seeking file... ");
+        fflush(stdout);
+        err = fseek(f, 0, SEEK_SET);
+        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+    }
+
+    // Go through and increment the numbers
+    for (int i = 0; i < 10; i++) {
+        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
+        fflush(stdout);
+
+        // Get current stream position
+        long pos = ftell(f);
+
+        // Parse out the number and increment
+        int32_t number;
+        fscanf(f, "%d", &number);
+        number += 1;
+
+        // Seek to beginning of number
+        fseek(f, pos, SEEK_SET);
+    
+        // Store number
+        fprintf(f, "    %d\n", number);
+    }
+    printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
+
+    // Close the file which also flushes any cached writes
+    printf("Closing \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    err = fclose(f);
+    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+
+    // Display the root directory
+    printf("Opening the root directory... ");
+    fflush(stdout);
+    DIR *d = opendir("/fs/");
+    printf("%s\n", (!d ? "Fail :(" : "OK"));
+
+    printf("root directory:\n");
     while (true) {
+        struct dirent *e = readdir(d);
+        if (!e) {
+            break;
+        }
+        printf("    %s\n", e->d_name);
+    }
+
+    printf("Closing the root directory... ");
+    fflush(stdout);
+    err = closedir(d);
+    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+
+    // Display the numbers file
+    printf("Opening \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    f = fopen("/fs/numbers.txt", "r");
+    printf("%s\n", (!f ? "Fail :(" : "OK"));
+
+    printf("numbers:\n");
+    while (!feof(f)) {
+        int c = fgetc(f);
+        printf("%c", c);
+    }
+
+    printf("\rClosing \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    err = fclose(f);
+    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+
+
+
+
+    // Switch to MSD
+//    printf("Unmounting... ");
+//    fflush(stdout);
+//    err = fs.unmount();
+//    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+    
+
+    printf("Starting MSD... ");
+    msd.disk_initialize();
+    err = msd.connect();
+    bLED = LED_ON;
+    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+
+    while (true) {
+        wait_ms(500);
+        printf(".");        
         gLED = !gLED;
-        Thread::wait(500);
     }
 }