Description

100%
100%

You already know what are you looking for and that’s why probably you are here. So I am not going to waste your time with the facts that what is an SD card ? How does it work and all these..

In this tutorial we will interface a SD card with stm32 micro controller using SPI mode. I am using STM32F103C8 controller and SD card size is 1 GB. Also we will do some basic file handling operations such as creating a file, writing, reading, deleting etc.

Some advanced operations, i.e. directory related operations, will be covered in another tutorial. So let’s jump right into it.

Reference: https://m.blog.naver.com/PostView.nhn?blogId=eziya76&logNo=221188701172&proxyReferer=https%3A%2F%2Fgithub.com%2Feziya%2FSTM32_SPI_SDCARD

HOW TO

Obviously first we need to create a project in CubeMx and make sure you follow the steps below.

Make sure you select the fatfs and chose the settings as above. I am also selecting uart in my project so that the data can be sent to the computer.

After the project is generated, open it and Now we need to copy some library files in the project folder.

copy the files fatfs_sd.c and fatfs_sd.h in the respective src and inc folders. After successfully copying, we need to do some basic defines and all.

First, open the fatfs_sd.c file and edit the following

/* defines for the CS PIN */
#define SD_CS_GPIO_Port GPIOB
#define SD_CS_Pin GPIO_PIN_0

/* manage your SPI handler below */
extern SPI_HandleTypeDef hspi1; 

Next, in the user_diskio.c file, we have to make some changes and you can see them, once you download the code.

We also need to setup the Timer 1 and Timer 2 functions. These values should constantly decrease every 1 ms and that’s why we define them inside the interrupt handler, in the systick handler.

So, open the STM32f1xx_it.c and define the following:

volatile uint8_t FatFsCnt = 0;
volatile uint8_t Timer1, Timer2;

void SDTimer_Handler(void)
{
  if(Timer1 > 0)
    Timer1--;

  if(Timer2 > 0)
    Timer2--;
}



void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

	  FatFsCnt++;
	  if(FatFsCnt >= 10)
	  {
	    FatFsCnt = 0;
	    SDTimer_Handler();
	  }

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

This completes the setup part. Now let’s take a look at the working

Some Insight into the CODE

  /* Mount SD Card */
    fresult = f_mount(&fs, "", 0);
    if (fresult != FR_OK) send_uart ("error in mounting SD CARD...\n");
    else send_uart("SD CARD mounted successfully...\n");

f_mount will mount the SD card. fs is the pointer to the file system, which we need to define as a global definition, as we are going to use it a lot.

once the card is mounted, we will create a file and to do that, we will use the following function

/* Open file to write/ create a file if it doesn't exist */
fresult = f_open(&fil, "file1.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);

if the file already exists, this will open it and if it doesn’t exist, it will create a new file and than open it.

fil is the pointer to the file, which is also global.
“file1.txt” is the name of the file, that we want to create
the third parameter is the attributes given to the file. Below is the table for that

once the file is opened, we can write the data using the function below

/* Writing text */
strcpy (buffer, "This is File 2 and it says Hello from controllerstech\n");

fresult = f_write(&fil, buffer, bufsize(buffer), &bw);

here bw is the variable to keep the count of the number of bytes written to the file.
buffer contains the string, that we want to write into the file.

After writing, it’s time to close the file and to do that we can use

/* Close file */
f_close(&fil);

Now it’s time to read the data, we just wrote into the file. So, let’s open it again with read attribute

/* Read data from the file
f_read (&fil, buffer, fil.fsize, &br);
send_uart(buffer);

/* Close file */
f_close(&fil);

the file opened, and saved the data into the buffer. bw is the count of the number of bytes read from the file.
than we can send the buffer to the uart and at last close the file.

To delete the above created file use the function below

fresult = f_unlink("/file1.txt");
if (fresult == FR_OK) send_uart("file1.txt removed successfully...\n");

/* Unmount SDCARD */
fresult = f_mount(NULL, "", 1);

so, f_unlink will delete the file.
“/file1.txt” is the path to the file that you want to delete

And finally we will unmount the CARD using the f_mount function again. But this time the file system pointer will be NULL.

YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST

100%
100%

Connections

100%
100%
100%
100%

Result

100%
100%
Check out the VIDEO Below

https://youtu.be/spVIZO-jbxE

100%
100%

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

100%
100%

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
MahdyTaylor Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Taylor
Guest
Taylor

This driver is overflowing my RAM by a whole lot. Are there any large memory allocations in it?

Mahdy
Guest
Mahdy

Thanks a lot 😊

Menu