Showing results for 
Search instead for 
Did you mean: 

Is SDIO f_write with DMA blocking - Need to log sensor data to SD card



I have a Nucleo-L476RG development board that I am evaluating for my application. One of my basic requirement is to read data from an IMU sensor ICM20948 ( using I2C and write those data to a SD card.

I am using an interrupt based I2C read method ( HAL_I2C_Mem_Read_IT + HAL_I2C_MemRxCpltCallback ) to read data at 300Hz from the sensor. I need to save 12 float variables and I am planning to hookup a SD card to the development board to save sensor data, something like which supports both SPI and SDIO interface. Since STM32-F476 also supports SDIO I figured I will give it a try since everywhere it says SDIO is faster than SPI.

So far from what I gathered from searching online is that STM32CubeIDE uses a middleware FatFs to interact with the SD card. There is f_write function which writes to SD card but that is a blocking function, I would much prefer a non blocking write function and I came across this application example by STM Bugs aside it doesn't explicitly say if f_write + DMA is non-blocking or not and no matter where I look I can't find the answer. Also in the example link the f_write call ends before the main while loop, in my application f_write needs to write at regular intervals and not just once.

So is f_write + DMA example as shown a non-blocking function? Ideally I was hoping that there will be a SDIO HAL interface similar to HAL_I2C_Mem_Read_IT and I can maintain a large data buffer from which I will kick off a non blocking f_write for half of the buffer while I fill the other half of the buffer and continue Ad infinitum. Is this possible with reasonable effort or the complexity involved is so huge that it is on the verge of being impossible? Looking forward to hearing this community thoughts regarding this.



ST Employee

Hello @nicobari007 , you can write to SD card in DMA mode (circular buffer with notification) by using function HAL_SD_WriteBlocks_DMA(). This function support only 512-bytes block length (the block size should be chosen as 512 bytes). You can choose either one block read operation or multiple block read operation by adjusting the "NumberOfBlocks" parameter. After this, you have to ensure that the transfer is done correctly. The check is done through HAL_SD_GetCardState() function for SD card state. You could also check the DMA transfer process through the SD Tx interrupt event.

Please mark my answer as best by clicking on the “Accept as solution" button if it fully answered your question. This will help other users find this solution faster.

Thanks for your contribution.