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
diff -r e7767b779c8c -r 12621f1fcbbe FatFs_Mon/mon.cpp
--- a/FatFs_Mon/mon.cpp	Sat May 02 03:43:51 2020 +0000
+++ b/FatFs_Mon/mon.cpp	Thu Jan 14 00:40:36 2021 +0000
@@ -2,13 +2,14 @@
  * mbed Application program for the mbed
  *  FatFs Check program / monitor part
  *
- * Copyright (c) 2015,'18,'19,'20 Kenji Arai / JH1PJL
+ * Copyright (c) 2015,'18,'19,'20,'21 Kenji Arai / JH1PJL
  *  http://www7b.biglobe.ne.jp/~kenjia/
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    May        5th, 2015
  *      Revised:    June      14th, 2015
  *      Revised:    April      7th, 2018
- *      Revised:    May        2nd, 2020
+ *      Revised:    August     8th, 2020
+ *      Revised:    January   14th, 2021
  */
 
 /*
@@ -24,35 +25,30 @@
 
 //  Include --------------------------------------------------------------------
 #include "mbed.h"
-#if (MBED_MAJOR_VERSION == 2)
-#include "SDFileSystem.h"
-#elif (MBED_MAJOR_VERSION == 5)
 #include "FATFileSystem.h"
-#endif
 #include "ff.h"
 #include "ffconf.h"
 #include "diskio.h"
 #include "mon.h"
+#include "uart_as_stdio.h"
 
 //  Definition -----------------------------------------------------------------
 #define DO_DEBUG    0
 
 #if DO_DEBUG
-#define DEBUG_LINE  pc.printf("line:%d\r\n", __LINE__);
+#define DEBUG_LINE  printf("line:%d\r\n", __LINE__);
 #else
 #define DEBUG_LINE  {;}
 #endif
 
 // Com
 #if  1
-#define BAUD(x)     pc.baud(x)
-#define GETC(x)     pc.getc(x)
-#define PUTC(x)     pc.putc(x)
-#define PUTS(x)     pc.puts(x)
-#define PRINTF(...) pc.printf(__VA_ARGS__)
-#define READABLE(x) pc.readable(x)
+#define GETC(x)     getc(x)
+#define PUTC(x)     putc(x)
+#define PUTS(x)     puts(x)
+#define PRINTF(...) printf(__VA_ARGS__)
+#define READABLE(x) readable(x)
 #else
-#define BAUD(x)     {;}
 #define GETC(x)     {;}
 #define PUTC(x)     {;}
 #define PRINTF(...) {;}
@@ -119,7 +115,7 @@
     "x     extend commands mode\r\n"
     "q     Return to main\r\n"
     "t     Show current time or Adjust time\r\n"
-    "      e.g. t 20 5 2 10 11 12 -> May 02,'20, 10:11:12\r\n"
+    "      e.g. t 21 1 13 10 11 12 -> January 13,'21, 10:11:12\r\n"
     "?     Help/You know the command\r\n"
     "\r\n";
 
@@ -164,12 +160,8 @@
     "\r\n";
 
 //  Function prototypes --------------------------------------------------------
-#if (MBED_MAJOR_VERSION == 2)
-extern SDFileSystem fs;
-#elif (MBED_MAJOR_VERSION == 5)
 extern HeapBlockDevice bd;
 extern FATFileSystem fs;
-#endif
 
 static void extended_mon( char *ptr );
 static void v_next( char *ptr );
@@ -192,7 +184,6 @@
 void get_line (char *buff, int len);
 
 //  Object ---------------------------------------------------------------------
-extern  Serial pc;
 Timer   t;
 
 //------------------------------------------------------------------------------
@@ -214,23 +205,23 @@
         ptr = Linebuf;
         get_line( ptr, sizeof(Linebuf) );
         switch ( *ptr++ ) {
-                // vol
+            // vol
             case 'v' :
                 v_next(ptr);
                 break;
-                // dir
+            // dir
             case 'd' :
                 d_next(ptr);
                 break;
-                // cd, copy
+            // cd, copy
             case 'c' :
                 c_next(ptr);
                 break;
-                // mkdir
+            // mkdir
             case 'm' :
                 m_next(ptr);
                 break;
-                // ren
+            // ren
             case 'r' :
                 r_next(ptr);
                 break;
@@ -240,15 +231,15 @@
             case 'x' :
                 extended_mon(ptr);
                 break;
-                // Help
+            // Help
             case '?' :
                 PUTS(HelpMsg0);
                 break;
-                // Exit monitor (return to main())
+            // Exit monitor (return to main())
             case 'q' :
                 PUTS("Return to main\r\n");
                 return;
-                // Not a command
+            // Not a command
             default:
                 PUTS("? [HELP]=?");
                 crlf();
@@ -257,36 +248,30 @@
     }
 }
 
-uint32_t get_disk_freespace(void)
+float get_disk_freespace(void)
 {
-    long  p1;
+    DWORD  p1;
     UINT   s1, s2;
     FATFS  *fs;
     BYTE res;
 
-    if (Dirx == NULL){
+    if (Dirx == NULL) {
         Dirx = new FATFS_DIR;
     }
-    char p = NULL;
-    res = f_opendir(Dirx, &p);
+    //char p = NULL;
+    char p[8] = {0};
+    res = f_opendir(Dirx, p);
     if (res) {
         return 0;
     }
-    p1 = s1 = s2 = 0;
-    for(;;) {
-        res = f_readdir(Dirx, &Finfo);
-        if ((res != FR_OK) || !Finfo.fname[0]) break;
-        if (Finfo.fattrib & AM_DIR) {
-            s2++;
-        } else {
-            s1++;
-            p1 += Finfo.fsize;
-        }
-    }
-    res = f_getfree(&p, (DWORD*)&p1, &fs);
-    uint32_t size = p1 * fs->csize * 512;
+    p1 = 0;
+    p[0] = '0';
+    p[1] = ':';
+    p[2] = 0;
+    res = f_getfree(p, (DWORD*)&p1, &fs);
+    float size_float = (float)p1 * (float)fs->csize * (float)fs->ssize;
     if (res == FR_OK) {
-        return size;
+        return size_float;
     } else {
         return 0;
     }
@@ -296,8 +281,8 @@
 {
     BYTE res;
     //const char *file_name ="acc_data.txt";
- 
-    if (Dirx == NULL){
+
+    if (Dirx == NULL) {
         Dirx = new FATFS_DIR;
     }
     char p = NULL;
@@ -309,7 +294,7 @@
     for(;;) {
         res = f_readdir(Dirx, &Finfo);
         if ((res != FR_OK) || !Finfo.fname[0]) break;
-        if (strcmp(Finfo.fname, file_name) == 0){
+        if (strcmp(Finfo.fname, file_name) == 0) {
             return Finfo.fsize;
         }
     }
@@ -675,7 +660,7 @@
                 p2 += s2;
                 if (cnt != s2) break;
             }
-            tim = t.read_ms();
+            tim = t.elapsed_time().count();
             PRINTF("%lu bytes read with %lu kB/sec.\r\n",
                    p2, tim ? (p2 / tim) : 0);
             break;
@@ -702,7 +687,7 @@
                 p2 += s2;
                 if (cnt != s2) break;
             }
-            tim = t.read_ms();
+            tim = t.elapsed_time().count();
             PRINTF("%lu bytes written with %lu kB/sec.\r\n",
                    p2, tim ? (p2 / tim) : 0);
             break;
@@ -737,8 +722,8 @@
             break;
 #endif
 #if 0
-            /* ft <year> <month> <day> <hour> <min> <sec> <name>
-                                                 - Change timestamp of an object */
+        /* ft <year> <month> <day> <hour> <min> <sec> <name>
+                                             - Change timestamp of an object */
         case 't' :
             if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) {
                 break;
@@ -866,7 +851,7 @@
                 put_rc(f_mkfs("", (BYTE)p2, (DWORD)p3, Buff, sizeof Buff));
             break;
 #endif  /* FF_USE_MKFS */
-            /* fz [<size>] - Change/Show R/W length for fr/fw/fx command */
+        /* fz [<size>] - Change/Show R/W length for fr/fw/fx command */
         case 'z' :
             if (xatoi(&ptr, &p1) && p1 >= 1 && p1 <= (long)sizeof Buff)
                 blen = p1;
@@ -1098,29 +1083,29 @@
 
     if (xatoi(&ptr, &p1)) {
         t.tm_year       = (uint8_t)p1 + 100;
-        pc.printf("Year:%ld ",p1);
+        printf("Year:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_mon        = (uint8_t)p1 - 1;
-        pc.printf("Month:%ld ",p1);
+        printf("Month:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_mday       = (uint8_t)p1;
-        pc.printf("Day:%ld ",p1);
+        printf("Day:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_hour       = (uint8_t)p1;
-        pc.printf("Hour:%ld ",p1);
+        printf("Hour:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_min        = (uint8_t)p1;
-        pc.printf("Min:%ld ",p1);
+        printf("Min:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_sec        = (uint8_t)p1;
-        pc.printf("Sec: %ld \r\n",p1);
+        printf("Sec: %ld \r\n",p1);
         seconds = mktime(&t);
         set_time(seconds);
     } else {
         seconds = time(NULL);
     }
     strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds));
-    pc.printf("[Time] %s", buf);
+    printf("[Time] %s", buf);
 }
 
 //  Get key input data
@@ -1130,6 +1115,7 @@
     int idx = 0;
 
     for (;;) {
+        //DEBUG_LINE
         c = GETC();
         //    Added by Kenji Arai / JH1PJL   May 9th, 2010
         if (c == '\r') {