Posted on October 10, 2018 at 12:14 PM
This guide will illustrate using the NUCLEO-F446RE SDIO with DMA and ChanFS. CubeMX is used to generate the project files. It aims to fill the gap between the manuals on the HAL drivers, the STM32F4 referece manual and the CubeMX cnfiguration boxes.
The document will follow the steps for configuring the NUCLEO-F446RE and setting up the project to:
The steps are:
This project will use a Nucleo baord, with the STM32F446re chip.
The STM32F446re datasheet tells us The Secure digital input/output interface (SDIO):
The nucleo donsn't have an SD so will need to add one. Options inlcude, Make your own board or buy a breakout.
Important points on hardware:
In this section we choose the SDIO mode, 1-Bit or 4-Bit. Up to you based on the pins available. The generated code will check if the card can supprot 4-bit and revert to 1-bit if it cannot. Here I use 4-bit.
There are options on which pins you use for the SDIO interface, make sure you have it matchng your hardware/wiring. The above is the one I default to.
Two additional GPIO are used in addition to the nucleos onboard led and push button.
The two additional GPIO are optional but nice to have. I use the SD present to see if there is a card and the SD power to turn on the SD when a card is inserted/removed. This was more a legacy solution from when I was having issues with power usage and MCU hard faults.
Important the maximum SDIO clock is 48MHz. I have found that 24Mhz if often the most can run in 4bit mode. If your card fails to mount check the speed and also that have Data and Command lines as pull-up.
Use expand is not needed for this example -- but I have left it here as a reminder -- it allows expanding a new file to a set a filesize and with continous blocks I can write driectly using low level access and not request the next block location each sector write. This allow me to speed things up significantly and with minimal effort but more on that anoter time.
Important point on SD present: FatFS has check box for SD present but only allows GPIO Input pins to be used... That is if you use a EXTI it will not allow you to select it which I think is a pain as I would like to run code when chip is removed or inserted. There are a number of ways around this:
To get it working
As I have High when SD is present, I just add one line tp fatfs_platform.c
/* USER CODE BEGIN 1 */
status =! status;
The following code is basic test that
/*## Initilise SD card hardware #################################*/
uint8_t mr = BSP_SD_Init();
if(mr != 0){
//turn off SD
while(1);
}
/*## Mount file system of the Initilised SD card #################################*/
FRESULT fr = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);
if(fr != FR_OK){
//turn off SD
while(1);
}
/*## Open and create a text file #################################*/
fr = f_open(&SDFile, "BoomLvL.txt", FA_WRITE | FA_CREATE_ALWAYS);
if (fr != FR_OK){
while(1);
}
char data[] = "Verion2_dma\n\0";
UINT written;
UINT len = (UINT)strlen(data);
/*## Write data to the text file ################################*/
fr = f_write(&SDFile, &data[0],len, &written);
if (fr != FR_OK || written != len) {
while(1);
}
/*## Close the open text file #################################*/
fr = f_close(&SDFile);
/*## Set the LD2 so know all good #################################*/
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
Run your deugger and make sure get to the end.
If you want to do somehting real, like make a logger follow examples in Chan FatFS
The guide has stepped through the process of creating a project to set up the SDIO interface on a STM32F4. The SD interface of these low cost Nucleo baords have proven to be are very powerful and convenient for a number of my projects and hope this guide will help you to realse the same.
Any questions or suggestions feel free use the contact me.
The following are the documents referenced in this guide and the key documents for any project using this STM32L476 microprocessor.