STM32F407ZG cubemx+SDIO+ DMA+FATFS +FreeRtos usage record

Keywords: stm32


cubemx+SDIO+ DMA+FATFS +FreeRtos (usage record)

preface

After three weeks, I finally got out the sd card read-write test
This is mainly based on the blogger's tutorial

Copyright notice: This article is the original article of CSDN blogger "zl199203", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/zl199203/article/details/83513768
I suggest you come back after reading it

1, Normal mode control SD

After following the steps of the blogger step by step, you may find that you can't achieve his effect
But the method of a little brother in the comment area is very useful
Between / * USER CODE BEGIN 2 / and / USER CODE END 2 * /
call

HAL_SD_InitCard(&hsd);
(this super key!!)
The specific locations are as follows:

Just compile it again
But if not
It is recommended to power off the board and then plug in and out the sd card again
ps: this method may only apply to my board, or it is possible, but you can try it

2, Using DMA to control SDIO

After the above operation, the test results of the above recommended bloggers can appear
Look at his second article
I also give a link here
https://blog.csdn.net/zl199203/article/details/83514030
Follow the steps of the blogger here. It's no big problem
But I got stuck at first
Because of the clock enable position of a DMA2
Here we provide two modification methods

  1. Directly modify the initialization sequence
    If your initial program initialization sequence is like this

    Then you can put the MX_DMA_Init(); Cut
    Then paste into MX_SDIO_SD_Init(); Above function
    As shown in the figure

    Just change the order
    2. Add code under sdio.c file
    You can keep the order of the first picture above unchanged
    Open the sdio.c file
    Find HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
    This function you will see

    Then add the code in the blank space in the red box
    __HAL_RCC_DMA2_CLK_ENABLE();
    //This sentence can be found in MX_DMA_Init(); Found in function
    The effect is shown in the figure

    Then compile
    But if not
    It is also recommended to power off the board and then plug in and out the sd card again
    It's still very effective to try it yourself

3, Using the FatFs file system

Post a link to the blogger's article
https://blog.csdn.net/zl199203/article/details/83514105
If you and I use the same board (punctual atomic Explorer)
I've tried to add something behind the blogger, but it can't achieve the effect of the blogger
Here's the point!
Just add a few sentences of code to achieve the effect of bloggers
First, let's now / * USER CODE END PV * / above
Add code

BYTE work[512];

The effect is shown in the figure

Then go to the main() function
Add code (provided you have bsp_driver_sd.c file, which seems to be configured with stm32cubemx)
BSP_SD_Init();
f_mkfs((TCHAR const*)SDPath,FM_ANY,0,work,512);
The specific locations are as follows:

	int main(void)
{
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  /* USER CODE END Init */
  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SDIO_SD_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  BSP_SD_Init();//Added
  MX_FATFS_Init();
  f_mkfs((TCHAR const*)SDPath,FM_ANY,0,work,512);//Added
  /* USER CODE BEGIN 2 */
	HAL_SD_InitCard(&hsd);
//  SD_EraseTest();
//  SD_Write_Read_Test();
		Fatfs_RW_test();
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }. 
  /* USER CODE END 3 */
}

After adding, it should be OK
Here I attach F_ The definition of parameters in the mkfs () function is worried that some friends may not
work: the first step above has been defined
SDPath: it seems that it is automatically defined in fatfs.c and fatfs.h
fatfs.c :

uint8_t retSD;    /* Return value for SD */
char SDPath[4];   /* SD logical drive path */
FATFS SDFatFS;    /* File system object for SD logical drive */
FIL SDFile;       /* File object for SD */

fatfs.h

extern uint8_t retSD; /* Return value for SD */
extern char SDPath[4]; /* SD logical drive path */
extern FATFS SDFatFS; /* File system object for SD logical drive */
extern FIL SDFile; /* File object for SD */

FM_ANY: defined in FF. H (this is also automatically generated and should be available)

#define FM_ANY		0x07

4, Add FreeRtos support.

Also attach the blogger's link
https://blog.csdn.net/zl199203/article/details/83514223
It feels a little different from our interface
The cubemx configuration FREERTOS interface is attached, and others that are not released are similar to the previous ones


NVIC :

Then after generating the code
Comment out or cut the following code in main()

	BSP_SD_Init();
  	MX_FATFS_Init();
	f_mkfs((TCHAR const*)SDPath,FM_ANY,0,work,512);
	HAL_SD_InitCard(&hsd);
	Fatfs_RW_test();

Then put the above sentences under the freertos.c file
void StartDefaultTask(void *argument) function
The function contents are as follows:

void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
	BSP_SD_Init();
 	MX_FATFS_Init();
	f_mkfs((TCHAR const*)SDPath,FM_ANY,0,work,512);
	HAL_SD_InitCard(&hsd);
	Fatfs_RW_test();
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

Then main() is like this

summary

First blog
Some places may not be very good
Please forgive me
I hope I can help you!
thank you

Posted by Huijari on Sat, 25 Sep 2021 22:55:43 -0700