TMS320F2812 FatFs f_write returns FR_DISK_ERR

RydelW picture RydelW · Aug 19, 2014 · Viewed 9k times · Source

I have problem with an SD card. I'm using the FatFs library ver R0.10b to access the SD card.

My code:

    // .... //
    FATFS fatfs;
    FIL plik;
    FRESULT fresult,res1,res2,res3,res4,res5;
    UINT zapisanych_bajtow = 0 , br;
    UINT zapianie_bajtow = 0;
    char * buffor = "123456789abcdef\r\n";
    unsigned short int i;

    void main(void) {

    // ... //

       res1 = f_mount(0,&fatfs); // returns FA_OK
        res2 = f_open( &plik, "f721.txt", FA_OPEN_ALWAYS | FA_WRITE ); // returns FA_OK
        if( res2 == FR_OK )
        {
                res3 = f_write( &plik, ( const void * ) buffor, 17, &zapisanych_bajtow ); // returns FR_DISK_ERR
        }

        res4 = f_close( &plik );// returns FR_DISK_ERR

        for(;;)
        {

        }
}

Any idea what might be wrong?

Answer

Jozef picture Jozef · Feb 5, 2016

I had similar error with just one difference. I tried to write 4096bytes with f_write function at once. And it always returned FR_DISK_ERR. And this was caused because I tried to write more then is size of IO buffer in FIL structure in FatFS (defined in ff.h).

typedef struct {
    FATFS*  fs;             /* Pointer to the related file system object (**do not change order**) */
    WORD    id;             /* Owner file system mount ID (**do not change order**) */
    BYTE    flag;           /* Status flags */
    BYTE    err;            /* Abort flag (error code) */
    DWORD   fptr;           /* File read/write pointer (Zeroed on file open) */
    DWORD   fsize;          /* File size */
    DWORD   sclust;         /* File start cluster (0:no cluster chain, always 0 when fsize is 0) */
    DWORD   clust;          /* Current cluster of fpter (not valid when fprt is 0) */
    DWORD   dsect;          /* Sector number appearing in buf[] (0:invalid) */
    DWORD   dir_sect;       /* Sector number containing the directory entry */
    BYTE*   dir_ptr;        /* Pointer to the directory entry in the win[] */
    DWORD*  cltbl;          /* Pointer to the cluster link map table (Nulled on file open) */
    UINT    lockid;         /* File lock ID origin from 1 (index of file semaphore table Files[]) */
    BYTE    buf[_MAX_SS];   /* File private data read/write window */
} FIL;

The last array buf[_MAX_SS] is the file IO buffer. But _MAX_SS is user defined parameter (defined in ff.h) so you can decrease the amount of bytes written at once or eventually change the _MAX_SS value.

I know this is not your case because you only write 17 bytes at once, but this can be helpful for others.