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.
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
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:
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
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
Now it’s time to read the data, we just wrote into the file. So, let’s open it again with read attribute
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
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