NuMaker SD-File-System with SD mode

Files at this revision

API Documentation at this revision

Comitter:
ccli8
Date:
Wed Mar 15 11:52:57 2017 +0800
Parent:
0:2ec42a097d17
Child:
2:ee10b396e545
Commit message:
Support Nuvoton SD block device

Changed in this revision

NuMaker-mbed-SD-driver.lib Show annotated file Show diff for this revision Revisions of this file
README.md 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NuMaker-mbed-SD-driver.lib	Wed Mar 15 11:52:57 2017 +0800
@@ -0,0 +1,1 @@
+https://github.com/OpenNuvoton/NuMaker-mbed-SD-driver/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Mar 15 11:52:57 2017 +0800
@@ -0,0 +1,5 @@
+# Example for SD card backed file system
+
+This example is cloned from the [FAT file system example](https://github.com/armmbed/mbed-os-example-fat-filesystem) released by ARM mbed team and
+is modified to use the [SD block device](https://github.com/OpenNuvoton/NuMaker-mbed-SD-driver)
+running in SD bus mode rather than SPI bus mode to back the file system.
--- a/main.cpp	Tue Mar 14 06:34:01 2017 +0000
+++ b/main.cpp	Wed Mar 15 11:52:57 2017 +0800
@@ -1,7 +1,82 @@
 #include "mbed.h"
+#include "FATFileSystem.h"
+#include "NuSDBlockDevice.h"
+#include <stdio.h>
+#include <errno.h>
+
+NuSDBlockDevice bd;
+FATFileSystem fs("fs");
+
+void return_error(int ret_val){
+  if (ret_val)
+    printf("Failure. %d\r\n", ret_val);
+  else
+    printf("done.\r\n");
+}
+
+void errno_error(void* ret_val){
+  if (ret_val == NULL)
+    printf(" Failure. %d \r\n", errno);
+  else
+    printf(" done.\r\n");
+}
 
 int main() {
-    // TO BE CONTINUED
-    return 0;
+  int error = 0;
+  printf("Welcome to the filesystem example.\r\n"
+         "Formatting a FAT, SD-backed filesystem. ");
+  error = FATFileSystem::format(&bd);
+  return_error(error);
+
+  printf("Mounting the filesystem on \"/fs\". ");
+  error = fs.mount(&bd);
+  return_error(error);
+
+  printf("Opening a new file, numbers.txt.");
+  FILE* fd = fopen("/fs/numbers.txt", "w");
+  errno_error(fd);
+
+  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");
+
+  printf("Closing file.");
+  fclose(fd);
+  printf(" done.\r\n");
+
+  printf("Re-opening file read-only.");
+  fd = fopen("/fs/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");
+
+  printf("Closing file.");
+  fclose(fd);
+  printf(" done.\r\n");
+
+  printf("Opening root directory.");
+  DIR* dir = opendir("/fs/");
+  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]);
+  }
+
+  printf("Closeing root directory. ");
+  error = closedir(dir);
+  return_error(error);
+  printf("Filesystem Demo complete.\r\n");
+
+  while (true) {}
 }