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 a SDIO feature before commenting.
Along with interfacing via SDIO, we will also control all the file operations via the UART using Ring buffer, which I already covered HERE.
We will start by setting up the CubeMx first. So select the SDIO and than select 4 bit bus, and leave everything else unchanged.
Next, select the FATFS, and than select SD CARD. We just need to make a small change here. Increase the maximum sector size to 4096.
Now in order to setup the UART part, all we need to do is select the UART, and turn the interrupt on and leave the BAUD rate at 115200.
Now open the project and copy
file_handling.c, file_handling.h, Uartringbuffer.c and Uartringbuffer.h files in the project folder.
To setup the Ring buffer, we need to redirect the default ISR to the one written in the ringbuffer.c file. So to do that, first open the stm32f4xx_it.c file and than define the following in it
extern void Uart_isr (UART_HandleTypeDef *huart);
After that, comment out the uartIRQhandler and replace it with the Uart_isr as shown in the picture below.
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("/dir1"); removes the dir1 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("/dir1/file1.txt"); will create a file named file1.txt inside the directory dir1.
are used to write, read, and update data to the file. These functions are specifically written, in order to be used with the UART ring buffer. You can modify them, if you want to use them in any other way. You can check my SD CARD WITH SPI tutorial for reference.
In the main function, we are going to initialize the ring buffer and also mount the SD card
Now, inside the while loop, we have to constantly check for the command that was sent via the UART and if the command matches to the preset ones, we will perform the respective operation
Get_string copies the entire string from the incoming data to the buffer. It looks for the terminating character (‘\n’) in the incoming data. Once the terminating char is detected, this function will copy the string from the beginning into the buffer.
cmdlength determined the length of the command in the string. It looks for the space (‘ ‘). Once the character is detected, it will return the length of the command.
get_path copies the path from the buffer to the path buffer. It start copying from the cmdlength and copies up to the end of the string in the buffer.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST