Using SD cards with STM32F407VET6 boards.

Dependencies:   SDFileSystem mbed

Using SD cards with STM32F407VET6 black boards

To use the on-board micro SD card slot with this demo program connect the board pins one another with wires as indicated below.

Wiring
SPI pinsSDIO pins
PC_3<=>PD_2
PC_2<=>PC_8
PB_10<=>PC_12
PC_0<=>PC_11

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sat Apr 28 10:03:42 2018 +0000
Commit message:
Initial release.

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fad6683294f8 SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Sat Apr 28 10:03:42 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/Whitworth-EN173-Resources/code/SDFileSystem/#ee903cb278a6
diff -r 000000000000 -r fad6683294f8 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Apr 28 10:03:42 2018 +0000
@@ -0,0 +1,77 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "errno.h"
+
+SDFileSystem    fs(PC_3, PC_2, PB_10, PC_0, "sd");  // mosi, miso, sck, cs
+FILE*           fp;
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+int main()
+{
+    printf("Mounting file system...\r\n");
+
+    int err = fs.mount();
+    printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+    if (err)
+        return err;
+
+    // Open the file.
+    printf("Opening file '/sd/mytest/sdtest.txt'... ");
+    fp = fopen("/sd/mytest/sdtest.txt", "w+");
+    printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n"));
+
+    if (!fp)
+    {
+        // Check whether directory '/sd/mytest' exists.
+        printf("\r\nChecking directory '/sd/mytest'...\r\n");
+
+        struct stat info;
+        err = stat("/sd/mytest", &info);
+        if (err)
+        {
+            printf("Directory '/sd/mytest' does not exist.\r\n");
+            printf("Trying to create it...");
+            err = mkdir("/sd/mytest", 0777);
+            printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+            if (err)
+                return err;
+        }
+
+        // Create a new 'sdtest.txt' file.
+        printf("File not found, creating a new one...\r\n");
+        fp = fopen("/sd/mytest/sdtest.txt", "w+");
+        printf("%s\r\n", (!fp ? "Failed :(" : "OK"));
+        if (!fp)
+        {
+            error("error: %s (%d)\r\n", strerror(errno), -errno);
+            return errno;
+        }
+    }
+
+    for (int i = 0; i < 10; i++)
+    {
+        printf("Writing numbers (%d/%d)... ", i, 10);
+        err = fprintf(fp, "    %d\r\n", i);
+        if (err < 0)
+        {
+            printf("Fail :(\r\n");
+            error("error: %s (%d)\r\n", strerror(errno), -errno);
+        }
+        else
+            printf("OK\r\n");
+    }
+
+    printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10);
+    err = fclose(fp);
+    printf("Closing file '/sd/mytest/sdtest.txt'... ");
+    printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+    if (err)
+        return err;
+
+    return 0;
+}
diff -r 000000000000 -r fad6683294f8 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Apr 28 10:03:42 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/5aab5a7997ee
\ No newline at end of file