最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

STM32CUBEMX

互联网 admin 4浏览 0评论

STM32CUBEMX

STM32CUBEMX_SDIO和FATFS_读写SD卡

简述
FATFS是一个完全免费开源,专为小型嵌入式系统设计的FAT(File Allocation Table)文件系统模块。FATFS的编写遵循ANSI C,并且完全与磁盘I/O层分开。支持FAT12/FAT16/FAT32,支持多个存储媒介,有独立的缓冲区,可对多个文件进行读写。兼容Windows文件系统。

1、使用STM32cubemx配置工程
2、移植工程测试SD卡的读写

1、配置外部时钟

2、配置st-link调试接口

3、配置SDIO的模式

4、配置FATFS文件系统

5、栈调大一点

6、移植工程

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "sdio.h"
#include "usb_device.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* 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 *//* 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_SDIO_SD_Init();MX_FATFS_Init();MX_USB_DEVICE_Init();/* USER CODE BEGIN 2 */uint32_t byteswritten;                /* File write counts */uint32_t bytesread;                   /* File read counts */uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */uint8_t rtext[100];                   /* File read buffers */char filename[] = "STM32cube.txt";char SensorBuff[100];usb_printf("********* STM32CubeMX FatFs Example *********\r\n\r\n");/**********新建STM32cube.txt并写入字符串**********/	if(f_mount(&SDFatFS,SDPath,1) == FR_OK){usb_printf("f_mount sucess!!! \r\n");if(f_open(&SDFile,filename,FA_CREATE_ALWAYS|FA_WRITE) == FR_OK){usb_printf("f_open file sucess!!! \r\n");if(f_write(&SDFile,wtext,sizeof(wtext),&byteswritten) == FR_OK){usb_printf("f_write file sucess!!! \r\n");usb_printf("f_write Data : %s\r\n",wtext);if(f_close(&SDFile) == FR_OK)usb_printf("f_close sucess!!! \r\n");elseusb_printf("f_close error : %d\r\n",retSD);}elseusb_printf("f_write file error\r\n");    }elseusb_printf("f_open file error\r\n");  }elseusb_printf("f_mount error : %d \r\n",retSD);/**********新建STM32cube.txt并写入字符串**********/	/**********读取STM32cube.txt内容*********/	retSD = f_open(&SDFile, filename, FA_READ);if(retSD)usb_printf("f_open file error : %d\r\n",retSD);elseusb_printf("f_open file sucess!!! \r\n");retSD = f_read(&SDFile, rtext, sizeof(rtext), (UINT*)&bytesread);if(retSD)usb_printf("f_read error!!! %d\r\n",retSD);else{usb_printf("f_read sucess!!! \r\n");usb_printf("f_read Data : %s\r\n",rtext);}retSD = f_close(&SDFile);if(retSD)usb_printf("f_close error!!! %d\r\n",retSD);elseusb_printf("f_close sucess!!! \r\n");if(bytesread == byteswritten)usb_printf("FatFs is working well!!!\r\n");/**********读取STM32cube.txt内容*********/	/*********模拟传感器数据存储在Sensor.csv表格文件中**********/	if(f_open(&SDFile,(const char*)"Sensor.csv",FA_CREATE_ALWAYS|FA_WRITE) == FR_OK){usb_printf("Sensor.csv was opened/created!!!\r\n");sprintf(SensorBuff, "Item,Temp,Humi,Light\r\n");f_write(&SDFile,SensorBuff,strlen(SensorBuff),&byteswritten);for(int i = 0; i < 10; i++){sprintf(SensorBuff, "%d,%d,%d,%d\r\n",i + 1, i + 20, i + 30, i + 40);f_write(&SDFile,SensorBuff,strlen(SensorBuff),&byteswritten);f_sync(&SDFile);}f_close(&SDFile);}/*********模拟传感器数据存储在Sensor.csv表格文件中**********/	/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */usb_printf("%s","hello");HAL_Delay(500);}/* 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 = 4;RCC_OscInitStruct.PLL.PLLN = 168;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 *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

7、执行效果

8、把SD卡插入电脑可以看到其中的文件

9、介绍几个常用操作函数

f_mount:在FATFS模块上注册/注销一个工作区(文件系统对象)

FRESULT f_mount(FATFS* fs, const TCHAR* path, BYTE opt);
参数--> fs:fs工作区(文件系统对象)指针,如果赋值为 NULL 可以取消物理设备挂载path:注册/注销工作区的逻辑设备编号,使用设备根路径表示opt:注册或注销选项(可选0或1),0表示不立即挂载,1表示立即挂载

f_mkfs:格式化物理设备

FRESULT f_mkfs(const TCHAR* path, BYTE sfd, UINT au);
参数--> path:逻辑设备编号,使用设备根路径表示sfd:0或1,0表示为硬盘设备;1表示为软盘设备au:指定扇区大小,若为0表示通过disk_ioctl函数获取

f_open:创建/打开一个文件对象

FRESULT f_open(FIL* fp, const TCHAR* path, BYTE mode);
参数--> fp:将创建或打开的文件对象指针path:文件名指针,指定将创建或打开的文件名(包含文件类型后缀名)mode:访问类型和打开方法
mode可选值:
FA_READ		指定读访问对象。可以从文件中读取数据。 与FA_WRITE结合可以进行读写访问。   
FA_WRITE	指定写访问对象。可以向文件中写入数据。与FA_READ结合可以进行读写访问。   
FA_OPEN_EXISTING	打开文件。如果文件不存在,则打开失败。(默认)  
FA_OPEN_ALWAYS		如果文件存在,则打开;否则,创建一个新文件。  
FA_CREATE_NEW   	创建一个新文件。如果文件已存在,则创建失败。   
FA_CREATE_ALWAYS  	创建一个新文件。如果文件已存在,则它将被截断并覆盖。

f_close:关闭一个打开的文件

FRESULT f_close (FIL *fp)
参数--> fp:将被关闭的已打开的文件对象结构的指针

f_write:写入数据到一个已打开的文件

FRESULT f_write (FIL* fp, const void *buff, UINT btw, UINT* bw)
参数--> fp:指向将被写入的已打开的文件对象结构的指针buff:指向存储写入数据的缓冲区的指针btw:要写入的字节数bw:指向返回已写入字节数的UINT变量的指针,返回为实际写入的字节数

f_read:从一个打开的文件中读取数据

FRESULT f_read (FIL* fp, const void *buff, UINT btr, UINT* br)
参数--> fp:指向将被读取的已打开的文件对象结构的指针buff:指向存储读取数据的缓冲区的指针btr:要读取的字节数br:指向返回已读取字节数的UINT变量的指针,返回为实际读取的字节数

10、电路图

STM32CUBEMX

STM32CUBEMX_SDIO和FATFS_读写SD卡

简述
FATFS是一个完全免费开源,专为小型嵌入式系统设计的FAT(File Allocation Table)文件系统模块。FATFS的编写遵循ANSI C,并且完全与磁盘I/O层分开。支持FAT12/FAT16/FAT32,支持多个存储媒介,有独立的缓冲区,可对多个文件进行读写。兼容Windows文件系统。

1、使用STM32cubemx配置工程
2、移植工程测试SD卡的读写

1、配置外部时钟

2、配置st-link调试接口

3、配置SDIO的模式

4、配置FATFS文件系统

5、栈调大一点

6、移植工程

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "sdio.h"
#include "usb_device.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* 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 *//* 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_SDIO_SD_Init();MX_FATFS_Init();MX_USB_DEVICE_Init();/* USER CODE BEGIN 2 */uint32_t byteswritten;                /* File write counts */uint32_t bytesread;                   /* File read counts */uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */uint8_t rtext[100];                   /* File read buffers */char filename[] = "STM32cube.txt";char SensorBuff[100];usb_printf("********* STM32CubeMX FatFs Example *********\r\n\r\n");/**********新建STM32cube.txt并写入字符串**********/	if(f_mount(&SDFatFS,SDPath,1) == FR_OK){usb_printf("f_mount sucess!!! \r\n");if(f_open(&SDFile,filename,FA_CREATE_ALWAYS|FA_WRITE) == FR_OK){usb_printf("f_open file sucess!!! \r\n");if(f_write(&SDFile,wtext,sizeof(wtext),&byteswritten) == FR_OK){usb_printf("f_write file sucess!!! \r\n");usb_printf("f_write Data : %s\r\n",wtext);if(f_close(&SDFile) == FR_OK)usb_printf("f_close sucess!!! \r\n");elseusb_printf("f_close error : %d\r\n",retSD);}elseusb_printf("f_write file error\r\n");    }elseusb_printf("f_open file error\r\n");  }elseusb_printf("f_mount error : %d \r\n",retSD);/**********新建STM32cube.txt并写入字符串**********/	/**********读取STM32cube.txt内容*********/	retSD = f_open(&SDFile, filename, FA_READ);if(retSD)usb_printf("f_open file error : %d\r\n",retSD);elseusb_printf("f_open file sucess!!! \r\n");retSD = f_read(&SDFile, rtext, sizeof(rtext), (UINT*)&bytesread);if(retSD)usb_printf("f_read error!!! %d\r\n",retSD);else{usb_printf("f_read sucess!!! \r\n");usb_printf("f_read Data : %s\r\n",rtext);}retSD = f_close(&SDFile);if(retSD)usb_printf("f_close error!!! %d\r\n",retSD);elseusb_printf("f_close sucess!!! \r\n");if(bytesread == byteswritten)usb_printf("FatFs is working well!!!\r\n");/**********读取STM32cube.txt内容*********/	/*********模拟传感器数据存储在Sensor.csv表格文件中**********/	if(f_open(&SDFile,(const char*)"Sensor.csv",FA_CREATE_ALWAYS|FA_WRITE) == FR_OK){usb_printf("Sensor.csv was opened/created!!!\r\n");sprintf(SensorBuff, "Item,Temp,Humi,Light\r\n");f_write(&SDFile,SensorBuff,strlen(SensorBuff),&byteswritten);for(int i = 0; i < 10; i++){sprintf(SensorBuff, "%d,%d,%d,%d\r\n",i + 1, i + 20, i + 30, i + 40);f_write(&SDFile,SensorBuff,strlen(SensorBuff),&byteswritten);f_sync(&SDFile);}f_close(&SDFile);}/*********模拟传感器数据存储在Sensor.csv表格文件中**********/	/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */usb_printf("%s","hello");HAL_Delay(500);}/* 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 = 4;RCC_OscInitStruct.PLL.PLLN = 168;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 *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

7、执行效果

8、把SD卡插入电脑可以看到其中的文件

9、介绍几个常用操作函数

f_mount:在FATFS模块上注册/注销一个工作区(文件系统对象)

FRESULT f_mount(FATFS* fs, const TCHAR* path, BYTE opt);
参数--> fs:fs工作区(文件系统对象)指针,如果赋值为 NULL 可以取消物理设备挂载path:注册/注销工作区的逻辑设备编号,使用设备根路径表示opt:注册或注销选项(可选0或1),0表示不立即挂载,1表示立即挂载

f_mkfs:格式化物理设备

FRESULT f_mkfs(const TCHAR* path, BYTE sfd, UINT au);
参数--> path:逻辑设备编号,使用设备根路径表示sfd:0或1,0表示为硬盘设备;1表示为软盘设备au:指定扇区大小,若为0表示通过disk_ioctl函数获取

f_open:创建/打开一个文件对象

FRESULT f_open(FIL* fp, const TCHAR* path, BYTE mode);
参数--> fp:将创建或打开的文件对象指针path:文件名指针,指定将创建或打开的文件名(包含文件类型后缀名)mode:访问类型和打开方法
mode可选值:
FA_READ		指定读访问对象。可以从文件中读取数据。 与FA_WRITE结合可以进行读写访问。   
FA_WRITE	指定写访问对象。可以向文件中写入数据。与FA_READ结合可以进行读写访问。   
FA_OPEN_EXISTING	打开文件。如果文件不存在,则打开失败。(默认)  
FA_OPEN_ALWAYS		如果文件存在,则打开;否则,创建一个新文件。  
FA_CREATE_NEW   	创建一个新文件。如果文件已存在,则创建失败。   
FA_CREATE_ALWAYS  	创建一个新文件。如果文件已存在,则它将被截断并覆盖。

f_close:关闭一个打开的文件

FRESULT f_close (FIL *fp)
参数--> fp:将被关闭的已打开的文件对象结构的指针

f_write:写入数据到一个已打开的文件

FRESULT f_write (FIL* fp, const void *buff, UINT btw, UINT* bw)
参数--> fp:指向将被写入的已打开的文件对象结构的指针buff:指向存储写入数据的缓冲区的指针btw:要写入的字节数bw:指向返回已写入字节数的UINT变量的指针,返回为实际写入的字节数

f_read:从一个打开的文件中读取数据

FRESULT f_read (FIL* fp, const void *buff, UINT btr, UINT* br)
参数--> fp:指向将被读取的已打开的文件对象结构的指针buff:指向存储读取数据的缓冲区的指针btr:要读取的字节数br:指向返回已读取字节数的UINT变量的指针,返回为实际读取的字节数

10、电路图

发布评论

评论列表 (0)

  1. 暂无评论