run on mbed-os6.6.0

NumSD Card pinModule pinPin NamePort NameSPI
1CD/DAT3CSD10PB6-
2CMD/DISDID11PA_7MOSI
3VSS1GNDGND--
4VDD3.3V3V3--
5CLKCLKD13PA_5SCLK
6VSS2GNDGND--
7DAT0/DOSDOD12PA_6MISO
8DAT1----
9DAT2----

電源+/-を含め合計6本の結線で接続します。
Nucleoシリーズは、ボード上のLED(プログラムでLED1)がD13に接続されSPIモード設定時にSCLKとなる構成が多いので、LED1制御を止める必要があります。

For DISCO-F769NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F769NI_BD_SD_Card_Control/
For DISCO-F469NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F469NI_BD_SD_Card_Control/

Revision:
7:29fece78d927
Parent:
6:7f8bc932cede
--- a/FatFs_Mon/mon.cpp	Sat May 02 03:44:56 2020 +0000
+++ b/FatFs_Mon/mon.cpp	Thu Jan 14 00:07:07 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') {