I am trying to use the FatFs module in the Atmel start for my project with the samd51j19a chip. The clock is @ 120mhz and the sd card is wired according to the diagram on the PINMUX. The program seems to be stuck in this loop @ /hpl/sdhc/hpl_sdhc.c:177:

/* Wait end of command */
    do {
        sr = hri_sdhc_read_EISTR_reg(hw);

        if (cmd & MCI_RESP_CRC) {
            if (sr
                   | SDHC_EISTR_ADMA)) {
                hri_sdhc_set_EISTR_reg(hw, SDHC_EISTR_MASK);
                return false;
        } else {
            if (sr
                hri_sdhc_set_EISTR_reg(hw, SDHC_EISTR_MASK);
                return false;
    } while (!hri_sdhc_get_NISTR_CMDC_bit(hw));

the output of bt in gdb is as following:

(gdb) bt
#0  _mci_send_cmd_execute (hw=0x45000000, cmdr=0, cmd=cmd@entry=16384, 
    arg=arg@entry=4541) at ../hri/hri_sdhc_d51.h:2980
#1  0x00001812 in _mci_sync_send_cmd (mci_dev=<optimized out>, 
    cmd=cmd@entry=16384, arg=4541, arg@entry=0) at ../hpl/sdhc/hpl_sdhc.c:353
#2  0x000011e2 in mci_sync_send_cmd (mci=<optimized out>, 
    cmd=cmd@entry=16384, arg=arg@entry=0) at ../hal/src/hal_mci_sync.c:115
#3  0x00000860 in sd_mmc_mci_card_init () at ../sd_mmc/sd_mmc.c:1127
#4  sd_mmc_check (slot=slot@entry=0 '\000') at ../sd_mmc/sd_mmc.c:1346
#5  0x00001e64 in sd_mmc_test_unit_ready (slot=<optimized out>)
    at ../diskio/sdmmc_diskio.c:50
#6  0x00001ec0 in disk_initialize (drv=drv@entry=0 '\000')
    at ../diskio/sdmmc_diskio.c:156
#7  0x00002782 in find_volume (rfs=rfs@entry=0x2000aca4, 
    path=path@entry=0x2000ac94, wmode=wmode@entry=10 '\n')
    at ../fatfs/src/ff.c:2105
#8  0x000031ae in f_open (fp=fp@entry=0x2000ace8, path=<optimized out>, 
    path@entry=0x54c4 "message.txt", mode=mode@entry=10 '\n')
    at ../fatfs/src/ff.c:2317
#9  0x00001df2 in main () at ../main.c:24

Here is my main file:

#include <atmel_start.h>
#include <fatfs/src/ff.h>
int main(void)
    /* Initializes MCU, drivers and middleware */
    PORT->Group[0].DIRSET.reg = PORT_PA16;
    PORT->Group[0].OUTTGL.reg = PORT_PA16;
    FATFS FatFs;
    FIL fil;        /* File object */
    BYTE buffer[4096] = "asdfggh";   /* File copy buffer */
    char line[100]; /* Line buffer */
    FRESULT fr;     /* FatFs return code */
    UINT br, bw;         /* File read/write count */
    fr = f_mount(&FatFs, "", 1);

    br = 8;

    printf("%d", fr);
    PORT->Group[0].OUTTGL.reg = PORT_PA16;
    fr = f_open(&fil, "message.txt", FA_WRITE | FA_CREATE_ALWAYS);
    f_write(&fil, buffer, br, &bw);
    while (1) 

here is my current clock configuration: clocks

  • \$\begingroup\$ You're not really providing enough information to know exactly where this is getting stuck. Likely however it's eternally waiting for some MCU peripheral engine status bit to change, but that's not going to happen because of something like that peripheral not being clocked or fully enabled. Chances are it behaves the same even with no SD card inserted (or at least would if you bypassed detection logic...) \$\endgroup\$ – Chris Stratton Oct 15 '20 at 14:32
  • \$\begingroup\$ @ChrisStratton the condition !hri_sdhc_get_NISTR_CMDC_bit(hw) in the do-while never gets set to false. \$\endgroup\$ – James B. Reese Oct 15 '20 at 14:38
  • \$\begingroup\$ Which probably indicates the peripheral isn't set up right. Spend some time reading the documentation for the hardware that controls. \$\endgroup\$ – Chris Stratton Oct 15 '20 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.