 Hi, my name is Nicolas Fillon. I'm a field application engineer supporting ST microcontrollers. In this video, I will explain you how to create a file system on a SD card using SDM42 and ST toolset. This is useful for data logging type of application where you need to write data files on an external media like a SD card. Let's start by looking at the most used types of memory cards. Here are the main memories available today on the markets. First, we have the PC cards or PC MCIA, then we have the compact flash or CF. We also have the multimedia cards or MMC. We can find memory sticks or MS, and then the secure digital cards or SD card. In this video, we will be using micro SD cards which are easy to find and affordable. Now, we will look at different STM42 peripherals that can be used to interface to SD card. Here are the three options with pros and cons. Starting from the left, the SPI is a very easy peripheral to use with reduced number of IOs that lacks hardware features and also lacks speed compared to some of the other interfaces. The SDIO offers higher speed and is adding more hardware features, but it's using a lot more IOs than SPI. The SDMMC offers a lot of flexibility because it embeds many features in hardware directly and is an evolution of the SDIO interface. So in this video, I will be using the SDMMC. At this point, we have chosen a type of memory and a peripheral to interface it. We can now look at the firmware blocks needed to create a file system on the micro SD card using SDMMC. Starting from the bottom, we'll be using STM42 HAL and SIMC drivers contained in our STM42 cube library. For the middleware in blue, we'll be using the FATFS, which is an open source file system stack. I added a link to it if you want to find more information about it. Finally, we'll be adding the user code, so in green here, to format the media, create a file and add data to it. For this exercise, I'm going to use the STM42 F746 disk array kit. So the part number is STM42 F746-DISCO and is available for order at st.com. So I'm using this kit because it has a micro SD card slot on the back of it. Regarding the software, I will be using the STM42 cube ID, which is an SD tool in order to create a STM42 project where you can graphically configure the pinouts, the peripherals, the middleware, the clocks and more. So you can find this tool on our website at st.com. As you can see here and search for STM42 cube ID. If you are new to the STM42 cube ID, we have videos describing how to use it. You can check them out on our ST YouTube channel. We're going to open STM42 cube ID and create a new project. We will start with a board selector and select the STM42 F746-DISCO. So I'm going to open STM42 cube ID, double click, select our workspace directory. Then here, file, new, STM42 project. Now here, we're going to go to board selector and select the STM42 F746-DISCO. Double click on it or press next right here. We are going to give a name to our project. So in my case, STM42 underscore FS for file system, underscore SD for SD card, underscore V1. And then the rest will keep it as default settings. Then click finish. We are not going to initialize all peripherals with their default mode because we're going to configure them individually. So we're going to click no. In this graphical interface, we will start by configuring the SDMMC. So this is the peripheral you will be using to interface to the SD card. So you will find it under pinout and configuration tab, under connectivity, then look for SDMMC1. In the mode on the right, you want to select SD 4-bits white bus mode. Then in the configuration below, select NVIC tab and enable the global interrupt. Okay, so in this graphical interface, select pinout and configuration tab here. Then under connectivity, look for SDMMC1. So I'm selecting it here, expanding here, select the mode SD 4-bits white bus mode, then we go to the NVIC settings and select enable. In the DMA settings, we will add the configuration for DMA in reception and transmission for the SDMMC1. We will keep the default settings. In the DMA settings of SDMMC1 still, we're going to add the two DMAs. So one for the reception, one for our X and the other one for TX, so transmission. We'll keep the rest of the parameters by default. At this point, we can configure FATFS, so our middleware. So we'll find it in the middleware sections, still in the pinout and configuration tab. So you want to click on FATFS and then select SD card. The second configuration we'll do is that we're going to click on platform settings. So in the configuration below and then we'll select detect SDIO as PC 13. Now we can configure so the FATFS, so we'll find it here. So you scroll a little bit down, click on middleware, click on FATFS, so file system. And then we enable the SD card. The second configuration, as we said, was to configure PC 13 as the microSD detects. So that's the way it has been done on the board. Because we're using the DMA, in the advanced settings of FATFS, we want to enable the usage of the DMA template. In the advanced settings, enable the use DMA template. As you can see here, in the clock configuration tab, so there is an issue with the clock configuration indicated by this icon right there. So to resolve it, we're going to click on this icon right there. Jump here, I say yes, I want to resolve it. And now it's resolved. So you see if you don't have the issue anymore, so the clock configuration is correct. In the project manager settings, so the project manager tab, we are going to increase the minimum heap size and the minimum stack size. Because we're using middleware on the FATFS, so we recommend to increase the minimum heap size to 0x400 and the minimum stack size to 0x800. So in the project manager tab right here, from top, we're going to increase the minimum heap size to 400 and the minimum stack size to 800. We can now generate the code. To do this, you're going to go to file, save all. So that will ask you generate the code and press yes. Now we're going to change the perspective in order to go to a C and C++ perspective in order to add some code. So press yes again. Now it's time to add our user code or application code. First, in main.c, in the main function, we'll be looking for the user code section one and we will be adding these variables listed here. So one of the variables, the first one, will be the FATFS function common result code. The two other are the file write read counts. Then I will have like a file write buffer and a file read buffer. So all of these code right there to be added can be found in the description of this video. So look for it and then copy and paste it inside the user code section one. So in main.c right here, or if it's not open for you, you go find it under core, source, main.c. Then look for the code section one. So user code begin one. Inside main.c. Let's find it. Now we're going to be adding the code that we need. As I was telling you, in the description of the video, you will find the code to be added right here. Last but not least, we're going to add our user code inside the main function. So the code will be adding, same thing can be found in the description of this video. So we're going to add it in the user code section two. So look for user code begin two section and add it in between. So the code to be added will first mount in our DSD card, then we'll format it and then create a file. So sm42.txt create this file and then I'm going to add a string into it. To add the user code, look for the section user code begin two this time and then we'll add the application code. Same thing, you will find it in the description. So here, you see this section there. Copy this code until the end. Now we're going to add it right here. So the code first will mount DSD card, format it. Then we're going to create a file called sm42.txt and inside I'm going to write a string to it. So the string I'm going to write is right here. Sm42 filefs works great. Now that we added our application code, we can build the project. So go to the project here on the project explorer side, select your project, right click and then click on build projects. So this is my board. This is the sm42f746 discovery or disco and I'm going to insert the SD card. So I'm going to flip the board and we're going to insert the SD card here where the slot is located. So we're going to put our micro SD card in. All right, now I can flip the board back. Okay, so now it's time to power the board and also connect to the SD link. So this is the connector to be used on this side. And then on the other side, I'm going to connect it to my computer. The code builds correctly. So as you can see, no errors. So that's good. Now we can flash the code. So to do this, you go here, select debug as and sm42 application. We're going to switch to debug mode and now we're going to execute the code. So here, F8 or resume or v-side code right there with a green arrow. So click it and execute the code. After a few seconds of code execution, we can terminate the debug session on sm42 cube ID. And now we can unplug the board and we will extract the SD card from the board. Now I have a reader. So a card reader that I'm going to use and we'll use this in order to check the contents of the micro SD card. Let's verify that the file has been created correctly. So I'm going to go to my drive where my SD card is connected to and we have our file right there. So sm42.txt as we did in our user code. Let's check the contents. Yes, sm42 filefs works great. Thank you very much for watching.