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

main.cpp

Committer:
kenjiArai
Date:
2019-06-04
Revision:
3:2134d3cb4e8e
Parent:
2:1397a54382ec
Child:
4:fc36c8ec2966

File content as of revision 3:2134d3cb4e8e:

/*
 * Mbed Application program
 *  SD Card file control function with FatFs on Mbed-os5 and/or os2
 *
 * 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
 */

//  Include --------------------------------------------------------------------
#include    "mbed.h"
#if (MBED_MAJOR_VERSION == 2)
#include    "SDFileSystem.h"
#elif (MBED_MAJOR_VERSION == 5)
#include    "SDBlockDevice.h"
#include    "FATFileSystem.h"
#endif
#include    "mon.h"
#include    <stdlib.h>

//  Definition -----------------------------------------------------------------
#define     USER_SW_ON      0

//  Constructor ----------------------------------------------------------------
//DigitalOut      led(LED1);      // same as D13 (equal to SPI CLK) STM Nucleo
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);
FATFileSystem   fs("fs");
#endif

//  RAM ------------------------------------------------------------------------

//  ROM / Constant data --------------------------------------------------------
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";
#endif

//  Function prototypes --------------------------------------------------------

//------------------------------------------------------------------------------
//  Control Program
//------------------------------------------------------------------------------
int main()
{
    time_t      seconds;
    uint32_t data0 = 10000U;
    uint32_t data1 = 20000U;
    uint32_t data2 = 30000U;
    uint32_t data3 = 40000U;
    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 (MBED_MAJOR_VERSION == 5)
    /* Init SD CARD reader */
    sd.init();
    fs.mount(&sd);
#endif
    FILE* fp = fopen("/fs/mydata.txt", "a");
    if (fp != 0) {
        pc.printf("%s%s",  opening_msg0, opening_msg1);
        fprintf(fp,"%s%s", opening_msg0, opening_msg1);
    } else {
        pc.printf("ERROR\r\n");
    }
    fclose(fp);
    while (pc.readable()) {
        char c = pc.getc(); // dummy read
    }
    while (true) {
        uint32_t size = get_disk_freespace();
        pc.printf("free %u  ", size);
        fp = fopen("/fs/mydata.txt", "a");
        if(fp != 0) {
            char tmp[64];
            seconds = time(NULL);
            strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
            pc.printf(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);
        } else {
            pc.printf("ERROR\r\n");
        }
        fclose(fp);
#if (MBED_MAJOR_VERSION == 2)
        wait(0.1f);
#elif (MBED_MAJOR_VERSION == 5)
        ThisThread::sleep_for(100);
#endif
        if (user_sw == USER_SW_ON) {
            break;
        }
        if (pc.readable()) {
            mon();
        }
    }
    while(true) {
        mon();
        NVIC_SystemReset();
    }
}