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.

CubeMX Setup

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


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

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

  Mount_SD("/");
  Format_SD();
  Create_File("FILE1.TXT");
  Create_File("FILE2.TXT");
  Unmount_SD("/");

Above, I am mounting the SD card, and than formatting it to remove any file 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.


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

indx++;

HAL_Delay(2000);

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



Result

file data
uart output

Check out the Video Below




Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Subscribe
Notify of

34 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
keyboard_arrow_up