Skip to main content
Graduate
February 25, 2025
Question

FATFS f_write error (9) when SAI/DMA is running

  • February 25, 2025
  • 3 replies
  • 2239 views

Hello, 

I am trying to record audio data from an ADC to a SD card with a nucleo STM32L552ZET6QU board. To write data into SD card using FatFs I initialised it  : 

"// Open the file system - mount the drive

fres = f_mount(&FatFs, "", 1); // 1 = mount now

if (fres != FR_OK) {

myprint("f_mount error (%i)\r\n", fres);

while(1);

}

// Statistics from the SD card

DWORD free_clusters, free_sectors, total_sectors;

FATFS* getFreeFs;

fres = f_getfree("", &free_clusters, &getFreeFs);

if (fres != FR_OK) {

myprint("f_getfree error (%i)\r\n", fres);

while(1);

}

//Formula comes from ChaN's documentation

total_sectors = (getFreeFs->n_fatent - 2) * getFreeFs->csize;

free_sectors = free_clusters * getFreeFs->csize;

myprint("SD card stats:\r\n%10lu KiB total drive space.\r\n%10lu KiB available.\r\n", total_sectors / 2, free_sectors / 2);

// Open file

fres = f_open(&fil, "record.bin", FA_WRITE | FA_CREATE_ALWAYS);

if(fres == FR_OK)

{

myprint("I was able to open 'record.bin' for writing\r\n");

}

else

{

myprint("f_open error (%i)\r\n", fres);

}

 

float data[4] = {1.2, 1.6, -0.5, 3.2};

UINT bytesWrote;

fres = f_write(&fil, data, sizeof(data), &bytesWrote);

if(fres == FR_OK)

{

myprint("Wrote %i bytes to 'record.bin'!\r\n", bytesWrote);

}

else

{

myprint("f_write error (%i)\r\n", fres);

}"

 

Everything goes fine, except when the SAI is enabled with I2S mode coupled with DMA1 and initialise with this line of code : 

"HAL_StatusTypeDef status = HAL_SAI_Receive_DMA(&hsai_BlockA1, (uint8_t *)adcData, BUFFER_SIZE*4);" 

After that, I only get "f_write error (9)" when I try to write into my file created before.

There must be a conflict between SAI/DMA and FATFS but I couldn't resolve it.

 

SD card is linked to SPI3 which is on APB1, SAI1 is on APB2 and DMA1 is on AHB1.

My SD card is formated in FAT32.

 

What could be the problem?

    This topic has been closed for replies.

    3 replies

    Technical Moderator
    February 27, 2025

    Hello @JulesSTM32,

    Could you please share your project so that we can support you more effectively?

    Otherwise, could you try using polling mode for SAI and SPI? The source of the issue might be a bad management of interrupt priorities.

     

    Graduate
    February 27, 2025

    Hello, here is the link of my github repository : https://github.com/JulesAcwa/myproject_I2S_SAI_and_FATFS .

    In theory, there is enough time to write my data in the SD card before another DMA interrupt happens. 

    I don't use polling mode with the SAI in order to avoid data loss. The SPI is used under FATFS f_write function, which is equivalent to a polling mode isn't it ?  

    Thank you for your reply. 

     

     

     

    Technical Moderator
    February 28, 2025

    Hello @JulesSTM32 

    Could you please check with different DMA interrupt priorities? I noticed that both channels are currently set with the same interrupt priorities.

    Does the failure occur immediately upon starting execution, or does it happen after some time has passed?

    Are you sure you have enough space on the uSD card?

     

    Super User
    February 28, 2025

    Hi,

    just - why dont you use SDMMC for sd-card ? L552 has a sdmmc interface , afaik.

    I use SAI + SDMMC /sd-card for my audio player and there is no problem with them working same time.

    Graduate
    March 3, 2025

    Hi, I couldn't find a good tutorial using SDMMC interface for this specific application. Could you share your project with me so I can see your configuration and how you interact with the SD card ? 

    Technical Moderator
    March 4, 2025

    Hello @JulesSTM32 

    Please refer to these materials if you want to use SDMMC.

    Graduate
    March 12, 2025

    Hello,

    I still have f_mount error (3). I double check de software configuration and I have no idea how to resolve it. However HAL_SD_GetCardStatus() works.

    Thanks, 

    Jules.

     

    Technical Moderator
    March 14, 2025

    Hello @JulesSTM32 

    Did you try to compare your config with the example Projects/STM32L552E-EV/Applications/FatFs/FatFs_uSD_DMA.