library for IZU2022

Dependencies:   mbed mpu9250_i2c IM920 BMP180 GPS millis

Dependents:   IZU2022

Revision:
2:3f286265fade
Parent:
1:690378b5f48a
Child:
3:3ce322ac8193
--- a/main.cpp	Tue Nov 22 07:41:45 2016 +0000
+++ b/main.cpp	Tue Nov 22 08:46:10 2016 +0000
@@ -4,40 +4,37 @@
  * Library
  * SDFileSystem: https://developer.mbed.org/users/neilt6/code/SDFileSystem/ Revision:26
  * mbed: Revision: 124
+ * mbed-rtos: Revision: 117
  *
  * 2016.11.22 created
  *
  */
  
 #include "mbed.h"
+#include "rtos.h"
 #include "SDFileSystem.h"
 
-//SDFileSystem sd(D11, D12, D13, D10, "sd", NC, SDFileSystem::SWITCH_NONE, 25000000);   // mosi, miso, sclk, cs
-SDFileSystem sd(PC_12, PC_11, PC_10, PA_14, "sd", NC, SDFileSystem::SWITCH_NONE, 25000000); // SPI3
+SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk
 
-struct {
+typedef struct {
     uint8_t x;
     uint8_t y;
     uint8_t z;
-} data, rdata;
-    
-int main()
+} DataT;
+
+void writeSD(DataT* data)
 {
-    FileHandle* file;
-    
-    data.x = 0xff;
-    data.y = 0x55;
-    data.z = 0xaa;
+    SDFileSystem sd(PC_12, PC_11, PC_10, PA_14, "sd", NC, SDFileSystem::SWITCH_NONE, 25000000); // SPI3: mosi, miso, sclk, cs    
     
     //Mount the filesystem
     sd.mount();
     
     //Perform a write test
     printf("\nWriting binary data to SD card...");
-    file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
+    FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
     if (file != NULL)
     {
-        if (file->write(&data, sizeof(data)) != sizeof(data))
+        if (file->write(data, sizeof(*data)) != sizeof(*data))
         {
             error("write error!\n");
         }
@@ -55,12 +52,23 @@
         printf("failed to create file!\n");
     }
     
+    //Unmount the filesystem
+    sd.unmount();
+}
+
+void readSD(DataT* data)
+{
+    SDFileSystem sd(PC_12, PC_11, PC_10, PA_14, "sd", NC, SDFileSystem::SWITCH_NONE, 25000000); // SPI3: mosi, miso, sclk, cs    
+    
+    //Mount the filesystem
+    sd.mount();
+    
     //Perform a read test
     printf("\nReading binary data from SD card...");
-    file = sd.open("Test File.bin", O_RDONLY);
+    FileHandle* file = sd.open("Test File.bin", O_RDONLY);
     if (file != NULL)
     {
-        if (file->read(&rdata, sizeof(rdata)) != sizeof(rdata))
+        if (file->read(data, sizeof(*data)) != sizeof(*data))
         {
             error("read error!\n");
         }
@@ -70,7 +78,7 @@
         }
         else
         {
-            printf("done! x:%02x y:%02x z:%02x\n", rdata.x, rdata.y, rdata.z);
+            printf("done!\n");
         }
     } 
     else
@@ -80,4 +88,18 @@
     
     //Unmount the filesystem
     sd.unmount();
-}
\ No newline at end of file
+}
+        
+int main()
+{
+    DataT data, rdata;    
+    
+    data.x = 0xff;
+    data.y = 0x55;
+    data.z = 0xaa;
+    
+    writeSD(&data);
+    readSD(&rdata);
+    
+    printf("data: x:%02x y:%02x z:%02x\n", rdata.x, rdata.y, rdata.z);
+}