Interface SD CARD with SDIO in STM32

I already covered How to use SD card in STM32 using SPI. You can check that out HERE.
Today in this tutorial, we will interface the SD CARD using SDIO in STM32. Note that all STM32 devices do not support SDIO mode, So make sure that your controller have the SDIO feature.


We will start by setting up the CubeMx first. So select the SDIO and than select 4 bit bus, and leave everything else unchanged. If you get FR_DISK_ERROR, than increase the SDIOCLK divide factor in the picture below

sdio setup

Next, select the FATFS, and than select SD CARD. I am leaving everything to default.

fatfs setup

Now open the project and copy file_handling.c, file_handling.h, files in the project folder.

Some insight into the code

You can check the functions used in the file_handling.h file. Some of those functions are discussed below.

/* mounts the sd card*/
void Mount_SD (const TCHAR* path);

/* unmounts the sd card*/
void Unmount_SD (const TCHAR* path);

As the name suggests, they are used for mounting and un-mounting the SD CARD

/* Start node to be scanned (***also used as work area***) */
FRESULT Scan_SD (char* pat);

Scan for the files and directories present in the SD CARD from the path input in the argument. It sends the details directly to the UART.

/* creates a directory
 * @ name: is the path to the directory
FRESULT Create_Dir (char *name);

Creates the directory in the input path, entered in the argument. For example Create_Dir ("/dir1"); will create a directory named dir1 in the root of the SD CARD itself.

/* Removes the file from the sd card
 * @ name : is the path to the file*/
FRESULT Remove_File (char *name);

Removes the file or the directory, whose name and path is in the argument. Note that directory can only be removed, if it is empty. For example Remove_File("/FILE.TXT"); removes the FILE.TXT from the root of the SD CARD.

/* creates the file, if it does not exists
 * @ name : is the path to the file*/
FRESULT Create_File (char *name);

Creates a file, (name and path is in the argument), at the entered path in the SD CARD. If the file already exists, it will return an error. For example Create_File("file1.txt"); will create a file named file1.txt in the root directory.

/* read data from the file
 * @ name : is the path to the file*/
FRESULT Read_File (char *name);

/* write the data to the file
 * @ name : is the path to the file*/
FRESULT Write_File (char *name, char *data);

/* updates the file. write pointer is set to the end of the file
 * @ name : is the path to the file
FRESULT Update_File (char *name, char *data);

are used to Read, Write, and Update data to the file. These functions will output the result of each operation to the Uart.

main function


Above, I am mounting the SD card, and than formatting it to remove any filr present in the root directory. It can’t remove directories right now
I have created 2 files. These are empty right now, and i will update the data later in the code.

sprintf(buffer, "Hello ---> %d\n", indx);
Update_File("FILE1.TXT", buffer);
sprintf(buffer, "world ---> %d\n", indx);
Update_File("FILE2.TXT", buffer);



As you can see above, the data in the files will keep updating every 2 seconds.


file data
uart output

. . . . . DOWNLOAD . . . . .

You can buy me a coffee Sensor by clicking DONATE OR Just click DOWNLOAD to download the code

Notify of
Oldest Most Voted
Inline Feedbacks
View all comments