Hello to all!
Today we'll try to connect SD-card to our STM32 microcontroller and to create a FAT filesystem on it. In order to test the filesystem we'll create a new file and write some data to it.
In previous posts I've used the STM32F4Discovery board, but today the development board with the STM32F10x MCU mounted on it will be tested. In spite of the fact that we'll use another board, all the phases of creating the project will be the same. Let's launch the STM32CubeMx and configure all needed units!
The SD-card is connected to the STM32 via SDIO interface, so, first of all, we should enable it. After that the SD-card checkbox will become available for checking:
As you see STM32CubeMx marks all pins involved in our project. Please note that UART4 and UART5 units became unavailable. It is connected with the fact that the pins needed for proper UART4/UART5 work are involved in SDIO interface connections. Let's proceed to the "Configuration" tab, where we can see the new modules available for some extra configuration:
If we click on the SDIO button, a new window, where the clock divider for SDIO interface can be set, will appear:
Similarly, we can open a FatFs configuration window, but in this project we have no need of any extra settings of FAT. Thus, we can start a new project generation!
If we open the project in IDE we'll see that besides initialization functions for all used units, STM32CubeMx calls one more function named FATFS_LinkDriver(). Let's look into this function more carefully. STM32CubeMx has generated the sd_diskio.c in order to implement write and read operations. It contains the following functions:
SD_initialize, SD_status, SD_read, SD_write, SD_ioctl
Moreover SD_Driver struct, which contains the pointers to these functions is defined. And the FATFS_LinkDriver() function is exactly what binds the SD_Driver struct and the current FAT disk number. As it is the first call of this function the disk number is set to 0. If we'll decide to use one more disk, for example, NAND, we should pass number "1" into this function.
So, now it's quite clear, let's add some code in order to create a new file:
/* USER CODE BEGIN 3 */ FATFS fileSystem; FIL testFile; uint8_t testBuffer[16] = "SD write success"; UINT testBytes; FRESULT res; if(f_mount(&fileSystem, SD_Path, 1) == FR_OK) { uint8_t path[13] = "testfile.txt"; path[12] = '\0'; res = f_open(&testFile, (char*)path, FA_WRITE | FA_CREATE_ALWAYS); res = f_write(&testFile, testBuffer, 16, &testBytes); res = f_close(&testFile); } /* Infinite loop */ while (1) { } /* USER CODE END 3 */
This code creates a new file and writes an amount of data to it. After that the file is closed. If we'd forget to close the file the data wouldn't be written. Now we can compile the project and program the MCU.
To check the result of our work we should take the SD-card out of the development board and insert it in the cardreader. We can see the new file named testfile.txt with our data:
So the write operation was properly completed and our project is correct! Well, I’ll stop here, thank you for your attention! 🙂
Full project can be found here - STM32Cube_SDCardProject.