7 years, 9 months ago.

Has anyone else had trouble using readdir() with the SDFileSystem library?

I'm having trouble reading the directory on an SD card. I followed the example in the link below, but my program crashed once "readdir()" was executed. I checked the return value, but it appears that the function never actually finishes executing.

Any help would be appreciated.


Question relating to:

SDFileSystem FAT, file, SD, System

1 Answer

7 years, 9 months ago.

Hi Matt, I was checking your problem on Freescale's KL45Z platform. I've wrote below below snippet to check readdir() issue:

simple directory listing

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

SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");

int main()
    bool result = true;
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir ("/sd/")) != NULL) {
        // print all the files and directories within directory
        while ((ent = readdir (dir)) != NULL) {
            printf("%s\r\n", ent->d_name);
        closedir (dir);
    } else {
        // could not open directory
        printf("Could not open directory");
        result = false;
    return !result;

And I've managed to get correct results from my SD card:


Everything looks OK, I've cloned mbed SDK git repo on this SD card. Can you tell me what is your target platform? Maybe I can check it with the same HW you have.

My target is the Nucleo 401RE. File operations like fopen work fine, so I'm not sure what the issue is. Thanks for your help.

posted by Matt McCarthy 24 Oct 2014

I think we have a problem with 401RE and SPI there. We need deeper investigation.

posted by Przemek Wirkus 31 Oct 2014

Ok, checked this issue for a while. Also on 401 everything works well:

c:\Work\mbed-przemek\workspace_tools> singletest.py -i test_spec.json -M muts_all.json -n MBED_35 -c cp -V
Building library CMSIS (NUCLEO_F401RE, uARM)
Building library MBED (NUCLEO_F401RE, uARM)
Building library FAT (NUCLEO_F401RE, uARM)
Building project SD_STAT (NUCLEO_F401RE, uARM)
Executing 'python host_test.py -p COM52 -d E:\ -t 15 -m NUCLEO_F401RE'
Host test instrumentation on port: "COM52" and disk: "E:\"
 file:  out.txt
file:   testfile.bin
dir:    mbed
TargetTest::NUCLEO_F401RE::uARM::MBED_35::FileSystem stat functionality [OK] in 1.43 of 15 sec
Test summary:
| Result | Target        | Toolchain | Test ID | Test Description              | Elapsed Time (sec) | Timeout (sec) | Loops |
| OK     | NUCLEO_F401RE | uARM      | MBED_35 | FileSystem stat functionality |        1.43        |       15      |  1/1  |
Result: 1 OK

Completed in 5.00 sec
posted by Przemek Wirkus 31 Oct 2014