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
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.
As the name suggests, they are used for mounting and un-mounting the SD CARD
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 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 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 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.
are used to Read, Write, and Update data to the file. These functions will output the result of each operation to the Uart.
Now the main function is as shown below
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.
As you can see above, the data in the files will keep updating every 2 seconds.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST