Step(1): CubeMX setup
- Enable LEDs and Button
- Enable USB_OTG_FS as Host only + VBUS
- USB Host as Mass Storage Host Class
- FatFS middleware select = USB Disk
- Enable HSE RCC
- Enable PC0 pin and set to OUTPUT (Power Switch enable for USB supply)
- MAX_SS = 4096 (FATFS Configuration parameters)
========================================================
Step(2): Flash Drive connected/disconnected indicator
- open usb_host.c and use this in main
extern ApplicationTypeDef Appli_state;
- USB application states (LED ON when USB Flash Drive is connected)
/* USER CODE BEGIN 3 */
switch(Appli_state)
{
case APPLICATION_READY:
break;
case APPLICATION_IDLE:
break;
case APPLICATION_DISCONNECT:
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
break;
case APPLICATION_START:
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
break;
default:
break;
}
=======================================================
Step(3): Mount SD Card code
- Add FATFS variable
//FatFS variable
FATFS myUsbFatFS;
- open fatfs.c files and add USB logical path drive as extern in main
extern char USBH_Path[4];
- Software mount of USB Flash Drive
case APPLICATION_START:
/* Register the file system object to the FatFs module */
if(f_mount(&myUsbFatFS, (TCHAR const*)USBH_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
}
break;
=======================================================
Step(4): Add File IO Variables
//File IO Variables
FIL myFile;
FRESULT res;
UINT byteswritten, bytesread;
char rwtext[100];
=======================================================
Step(5): File write function
- Include string and boolean header files
//String library
#include <string.h>
#include <stdbool.h>
- Test Write function body
//1. USB test Write function
bool UsbTest_Write(void)
{
//Open/Create file for Writing
if(f_open(&myFile, "TEST.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
return 0; //error
}
else
{
//Set text string to write to file
sprintf(rwtext, "Hello world from Mohamed Yaqoob!");
//write to file
res = f_write(&myFile, (uint8_t *)rwtext, strlen(rwtext), &byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
return 0; //error
}
}
//Close file
f_close(&myFile);
return 1; //success
}
- Call write function in the main when button is pressed
case APPLICATION_READY:
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_SET)
{
if(UsbTest_Write()) HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
else HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
HAL_Delay(1000);
}
=======================================================
Step(6): File read function
//2. USB test Read function
bool UsbTest_Read(void)
{
//Open file for Reading
if(f_open(&myFile, "TEST.TXT", FA_READ) != FR_OK)
{
return 0; //error
}
else
{
//Read text from files until NULL
for(uint8_t i=0; i<100; i++)
{
res = f_read(&myFile, (uint8_t*)&rwtext[i], 1, &bytesread);
if(rwtext[i] == 0x00)
{
bytesread = i;
break;
}
}
//Reading error handling
if(bytesread==0) return 0;
}
//Close file
f_close(&myFile);
return 1; //success
}
- Call read file function in the main
case APPLICATION_READY:
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_SET)
{
if(UsbTest_Read()) HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
else HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
HAL_Delay(1000);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该程序主要用于嵌入式系统中对USB设备的检测、读操作及相应的状态指示。 其功能包括: 1.初始化环境:使用HAL_Init()函数初始化所有外设,并配置系统时钟,为后续操作打下基础。 2.外设配置:初始化GPIO(MX_GPIO_Init())、USB主机(MX_USB_HOST_Init())和文件系统(MX_FATFS_Init())。 3.USB闪存检测:在APPLICATION_START状态下,如果成功挂载USB闪存,程序将点亮绿色LED(LED1)。 4.按钮触发读操作:在APPLICATION_READY状态下,程序检测按钮输入,如果按钮被按下且成功读取USB内的数据,则点亮LED4,否则点亮LED3。 5. USB断开处理:在APPLICATION_DISCONNECT状态下,绿色LED(LED1)熄灭,表示USB设备断开连接。 无限循环:程序主体在无限循环中运行,确保系统持续响应
资源推荐
资源详情
资源评论
收起资源包目录
Tutorial 34 - USB MSC Flash Drive.rar (2个子文件)
Tutorial 34 - USB MSC Flash Drive
USB Flash - Tutorial steps.txt 4KB
main.c 11KB
共 2 条
- 1
资源评论
香菜配汤
- 粉丝: 503
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功