Kenji Arai / Mbed OS SD_Card_Control_on_OS6
Revision:
0:342895df3f8f
Child:
1:2fb39b6b3d96
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Apr 07 01:33:25 2018 +0000
@@ -0,0 +1,116 @@
+/*
+ * Mbed Application program
+ *  SD Card file control function with FatFs on Mbed-os5 or os2
+ *
+ * Copyright (c) 2018 Kenji Arai / JH1PJL
+ *  http://www.page.sannet.ne.jp/kenjia/index.html
+ *  https://os.mbed.com/users/kenjiArai/
+ *      Created:    April      4th, 2018
+ *      Revised:    April      7th, 2018
+ */
+
+//  Include --------------------------------------------------------------------
+#include    "mbed.h"
+#if (MBED_MAJOR_VERSION == 2)
+#include    "SDFileSystem.h"
+#elif (MBED_MAJOR_VERSION == 5)
+#include    "SDBlockDevice.h"
+#include    "FATFileSystem.h"
+#endif
+#include    "mon.h"
+#include    <stdlib.h>
+
+//  Definition -----------------------------------------------------------------
+#define     USER_SW_ON      0
+
+//  Constructor ----------------------------------------------------------------
+//DigitalOut      led(LED1);      // same as D13 (equal to SPI CLK) STM Nucleo
+DigitalIn       user_sw(USER_BUTTON);
+Serial          pc(USBTX, USBRX, 115200);
+#if (MBED_MAJOR_VERSION == 2)
+SDFileSystem    sd(D11, D12, D13, D10, "fs");  // do,di,clk,cs
+#elif (MBED_MAJOR_VERSION == 5)
+SDBlockDevice   sd(D11, D12, D13, D10, 8000000);
+FATFileSystem   fs("fs");
+#endif
+
+//  RAM ------------------------------------------------------------------------
+
+//  ROM / Constant data --------------------------------------------------------
+char *const opening_msg0 = "microSD Card test program";
+#if (MBED_MAJOR_VERSION == 2)
+char *const opening_msg1 = " -> run on Mbed Classic\r\n";
+#elif (MBED_MAJOR_VERSION == 5)
+char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
+#endif
+
+//  Function prototypes --------------------------------------------------------
+
+//------------------------------------------------------------------------------
+//  Control Program
+//------------------------------------------------------------------------------
+int main()
+{
+    time_t      seconds;
+    uint32_t data0 = 10000U;
+    uint32_t data1 = 20000U;
+    uint32_t data2 = 30000U;
+    uint32_t data3 = 40000U;
+    uint32_t data4 = 50000U;
+    uint32_t data5 = 60000U;
+
+    if (user_sw == USER_SW_ON) {
+        mon();
+    }
+    //pc.printf("line:%d\r\n", __LINE__);
+#if (MBED_MAJOR_VERSION == 5)
+    /* Init SD CARD reader */
+    sd.init();
+    fs.mount(&sd);
+#endif
+    FILE* fp = fopen("/fs/mydata.txt", "a");
+    if (fp != 0) {
+        pc.printf("%s%s",  opening_msg0, opening_msg1);
+        fprintf(fp,"%s%s", opening_msg0, opening_msg1);
+    } else {
+        pc.printf("ERROR\r\n");
+    }
+    fclose(fp);
+    while (pc.readable()) {
+        char c = pc.getc(); // dummy read
+    }
+    while (true) {
+        uint32_t size = get_disk_freespace();
+        pc.printf("free %u  ", size);
+        fp = fopen("/fs/mydata.txt", "a");
+        if(fp != 0) {
+            char tmp[64];
+            seconds = time(NULL);
+            strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
+            pc.printf(tmp);
+            fprintf(fp, "%s", tmp);
+            pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n",
+                      ++data0, ++data1, ++data2, ++data3, ++data4, ++data5);
+            fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n",
+                        data0,   data1,   data2,   data3,   data4,   data5);
+        } else {
+            pc.printf("ERROR\r\n");
+        }
+        fclose(fp);
+#if (MBED_MAJOR_VERSION == 2)
+        wait(0.1f);
+#elif (MBED_MAJOR_VERSION == 5)
+        Thread::wait(100);
+#endif
+        if (user_sw == USER_SW_ON) {
+            break;
+        }
+        if (pc.readable()) {
+            mon();
+        }
+    }
+    while(true) {
+        mon();
+        NVIC_SystemReset();
+    }
+}