SD+FAT FS

Files at this revision

API Documentation at this revision

Comitter:
zhangyx
Date:
Sun Nov 25 10:06:32 2018 +0000
Parent:
15:9b74f9eb0a51
Commit message:
demo code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r 9b74f9eb0a51 -r 8051d8156c91 main.cpp
--- a/main.cpp	Thu May 10 05:16:42 2018 +0100
+++ b/main.cpp	Sun Nov 25 10:06:32 2018 +0000
@@ -1,82 +1,80 @@
 #include "mbed.h"
 #include "FATFileSystem.h"
-#include "HeapBlockDevice.h"
+#include "SDBlockDevice.h"
 #include <stdio.h>
 #include <errno.h>
+/* mbed_retarget.h is included after errno.h so symbols are mapped to
+ * consistent values for all toolchains */
+#include "platform/mbed_retarget.h"
 
-HeapBlockDevice bd(128 * 512, 512);
-FATFileSystem fs("fs");
+
+Serial pc(PA_9, PA_10);
+SDBlockDevice sd(PB_15, PB_14, PB_13, PB_12);
+FATFileSystem fs("sd", &sd);
 
 void return_error(int ret_val){
   if (ret_val)
-    printf("Failure. %d\r\n", ret_val);
+    pc.printf("Failure. %d\n", ret_val);
   else
-    printf("done.\r\n");
+    pc.printf("done.\n");
 }
 
 void errno_error(void* ret_val){
   if (ret_val == NULL)
-    printf(" Failure. %d \r\n", errno);
+    pc.printf(" Failure. %d \n", errno);
   else
-    printf(" done.\r\n");
+    pc.printf(" done.\n");
 }
 
-int main() {
-  int error = 0;
-  printf("Welcome to the filesystem example.\r\n"
-         "Formatting a FAT, RAM-backed filesystem. ");
-  error = FATFileSystem::format(&bd);
-  return_error(error);
+int main()
+{
+    int error = 0;
+    pc.printf("Welcome to the filesystem example.\n");
 
-  printf("Mounting the filesystem on \"/fs\". ");
-  error = fs.mount(&bd);
-  return_error(error);
+    pc.printf("Opening a new file, numbers.txt.");
+    FILE* fd = fopen("/sd/numbers.txt", "w+");
+    errno_error(fd);
 
-  printf("Opening a new file, numbers.txt.");
-  FILE* fd = fopen("/fs/numbers.txt", "w");
-  errno_error(fd);
+    for (int i = 0; i < 20; i++){
+        pc.printf("Writing decimal numbers to a file (%d/20)\r", i);
+        fprintf(fd, "%d\n", i);
+    }
+    pc.printf("Writing decimal numbers to a file (20/20) done.\n");
 
-  for (int i = 0; i < 20; i++){
-    printf("Writing decimal numbers to a file (%d/20)\r", i);
-    fprintf(fd, "%d\r\n", i);
-  }
-  printf("Writing decimal numbers to a file (20/20) done.\r\n");
+    pc.printf("Closing file.");
+    fclose(fd);
+    pc.printf(" done.\n");
 
-  printf("Closing file.");
-  fclose(fd);
-  printf(" done.\r\n");
-
-  printf("Re-opening file read-only.");
-  fd = fopen("/fs/numbers.txt", "r");
-  errno_error(fd);
+    pc.printf("Re-opening file read-only.");
+    fd = fopen("/sd/numbers.txt", "r");
+    errno_error(fd);
 
-  printf("Dumping file to screen.\r\n");
-  char buff[16] = {0};
-  while (!feof(fd)){
-    int size = fread(&buff[0], 1, 15, fd);
-    fwrite(&buff[0], 1, size, stdout);
-  }
-  printf("EOF.\r\n");
+    pc.printf("Dumping file to screen.\n");
+    char buff[16] = {0};
+    while (!feof(fd)){
+        int size = fread(&buff[0], 1, 15, fd);
+        fwrite(&buff[0], 1, size, stdout);
+    }
+    pc.printf("EOF.\n");
 
-  printf("Closing file.");
-  fclose(fd);
-  printf(" done.\r\n");
+    pc.printf("Closing file.");
+    fclose(fd);
+    pc.printf(" done.\n");
 
-  printf("Opening root directory.");
-  DIR* dir = opendir("/fs/");
-  errno_error(fd);
+    pc.printf("Opening root directory.");
+    DIR* dir = opendir("/sd/");
+    errno_error(fd);
 
-  struct dirent* de;
-  printf("Printing all filenames:\r\n");
-  while((de = readdir(dir)) != NULL){
-    printf("  %s\r\n", &(de->d_name)[0]);
-  }
+    struct dirent* de;
+    pc.printf("Printing all filenames:\n");
+    while((de = readdir(dir)) != NULL){
+        pc.printf("  %s\n", &(de->d_name)[0]);
+    }
 
-  printf("Closing root directory. ");
-  error = closedir(dir);
-  return_error(error);
-  printf("Filesystem Demo complete.\r\n");
+    pc.printf("Closeing root directory. ");
+    error = closedir(dir);
+    return_error(error);
+    pc.printf("Filesystem Demo complete.\n");
 
-  while (true) {}
+    while (true) {}
 }
-
diff -r 9b74f9eb0a51 -r 8051d8156c91 mbed-os.lib
--- a/mbed-os.lib	Thu May 10 05:16:42 2018 +0100
+++ b/mbed-os.lib	Sun Nov 25 10:06:32 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#ae6c7c60f91c89cbf755a2f3c8ec9c66635849fd
+https://github.com/ARMmbed/mbed-os/#2fd0c5cfbd83fce62da6308f9d64c0ab64e1f0d6
diff -r 9b74f9eb0a51 -r 8051d8156c91 mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Sun Nov 25 10:06:32 2018 +0000
@@ -0,0 +1,8 @@
+{
+    "target_overrides": {
+        "*": {
+            "target.components_add": ["SD"],
+            "platform.stdio-convert-newlines": true
+        }
+    }
+}