This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SD Card Interfacing (FatFS specific) : f_sync() problem

I'm not sure if this is the correct place to ask questions, but here it is anyway. I'm working on an interfacing with SD card using ELM ChaN's FatFS. This is my offending piece of code that doesn't work:

 res = f_open(&fsrc, "0:/dieshit.txt", FA_CREATE_ALWAYS | FA_WRITE);

 writeCount = f_printf(&fsrc, "Hello Chris!"); // returns 12 bytes written, which is correct
res_sync = f_sync(&fsrc) // returns FR_DISK_ERR
 res_close = f_close(&fsrc); // returns FR_NOT_READY f_mount(0, 0);

After tracing the f_sync() calls, I found out that the last two function calls are done to send_cmd() and xmit_datablock. These calls returned 0xFF and FALSE, which is not how it's supposed to happen. The last call to send_cmd passed CMD24 argument (single block write). The send_cmd() function is as follows:

static
BYTE send_cmd (
    BYTE cmd,        /* Command byte */
    DWORD arg        /* Argument */
)
{
    BYTE n, res;


    if (wait_ready() != 0xFF) return 0xFF;

    /* Send command packet */
    xmit_spi(cmd);                        /* Command */
    xmit_spi((BYTE)(arg >> 24));        /* Argument[31..24] */
    xmit_spi((BYTE)(arg >> 16));        /* Argument[23..16] */
    xmit_spi((BYTE)(arg >> 8));            /* Argument[15..8] */
    xmit_spi((BYTE)arg);                /* Argument[7..0] */
    n = 0;
    if (cmd == CMD0) n = 0x95;            /* CRC for CMD0(0) */
    if (cmd == CMD8) n = 0x87;            /* CRC for CMD8(0x1AA) */
    xmit_spi(n);

    /* Receive command response */
    if (cmd == CMD12) rcvr_spi();        /* Skip a stuff byte when stop reading */
    n = 10;                                /* Wait for a valid response in timeout of 10 attempts */
    do
        res = rcvr_spi();
    while ((res & 0x80) && --n);

    return res;            /* Return with the response value */
}

This returns 0xFF to res??? I'm wondering what went wrong where it supposed to return 0x00 or 0x01 (not sure which one)

Can someone guide me here?

Thanks

  • Was your problem solved ?
    I'm trying to use f_sync() after couple of f_write() but getting FR_DISK_ERR returned. But f_close() works fine. For every FatFs function call I have a timeout of 10 attempts. My application is data logger and so I ideally do not want to close the file everytime after few f_write(). Does any one know of particular reasons why f_sync() would return error but f_close() works fine ?
  • Hello,

    Please consider creating a new post as old posts are generally closed and no longer tracked. You can provide the necessary information regarding your issue in the new post and include a link to this post if you'd like.

    Elizabeth