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:
Sat May 02 03:43:51 2020 +0000
Parent:
6:3c8b8e3cc0f8
Child:
8:12621f1fcbbe
Commit message:
updated os5.changed starting process(wait key input)

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
check_revision.h 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
--- a/FatFs_Mon/mon.cpp	Fri Dec 27 02:56:15 2019 +0000
+++ b/FatFs_Mon/mon.cpp	Sat May 02 03:43:51 2020 +0000
@@ -2,12 +2,13 @@
  * mbed Application program for the mbed
  *  FatFs Check program / monitor part
  *
- * Copyright (c) 2015,'18 Kenji Arai / JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
+ * Copyright (c) 2015,'18,'19,'20 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
  */
 
 /*
@@ -62,7 +63,6 @@
 
 // from ffconf.h
 #define _VOLUMES    1
-#define FF_USE_LFN  0
 
 #if !defined(FF_FS_RPATH)
 #define FF_FS_RPATH 0
@@ -101,16 +101,12 @@
 FIL File1, File2;           // File objects
 FATFS_DIR* Dirx;
 FILINFO Finfo;
-#if FF_USE_LFN
-//inside of FILINFO
-char Lfname[512];
-#endif
 DWORD AccSize;              // Work register for fs command
 WORD AccFiles, AccDirs;
 
 //  ROM / Constant data --------------------------------------------------------
-char *const monmsg0 = "Start monitor program for FatFs File System\r\n";
-char *const monmsg1 = " <Please press any key to start the monitor>";
+const char *const monmsg0 = "Start monitor program for FatFs File System\r\n";
+const char *const monmsg1 = " <Please press any key to start the monitor>";
 
 static const char HelpMsg0[] =
     "dir   <full_pass>\r\n"
@@ -123,7 +119,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 18 3 28 14 48 20 -> 2018-03-28 14:48:20\r\n"
+    "      e.g. t 20 5 2 10 11 12 -> May 02,'20, 10:11:12\r\n"
     "?     Help/You know the command\r\n"
     "\r\n";
 
@@ -212,11 +208,6 @@
     PUTS(monmsg0);
     PUTS(monmsg1);
     crlf();
-#if FF_USE_LFN
-    // no needs because FILINFO structure is changed
-    Finfo.lfname = Lfname;
-    Finfo.lfsize = sizeof Lfname;
-#endif
     for (;;) {
         DEBUG_LINE
         PUTC('>');
@@ -301,6 +292,30 @@
     }
 }
 
+uint32_t get_data_file_size(const char *const file_name)
+{
+    BYTE res;
+    //const char *file_name ="acc_data.txt";
+ 
+    if (Dirx == NULL){
+        Dirx = new FATFS_DIR;
+    }
+    char p = NULL;
+    res = f_opendir(Dirx, &p);
+    if (res) {
+        put_rc((FRESULT)res);
+        return 0;
+    }
+    for(;;) {
+        res = f_readdir(Dirx, &Finfo);
+        if ((res != FR_OK) || !Finfo.fname[0]) break;
+        if (strcmp(Finfo.fname, file_name) == 0){
+            return Finfo.fsize;
+        }
+    }
+    return 0;
+}
+
 static void extended_mon( char *ptr )
 {
     PUTS(HelpMsg1);
@@ -446,16 +461,7 @@
             if (FF_FS_RPATH && Finfo.fname[0] == '.') {
                 continue;
             }
-#if FF_USE_LFN
-            //fn = *Finfo.lfname ? Finfo.lfname : Finfo.fname;
-            if (Finfo.altname[0] == 0) {
-                fn = Finfo.fname;
-            } else {
-                fn = Finfo.altname;
-            }
-#else
             fn = Finfo.fname;
-#endif
             if (Finfo.fattrib & AM_DIR) {
                 AccDirs++;
                 *(path+i) = '/';
@@ -931,13 +937,13 @@
             for (;;) {  /* 2nd parameter is not given (interactive mode) */
                 switch (p3) {
                     case DW_LONG:
-                        PRINTF("%08X 0x%08X-", p1, *(DWORD*)p1);
+                        PRINTF("%08X 0x%08X-", (unsigned int)p1, *(unsigned int*)p1);
                         break;
                     case DW_SHORT:
-                        PRINTF("%08X 0x%04X-", p1, *(WORD*)p1);
+                        PRINTF("%08X 0x%04X-", (unsigned int)p1, *(WORD*)p1);
                         break;
                     default:
-                        PRINTF("%08X 0x%02X-", p1, *(BYTE*)p1);
+                        PRINTF("%08X 0x%02X-", (unsigned int)p1, *(BYTE*)p1);
                 }
                 ptr = Linebuf;
                 get_line(ptr, sizeof Linebuf);
@@ -1092,22 +1098,22 @@
 
     if (xatoi(&ptr, &p1)) {
         t.tm_year       = (uint8_t)p1 + 100;
-        pc.printf("Year:%d ",p1);
+        pc.printf("Year:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_mon        = (uint8_t)p1 - 1;
-        pc.printf("Month:%d ",p1);
+        pc.printf("Month:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_mday       = (uint8_t)p1;
-        pc.printf("Day:%d ",p1);
+        pc.printf("Day:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_hour       = (uint8_t)p1;
-        pc.printf("Hour:%d ",p1);
+        pc.printf("Hour:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_min        = (uint8_t)p1;
-        pc.printf("Min:%d ",p1);
+        pc.printf("Min:%ld ",p1);
         xatoi( &ptr, &p1 );
         t.tm_sec        = (uint8_t)p1;
-        pc.printf("Sec: %d \r\n",p1);
+        pc.printf("Sec: %ld \r\n",p1);
         seconds = mktime(&t);
         set_time(seconds);
     } else {
--- a/FatFs_Mon/mon.h	Fri Dec 27 02:56:15 2019 +0000
+++ b/FatFs_Mon/mon.h	Sat May 02 03:43:51 2020 +0000
@@ -2,14 +2,15 @@
  * mbed Application program for the mbed
  *  FatFs Check program /monitor part
  *
- * Copyright (c) 2015,'18 Kenji Arai / JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
+ * Copyright (c) 2015,'18,'19,'20 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
  */
 
 //  Function prototypes --------------------------------------------------------
 void mon(void);
 uint32_t get_disk_freespace(void);
+uint32_t get_data_file_size(const char *const file_name);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_revision.cpp	Sat May 02 03:43:51 2020 +0000
@@ -0,0 +1,28 @@
+/*
+ * Check Mbed revision
+ *
+ * Copyright (c) 2019,'20 Kenji Arai / JH1PJL
+ *  http://www7b.biglobe.ne.jp/~kenjia/
+ *  https://os.mbed.com/users/kenjiArai/
+ *      Created:    July      17th, 2019
+ *      Revised:    May        2nd, 2020
+ */
+
+#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)
+#else
+    //#warning "Please use mbed-os-5.15.3"
+    #error "Please use mbed-os-5.15.3"
+#endif
+
+void print_revision(void)
+{
+    printf("MBED_MAJOR_VERSION = %d, ", MBED_MAJOR_VERSION);
+    printf("MINOR = %d, ", MBED_MINOR_VERSION);
+    printf("PATCH = %d\r\n", MBED_PATCH_VERSION);
+}
--- a/check_revision.h	Fri Dec 27 02:56:15 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-/*
- * Check Mbed revision
- *
- * Copyright (c) 2019 Kenji Arai / JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
- *  https://os.mbed.com/users/kenjiArai/
- *      Created:    July      17th, 2019
- *      Revised:    July      25th, 2019
- */
-
-//    RUN ONLY ON mbed-os5.13.0
-#if (MBED_MAJOR_VERSION == 5) &&\
-    (MBED_MINOR_VERSION == 13) &&\
-    (MBED_PATCH_VERSION == 0)
-    #if defined(TARGET_STM32F769NI)
-        // correct mbed-os version
-    #else
-        #error "DISCO-F769NI"
-    #endif
-#else
-    //#warning "Please use Mbed-os5.13.0"
-    #error "Please use Mbed-os5.13.0"
-#endif
--- a/main.cpp	Fri Dec 27 02:56:15 2019 +0000
+++ b/main.cpp	Sat May 02 03:43:51 2020 +0000
@@ -2,11 +2,11 @@
  * Mbed Application program
  *  SD Card file control function with FatFs on Mbed-os5
  *
- * Copyright (c) 2018,'19 Kenji Arai / JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
+ * Copyright (c) 2018,'19,'20 Kenji Arai / JH1PJL
+ *  http://www7b.biglobe.ne.jp/~kenjia/
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    April      7th, 2018
- *      Revised:    December  27th, 2019
+ *      Revised:    May        2nd, 2020
  */
 
 //  Include --------------------------------------------------------------------
@@ -14,13 +14,18 @@
 #include    "FATFileSystem.h"
 #include    "SDBlockDeviceDISCOF769NI.h"
 #include    "mon.h"
-#include    <stdlib.h>
-#include    <stdio.h>
-#include    <errno.h>
-#include    "check_revision.h"
 
 //  Definition -----------------------------------------------------------------
 #define     USER_SW_ON      1
+#define     LOOP_TIME       25      // 25mS
+
+#define DEBUG  0
+
+#if DEBUG
+#define DBG(...)    pc.printf(__VA_ARGS__)
+#else
+#define DBG(...)    {;}
+#endif
 
 //  Constructor ----------------------------------------------------------------
 DigitalOut      led(LED1);
@@ -29,16 +34,22 @@
 // Instantiate the Block Device for sd card on DISCO-F769NI
 SDBlockDeviceDISCOF769NI bd;
 FATFileSystem   fs("fs");
+Timer           tmr;
 
 //  RAM ------------------------------------------------------------------------
 
 //  ROM / Constant data --------------------------------------------------------
-const char *opening_msg0 = "microSD Card test program";
-const char *opening_msg1 = " -> run on Mbed OS-5\r\n";
+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_msg2 = "microSD Card is ready for use\r\n";
+const char *const opening_msg3 = "Please hit any key to start!\r\n";
 
 //  Function prototypes --------------------------------------------------------
 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
@@ -53,6 +64,9 @@
     uint32_t data4 = 50000U;
     uint32_t data5 = 60000U;
 
+    pc.printf("\r\n\r\n");
+    print_revision();
+    DBG("line:%d\r\n", __LINE__);
     pc.printf("\r\nStart\r\n");
     int error = 0;
     pc.printf("Welcome to the filesystem example.\r\n");
@@ -63,26 +77,29 @@
     FILE* fp = fopen("/fs/mydata.txt", "a");
     errno_error(fp);
     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);
+        pc.printf("%s",  opening_msg2);
         pc.printf("File: mydata.txt\r\n");
         fclose(fp);
     } else {
         pc.printf("ERROR\r\n");
     }
-    while (pc.readable()) {
-        char c = pc.getc(); // dummy read
-    }
+    pc.printf("%s",  opening_msg3);
+    while (pc.readable() == 0) { ;}
+    char c = pc.getc(); // dummy read
     while (true) {
-        uint32_t size = get_disk_freespace();
-        pc.printf("free %u  ", size);
+        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);
         fp = fopen("/fs/mydata.txt", "a");
-        /*if (size <= DISK_SIZE_LIMIT) {
-            pc.printf("Reached RAM Disk size limitation!!\r\n");
-            break;
-        }*/
         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);
@@ -95,8 +112,14 @@
         } else {
             pc.printf("ERROR\r\n");
         }
-        ThisThread::sleep_for(100);
+        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);
+        }
         if (user_sw == USER_SW_ON) {
+            DBG("line:%d\r\n", __LINE__);
             break;
         }
         if (pc.readable()) {
--- a/mbed-os.lib	Fri Dec 27 02:56:15 2019 +0000
+++ b/mbed-os.lib	Sat May 02 03:43:51 2020 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#7482462434d5cf718177653ef797547a976a7c5e
+https://github.com/ARMmbed/mbed-os/#b6370b4c37f3d4665ed1cdcb1afea85396bba1b3