Simple SD File System test using mbed-os, FATFileSystem + sd-driver

Dependencies:  

Fork of mbed-os-example-fat-filesystem by mbed-os-examples

Files at this revision

API Documentation at this revision

Comitter:
loopsva
Date:
Wed Aug 02 21:47:39 2017 +0000
Parent:
8:e9792c7eec2c
Commit message:
Initial commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sd-driver.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Jul 28 00:30:17 2017 +0100
+++ b/main.cpp	Wed Aug 02 21:47:39 2017 +0000
@@ -1,82 +1,50 @@
 #include "mbed.h"
 #include "FATFileSystem.h"
-#include "HeapBlockDevice.h"
-#include <stdio.h>
-#include <errno.h>
+#include "SDBlockDevice.h"
+
+DigitalOut gpo(D0);
+DigitalOut led(LED_RED);
+
+RawSerial pc(USBTX, USBRX);
+SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
+FATFileSystem fs("sd", &sd);
+
+//--------------------------------------------------------------------------------------------------------------------------------------//
+// Initialize the file system
+
+#define FS_INIT_FILENAME "/sd/hello.txt"
 
-HeapBlockDevice bd(128 * 512, 512);
-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 initFS() {
+    pc.printf("Initializing the SDFileSystem...\r\n");
+    FILE* fhr = fopen(FS_INIT_FILENAME, "r");
+    if(fhr != NULL) {
+        fclose(fhr);
+        pc.printf("Found test file %s\r\n...", FS_INIT_FILENAME);
+    } else {
+        pc.printf("File: %s not found. Creating\r\n", FS_INIT_FILENAME);
+        FILE* fhw = fopen(FS_INIT_FILENAME, "w");
+        if(fhw != NULL) {
+            fwrite("Hello World!", 12, 1, fhw);
+            fprintf(fhw, "Hello World!\n");
+            fclose(fhw);
+        } else {
+            pc.printf("*** Cannot write file: %s\r\n", FS_INIT_FILENAME);
+        }
+    }
 }
 
-void errno_error(void* ret_val){
-  if (ret_val == NULL)
-    printf(" Failure. %d \r\n", errno);
-  else
-    printf(" done.\r\n");
-}
+//--------------------------------------------------------------------------------------------------------------------------------------//
+// main
 
 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);
-
-  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) {}
-}
-
+    pc.baud(230400);
+    pc.printf("\r\n\r\n--------------------------------------------------------------------------------\r\n");
+    pc.printf("FRDM-K64F + OS5 + FS");
+    pc.printf("   -> build: " __DATE__ " " __TIME__ "(UTC)  K Braun\n");
+    initFS();
+    while (true) {
+        gpo = !gpo; // toggle pin
+        led = !led; // toggle led
+        wait(0.2f);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd-driver.lib	Wed Aug 02 21:47:39 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/coverxit/code/sd-driver/#0df98d0fd2a5