Connect SD-Card to Nucleo F411RE

17 May 2017

Hello, I tried to connect nucleo-f411re to an SD-Card according to the diagram in https://developer.mbed.org/users/mimi3/code/wave_player_pwm_lite/

When all connected I’m getting failure for trying to open a file from the sd-card or write to it. I’m a newbie and appreciate any help.

This is what I did: /media/uploads/yatuvy/20170517_115505.jpg /media/uploads/yatuvy/20170517_115644.jpg /media/uploads/yatuvy/20170517_115611.jpg /media/uploads/yatuvy/20170517_115956.jpg /media/uploads/yatuvy/20170517_120221.jpg

That’s the text file I’m trying to read: /media/uploads/yatuvy/image20170516223701.png

This is the code:

SD Card Test

#include "mbed.h"
#include <fstream>
#include "SDFileSystem.h"

SDFileSystem sd(D4, D5, D3, D10, "sd");
			//(mosi,miso,sclk,cs,name)

char buffer[4096];

void readTest1() {
    printf("\n\rreadTest1() Read /sd/DirX/textfile.txt");
    FileHandle* file = sd.open("/sd/DirX/textfile.txt", O_RDONLY);
    if (file == NULL) {
        printf("\n\r\tFailed sd.open(...)");
    } else {
        printf("\n\rSuccess sd.open(...)");
    }
}

void readTest2() {
    printf("\n\rreadTest2() fopen /sd/DirX/textfile.txt");
    FILE *fp = fopen("/sd/DirX/textfile.txt", "r");
    if (fp == NULL) {
        printf("\n\r\tFailed fopen(...)");
    } else {
        printf("\n\r\tSuccess fopen(...)");
    }
}

void writeFile(){

    const char* filePath = "/sd/DirX/mydir/sdtest.txt";
    printf("\n\rwriteFile() Write to SD card: %s", filePath);
    mkdir("/sd/DirX/mydir", 0777);
    FILE *fp = fopen("/sd/DirX/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        printf("\n\r\tFailed writeFile()");
    }
    else{
        fprintf(fp, "\n\rSuccess writeFile()");
        fclose(fp);
    }
}

int main() {
	printf("\n\rSD Card Test");
	readTest1();
	readTest2();
	writeFile();
}

And the upsetting result: /media/uploads/yatuvy/image20170517123427.png

Thanks for any help :)

Import programSD_Card_Test-yonatan

Just a test for read/write operations on SD-Card, using Nucleo-f411re

23 May 2017

This post deserves a response, if only for the very clean photographs. I had SD card read/write working with mbed-dev "#define MBED_LIBRARY_VERSION 118" and "SDFileSystem.h" from a similar time period. I have the Nucle_F401RE though.

First thing. Do not Exit main(). Ever. I believe this is undefined and will definitely crash your program. Throw an empty loop in there if you need to.

while(1);

Then, be careful about spamming the SD card too fast. Throw some waits in there between calls to write and read.

Then when you make the directory, I think you need to make One directory at a time. As written it tries to create mydir inside DirX, but DirX doesn't exist yet and it's not smart enough to make Both directories for you.

I note in that Link, they are not showing SD ground connected. It needs to be connected so it is common with the micro. I have the same sparkfun breakout board. Every pin except CD is connected directly to my micro - no external resistors, not even any caps. CD is not connected anywhere.

Here, I tested this in online compiler with mbed revision 118 and SDFileSystem rev 23:6bb3c19 on F401RE board. Works. Pins of course are just how I have the breakout board connected. I rolled these libraries back to this point because I knew it worked. The latest version of each did not work out of the box and I did not want to investigate.

#include "mbed.h"


// https://developer.mbed.org/users/neilt6/code/SDFileSystem/
#include "SDFileSystem.h"
#include <stdio.h>

// #include <stdlib.h>
// #include "rtos.h"

SDFileSystem sd(D11, D12, D13, D10, "sd");

// (mosi,miso,sclk,cs,name)

void readTest2() {
    unsigned char c; // a single byte buffer

    sd.mount();
    printf("\r\nreadTest2() fopen /sd/DirX/sdtest.txt\r\n");
    FILE *file = fopen("/sd/DirX/sdtest.txt", "r");

    if (file == NULL) {
        printf("\tFailed fopen(...)\r\n");
    }
    else {
        printf("\tSuccess fopen(...)\r\n");

      while (c != '\n') {
            c = getc(file); 
            printf("%c\r\n",c);
      }
        fclose(file);
    }
    sd.unmount();
}

void writeFile() {
    sd.mount();

    const char *filePath = "/sd/DirX/sdtest.txt";
    printf("writeFile() Write to SD card: %s\r\n", filePath);
    int result = mkdir("/sd/DirX", 0777);

    if (result == -1) {
        printf("Could not create Directory.\r\n");
    }

    // w = fresh write, a = append
    FILE *file = fopen("/sd/DirX/sdtest.txt", "w");

    if (file == NULL) {
        printf("\n\r\tFailed writeFile()");
    }
    else {
        fprintf(file, "0,1,2,3,4,5,6,7,8,9\n");
        fclose(file);
    }

    // should free card to be removed
    sd.unmount();
}

int main() {
    printf("\r\n--- SD Card Test ---\r\n");

    writeFile();

    wait(0.1);
    readTest2();
    wait(0.1);

    // readTest2();

    // Do not exit main.
    while (true) {
        wait(0.5);
    }
}
24 May 2017

Hi Graham, I tried connect SD to GND and also connect SD pins (mosi, miso, clk) directly to board. still getting Failed, also with your code.

But thanks anyway :)

02 Jun 2017

Hello,

Please, could you try this example - https://github.com/armmbed/mbed-os-example-fat-filesystem and follow on to this section (https://github.com/armmbed/mbed-os-example-fat-filesystem#switch-from-ram-backed-block-device-to-an-sd-card) for the SD card? That way we can get a baseline for your setup, as we know this example code works.

Thanks, Sarah

19 Jul 2017

Hi,

I'm having a similar issue using the ST Micro Nucleo-F446RE and the SparkFun microSD Transflash breakout. The connections that I'm using are.

SD BreakoutF466RE
CDPC_8
DOPB_14
GND<GND>
SCKPB_13
VCC+3.3V
DIPB_15
CSPB_1

I tried the example fat file system example that Sarah mentions above which works. When I switch to the SD card it fails. I decided to remove the FAT Filesystem out of the equation and try to use it as an SDBlock device and it fails on the .init method below with a -5005. The SD card is a formatted FAT 4GB sdMicro.

int main()
{
    // mosi, miso, sclk, cs
    SDBlockDevice bd(PB_15, PB_14, PB_13, PB_12);    //HeapBlockDevice bd(128 * 512, 512); 

    uint8_t block[512] = "Hello World!\n";
    
    // call the SDBlockDevice instance initialisation method.
    int i = bd.init();
        
    // Write some the data block to the device 
    i = bd.program(block, 0, 512);
        
    // read the data block from the device
    i = bd.read(block, 0, 512);
        
    // print the contents of the block
    printf("%s", block);
        
    // call the SDBlockDevice instance de-initialisation method.
    i = bd.deinit();
}

I'm using Mbed Library version 123 along with the VisualGDB toolset and Visual Studio 2013. I also have the Nucleo-F411RE if you want me to test with that one.

Thanks, -steve

24 Aug 2017

Hey Steve,

You will receive the most support for this if you use the latest version of mbed OS - 5.5.5 in that example. That way, if you do find a problem in that example (using the latest version of mbed OS), you can file an issue against mbed OS - https://github.com/ARMmbed/mbed-os/issues.