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.
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);
}
}
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:
That’s the text file I’m trying to read:
This is the code:
SD Card Test
And the upsetting result:
Thanks for any help :)
Import programSD_Card_Test-yonatan
Just a test for read/write operations on SD-Card, using Nucleo-f411re
Last commit 17 May 2017 by Yonatan Yatuv