Description

100%
100%

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.

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.

SDIO

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.

FATFS

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.

UART

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.

ISR

Some insight into the code

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

void mount_sd (void);
void unmount_sd (void);

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

FRESULT scan_files (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.

void 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.

void 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("/dir1"); removes the dir1 from the root of the SD CARD.

void 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("/dir1/file1.txt"); will create a file named file1.txt inside the directory dir1.

read_file (path);
write_file (path);
update_file (path);

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

Ringbuf_init();
mount_sd();

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

while (IsDataAvailable())
{
  Get_string(buffer);
  int len = cmdlength(buffer);
  get_path ();
  if (!(strncmp ("ls", buffer,len))) cmd = 'l';
  if (!(strncmp ("mkdir", buffer,len))) cmd = 'm';
  switch (cmd)
  {
	  case ('l'):
		  scan_files(path);
      	  	  cmd = 0;
     	  	  clear_buffer();
      	  	  clear_path();
      	  	  break;

	  case ('m'):
		  create_dir (path);
  	  	  cmd=0;
  	  	  clear_path();
  	  	  break;
}
}

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

100%
100%

Result

100%
100%
Result- SDIO UART
Check out the VIDEO Below
100%
100%

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

100%
100%

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
İsmail Erol Coder Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
İsmail Erol Coder
Guest
İsmail Erol Coder

Hi , controllerstech’s family. I have this kit . I follow your channel. Can u share ili9481 LCD screen using with this kit.
I didn’t find it.

Menu