11 years, 8 months ago.

Cannot open file for read in microSD card with KL25Z

Hi, I tried to read a file from a micro SD card with KL25Z, then I cannot open file for read. But I was able to write a file. Why?? I don't know how to do?

Thanks.

Hi, can you publish your code please?

posted by Samuel Mokrani 05 Mar 2013

1 Answer

11 years, 8 months ago.

Hi, my code is here. Writing(below) is possible, but reading(above) is impossible. ??

SD card module is sparkfun micro SD shield. I don't use "/sd/mydir" folder.

#include "mbed.h"
#include "SDFileSystem.h"
 
SDFileSystem sd(PTD2, PTD3, PTD1, PTA4, "sd"); // the pinout on the mbed Cool Components workshop board
char buffer[64]={"abc"};

DigitalOut grn(PTB19);
DigitalOut red(PTB18);
DigitalOut blu(PTD1); 
int main() {
    grn = 1;
    red = 1;
    blu = 1;
    printf("Hello World!\n");   
    //mkdir("/sd/mydir", 0777);
    blu = 0;    
    FILE *fp = fopen("/sd/sdtest.txt", "r");
    grn = 0;
    blu = 1;    
    if(fp == NULL) {
        error("\r\nCould not open file for read\n");
    }
    //fprintf(fp, "Hello fun SD Card World!");
    grn = 1;
    red = 0;
    fgets(buffer, 64, fp);
    red = 1;
    blu = 0; 
    fclose(fp);
   
    printf("\r\n%s", buffer);
    printf("Goodbye World!");
    while(1)
    {
    
    }
}
/*#include "mbed.h"
#include "SDFileSystem.h"

SDFileSystem sd(PTD2, PTD3, PTD1, PTA4, "sd");  // the pinout on the mbed Cool Components workshop board
 
int main() {
    printf("Hello World!\n");   
 
    //mkdir("/sd/mydir", 0777);
    
    FILE *fp = fopen("/sd/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    fprintf(fp, "Hello fun SD Card World!");
    fclose(fp); 
 
    printf("Goodbye World!\n");
}*/

Accepted Answer

I changed CS pin in micro SD shield from 8 to 4(PTA4). But I didn't change the other 3pins(SCK, MISO, MOSI).

posted by takahiro maeda 07 Mar 2013

Taking a look at your code, i noticed that you are using PTD1 for digitalout and spi. can you try to remove

DigitalOut blu(PTD1);
posted by Samuel Mokrani 07 Mar 2013

I've forgotten the LED connection. Thank you!

posted by takahiro maeda 08 Mar 2013