6 years, 8 months ago.

Have an error from SD-Driver

I use Nucleo -F746ZG and mbed-os version 5

I have error when runtime

ERROR

Welcome to the filesystem example.
Opening a new file, numbers.txt. Failure. 5
Writing decimal numbers to a file (20/20) done.
Closing file.

++ MbedOS Fault Handler ++
FaultType: HardFault
Context:
R0   : E0FA556F
R1   : 923099F0
R2   : 00000000
R3   : 080088B9
R4   : E0FA556F
R5   : 923099F0
 R6   : 20000A3C
R7   : E0FA556F
R8   : FFFFFFFF
R9   : EADEC0EA
R10  : 00000000
R11  : 00000000
R12  : 080109D1
SP   : 20005280
LR   : 0800B9D9
PC   : 08007FAC
xPSR : A1000000
PSP  : 20005218
MSP  : 2004FFD8
CPUID: 410FC271
HFSR : 40000000
MMFSR: 00000000
BFSR : 00000082
UFSR : 00000000
DFSR : 00000009
AFSR : 00000000
SHCSR: 00000000
BFAR : A3E95FF8
Mode : Thread
Priv : Privileged
Stack: PSP

Thread Info:
Current:
State: 00000002 EntryFn: 0800EFA1 Stack Size: 00001000 Mem: 20004318 SP: 20005078
Next:
State: 00000002 EntryFn: 0800EFA1 Stack Size: 00001000 Mem: 20004318 SP: 20005078
Wait Threads:
State: 00000083 EntryFn: 0800E949 Stack Size: 00000300 Mem: 20000710 SP: 200009A0
Delay Threads:
Idle Thread:
State: 00000001 EntryFn: 0800DE7D Stack Size: 00000200 Mem: 20000510 SP: 200006C8

-- MbedOS Fault Handler --

SOURCE CODE with MBED-OS

source code

#include "mbed.h"
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
#include <stdio.h>
#include <errno.h>
/* mbed_retarget.h is included after errno.h so symbols are mapped to
 * consistent values for all toolchains */
#include "platform/mbed_retarget.h"


SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS);
FATFileSystem fs("sd", &sd);

void return_error(int ret_val){
  if (ret_val)
    printf("Failure. %d\n", ret_val);
  else
    printf("done.\r\n");
}

void errno_error(void* ret_val){
  if (ret_val == NULL)
    printf(" Failure. %d \r\n", errno);
  else
    printf(" done.\r\n");
}

int main()
{
    int error = 0;
    printf("Welcome to the filesystem example.\r\n");

    printf("Opening a new file, numbers.txt.");
    FILE* fd = fopen("/sd/numbers.txt", "w+");
    errno_error(fd);

    for (int i = 0; i < 20; i++){
        printf("Writing decimal numbers to a file (%d/20)\r", i);
        fprintf(fd, "%d\n", i);
    }
    printf("Writing decimal numbers to a file (20/20) done.\r\n");

    printf("Closing file.\r\n");
    fclose(fd);
    printf(" done.\r\n");

    printf("Re-opening file read-only.\r\n");
    fd = fopen("/sd/numbers.txt", "r");
    errno_error(fd);

    printf("Dumping file to screen.\r\n");
    char buff[16] = {0};
    while (!feof(fd)){
        int size = fread(&buff[0], 1, 15, fd);
        fwrite(&buff[0], 1, size, stdout);
    }
    printf("EOF.\r\n");

    printf("Closing file.\r\n");
    fclose(fd);
    printf(" done.\n");

    printf("Opening root directory.");
    DIR* dir = opendir("/sd/");
    errno_error(fd);

    struct dirent* de;
    printf("Printing all filenames:\r\n");
    while((de = readdir(dir)) != NULL){
        printf("  %s\n", &(de->d_name)[0]);
    }

    printf("Closeing root directory. ");
    error = closedir(dir);
    return_error(error);
    printf("Filesystem Demo complete.\n");

    while (true) {}
}

1 Answer

6 years, 8 months ago.

Hi Teerawat,

The sd-driver doesn't have native support for the Nucleo -F746ZG. So you will have to use it with a CI Test Shield or modify the mbed_app.json file as described in the "Testing with an SDCard on Target XYZ" portion of the readme:

https://github.com/ARMmbed/mbed-os-example-sd-driver

If you do have a shield or have modified the json file, do you have some other SD cards to try? We've just done some testing with a K64 target and on a few SD cards we see the same error 5 which indicates that the K64 simply isn't playing well with that particular card.

-Ralph, Team Mbed