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/#

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Thu Jan 14 00:40:36 2021 +0000
Parent:
7:e7767b779c8c
Commit message:
run on mbed-os6.6.0

Changed in this revision

FatFs_Mon/mon.cpp Show annotated file Show diff for this revision Revisions of this file
FatFs_Mon/mon.h Show annotated file Show diff for this revision Revisions of this file
check_revision.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
uart_as_stdio/uart_as_stdio.cpp Show annotated file Show diff for this revision Revisions of this file
uart_as_stdio/uart_as_stdio.h Show annotated file Show diff for this revision Revisions of this file
--- 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') {
--- a/FatFs_Mon/mon.h	Sat May 02 03:43:51 2020 +0000
+++ b/FatFs_Mon/mon.h	Thu Jan 14 00:40:36 2021 +0000
@@ -2,15 +2,16 @@
  * 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:    May        2nd, 2020
+ *      Revised:    August     8th, 2020
+ *      Revised:    January   14th, 2021
  */
 
 //  Function prototypes --------------------------------------------------------
 void mon(void);
-uint32_t get_disk_freespace(void);
+float get_disk_freespace(void);
 uint32_t get_data_file_size(const char *const file_name);
--- a/check_revision.cpp	Sat May 02 03:43:51 2020 +0000
+++ b/check_revision.cpp	Thu Jan 14 00:40:36 2021 +0000
@@ -1,23 +1,22 @@
 /*
  * Check Mbed revision
  *
- * Copyright (c) 2019,'20 Kenji Arai / JH1PJL
+ * Copyright (c) 2019,'20,'21 Kenji Arai / JH1PJL
  *  http://www7b.biglobe.ne.jp/~kenjia/
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    July      17th, 2019
- *      Revised:    May        2nd, 2020
+ *      Revised:    January   11th, 2021
  */
 
 #include "mbed.h"
 
-//    RUN ONLY ON mbed-os-5.15.3
-//      https://github.com/ARMmbed/mbed-os/releases/tag/mbed-os-5.15.3
-#if (MBED_MAJOR_VERSION == 5) &&\
-    (MBED_MINOR_VERSION == 15) &&\
-    (MBED_PATCH_VERSION == 3)
+//    RUN ONLY ON mbed-os-6.6.0
+//      https://github.com/ARMmbed/mbed-os/releases/tag/mbed-os-6.6.0
+#if (MBED_MAJOR_VERSION == 6) &&\
+    (MBED_MINOR_VERSION == 6) &&\
+    (MBED_PATCH_VERSION == 0)
 #else
-    //#warning "Please use mbed-os-5.15.3"
-    #error "Please use mbed-os-5.15.3"
+#   error "Please use mbed-os-6.6.0"
 #endif
 
 void print_revision(void)
--- 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);
     }
 }
--- a/mbed-os.lib	Sat May 02 03:43:51 2020 +0000
+++ b/mbed-os.lib	Thu Jan 14 00:40:36 2021 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#b6370b4c37f3d4665ed1cdcb1afea85396bba1b3
+https://github.com/ARMmbed/mbed-os/#84d991342a41011fc417aab62c0f5a8832f1d18f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Jan 14 00:40:36 2021 +0000
@@ -0,0 +1,10 @@
+{
+    "target_overrides": {
+        "*": {
+            "target.printf_lib": "std",
+            "target.features_add": ["STORAGE"],
+            "target.components_add": ["SD"],
+            "sd.INIT_FREQUENCY": 400000
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart_as_stdio/uart_as_stdio.cpp	Thu Jan 14 00:40:36 2021 +0000
@@ -0,0 +1,46 @@
+/*
+ * mbed Application program
+ *      Redirect Standard Input/Output
+ *
+ * Copyright (c) 2021 Kenji Arai / JH1PJL
+ *  http://www7b.biglobe.ne.jp/~kenjia/
+ *  https://os.mbed.com/users/kenjiArai/
+ *      Created:    January   13th, 2021
+ *      Revised:    January   14th, 2021
+ */
+
+//  Include --------------------------------------------------------------------
+#include "mbed.h"
+
+//  Definition -----------------------------------------------------------------
+
+//  Constructor ----------------------------------------------------------------
+static BufferedSerial pc(USBTX, USBRX, 115200);
+
+//  RAM ------------------------------------------------------------------------
+
+//  ROM / Constant data --------------------------------------------------------
+
+//  Function prototypes --------------------------------------------------------
+
+//------------------------------------------------------------------------------
+//  Control Program
+//------------------------------------------------------------------------------
+uint8_t readable()
+{
+    return pc.readable();
+}
+
+void putc(uint8_t c)
+{
+    char dt[4];
+    dt[0] = (char)c;
+    pc.write(dt, 1);
+}
+
+uint8_t getc()
+{
+    char dt[4];
+    pc.read(dt, 1);
+    return (uint8_t)dt[0];
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart_as_stdio/uart_as_stdio.h	Thu Jan 14 00:40:36 2021 +0000
@@ -0,0 +1,17 @@
+/*
+ * mbed Application program
+ *      Redirect Standard Input/Output
+ *
+ * Copyright (c) 2021 Kenji Arai / JH1PJL
+ *  http://www7b.biglobe.ne.jp/~kenjia/
+ *  https://os.mbed.com/users/kenjiArai/
+ *      Created:    January   13th, 2021
+ *      Revised:    January   14th, 2021
+ */
+
+#include "mbed.h"
+
+//  Function prototypes --------------------------------------------------------
+uint8_t readable(void);
+void putc(uint8_t c);
+uint8_t getc(void);