Several examples run on only mbed-os5.13.0 (not 5.14.0)

Dependencies:   BD_SD_DISCO_F769NI BSP_DISCO_F769NI LCD_DISCO_F769NI TS_DISCO_F769NI USBHost_F769NI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 4_microSD.cpp Source File

4_microSD.cpp

00001 /*
00002  * Mbed Application program
00003  *  SD Card file control function with FatFs on Mbed-os5
00004  *
00005  * Copyright (c) 2018,'19 Kenji Arai / JH1PJL
00006  *  http://www.page.sannet.ne.jp/kenjia/index.html
00007  *  https://os.mbed.com/users/kenjiArai/
00008  *      Created:    April      7th, 2018
00009  *      Revised:    October   14th, 2019
00010  */
00011 
00012 #include "select_program.h"
00013 //#define EXAMPLE_4_MICROSD
00014 #ifdef EXAMPLE_4_MICROSD
00015 
00016 //  Include --------------------------------------------------------------------
00017 #include    "mbed.h"
00018 #include    "FATFileSystem.h"
00019 #include    "SDBlockDeviceDISCOF769NI.h"
00020 #include    "mon.h"
00021 
00022 //  Definition -----------------------------------------------------------------
00023 #define     USER_SW_ON      1
00024 
00025 //  Constructor ----------------------------------------------------------------
00026 DigitalOut      led(LED1);
00027 DigitalIn       user_sw(USER_BUTTON);
00028 Serial          pc(USBTX, USBRX, 115200);
00029 // Instantiate the Block Device for sd card on DISCO-F769NI
00030 SDBlockDeviceDISCOF769NI bd;
00031 FATFileSystem   fs("fs");
00032 
00033 //  RAM ------------------------------------------------------------------------
00034 
00035 //  ROM / Constant data --------------------------------------------------------
00036 const char * opening_msg0 = "microSD Card test program";
00037 const char * opening_msg1 = " -> run on Mbed OS-5\r\n";
00038 
00039 //  Function prototypes --------------------------------------------------------
00040 void return_error (int ret_val);
00041 void errno_error (void* ret_val);
00042 
00043 //------------------------------------------------------------------------------
00044 //  Control Program
00045 //------------------------------------------------------------------------------
00046 int main()
00047 {
00048     time_t      seconds;
00049     uint32_t data0 = 10000U;
00050     uint32_t data1 = 20000U;
00051     uint32_t data2 = 30000U;
00052     uint32_t data3 = 40000U;
00053     uint32_t data4 = 50000U;
00054     uint32_t data5 = 60000U;
00055 
00056     pc.printf("\x1b[2J\x1b[H %s\r\n %s %s (UTC)\r\n",
00057               __FILE__, __DATE__, __TIME__);
00058     printf(" microSD FOR DISCO-F769NI:\r\n");
00059 
00060     //pc.printf("line:%d\r\n", __LINE__);
00061     pc.printf("\r\nStart\r\n");
00062     int error = 0;
00063     pc.printf("Welcome to the filesystem example.\r\n");
00064     pc.printf("Mounting the filesystem on \"/fs\". \r\n");
00065     error = fs.mount(&bd);
00066     return_error(error);
00067 
00068     if (user_sw == USER_SW_ON) {
00069         mon();
00070     }
00071 
00072     FILE* fp = fopen("/fs/mydata.txt", "a");
00073     errno_error(fp);
00074     if (fp != 0) {
00075         pc.printf("%s%s",  opening_msg0, opening_msg1);
00076         fprintf(fp,"%s%s", opening_msg0, opening_msg1);
00077     } else {
00078         pc.printf("ERROR\r\n");
00079     }
00080     fclose(fp);
00081     while (pc.readable()) {
00082         char c = pc.getc(); // dummy read
00083     }
00084     while (true) {
00085         uint32_t size = get_disk_freespace();
00086         pc.printf("free %u  ", size);
00087         fp = fopen("/fs/mydata.txt", "a");
00088         /*if (size <= DISK_SIZE_LIMIT) {
00089             pc.printf("Reached RAM Disk size limitation!!\r\n");
00090             break;
00091         }*/
00092         errno_error(fp);
00093         if(fp != 0) {
00094             char tmp[64];
00095             seconds = time(NULL);
00096             strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
00097             pc.printf("%s", tmp);
00098             fprintf(fp, "%s", tmp);
00099             pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n",
00100                       ++data0, ++data1, ++data2, ++data3, ++data4, ++data5);
00101             fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n",
00102                         data0,   data1,   data2,   data3,   data4,   data5);
00103             fclose(fp);
00104         } else {
00105             pc.printf("ERROR (Cannot open /fs/mydata.txt)\r\n");
00106             // start retry
00107             error = fs.mount(&bd);
00108             return_error(error);
00109             FILE* fp = fopen("/fs/mydata.txt", "a");
00110             errno_error(fp);
00111             if (fp != 0) {
00112                 pc.printf("%s%s",  opening_msg0, opening_msg1);
00113                 fprintf(fp,"%s%s", opening_msg0, opening_msg1);
00114                 fclose(fp);
00115             } else {
00116                 system_reset();
00117             }
00118         }
00119         ThisThread::sleep_for(100);
00120         if (user_sw == USER_SW_ON) {
00121             break;
00122         }
00123         if (pc.readable()) {
00124             mon();
00125         }
00126         led = !led;
00127     }
00128     while(true) {
00129         mon();
00130         NVIC_SystemReset();
00131     }
00132 }
00133 
00134 void return_error (int ret_val)
00135 {
00136     if (ret_val) {
00137         pc.printf("retrun error/Failure. %d\r\n", ret_val);
00138     }
00139 }
00140 
00141 void errno_error (void* ret_val)
00142 {
00143     if (ret_val == NULL) {
00144         pc.printf("error #/Failure. %d \r\n", errno);
00145     }
00146 }
00147 
00148 #endif