FATFS第二次使用相同的代码行时返回FR_DISK_ERR

时间:2019-01-04 14:35:22

标签: c arduino avr atmega fatfs

我正在使用FATFS将数据写入SD卡。它可以部分工作,并且我能够将EEPROM数据写入SD卡。但是,当我稍后在代码中使用其他函数时,即使我使用的是同一行代码,它也会返回“ FR_DISK_ERR”。

我第一次尝试写入SD卡的操作如下(此时,我已经初始化了SD卡并制作了文件,这不是问题)

        //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;

函数fr = f_open(&File,file_name,FA_OPEN_APPEND | FA_WRITE);返回FR_OK并将数据正确写入SD卡。每当数据准备就绪时就会调用此函数,并在数据收集后停止。

我第二次调用该函数是这样编写的:

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;

我无法完全显示我的代码。我认为没关系。令我感到困惑的是,第二次(不是真的第二次,只是另一个函数)在我调用函数open_append时,它返回一个错误(PB6上的LED点亮)。 FATFS网站没有完全解释该错误。有谁知道为什么会这样吗?

我知道代码的第二部分以前已经工作过,并且已经在同一硬件上对其进行了全面测试。该软件的第一部分以某种方式在第二部分中创建了一个错误,该错误没有改变。

我希望第一段代码写入16行的16字节EEPROM。下次必须显示其他数据时,例如当前日期/时间等。

编辑: 我将其追溯到以下功能:

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;
}

函数'if(disk_read(fs-> pdrv,fs-> win,扇区,1)!= RES_OK)'生成FR_DISK_ERR。这是什么意思?它显示/ *如果读取的数据无效,则使窗口无效* /,但我没有读取任何数据

0 个答案:

没有答案