日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

【无标题】STM32F407VGT6文件系统挂载TF卡学习笔记CUBEMXKEIL5

發(fā)布時間:2023/12/31 windows 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【无标题】STM32F407VGT6文件系统挂载TF卡学习笔记CUBEMXKEIL5 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔

目錄

前言

一、CUBEMX配置

二、添加代碼main.c文件

1.main函數(shù)定義變量

2.文件系統(tǒng)函數(shù)

三、調(diào)試結(jié)果

總結(jié)


前言

開發(fā)工具:STM32F407VGT6、TF卡(32G)、JLINK調(diào)試器

參考:野火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,中斷優(yōu)先級(5 6 6 )在后面再設(shè)置。SDIO其他信息默認即可。

  • FATFS處勾選SD Card,下面配置框的數(shù)據(jù)和選項默認即可,可以不修改。

?

System view選擇左邊的GPIO,打開GPIO窗口后選擇SDIO窗口上拉所有SDIO的接口。

?

?

選擇NVIC選項打開窗口后修改SDIO global iuterrupt的中斷優(yōu)先級為5,DMA2 stream3 為6,

DMA2 stream6?為6。

?

?

  • ?時鐘樹 HCLK處的時鐘頻率改為最大值(此處最大值為168)168。

?

  • ?Project? Manager(MX上方的四個大窗口中的第三個)的第一個選項Project處修改圖片上的數(shù)據(jù),MDK版本根據(jù)自己的修改。

?

最后生成代碼GENERATE CODE。

二、添加代碼main.c文件

1.main函數(shù)定義變量

頭文件添加和相關(guān)變量的定義。

/* USER CODE BEGIN Includes */ #include "stdio.h" #include "string.h" /* USER CODE END Includes *//* USER CODE BEGIN PD */ //定義讀寫緩沖區(qū) 512字節(jié)(1個塊的大小) uint8_t wBuff[512];//寫 uint8_t rBuff[512];//讀//實際讀取/寫入文件的數(shù)據(jù)數(shù) uint32_t numRW; /* USER CODE END PD */

2.文件系統(tǒng)函數(shù)

完成文件系統(tǒng)掛載和讀寫的功能實現(xiàn)。

/* USER CODE BEGIN 0 */ static void FS_File(void) {//掛載文件系統(tǒng) 0是稍后掛載,1是立即掛載retSD = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);//新建文件(如果文件不存在則新建,文件已存在則覆蓋原有文件創(chuàng)建新文件)+ 寫權(quán)限r(nóng)etSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_CREATE_ALWAYS|FA_WRITE);//填充數(shù)據(jù)到寫緩沖區(qū) 要添加#include "stdio.h"和#include "string.h"頭文件才能調(diào)用sprintf((char *)wBuff,"%s","今天下雨\n");//將寫緩沖區(qū)的數(shù)據(jù)寫到SD卡中retSD = f_write(&SDFile,wBuff,strlen((const char *)wBuff),&numRW);//關(guān)閉文件 一個open對應(yīng)一個close否則會出錯retSD = f_close(&SDFile);//打開已存在的文件 + 讀權(quán)限r(nóng)etSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_OPEN_EXISTING|FA_READ);//將SD卡中的數(shù)據(jù)讀到讀緩沖區(qū)中retSD = f_read(&SDFile,rBuff,sizeof(rBuff),&numRW);//關(guān)閉文件f_close(&SDFile);retSD = f_mount(0, (TCHAR const*)SDPath, 0);//卸載文件系統(tǒng) } /* USER CODE END 0 */

在初始化都完成后,在while循環(huán)里添加測試函數(shù)。

? /* USER CODE BEGIN WHILE */
? while (1)
? {
? ? /* USER CODE END WHILE */
? ? FS_File();
? ? /* USER CODE BEGIN 3 */
? }
? /* USER CODE END 3 */

三、調(diào)試結(jié)果

f_mount處的retSD返回0x00是掛載成功了。


?

總結(jié)

文件掛載重點:

  • 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 */ //讀寫緩沖區(qū) uint8_t wBuff[512]; uint8_t rBuff[512];//實際文件中讀寫數(shù)據(jù)數(shù) 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);//填充寫緩沖區(qū)wBuffsprintf((char *)wBuff,"%s","??ìì??óê\n");//寫數(shù)據(jù)retSD = f_write(&SDFile,wBuff,strlen((const char *)wBuff),&numRW);//關(guān)閉文件retSD = f_close(&SDFile);//打開已存在的文件并賦上讀權(quán)限r(nóng)etSD = f_open(&SDFile,(const char*)"ceshi.txt",FA_OPEN_EXISTING|FA_READ);//讀數(shù)據(jù)retSD = f_read(&SDFile,rBuff,sizeof(rBuff),&numRW);//關(guān)f_close(&SDFile);retSD = f_mount(0, (TCHAR const*)SDPath, 0);//卸載文件系統(tǒng) } /* 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 */

    總結(jié)

    以上是生活随笔為你收集整理的【无标题】STM32F407VGT6文件系统挂载TF卡学习笔记CUBEMXKEIL5的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。