cancel
Showing results for 
Search instead for 
Did you mean: 

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

JulesSTM32
Visitor

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?

0 REPLIES 0