提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
前言
开发工具:STM32F407VGT6、TF卡(32G)、JLINK调试器
参考:野火HAL库视频讲解(FATFS挂载SD卡读写)
代码放在文末。
一、CUBEMX配置
- SYS配置
- RCC配置
- SDIO配置 选择SD 4 bits Wide bus
Parameter Settings 选项中的 SDIOCLK clock divide factor 配置为2。
DMA Settings 选择左下角的Add选项添加SDIO_RX和SDIO_TX
NVIC Settings 处先勾选Enable,中断优先级(5 6 6 )在后面再设置。SDIO其他信息默认即可。
- FATFS处勾选SD Card,下面配置框的数据和选项默认即可,可以不修改。
System view选择左边的GPIO,打开GPIO窗口后选择SDIO窗口上拉所有SDIO的接口。
选择NVIC选项打开窗口后修改SDIO global iuterrupt的中断优先级为5,DMA2 stream3 为6,
DMA2 stream6 为6。
- 时钟树 HCLK处的时钟频率改为最大值(此处最大值为168)168。
- Project Manager(MX上方的四个大窗口中的第三个)的第一个选项Project处修改图片上的数据,MDK版本根据自己的修改。
最后生成代码GENERATE CODE。
二、添加代码main.c文件
1.main函数定义变量
头文件添加和相关变量的定义。
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PD */
//定义读写缓冲区 512字节(1个块的大小)
uint8_t wBuff[512];//写
uint8_t rBuff[512];//读
//实际读取/写入文件的数据数
uint32_t numRW;
/* USER CODE END PD */
2.文件系统函数
完成文件系统挂载和读写的功能实现。
/* USER CODE BEGIN 0 */
static void FS_File(void)
{
//挂载文件系统 0是稍后挂载,1是立即挂载
retSD = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);
//新建文件(如果文件不存在则新建,文件已存在则覆盖原有文件创建新文件)+ 写权限
retSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_CREATE_ALWAYS|FA_WRITE);
//填充数据到写缓冲区 要添加#include "stdio.h"和#include "string.h"头文件才能调用
sprintf((char *)wBuff,"%s","今天下雨\n");
//将写缓冲区的数据写到SD卡中
retSD = f_write(&SDFile,wBuff,strlen((const char *)wBuff),&numRW);
//关闭文件 一个open对应一个close否则会出错
retSD = f_close(&SDFile);
//打开已存在的文件 + 读权限
retSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_OPEN_EXISTING|FA_READ);
//将SD卡中的数据读到读缓冲区中
retSD = f_read(&SDFile,rBuff,sizeof(rBuff),&numRW);
//关闭文件
f_close(&SDFile);
retSD = f_mount(0, (TCHAR const*)SDPath, 0);//卸载文件系统
}
/* USER CODE END 0 */
在初始化都完成后,在while循环里添加测试函数。
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
FS_File();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
三、调试结果
f_mount处的retSD返回0x00是挂载成功了。
总结
文件挂载重点:
- SDIO的接口全部pull up
- NVIC的高低 SD卡的SDIO > SD卡的DMA
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "fatfs.h"
#include "sdio.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
//读写缓冲区
uint8_t wBuff[512];
uint8_t rBuff[512];
//实际文件中读写数据数
uint32_t numRW;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static void FS_File(void)
{
retSD = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);//挂载
retSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_CREATE_ALWAYS|FA_WRITE);
//填充写缓冲区wBuff
sprintf((char *)wBuff,"%s","½ñÌìÏÂÓê\n");
//写数据
retSD = f_write(&SDFile,wBuff,strlen((const char *)wBuff),&numRW);
//关闭文件
retSD = f_close(&SDFile);
//打开已存在的文件并赋上读权限
retSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_OPEN_EXISTING|FA_READ);
//读数据
retSD = f_read(&SDFile,rBuff,sizeof(rBuff),&numRW);
//关
f_close(&SDFile);
retSD = f_mount(0, (TCHAR const*)SDPath, 0);//卸载文件系统
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
FS_File();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */