SD card

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task680solution-mbed-os-FZ429ZI by University of Plymouth - Stages 1, 2 and 3

Files at this revision

API Documentation at this revision

Comitter:
rubbehducky
Date:
Sun Jan 07 19:41:09 2018 +0000
Parent:
4:958cfd74ef1c
Commit message:
SD read code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Dec 06 11:20:21 2017 +0000
+++ b/main.cpp	Sun Jan 07 19:41:09 2018 +0000
@@ -6,18 +6,20 @@
  #include "sample_hardware.hpp"
  
  //SD Card Object
- SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
+ SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+
+// DATA TYPE
 
- uint8_t block[512] = "Hello World!\n";
+
+
+
  int main()
 {
-    //POWER ON SELF TEST
-    post();
     
     printf("Initialise\n");
     //FileSystemLike(*sd);
 
-    // call the SDBlockDevice instance initialisation method.
+    // call the SDBlockDevice instance initialisation method. (not needed)
     if ( sd.init() != 0) {
         printf("Init failed \n");
         errorCode(FATAL);
@@ -26,28 +28,12 @@
     //Create a filing system for SD Card
     FATFileSystem fs("sd", &sd);
     
-    // *************
-    // Open to WRITE
-    // *************
-    printf("Write to a file\n");
-    FILE* fp = fopen("/sd/test.txt","a");
-    //Check file handle (stream)
-    if (fp == NULL) {
-        error("Could not open file for write\n");
-        errorCode(FATAL);
-    }
-    
-    //Put some text in the file...
-    fprintf(fp, "Welcome to ELEC350\n");
-    
-    //Close the file
-    fclose(fp);
-    
     // ************
     // Open to READ
     // ************
     printf("Read a file\n");
-    fp = fopen("/sd/test.txt","r");
+    FILE* fp = fopen("/sd/test.txt","r");
+    
     if (fp == NULL) {
         error("Could not open file for read\n");
         errorCode(FATAL);
@@ -56,7 +42,7 @@
     //Read back all strings
     char s1[64];
     while (fscanf(fp, "%s", s1) == 1) {
-        printf("READ BACK: %s\n", s1);
+        fgets(s1, sizeof(s1), fp);
     }
     //To read a whole line, use: fgets(s1, sizeof(s1), fp);