Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
CherryUSB
RT-Thread一般讨论
STM32H7R7(ART-Pi2)运行CherryUSB
发布于 2024-12-12 20:51:46 浏览:1651
订阅该版
[tocm] # 一、随着越来越多mcu的usb自带hs phy,stm32也出了一款stm32h7r7,它自带usb hs phy。所以尝试用它运行cherryusb。下面是创建构建的工程与处理遇到问题的详细过程。 # 二、使用cubemx创建keil工程 1、我手头的是stm32h7r7l8hxh 2、配置rcc、sys(注:我们只配置Boot下的配置,不勾选App的)  3、关闭MPU且不启用cache,简化操作,usb缓冲要放到noncacheable  4、配置uart usb(记得在配置勾选usb otg hs 中断)(usb用到PM5 PM6引脚不需要额外的初始化)  5、勾选debug  6、配置时钟   7、工程选项   # 三、向keil工程添加cherryUSB源代码文件 1、添加cherryusb文件和头文件路径   2、修改usb中断函数  ```c void OTG_HS_IRQHandler(void) { /* USER CODE BEGIN OTG_HS_IRQn 0 */ /* USER CODE END OTG_HS_IRQn 0 */ // HAL_PCD_IRQHandler(&hpcd_USB_OTG_HS); /* USER CODE BEGIN OTG_HS_IRQn 1 */ extern void USBD_IRQHandler(uint8_t busid); USBD_IRQHandler(0); /* USER CODE END OTG_HS_IRQn 1 */ } ``` 3、添加代码,使得keil的printf函数输出到uart外设  ```c /* USER CODE BEGIN 1 */ #include
#include
#include
__asm(".global __use_no_semihosting"); //struct __FILE { int handle; /* Add whatever you need here */ }; FILE __stdout; FILE __stdin; int fputc(int c, FILE *f) { HAL_UART_Transmit(&huart4, (uint8_t *)&c, 1, 1000); return (c); } int fgetc(FILE *f) { return EOF; } int ferror(FILE *f) { /* Your implementation of ferror */ return EOF; } void _ttywrch(int c) { HAL_UART_Transmit(&huart4, (uint8_t *)&c, 1, 1000); } void _sys_exit(int return_code) { label: goto label; /* endless loop */ } /* USER CODE END 1 */ ``` 4、添加CherryUSB底层初始化代码  ```c /* USER CODE BEGIN 1 */ void usb_dc_low_level_init(uint8_t busid) { // MX_USB_OTG_HS_PCD_Init(); RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USBPHYC; PeriphClkInit.UsbPhycClockSelection = RCC_USBPHYCCLKSOURCE_HSE; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } HAL_PWREx_EnableUSBVoltageDetector(); /* Enable the USB HS regulator. */ HAL_PWREx_EnableUSBHSregulator(); /* USB_OTG_HS clock enable */ __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); __HAL_RCC_USBPHYC_CLK_ENABLE(); /* USB_OTG_HS interrupt Init */ HAL_NVIC_SetPriority(OTG_HS_IRQn, 0, 0); HAL_NVIC_EnableIRQ(OTG_HS_IRQn); } void usb_dc_low_level_deinit(uint8_t busid) { HAL_PCD_DeInit(&hpcd_USB_OTG_HS); } /* USER CODE END 1 */ ``` 5、添加修改main函数 (注释掉main函数中的MX_USB_OTG_HS_PCD_Init())  ```c /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include
#include "usb_config.h" /* USER CODE END Includes */ // MX_USB_OTG_HS_PCD_Init(); /* USER CODE BEGIN 2 */ printf("ART_Pi2 CherryUSB demo \r\n"); extern void cdc_acm_msc_init(uint8_t busid, uint32_t reg_base); cdc_acm_msc_init(0, USB_OTG_HS_PERIPH_BASE); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { extern void cdc_acm_data_send_with_dtr_test(uint8_t busid); cdc_acm_data_send_with_dtr_test(0); HAL_Delay(500); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ ``` 6、复制一份usb_config.h到工程里,开始构建代码。 # 四、其他问题 1、cherryusb开始运行了,串口输出也没提示错误,但是不枚举  修改usb_glue_st.c文件,return 0 -> return 1 << 21 ```c uint32_t usbd_get_dwc2_gccfg_conf(uint32_t reg_base) { #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h") #define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(reg_base)) /* B-peripheral session valid override enable */ USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN; USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; #endif #ifdef CONFIG_USB_HS #if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F732xx) || defined(STM32F733xx) USB_OTG_GLB->GCCFG = (1 << 23); usb_hsphy_init(25000000U); return (1 << 23); /* Enable USB HS PHY USBx->GCCFG |= USB_OTG_GCCFG_PHYHSEN;*/ #else return (1 << 21); //return 0; #endif #else #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h") return (1 << 16); #else return ((1 << 16) | (1 << 21)); #endif #endif } ``` 2、提示枚举成功,但fifo溢出  修改usb_config.h文件,取消注释,并修改CONFIG_USB_DWC2_TX4_FIFO_SIZE的值 ```c /* ---------------- DWC2 Configuration ---------------- */ #define CONFIG_USB_DWC2_RXALL_FIFO_SIZE (1024 / 4) #define CONFIG_USB_DWC2_TX0_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX1_FIFO_SIZE (512 / 4) #define CONFIG_USB_DWC2_TX2_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX3_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX4_FIFO_SIZE (512 / 4) #define CONFIG_USB_DWC2_TX5_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX6_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX7_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX8_FIFO_SIZE (0 / 4) ``` 运行成功  usb_config.h代码 ```c /* * Copyright (c) 2022, sakumisu * * SPDX-License-Identifier: Apache-2.0 */ #ifndef CHERRYUSB_CONFIG_H #define CHERRYUSB_CONFIG_H /* ================ USB common Configuration ================ */ #define CONFIG_USB_PRINTF(...) printf(__VA_ARGS__) #ifndef CONFIG_USB_DBG_LEVEL #define CONFIG_USB_DBG_LEVEL USB_DBG_INFO #endif /* Enable print with color */ #define CONFIG_USB_PRINTF_COLOR_ENABLE /* data align size when use dma */ #ifndef CONFIG_USB_ALIGN_SIZE #define CONFIG_USB_ALIGN_SIZE 4 #endif /* attribute data into no cache ram */ #define USB_NOCACHE_RAM_SECTION __attribute__((section(".noncacheable"))) /* ================= USB Device Stack Configuration ================ */ /* Ep0 in and out transfer buffer */ #ifndef CONFIG_USBDEV_REQUEST_BUFFER_LEN #define CONFIG_USBDEV_REQUEST_BUFFER_LEN 512 #endif /* Setup packet log for debug */ // #define CONFIG_USBDEV_SETUP_LOG_PRINT /* Check if the input descriptor is correct */ // #define CONFIG_USBDEV_DESC_CHECK /* Enable test mode */ // #define CONFIG_USBDEV_TEST_MODE #ifndef CONFIG_USBDEV_MSC_MAX_LUN #define CONFIG_USBDEV_MSC_MAX_LUN 1 #endif #ifndef CONFIG_USBDEV_MSC_MAX_BUFSIZE #define CONFIG_USBDEV_MSC_MAX_BUFSIZE 512 #endif #ifndef CONFIG_USBDEV_MSC_MANUFACTURER_STRING #define CONFIG_USBDEV_MSC_MANUFACTURER_STRING "" #endif #ifndef CONFIG_USBDEV_MSC_PRODUCT_STRING #define CONFIG_USBDEV_MSC_PRODUCT_STRING "" #endif #ifndef CONFIG_USBDEV_MSC_VERSION_STRING #define CONFIG_USBDEV_MSC_VERSION_STRING "0.01" #endif // #define CONFIG_USBDEV_MSC_THREAD #ifndef CONFIG_USBDEV_MSC_PRIO #define CONFIG_USBDEV_MSC_PRIO 4 #endif #ifndef CONFIG_USBDEV_MSC_STACKSIZE #define CONFIG_USBDEV_MSC_STACKSIZE 2048 #endif #ifndef CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE #define CONFIG_USBDEV_RNDIS_RESP_BUFFER_SIZE 156 #endif #ifndef CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE #define CONFIG_USBDEV_RNDIS_ETH_MAX_FRAME_SIZE 2048 #endif #ifndef CONFIG_USBDEV_RNDIS_VENDOR_ID #define CONFIG_USBDEV_RNDIS_VENDOR_ID 0x0000ffff #endif #ifndef CONFIG_USBDEV_RNDIS_VENDOR_DESC #define CONFIG_USBDEV_RNDIS_VENDOR_DESC "CherryUSB" #endif #define CONFIG_USBDEV_RNDIS_USING_LWIP /* ================ USB HOST Stack Configuration ================== */ #define CONFIG_USBHOST_MAX_RHPORTS 1 #define CONFIG_USBHOST_MAX_EXTHUBS 1 #define CONFIG_USBHOST_MAX_EHPORTS 4 #define CONFIG_USBHOST_MAX_INTERFACES 8 #define CONFIG_USBHOST_MAX_INTF_ALTSETTINGS 8 #define CONFIG_USBHOST_MAX_ENDPOINTS 4 #define CONFIG_USBHOST_MAX_CDC_ACM_CLASS 4 #define CONFIG_USBHOST_MAX_HID_CLASS 4 #define CONFIG_USBHOST_MAX_MSC_CLASS 2 #define CONFIG_USBHOST_MAX_AUDIO_CLASS 1 #define CONFIG_USBHOST_MAX_VIDEO_CLASS 1 #define CONFIG_USBHOST_DEV_NAMELEN 16 #ifndef CONFIG_USBHOST_PSC_PRIO #define CONFIG_USBHOST_PSC_PRIO 0 #endif #ifndef CONFIG_USBHOST_PSC_STACKSIZE #define CONFIG_USBHOST_PSC_STACKSIZE 2048 #endif //#define CONFIG_USBHOST_GET_STRING_DESC // #define CONFIG_USBHOST_MSOS_ENABLE #ifndef CONFIG_USBHOST_MSOS_VENDOR_CODE #define CONFIG_USBHOST_MSOS_VENDOR_CODE 0x00 #endif /* Ep0 max transfer buffer */ #ifndef CONFIG_USBHOST_REQUEST_BUFFER_LEN #define CONFIG_USBHOST_REQUEST_BUFFER_LEN 512 #endif #ifndef CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT #define CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT 500 #endif #ifndef CONFIG_USBHOST_MSC_TIMEOUT #define CONFIG_USBHOST_MSC_TIMEOUT 5000 #endif /* This parameter affects usb performance, and depends on (TCP_WND)tcp eceive windows size, * you can change with 2K,4K,8K,16K,default is 2K to get one TCP_MSS */ #ifndef CONFIG_USBHOST_RNDIS_ETH_MAX_RX_SIZE #define CONFIG_USBHOST_RNDIS_ETH_MAX_RX_SIZE (2048) #endif #ifndef CONFIG_USBHOST_RNDIS_ETH_MAX_TX_SIZE #define CONFIG_USBHOST_RNDIS_ETH_MAX_TX_SIZE (2048) #endif /* This parameter affects usb performance, and depends on (TCP_WND)tcp eceive windows size, * you can change with 2K,4K,8K,16K,default is 2K to get one TCP_MSS */ #ifndef CONFIG_USBHOST_CDC_NCM_ETH_MAX_RX_SIZE #define CONFIG_USBHOST_CDC_NCM_ETH_MAX_RX_SIZE (2048) #endif #ifndef CONFIG_USBHOST_CDC_NCM_ETH_MAX_TX_SIZE #define CONFIG_USBHOST_CDC_NCM_ETH_MAX_TX_SIZE (2048) #endif #define CONFIG_USBHOST_BLUETOOTH_HCI_H4 // #define CONFIG_USBHOST_BLUETOOTH_HCI_LOG #ifndef CONFIG_USBHOST_BLUETOOTH_TX_SIZE #define CONFIG_USBHOST_BLUETOOTH_TX_SIZE 2048 #endif #ifndef CONFIG_USBHOST_BLUETOOTH_RX_SIZE #define CONFIG_USBHOST_BLUETOOTH_RX_SIZE 2048 #endif /* ================ USB Device Port Configuration ================*/ #ifndef CONFIG_USBDEV_MAX_BUS #define CONFIG_USBDEV_MAX_BUS 1 // for now, bus num must be 1 except hpm ip #endif #ifndef CONFIG_USBDEV_EP_NUM #define CONFIG_USBDEV_EP_NUM 6 #endif /* ---------------- FSDEV Configuration ---------------- */ //#define CONFIG_USBDEV_FSDEV_PMA_ACCESS 2 // maybe 1 or 2, many chips may have a difference /* ---------------- DWC2 Configuration ---------------- */ #define CONFIG_USB_DWC2_RXALL_FIFO_SIZE (1024 / 4) #define CONFIG_USB_DWC2_TX0_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX1_FIFO_SIZE (512 / 4) #define CONFIG_USB_DWC2_TX2_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX3_FIFO_SIZE (64 / 4) #define CONFIG_USB_DWC2_TX4_FIFO_SIZE (512 / 4) #define CONFIG_USB_DWC2_TX5_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX6_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX7_FIFO_SIZE (0 / 4) #define CONFIG_USB_DWC2_TX8_FIFO_SIZE (0 / 4) /* ---------------- MUSB Configuration ---------------- */ // #define CONFIG_USB_MUSB_SUNXI /* ================ USB Host Port Configuration ==================*/ #ifndef CONFIG_USBHOST_MAX_BUS #define CONFIG_USBHOST_MAX_BUS 1 #endif #ifndef CONFIG_USBHOST_PIPE_NUM #define CONFIG_USBHOST_PIPE_NUM 12 #endif /* ---------------- EHCI Configuration ---------------- */ #define CONFIG_USB_EHCI_HCCR_OFFSET (0x0) #define CONFIG_USB_EHCI_FRAME_LIST_SIZE 1024 #define CONFIG_USB_EHCI_QH_NUM CONFIG_USBHOST_PIPE_NUM #define CONFIG_USB_EHCI_QTD_NUM 3 #define CONFIG_USB_EHCI_ITD_NUM 20 // #define CONFIG_USB_EHCI_HCOR_RESERVED_DISABLE // #define CONFIG_USB_EHCI_CONFIGFLAG // #define CONFIG_USB_EHCI_ISO // #define CONFIG_USB_EHCI_WITH_OHCI /* ---------------- OHCI Configuration ---------------- */ #define CONFIG_USB_OHCI_HCOR_OFFSET (0x0) /* ---------------- XHCI Configuration ---------------- */ #define CONFIG_USB_XHCI_HCCR_OFFSET (0x0) /* ---------------- DWC2 Configuration ---------------- */ /* largest non-periodic USB packet used / 4 */ // #define CONFIG_USB_DWC2_NPTX_FIFO_SIZE (512 / 4) /* largest periodic USB packet used / 4 */ // #define CONFIG_USB_DWC2_PTX_FIFO_SIZE (1024 / 4) /* * (largest USB packet used / 4) + 1 for status information + 1 transfer complete + * 1 location each for Bulk/Control endpoint for handling NAK/NYET scenario */ // #define CONFIG_USB_DWC2_RX_FIFO_SIZE ((1012 - CONFIG_USB_DWC2_NPTX_FIFO_SIZE - CONFIG_USB_DWC2_PTX_FIFO_SIZE)) /* ---------------- MUSB Configuration ---------------- */ // #define CONFIG_USB_MUSB_SUNXI #define CONFIG_USB_HS #endif ```
6
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
lizimu
这家伙很懒,什么也没写!
文章
11
回答
23
被采纳
3
关注TA
发私信
相关文章
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
rt-smart
RTC
FAL
I2C_IIC
cubemx
ESP8266
UART
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
GD32
定时器
ADC
flashDB
编译报错
socket
中断
rt_mq_消息队列_msg_queue
keil_MDK
Debug
SFUD
ulog
msh
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
a1012112796
2
个答案
1
次被采纳
聚散无由
2
个答案
1
次被采纳
加缪
1
个答案
1
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部