STM32H7 SDMMC+FATFS+USBMSC 虚拟U盘
通过cubemx配置 实现STM32H7 SDMMC+FATFS+USBMSC 虚拟U盘
·
通过cubemx配置 实现STM32H7 SDMMC+FATFS+USBMSC 虚拟U盘
1.实验目的
使用FAFTS文件操作系统,实现STM32虚拟U盘,读写外部SD卡
2.实验平台
硬件平台:正点原子阿波罗 STM32H734IIT6
开发工具:STM32CubeIDE 1.12.0
HAL库:STM32Cube FW_H7 V1.11.0
3.CubeMX配置
(1)配置SDMMC
GPIO都是默认引脚,开启SDMMC全局中断
(2)配置FATFS
打开USE_LABEL使能f_setlabel,可设置盘符的名称
使能H7内建的MDMA
(3)配置全速USB
仅作为从设备(4)配置USB_DEVICE
设置USB为大容量存储设备
适当增加MSC_MEDIA_PACKET,提高USB读写速度
(5)配置时钟
USB必须设置为48MHz
SDMMC 输出频率为 200 /(2*5)= 20MHz,为了稳定最大频率不超过24MHz
注意:SDMMC中断优先级必须高于 USB_OTG中断
4.代码部分
sd卡测试代码
// 函数:FatFs_Check
// 功能:进行FatFs文件系统的挂载
//
void FatFs_Check(void) // 判断FatFs是否挂载成功,若没有创建FatFs则格式化SD卡
{
BYTE work[_MAX_SS];
// FATFS_LinkDriver(&SD_Driver, SDPath); // 初始化驱动
MyFile_Res = f_mount(&SDFatFS, (const TCHAR*) SDPath, 1); // 挂载SD卡
if (MyFile_Res == FR_OK) // 判断是否挂载成功
{
f_setlabel("UFO");
printf("\r\nSD文件系统挂载成功\r\n");
} else {
printf("SD卡还未创建文件系统,即将格式化\r\n");
MyFile_Res = f_mkfs("0:", FM_FAT32, 0, work, sizeof work); // 格式化SD卡,FAT32,簇默认大小16K
if (MyFile_Res == FR_OK) // 判断是否格式化成功
printf("SD卡格式化成功!\r\n");
else
printf("格式化失败,请检查或更换SD卡!\r\n");
}
}
// 函数:FatFs_GetVolume
// 功能:计算设备的容量,包括总容量和剩余容量
//
void FatFs_GetVolume(void) // 计算设备容量
{
FATFS *fs; // 定义结构体指针
uint32_t SD_CardCapacity = 0; // SD卡的总容量
uint32_t SD_FreeCapacity = 0; // SD卡空闲容量
DWORD fre_clust, fre_sect, tot_sect; // 空闲簇,空闲扇区数,总扇区数
f_getfree((const TCHAR*) SDPath, &fre_clust, &fs); // 获取SD卡剩余的簇
tot_sect = (fs->n_fatent - 2) * fs->csize; // 总扇区数量 = 总的簇 * 每个簇包含的扇区数
fre_sect = fre_clust * fs->csize; // 计算剩余的可用扇区数
SD_CardCapacity = tot_sect / 2048; // SD卡总容量 = 总扇区数 * 512( 每扇区的字节数 ) / 1048576(换算成MB)
SD_FreeCapacity = fre_sect / 2048; // 计算剩余的容量,单位为M
printf("-------------------获取设备容量信息-----------------\r\n");
printf("SD容量:%ldMB\r\n", SD_CardCapacity);
printf("SD剩余:%ldMB\r\n", SD_FreeCapacity);
}
// 函数:FatFs_FileTest
// 功能:进行文件写入和读取测试
//
uint8_t FatFs_FileTest(void) // 文件创建和写入测试
{
uint8_t i = 0;
uint16_t BufferSize = 0;
FIL MyFile; // 文件对象
UINT MyFile_Num; // 数据长度
BYTE MyFile_WriteBuffer[] = "STM32H7B0 SD卡 文件系统测试"; // 要写入的数据
BYTE MyFile_ReadBuffer[1024]; // 要读出的数据
printf("-------------FatFs 文件创建和写入测试---------------\r\n");
MyFile_Res = f_open(&MyFile, "0:FatFs Test.txt",
FA_CREATE_ALWAYS | FA_WRITE); // 打开文件,若不存在则创建该文件
if (MyFile_Res == FR_OK) {
printf("文件打开/创建成功,准备写入数据...\r\n");
MyFile_Res = f_write(&MyFile, MyFile_WriteBuffer,
sizeof(MyFile_WriteBuffer), &MyFile_Num); // 向文件写入数据
if (MyFile_Res == FR_OK) {
printf("写入成功,写入内容为:\r\n");
printf("%s\r\n", MyFile_WriteBuffer);
} else {
printf("文件写入失败,请检查SD卡或重新格式化!\r\n");
f_close(&MyFile); // 关闭文件
return ERROR;
}
f_close(&MyFile); // 关闭文件
} else {
printf("无法打开/创建文件,请检查SD卡或重新格式化!\r\n");
f_close(&MyFile); // 关闭文件
return ERROR;
}
printf("-------------FatFs 文件读取测试---------------\r\n");
BufferSize = sizeof(MyFile_WriteBuffer) / sizeof(BYTE); // 计算写入的数据长度
MyFile_Res = f_open(&MyFile, "0:FatFs Test.txt",
FA_OPEN_EXISTING | FA_READ); // 打开文件,若不存在则创建该文件
MyFile_Res = f_read(&MyFile, MyFile_ReadBuffer, BufferSize, &MyFile_Num); // 读取文件
if (MyFile_Res == FR_OK) {
printf("文件读取成功,正在校验数据...\r\n");
for (i = 0; i < BufferSize; i++) {
if (MyFile_WriteBuffer[i] != MyFile_ReadBuffer[i]) // 校验数据
{
printf("校验失败,请检查SD卡或重新格式化!\r\n");
f_close(&MyFile); // 关闭文件
return ERROR;
}
}
printf("校验成功,读出的数据为:\r\n");
printf("%s\r\n", MyFile_ReadBuffer);
} else {
printf("无法读取文件,请检查SD卡或重新格式化!\r\n");
f_close(&MyFile); // 关闭文件
return ERROR;
}
f_close(&MyFile); // 关闭文件
return SUCCESS;
}
修改USB_DEVICE->App->usbd_storage_if.c文件
重写这几个方法,把SD卡映射到USB_DEVICE
//初始化
static int8_t STORAGE_Init_FS(uint8_t lun);
//获取容量
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
//判断状态
static int8_t STORAGE_IsReady_FS(uint8_t lun);
//读取
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
//写入
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : usbd_storage_if.c
* @version : v1.0_Cube
* @brief : Memory management layer.
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 "usbd_storage_if.h"
/* USER CODE BEGIN INCLUDE */
#include "fatfs.h"
/* USER CODE END INCLUDE */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @brief Usb device.
* @{
*/
/** @defgroup USBD_STORAGE
* @brief Usb mass storage device module
* @{
*/
/** @defgroup USBD_STORAGE_Private_TypesDefinitions
* @brief Private types.
* @{
*/
/* USER CODE BEGIN PRIVATE_TYPES */
/* USER CODE END PRIVATE_TYPES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Defines
* @brief Private defines.
* @{
*/
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
/* USER CODE BEGIN PRIVATE_DEFINES */
/* USER CODE END PRIVATE_DEFINES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Macros
* @brief Private macros.
* @{
*/
/* USER CODE BEGIN PRIVATE_MACRO */
/* USER CODE END PRIVATE_MACRO */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Variables
* @brief Private variables.
* @{
*/
/* USER CODE BEGIN INQUIRY_DATA_FS */
/** USB Mass storage Standard Inquiry Data. */
const int8_t STORAGE_Inquirydata_FS[] = {/* 36 */
/* LUN 0 */
0x00, 0x80, 0x02, 0x02, (STANDARD_INQUIRY_DATA_LEN - 5), 0x00, 0x00, 0x00, 'S',
'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '.', '0', '1' /* Version : 4 Bytes */
};
/* USER CODE END INQUIRY_DATA_FS */
/* USER CODE BEGIN PRIVATE_VARIABLES */
uint8_t pdrv_type;
/* USER CODE END PRIVATE_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_Variables
* @brief Public variables.
* @{
*/
extern USBD_HandleTypeDef hUsbDeviceFS;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_FunctionPrototypes
* @brief Private functions declaration.
* @{
*/
static int8_t STORAGE_Init_FS(uint8_t lun);
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
static int8_t STORAGE_IsReady_FS(uint8_t lun);
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_GetMaxLun_FS(void);
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
/**
* @}
*/
USBD_StorageTypeDef USBD_Storage_Interface_fops_FS =
{
STORAGE_Init_FS,
STORAGE_GetCapacity_FS,
STORAGE_IsReady_FS,
STORAGE_IsWriteProtected_FS,
STORAGE_Read_FS,
STORAGE_Write_FS,
STORAGE_GetMaxLun_FS,
(int8_t *)STORAGE_Inquirydata_FS
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the storage unit (medium) over USB FS IP
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Init_FS(uint8_t lun)
{
/* USER CODE BEGIN 2 */
UNUSED(lun);
pdrv_type = retSD;
// sd卡已经在主函数中初始化
// 这边仅把sd映射到usb
return (USBD_OK);
/* USER CODE END 2 */
}
/**
* @brief Returns the medium capacity.
* @param lun: Logical unit number.
* @param block_num: Number of total block number.
* @param block_size: Block size.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
int8_t ret = -1;
ret = disk_ioctl(pdrv_type, GET_SECTOR_COUNT, (void*) block_num);
if (ret < 0) {
return ret;
}
*block_num = *block_num - 1;
ret = disk_ioctl(pdrv_type, GET_SECTOR_SIZE, (void*) block_size);
return ret;
/* USER CODE END 3 */
}
/**
* @brief Checks whether the medium is ready.
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
/* USER CODE BEGIN 4 */
int8_t ret = -1;
UNUSED(lun);
ret = disk_status(pdrv_type);
return ret;
/* USER CODE END 4 */
}
/**
* @brief Checks whether the medium is write protected.
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{
/* USER CODE BEGIN 5 */
UNUSED(lun);
return (USBD_OK);
/* USER CODE END 5 */
}
/**
* @brief Reads data from the medium.
* @param lun: Logical unit number.
* @param buf: data buffer.
* @param blk_addr: Logical block address.
* @param blk_len: Blocks number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
int8_t ret = -1;
UNUSED(lun);
// HAL_GPIO_WritePin(LED_PC1_GPIO_Port, LED_PC1_Pin, 0);
ret = disk_read(pdrv_type, (BYTE*) buf, (DWORD) blk_addr, (UINT) blk_len);
// HAL_GPIO_WritePin(LED_PC1_GPIO_Port, LED_PC1_Pin, 1);
return ret;
/* USER CODE END 6 */
}
/**
* @brief Writes data into the medium.
* @param lun: Logical unit number.
* @param buf: data buffer.
* @param blk_addr: Logical block address.
* @param blk_len: Blocks number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
int8_t ret = -1;
UNUSED(lun);
// HAL_GPIO_WritePin(LED_PC1_GPIO_Port, LED_PC1_Pin, 0);
ret = disk_write(pdrv_type, (BYTE*) buf, (DWORD) blk_addr, (UINT) blk_len);
// HAL_GPIO_WritePin(LED_PC1_GPIO_Port, LED_PC1_Pin, 1);
return ret;
/* USER CODE END 7 */
}
/**
* @brief Returns the Max Supported LUNs.
* @param None
* @retval Lun(s) number.
*/
int8_t STORAGE_GetMaxLun_FS(void)
{
/* USER CODE BEGIN 8 */
return (STORAGE_LUN_NBR - 1);
/* USER CODE END 8 */
}
/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
/**
* @}
*/
/**
* @}
*/
5.实验效果
无视上面的乱码,是串口工具的问题。
表面上看fatfs和u盘功能都正常了,其实faffs和usb msc是不能共存的,下一篇贴子,我加入了freeRTOS,并实现fatfs文件系统和usb mass storage class共存
跳转到下一贴
6.源码下载
更多推荐
所有评论(0)