This example demonstrates the reading of the SD card in the Nucleo.

Dependencies:   mbed SDFileSystem

main.cpp

Committer:
hakusan270
Date:
2020-12-03
Revision:
1:9d8797b91dda
Parent:
0:b0a3ecd53c7d

File content as of revision 1:9d8797b91dda:

#include "mbed.h"
#include "SDFileSystem.h"

DigitalIn btn(USER_BUTTON);

// trim '\n'
void ntrim(char *str)
{
    int i;
    for (i = 0; str[i] != 0; ++i);

    if (i > 0 && str[i - 1] == '\n')
        str[i - 1] = 0;
}

int main()
{
    printf("Hello\r\n");
    // SD filesystem
    SDFileSystem *sd = new SDFileSystem(D11, D12, D13, D10, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); // mosi, miso, sclk, name, card detect, sw type, freq

    while (1)
    {
        if (btn) continue;

        // file open
        FILE *fp = fopen("/sd/sd/test.txt", "r");
        if (fp == NULL)
        {
            printf("open error!!\r\n");
            while(1);
        }
//        fprintf(fp,"test write\n");
//        fclose(fp);        
        // read text file
        char buf[1024];
        while (fgets(buf, sizeof(buf), fp) != NULL)
        {
            ntrim(buf);            
            printf("%s\r\n", buf);
        }
    
        // file close
        fclose(fp);        

        wait(1);
    }
}