usb-otg-hs u-disk rt-thread studio
github progect
1.RT-Thread Studio
New >> RT-Thread Protect
2.CubeMX Settings
- System Core >> RCC >> HSE >> Crystal(外部晶振)
- System Core >> SYS >> Debug(选择调试接口)
- Connectivity >> USART1 >> Asynchronous(用于finsh命令行)
- Connectivity >> USB_OTG_HS >> Host_Only(开发板无供电控制故未勾选VBUS)
- Enable HS global interrupt
Clock Configuration(配置时钟)
HCLK >> 168 >> Enter
- GENERATE COED (default seting)
3.Code
- applications/main.c
1 2 3 4
| #define DBG_TAG "main"
#define DBG_LVL DBG_INFO #include <rtdbg.h>
|
- drivers/board.h
1 2 3 4 5 6
| #define BSP_USING_UART1 #define BSP_UART1_TX_PIN "PA9" #define BSP_UART1_RX_PIN "PA10"
#define BSP_USING_USBHOST #define BSP_USBD_TYPE_HS
|
- drivers/drv_clk.c (copy from cubemx/Src/main.c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
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(); }
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(); } }
|
- new source file
applications/filesystem.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <rtthread.h> #include <dfs_fs.h> #include "dfs_romfs.h"
#define DBG_TAG "app.filesystem" #define DBG_LVL DBG_INFO #include <rtdbg.h>
static const struct romfs_dirent _romfs_root[] = { {ROMFS_DIRENT_DIR, "udisk", RT_NULL, 0} };
const struct romfs_dirent romfs_root = { ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0]) };
int mount_init(void) { if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0) { LOG_E("rom mount to '/' failed!"); return RT_ERROR; } return RT_EOK; } INIT_APP_EXPORT(mount_init);
|
- drivers/drv_common.c
STM32F407遇到USB读取U盘无法读取问题
1 2 3 4
| void HAL_Delay(__IO uint32_t Delay) { rt_thread_mdelay(Delay); }
|
- drivers/drv_usbh.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #define DBG_TAG "drv_usbh" #define DBG_LVL DBG_LOG #include <rtdbg.h>
void OTG_HS_IRQHandler(void) { rt_interrupt_enter(); HAL_HCD_IRQHandler(&stm32_hhcd_fs); rt_interrupt_leave(); }
#ifdef BSP_USBD_TYPE_HS static rt_err_t stm32_hcd_init(rt_device_t device) { HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)device->user_data; hhcd->Instance = USB_OTG_HS; hhcd->Init.Host_channels = 12; hhcd->Init.speed = HCD_SPEED_FULL; hhcd->Init.dma_enable = DISABLE; hhcd->Init.phy_itface = USB_OTG_EMBEDDED_PHY; hhcd->Init.Sof_enable = DISABLE; hhcd->Init.low_power_enable = DISABLE; hhcd->Init.vbus_sensing_enable = DISABLE; hhcd->Init.use_external_vbus = DISABLE; RT_ASSERT(HAL_HCD_Init(hhcd) == HAL_OK); HAL_HCD_Start(hhcd); #ifdef USBH_USING_CONTROLLABLE_POWER rt_pin_mode(USBH_POWER_PIN, PIN_MODE_OUTPUT); rt_pin_write(USBH_POWER_PIN, PIN_LOW); #endif return RT_EOK; }
#else
static rt_err_t stm32_hcd_init(rt_device_t device) { HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)device->user_data; hhcd->Instance = USB_OTG_FS; hhcd->Init.Host_channels = 8; hhcd->Init.speed = HCD_SPEED_FULL; hhcd->Init.dma_enable = DISABLE; hhcd->Init.phy_itface = HCD_PHY_EMBEDDED; hhcd->Init.Sof_enable = DISABLE; RT_ASSERT(HAL_HCD_Init(hhcd) == HAL_OK); HAL_HCD_Start(hhcd); #ifdef USBH_USING_CONTROLLABLE_POWER rt_pin_mode(USBH_POWER_PIN, PIN_MODE_OUTPUT); rt_pin_write(USBH_POWER_PIN, PIN_LOW); #endif return RT_EOK; } #endif
|
RT-Thread Settings
- Exclude from Build
cubemx/Src/main.c
cubemx/Src/stm32f4xx_it.c
- Components >> Device Drivers >> Using USB >> Using USB host >> Enable Udisk Drivers >> Udisk mount dir >> /udisk
- Components >> Device virtual file system >> Using device virtual file system >> Enable elm-chan fatfs / Enable ReadOnlay file system on flash
4.Test
- Build and Flash download
- Terminal
Ctrl+Atl+Shift +T
- Insert the u-disk(插入U盘)