SD-Card Control Program / Using Micro-SD / based on SDCardTest Program (http://mbed.org/users/simon/programs/SDCardTest/gpdz4x/)

Dependencies:   mbed SDFileSystem

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 Aug 17 03:28:33 2019 +0000
Parent:
3:2134d3cb4e8e
Child:
5:e222c59853e2
Commit message:
modified main.cpp. added check_revision.cpp. run on latest revision.

Changed in this revision

FatFs_Mon/mon.cpp 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
--- a/FatFs_Mon/mon.cpp	Tue Jun 04 11:28:09 2019 +0000
+++ b/FatFs_Mon/mon.cpp	Sat Aug 17 03:28:33 2019 +0000
@@ -2,12 +2,13 @@
  * mbed Application program for the mbed
  *  FatFs Check program / monitor part
  *
- * Copyright (c) 2015,'18 Kenji Arai / JH1PJL
+ * Copyright (c) 2015,'18,'19 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    May        5th, 2015
  *      Revised:    June      14th, 2015
  *      Revised:    April      7th, 2018
+ *      Revised:    August    17th, 2019
  */
 
 /*
@@ -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"
@@ -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('>');
@@ -446,16 +437,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 +913,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 +1074,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 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_revision.cpp	Sat Aug 17 03:28:33 2019 +0000
@@ -0,0 +1,20 @@
+/*
+ * 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:    August    17th, 2019
+ */
+
+#include "mbed.h"
+
+//    RUN ONLY ON mbed 2.0.165
+#if (MBED_MAJOR_VERSION == 2) &&\
+    (MBED_MINOR_VERSION == 0) &&\
+    (MBED_PATCH_VERSION == 165)
+#else
+    //#warning "Please use mbed 2.0.165"
+    #error "Please use mbed 2.0.165"
+#endif
\ No newline at end of file
--- a/main.cpp	Tue Jun 04 11:28:09 2019 +0000
+++ b/main.cpp	Sat Aug 17 03:28:33 2019 +0000
@@ -1,19 +1,17 @@
 /*
  * Mbed Application program
- *  SD Card file control function with FatFs on Mbed-os5 and/or os2
+ *  SD Card file control function with FatFs on Mbed-os5 or os2
  *
- * Copyright (c) 2018,19 Kenji Arai / JH1PJL
+ * Copyright (c) 2018,'19 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  https://os.mbed.com/users/kenjiArai/
  *      Created:    April      4th, 2018
- *      Revised:    June       6th, 2019
- *
- *  os2 release version 165 / Feb. 20, 2019
- *  os5.12.4  / Jun. 2, 2019
- *  
- *  Tested board
- *      Nucleo-F446RE, Nucleo-F411RE, Nucleo-F401RE
- *      Nucleo-L476RG, Nucleo-L152RE
+ *      Revised:    August    17th, 2019
+ */
+
+/*
+    Tested board on OS5 & OS2:
+        Nucleo-F401RE, -F411RE, -F446RE, -L073RZ, -L152RE, -L476RG
  */
 
 //  Include --------------------------------------------------------------------
@@ -29,26 +27,38 @@
 
 //  Definition -----------------------------------------------------------------
 #define     USER_SW_ON      0
+#if (MBED_MAJOR_VERSION == 2)
+#define     LOOP_TIME       100     // 100mS
+#elif (MBED_MAJOR_VERSION == 5)
+#define     LOOP_TIME       50      // 50mS
+#endif
+
+#define DEBUG  0
+
+#if DEBUG
+#define DBG(...)    pc.printf(__VA_ARGS__)
+#else
+#define DBG(...)    {;}
+#endif
 
 //  Constructor ----------------------------------------------------------------
-//DigitalOut      led(LED1);      // same as D13 (equal to SPI CLK) STM Nucleo
+Serial          pc(USBTX, USBRX, 115200);
 DigitalIn       user_sw(USER_BUTTON);
-Serial          pc(USBTX, USBRX, 115200);
-#if (MBED_MAJOR_VERSION == 2)
-SDFileSystem    sd(D11, D12, D13, D10, "fs");  // do,di,clk,cs
-#elif (MBED_MAJOR_VERSION == 5)
-SDBlockDevice   sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 8000000);
+#if (MBED_MAJOR_VERSION == 5)
+SDBlockDevice   sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 12000000);
+//SDBlockDevice   sd(D11, D12, D13, D10, 8000000);
 FATFileSystem   fs("fs");
 #endif
+Timer tmr;
 
 //  RAM ------------------------------------------------------------------------
 
 //  ROM / Constant data --------------------------------------------------------
-char *const opening_msg0 = "microSD Card test program";
+const char *const opening_msg0 = "microSD Card test program";
 #if (MBED_MAJOR_VERSION == 2)
 char *const opening_msg1 = " -> run on Mbed Classic\r\n";
 #elif (MBED_MAJOR_VERSION == 5)
-char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
+const char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
 #endif
 
 //  Function prototypes --------------------------------------------------------
@@ -66,16 +76,21 @@
     uint32_t data4 = 50000U;
     uint32_t data5 = 60000U;
 
-
-    pc.printf("Start SD Card control program\r\n");
-    //pc.printf("line:%d\r\n", __LINE__);
+    if (user_sw == USER_SW_ON) {
+        mon();
+    }
+    DBG("line:%d\r\n", __LINE__);
 #if (MBED_MAJOR_VERSION == 5)
     /* Init SD CARD reader */
     sd.init();
     fs.mount(&sd);
+#else
+    SDFileSystem    sd(D11, D12, D13, D10, "fs");  // do,di,clk,cs
+    wait_ms(100);
 #endif
     FILE* fp = fopen("/fs/mydata.txt", "a");
     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);
     } else {
@@ -86,27 +101,37 @@
         char c = pc.getc(); // dummy read
     }
     while (true) {
+        DBG("line:%d\r\n", __LINE__);
+        tmr.reset();
+        tmr.start();
         uint32_t size = get_disk_freespace();
         pc.printf("free %u  ", size);
         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(tmp);
+            pc.printf("%s", tmp);
             fprintf(fp, "%s", tmp);
             pc.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);
         } else {
             pc.printf("ERROR\r\n");
         }
         fclose(fp);
+        uint32_t time_sd = tmr.read_ms();
+        pc.printf("time:%d ", time_sd);
 #if (MBED_MAJOR_VERSION == 2)
-        wait(0.1f);
+        if (time_sd < LOOP_TIME -2){
+            wait_ms(LOOP_TIME - time_sd);
+        }
 #elif (MBED_MAJOR_VERSION == 5)
-        ThisThread::sleep_for(100);
+        if (time_sd < LOOP_TIME -2){
+            ThisThread::sleep_for(LOOP_TIME - time_sd);
+        }
 #endif
         if (user_sw == USER_SW_ON) {
             break;
@@ -117,6 +142,6 @@
     }
     while(true) {
         mon();
-        NVIC_SystemReset();
+        system_reset();
     }
 }