microSD Card control function for DISCO-F769NI based on BD_SD_DISCO_F746NG library by Roy Krikke

Dependencies:   BD_SD_DISCO_F769NI BSP_DISCO_F769NI

Fork of DISCO-F746NG_BLOCK_DEVICE_WITH_FAT_FILESYSTEM_ON_SDCARD by Roy Krikke

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

Revision:
8:12621f1fcbbe
Parent:
7:e7767b779c8c
--- a/main.cpp	Sat May 02 03:43:51 2020 +0000
+++ b/main.cpp	Thu Jan 14 00:40:36 2021 +0000
@@ -1,12 +1,12 @@
 /*
  * Mbed Application program
- *  SD Card file control function with FatFs on Mbed-os5
+ *  SD Card file control function with FatFs on Mbed-os6
  *
- * Copyright (c) 2018,'19,'20 Kenji Arai / JH1PJL
+ * Copyright (c) 2018,'19,'20,'21 Kenji Arai / JH1PJL
  *  http://www7b.biglobe.ne.jp/~kenjia/
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    April      7th, 2018
- *      Revised:    May        2nd, 2020
+ *      Revised:    January   14th, 2021
  */
 
 //  Include --------------------------------------------------------------------
@@ -14,15 +14,17 @@
 #include    "FATFileSystem.h"
 #include    "SDBlockDeviceDISCOF769NI.h"
 #include    "mon.h"
+#include    "uart_as_stdio.h"
 
 //  Definition -----------------------------------------------------------------
 #define     USER_SW_ON      1
-#define     LOOP_TIME       25      // 25mS
+//#define     LOOP_TIME       25      // 25mS
+#define     LOOP_TIME       150     // 150mS
 
 #define DEBUG  0
 
 #if DEBUG
-#define DBG(...)    pc.printf(__VA_ARGS__)
+#define DBG(...)    printf(__VA_ARGS__)
 #else
 #define DBG(...)    {;}
 #endif
@@ -30,7 +32,6 @@
 //  Constructor ----------------------------------------------------------------
 DigitalOut      led(LED1);
 DigitalIn       user_sw(USER_BUTTON);
-Serial          pc(USBTX, USBRX, 115200);
 // Instantiate the Block Device for sd card on DISCO-F769NI
 SDBlockDeviceDISCOF769NI bd;
 FATFileSystem   fs("fs");
@@ -40,7 +41,7 @@
 
 //  ROM / Constant data --------------------------------------------------------
 const char *const opening_msg0 = "microSD Card test program";
-const char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
+const char *const opening_msg1 = " -> run on Mbed OS-6\r\n";
 const char *const opening_msg2 = "microSD Card is ready for use\r\n";
 const char *const opening_msg3 = "Please hit any key to start!\r\n";
 
@@ -48,8 +49,6 @@
 void return_error (int ret_val);
 void errno_error (void* ret_val);
 extern void print_revision(void);
-extern uint32_t get_disk_freespace(void);
-extern uint32_t get_data_file_size(const char *const file_name);
 
 //------------------------------------------------------------------------------
 //  Control Program
@@ -64,13 +63,13 @@
     uint32_t data4 = 50000U;
     uint32_t data5 = 60000U;
 
-    pc.printf("\r\n\r\n");
+    printf("\r\n\r\n");
     print_revision();
     DBG("line:%d\r\n", __LINE__);
-    pc.printf("\r\nStart\r\n");
+    printf("\r\nStart\r\n");
     int error = 0;
-    pc.printf("Welcome to the filesystem example.\r\n");
-    pc.printf("Mounting the filesystem on \"/fs\". \r\n");
+    printf("Welcome to the filesystem example.\r\n");
+    printf("Mounting the filesystem on \"/fs\". \r\n");
     error = fs.mount(&bd);
     return_error(error);
 
@@ -78,51 +77,51 @@
     errno_error(fp);
     if (fp != 0) {
         DBG("line:%d\r\n", __LINE__);
-        pc.printf("%s%s",  opening_msg0, opening_msg1);
+        printf("%s%s",  opening_msg0, opening_msg1);
         fprintf(fp,"%s%s", opening_msg0, opening_msg1);
-        pc.printf("%s",  opening_msg2);
-        pc.printf("File: mydata.txt\r\n");
+        printf("%s",  opening_msg2);
+        printf("File: mydata.txt\r\n");
         fclose(fp);
     } else {
-        pc.printf("ERROR\r\n");
+        printf("ERROR\r\n");
     }
-    pc.printf("%s",  opening_msg3);
-    while (pc.readable() == 0) { ;}
-    char c = pc.getc(); // dummy read
+    printf("%s",  opening_msg3);
+    while (readable() == 0) { ;}
+    char c = getc(); // dummy read
     while (true) {
         DBG("line:%d\r\n", __LINE__);
         tmr.reset();
         tmr.start();
-        uint32_t size_disk = get_disk_freespace();
-        uint32_t size_file = get_data_file_size("mydata.txt");
-        pc.printf("free disk:%10u, file:%8u  ", size_disk, size_file);
+        float size_disk = get_disk_freespace() / (1024.0f * 1024.0f * 1024.0f);
+        uint32_t size_file = get_data_file_size("mydata.txt") / 1024;
+        printf(", free disk: %.3f GB, file: %u kB, ", size_disk, size_file);
         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("%s", tmp);
+            printf("%s", tmp);
             fprintf(fp, "%s", tmp);
-            pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n",
-                      ++data0, ++data1, ++data2, ++data3, ++data4, ++data5);
+            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);
             fclose(fp);
         } else {
-            pc.printf("ERROR\r\n");
+            printf("ERROR\r\n");
         }
-        uint32_t time_sd = tmr.read_us();
-        pc.printf("time[uS]:%6d ", time_sd);
-        time_sd /= 1000;    // change uS to ms
-        if (time_sd < LOOP_TIME -2){
-            ThisThread::sleep_for(LOOP_TIME - time_sd);
+        uint32_t time_sd = chrono::duration_cast<chrono::milliseconds>(
+                               tmr.elapsed_time()).count();
+        printf("time:%3d ", time_sd);
+        if (time_sd < LOOP_TIME -2) {
+            ThisThread::sleep_for(chrono::milliseconds(LOOP_TIME - time_sd));
         }
         if (user_sw == USER_SW_ON) {
             DBG("line:%d\r\n", __LINE__);
             break;
         }
-        if (pc.readable()) {
+        if (readable()) {
             mon();
         }
         led = !led;
@@ -136,13 +135,13 @@
 void return_error (int ret_val)
 {
     if (ret_val) {
-        pc.printf("retrun error/Failure. %d\r\n", ret_val);
+        printf("retrun error/Failure. %d\r\n", ret_val);
     }
 }
 
 void errno_error (void* ret_val)
 {
     if (ret_val == NULL) {
-        pc.printf("error #/Failure. %d \r\n", errno);
+        printf("error #/Failure. %d \r\n", errno);
     }
 }