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