FATFS returns FR_DISK_ERR the second time I use an

2019-08-17 05:17发布

问题:

I am using FATFS to write data to an SD card. It partially works and I am able to write EEPROM data to the SD card. But when I use a different function later on in the code it returns 'FR_DISK_ERR' even though I'm using the same line of code.

The first time I try to write to the SD card is as follows (At this point I have already initialized the SD card and made the file, that is not the issue):

        //write EEPROM to EEPROM file
        fr = f_open(&File, file_name, FA_OPEN_APPEND | FA_WRITE);

        if (fr == FR_OK) 
        {
            //I write some data here, for company privacy purposes I have deleted this. I mainly use f_printf(&file,"data");  functions here


            /* Close the file */
            f_close(&File);
        }



        if(bEEPROMCollected && bEEPROMDataReady)
        {       
                                //stop collecting data
                bCollectEEPROM = false;                 
        }

        bEEPROMDataReady = false;

The function fr = f_open(&File, file_name, FA_OPEN_APPEND | FA_WRITE); returns FR_OK and correctly writes the data to the SD card. This function is called whenever data is ready and stops after the data is collected.

The second time I Call the function it is programmed like this:

if(bWriteSDOK == true && (/*some other values are true*/)
        {
            //get current time
            RTC_GetDateTime(&rtcCurrentTime);

            fr = f_open(&File, file_name, FA_OPEN_APPEND | FA_WRITE);

            if (fr == FR_OK) 
            {
                //print the current date and time to the SD card
                f_printf(&File, "%02x/", rtcCurrentTime.date);
                f_printf(&File, "%02x/", rtcCurrentTime.month);
                f_printf(&File, "%02x ", rtcCurrentTime.year);
                f_printf(&File, "%02x:", rtcCurrentTime.hour);
                f_printf(&File, "%02x:", rtcCurrentTime.min);
                f_printf(&File, "%02x,", rtcCurrentTime.sec);


                f_printf (&File, "\r\n");                       /* Put a formatted string to the file */

                /* Close the file */
                f_close(&File);


            }
            else if(fr == FR_DISK_ERR)
            {
                PORTD |= (1 << 6);
                f_close(&File);
            }


            bWriteSDOK = false;

I can't display my code fully. I don't think it matters. What confuses me is that the second (not really second, just another function) time I call the function to open_append the file, it returns an error (the LED on PB6 turns on). The FATFS website doesn't fully explain the error. Does anyone know why this happens?

I know the second part of the code has worked before and fully tested this on the same hardware. Somehow the first part of the software created a bug in the second part, which has not changed.

I expect the first piece of code to write 16 lines of 16 bytes of EEPROM. The next time it has to show other data, like current date/time etc.

EDIT: I have traced it back to the following function:

static FRESULT move_window (    /* Returns FR_OK or FR_DISK_ERR */
    FATFS* fs,          /* Filesystem object */
    DWORD sector        /* Sector number to make appearance in the fs->win[] */
)
{
    FRESULT res = FR_OK;


     if (sector != fs->winsect) {   /* Window offset changed? */
#if !FF_FS_READONLY
        res = sync_window(fs);      /* Write-back changes */
#endif
        if (res == FR_OK) {         /* Fill sector window with new data */
            if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK) {
                sector = 0xFFFFFFFF;    /* Invalidate window if read data is not valid */
                res = FR_DISK_ERR;
            }
            fs->winsect = sector;
        }
    }
    return res;
}

the function 'if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK)' generates the FR_DISK_ERR. What does this mean? It says /* Invalidate window if read data is not valid */, but I am not reading any data