SD-Card Control Program / Using Micro-SD / based on SDCardTest Program (http://mbed.org/users/simon/programs/SDCardTest/gpdz4x/)

Dependencies:   mbed SDFileSystem

Please refer following my Notebook page.
/users/kenjiArai/notebook/sd-card-control-new/#

Revision:
4:fc36c8ec2966
Parent:
3:2134d3cb4e8e
Child:
5:e222c59853e2
--- a/main.cpp	Tue Jun 04 11:28:09 2019 +0000
+++ b/main.cpp	Sat Aug 17 03:28:33 2019 +0000
@@ -1,19 +1,17 @@
 /*
  * Mbed Application program
- *  SD Card file control function with FatFs on Mbed-os5 and/or os2
+ *  SD Card file control function with FatFs on Mbed-os5 or os2
  *
- * Copyright (c) 2018,19 Kenji Arai / JH1PJL
+ * Copyright (c) 2018,'19 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    April      4th, 2018
- *      Revised:    June       6th, 2019
- *
- *  os2 release version 165 / Feb. 20, 2019
- *  os5.12.4  / Jun. 2, 2019
- *  
- *  Tested board
- *      Nucleo-F446RE, Nucleo-F411RE, Nucleo-F401RE
- *      Nucleo-L476RG, Nucleo-L152RE
+ *      Revised:    August    17th, 2019
+ */
+
+/*
+    Tested board on OS5 & OS2:
+        Nucleo-F401RE, -F411RE, -F446RE, -L073RZ, -L152RE, -L476RG
  */
 
 //  Include --------------------------------------------------------------------
@@ -29,26 +27,38 @@
 
 //  Definition -----------------------------------------------------------------
 #define     USER_SW_ON      0
+#if (MBED_MAJOR_VERSION == 2)
+#define     LOOP_TIME       100     // 100mS
+#elif (MBED_MAJOR_VERSION == 5)
+#define     LOOP_TIME       50      // 50mS
+#endif
+
+#define DEBUG  0
+
+#if DEBUG
+#define DBG(...)    pc.printf(__VA_ARGS__)
+#else
+#define DBG(...)    {;}
+#endif
 
 //  Constructor ----------------------------------------------------------------
-//DigitalOut      led(LED1);      // same as D13 (equal to SPI CLK) STM Nucleo
+Serial          pc(USBTX, USBRX, 115200);
 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(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 8000000);
+#if (MBED_MAJOR_VERSION == 5)
+SDBlockDevice   sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 12000000);
+//SDBlockDevice   sd(D11, D12, D13, D10, 8000000);
 FATFileSystem   fs("fs");
 #endif
+Timer tmr;
 
 //  RAM ------------------------------------------------------------------------
 
 //  ROM / Constant data --------------------------------------------------------
-char *const opening_msg0 = "microSD Card test program";
+const 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";
+const char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
 #endif
 
 //  Function prototypes --------------------------------------------------------
@@ -66,16 +76,21 @@
     uint32_t data4 = 50000U;
     uint32_t data5 = 60000U;
 
-
-    pc.printf("Start SD Card control program\r\n");
-    //pc.printf("line:%d\r\n", __LINE__);
+    if (user_sw == USER_SW_ON) {
+        mon();
+    }
+    DBG("line:%d\r\n", __LINE__);
 #if (MBED_MAJOR_VERSION == 5)
     /* Init SD CARD reader */
     sd.init();
     fs.mount(&sd);
+#else
+    SDFileSystem    sd(D11, D12, D13, D10, "fs");  // do,di,clk,cs
+    wait_ms(100);
 #endif
     FILE* fp = fopen("/fs/mydata.txt", "a");
     if (fp != 0) {
+        DBG("line:%d\r\n", __LINE__);
         pc.printf("%s%s",  opening_msg0, opening_msg1);
         fprintf(fp,"%s%s", opening_msg0, opening_msg1);
     } else {
@@ -86,27 +101,37 @@
         char c = pc.getc(); // dummy read
     }
     while (true) {
+        DBG("line:%d\r\n", __LINE__);
+        tmr.reset();
+        tmr.start();
         uint32_t size = get_disk_freespace();
         pc.printf("free %u  ", size);
         fp = fopen("/fs/mydata.txt", "a");
         if(fp != 0) {
             char tmp[64];
+            DBG("line:%d\r\n", __LINE__);
             seconds = time(NULL);
             strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
-            pc.printf(tmp);
+            pc.printf("%s", 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);
+                        data0,   data1,   data2,   data3,   data4,   data5);
         } else {
             pc.printf("ERROR\r\n");
         }
         fclose(fp);
+        uint32_t time_sd = tmr.read_ms();
+        pc.printf("time:%d ", time_sd);
 #if (MBED_MAJOR_VERSION == 2)
-        wait(0.1f);
+        if (time_sd < LOOP_TIME -2){
+            wait_ms(LOOP_TIME - time_sd);
+        }
 #elif (MBED_MAJOR_VERSION == 5)
-        ThisThread::sleep_for(100);
+        if (time_sd < LOOP_TIME -2){
+            ThisThread::sleep_for(LOOP_TIME - time_sd);
+        }
 #endif
         if (user_sw == USER_SW_ON) {
             break;
@@ -117,6 +142,6 @@
     }
     while(true) {
         mon();
-        NVIC_SystemReset();
+        system_reset();
     }
 }