前言
- 使用usb-host组件读取u盘
- 记录同时挂载sd和u盘
- 用到的芯片为stm32f407zgt6
- u盘的格式为fat
组件选择
文件相关的宏定义
/* DFS: device virtual file system */
/* 设备虚拟文件系统 */
#define RT_USING_DFS
#define DFS_USING_WORKDIR
#define DFS_FILESYSTEMS_MAX 3 // 这里
#define DFS_FILESYSTEM_TYPES_MAX 2
#define DFS_FD_MAX 16
//#define RT_USING_DFS_MNTTABLE /*可移植操作系统接口(Portable Operating System Interface of UNIX,缩写 POSIX)*/
/* elm-chan's FatFs, Generic FAT Filesystem Module */
#define RT_MMCSD_STACK_SIZE 2048
#define RT_DFS_ELM_CODE_PAGE 936
#define RT_DFS_ELM_USE_LFN 3
#define RT_DFS_ELM_LFN_UNICODE 0
#define RT_DFS_ELM_MAX_LFN 255
#define RT_DFS_ELM_DRIVES 2
#define RT_DFS_ELM_MAX_SECTOR_SIZE 512
#define RT_DFS_ELM_REENTRANT
#define RT_DFS_ELM_MUTEX_TIMEOUT 3000
#define RT_USING_DFS_DEVFS
// -----------------------------USB 组件
#define RT_USING_USB
#define RT_USING_USB_HOST
#define RT_USBH_MSTORAGE
#define UDISK_MOUNTPOINT "/disk" // 挂载节点要在sd卡目录上存在
#define RT_USBD_THREAD_STACK_SZ 4096
注意,这里“DFS_FILESYSTEMS_MAX ”的配置
- 要配置最少数目是3., SD卡占一个,U盘占一个,还有dfs内置了一个dev要占一个,源码如下
SD卡配置
SD卡硬件驱动的配置(cubemx生成)
//
// Created by shchl on 2024/3/10.
//
#include "board.h"
#ifdef BSP_USING_SDIO
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (hsd->Instance == SDIO) {
/* USER CODE BEGIN SDIO_MspInit 0 */
/* USER CODE END SDIO_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SDIO_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/**SDIO GPIO Configuration
PC8 ------> SDIO_D0
PC9 ------> SDIO_D1
PC10 ------> SDIO_D2
PC11 ------> SDIO_D3
PC12 ------> SDIO_CK
PD2 ------> SDIO_CMD
*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* USER CODE BEGIN SDIO_MspInit 1 */
/* USER CODE END SDIO_MspInit 1 */
}
}
#endif
SD卡文件系统挂载
//
// Created by shchl on 2024/3/10.
//
#include <board.h>
#include "dfs.h"
#include "dfs_fs.h"
const struct dfs_mount_tbl mount_table[] = {
{"sd0", "/", "elm", 0, 0},
};
#ifndef RT_USING_DFS_MNTTABLE
int dfs_mount_table(void) {
static rt_uint8_t try_cnt;
for (int i = 0; i < sizeof(mount_table) / sizeof(mount_table[0]); ++i) {
retry_mount:
if (dfs_mount(mount_table[i].device_name,
mount_table[i].path,
mount_table[i].filesystemtype,
mount_table[i].rwflag,
mount_table[i].data) != 0) {
if (try_cnt < 10) {
rt_thread_mdelay(20);
try_cnt++;
goto retry_mount;
} else {
rt_kprintf("mount fs[%s] on %s failed.\n",
mount_table[i].filesystemtype,
mount_table[i].path);
}
}
try_cnt = 0;
}
return 0;
}
INIT_ENV_EXPORT(dfs_mount_table);
#endif
USB硬件驱动配置(cubemx生成)
//
// Created by shchl on 2024/3/16.
//
#include "board.h"
#ifdef BSP_USING_USBHOST
//USB主机电源控制口
#define USB_HOST_PWRCTRL GET_PIN(A,15) //PA15
void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd) {
if (hhcd->Instance == USB_OTG_FS) {
/**USB_OTG_FS GPIO Configuration
PA11 ------> USB_OTG_FS_DM
PA12 ------> USB_OTG_FS_DP
*/
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟
//配置PA11 12
GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; //PA11 12
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; //复用
GPIO_InitStruct.Pull = GPIO_NOPULL; //无上下拉
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; //高速
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; //复用为OTG FS
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化
//PA15
GPIO_InitStruct.Pin = GPIO_PIN_15; //PA15
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_InitStruct.Pull = GPIO_NOPULL; //无上下拉
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; //高速
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
rt_pin_write(USB_HOST_PWRCTRL, PIN_HIGH); //开启USB HOST电源供电
/* Peripheral clock enable */
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
/* USB_OTG_FS interrupt Init */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
}
}
#endif
测试结果