cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 FreeRTOS+FatFs+SD f_open() throws FR_DISK_ERR in application startup hook but not in any other tasks

JRade
Associate II

I am using an STM32L475VG on the B-L475E-IOT01A discovery board. I am running into a strange problem where whenever I try to create a file within a task it throws back FR_DISK_ERR every time, but succeeds within the startup hook or within the main() before the scheduler starts.

The other strange thing occurring is that I CAN read/write from and to files within a task, as long as the file exists on the SD card beforehand. I would like to be able to create the file within a task as it is dependent on user input.

A snippet is below. Currently this exists within the startup hook (so it works) but as soon as I place it into a task that's started from somewhere else, the f_open() call returns FR_DISK_ERR. If i replace the third argument to f_open with ONLY FA_WRITE or FA_READ, as long as the file exists the call will succeed in both the startup hook and any task.

    FIL LogFile;
    FRESULT res;
    char fr_str[prvSD_FR_STR_LEN];
    UINT bw;
    BaseType_t retval;
 
    char buffer[27] = "Time,A0,A1,A2,A3,A4,T0,T1\n";
 
    res = f_open(&LogFile, prvSD_LOG_FILENAME, FA_CREATE_ALWAYS | FA_WRITE);
    prvFAT_FResult_To_String(fr_str, res);
 
    configPRINTF(("Log file open result is %s\r\n", fr_str));
    
    if(res == FR_OK) {
        res = f_write(&LogFile, buffer, 26, &bw);
        prvFAT_FResult_To_String(fr_str, res);
 
        configPRINTF(("Log file write result is %s, bw is %d\r\n", fr_str, bw));
 
        if(res == FR_OK && bw != 0) {
            retval = pdPASS;
        }
        else {
            retval = pdFAIL;
        }
    }
    else {
        retval = pdFAIL;
    }

I have reentrancy enabled and I have all the interrupts configured for the SPI driver. Has anyone run into this problem before?

1 REPLY 1

You'd really need to keep the FATFS or SPI stuff in a singular thread, or have mutex to arbitrate usage.

Instrument the error paths of the read/write functions in DISKIO driver layer to understand interactions. Failure is frequently cascading.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..