cancel
Showing results for 
Search instead for 
Did you mean: 

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

JulesSTM32
Associate II

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?

16 REPLIES 16
Saket_Om
ST Employee

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.

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

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. 

 

 

 

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?

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar
AScha.3
Chief III

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.

If you feel a post has answered your question, please click "Accept as Solution".

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 ? 

Hello, I tryed to change the interrupt priorities in differents cases but it didn't solve the problem. 

I print the free memory at the beginning of the execution so I am sure I have enough space on my SD card. After that, I start the SAI DMA and then I cannot access to the SD card anymore. 

Hello @JulesSTM32 

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

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

The examples doesn't provide .ioc file so I cannot compare the settings. 

I reproduced the tutorial but it didn't work on my board. (f_getfree error (3), f_open error (1)) 

With a logic analyser I noticed that nothing happens on the CLK pin. 

I observed a diffence between my case an the tutorial : in CubeIDE, I don't have the window "DMA settings" in the SDMMC settings section. Moreover, in the video, there is nothing done to bind the SDMMC instance "hsd1" and FatFs. Is it normal ? 

I think my SD card board is only compatible with SPI protocol, I'll buy one that accepts also SDIO. 

 

 

 

Here is a part of my code.