打印
[STM32F1]

STM32F103 USB 设备描述符问题请教

[复制链接]
406|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
elephant00|  楼主 | 2024-8-20 16:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我用STM32F103 开发一个USB 自定义设备, 因为STM32CUBE 里没有自定义设备,所以我选择CDC然后更改设备描述符,我之前在407的芯片上这样做没有任何问题,但是我在103上更改,电脑就经常是显示还是CDC设备,有时候就找不到设备,很奇怪

我尝试单步调试USB的描述符加载过程,发现在USBD_LL_Init 里有强行指定了3个endpoint ,跟我的设置不一样,但是设备描述符他自己还是加载的CDC的没搞清楚

HAL_PCDEx_PMAConfig((PCD_HandleTypeDef)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18); HAL_PCDEx_PMAConfig((PCD_HandleTypeDef)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58); / USER CODE END EndPoint_Configuration / / USER CODE BEGIN EndPoint_Configuration_CDC / HAL_PCDEx_PMAConfig((PCD_HandleTypeDef)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0); HAL_PCDEx_PMAConfig((PCD_HandleTypeDef)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110); HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);

下面是我修改的 usb_cdc.C

/ USB CDC device Configuration Descriptor / ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] ALIGN_END = { /Configuration Descriptor/ 0x09, / bLength: Configuration Descriptor size / USB_DESC_TYPE_CONFIGURATION, / bDescriptorType: Configuration / USB_CDC_CONFIG_DESC_SIZ, / wTotalLength:no of returned bytes / 0x00, 0x01, / bNumInterfaces: 2 interface / 0x01, / bConfigurationValue: Configuration value / 0x00, / iConfiguration: Index of string descriptor describing the configuration / 0xC0, / bmAttributes: self powered / 0x32, / MaxPower 0 mA /

/---------------------------------------------------------------------------/

/Interface Descriptor / 0x09, / bLength: Interface Descriptor size / USB_DESC_TYPE_INTERFACE, / bDescriptorType: Interface / / Interface descriptor type / 0x00, / bInterfaceNumber: Number of Interface / 0x00, / bAlternateSetting: Alternate setting / 0x02, / bNumEndpoints: One endpoints used / 0xff, / bInterfaceClass: Communication Interface Class / 0xff, / bInterfaceSubClass: Abstract Control Model / 0x00, / bInterfaceProtocol: Common AT commands / 0x00, / iInterface: /

/Endpoint OUT Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_OUT_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), / wMaxPacketSize: / HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), 0x00, / bInterval: ignore for Bulk transfer /

/Endpoint IN Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_IN_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), / wMaxPacketSize: / HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), 0x00 / bInterval: ignore for Bulk transfer / } ;

/ USB CDC device Configuration Descriptor / ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] ALIGN_END = { /Configuration Descriptor/ 0x09, / bLength: Configuration Descriptor size / USB_DESC_TYPE_CONFIGURATION, / bDescriptorType: Configuration / USB_CDC_CONFIG_DESC_SIZ, / wTotalLength:no of returned bytes / 0x00, 0x01, / bNumInterfaces: 2 interface / 0x01, / bConfigurationValue: Configuration value / 0x00, / iConfiguration: Index of string descriptor describing the configuration / 0xC0, / bmAttributes: self powered / 0x32, / MaxPower 0 mA /

/---------------------------------------------------------------------------/

/Interface Descriptor / 0x09, / bLength: Interface Descriptor size / USB_DESC_TYPE_INTERFACE, / bDescriptorType: Interface / / Interface descriptor type / 0x00, / bInterfaceNumber: Number of Interface / 0x00, / bAlternateSetting: Alternate setting / 0x02, / bNumEndpoints: One endpoints used / 0xff, / bInterfaceClass: Communication Interface Class / 0xff, / bInterfaceSubClass: Abstract Control Model / 0x00, / bInterfaceProtocol: Common AT commands / 0x00, / iInterface: /

/Endpoint OUT Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_OUT_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), / wMaxPacketSize: / HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), 0x00, / bInterval: ignore for Bulk transfer /

/Endpoint IN Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_IN_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), / wMaxPacketSize: / HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), 0x00 / bInterval: ignore for Bulk transfer / } ;

ALIGN_BEGIN uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] ALIGN_END = { 0x09, / bLength: Configuation Descriptor size / USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, USB_CDC_CONFIG_DESC_SIZ, 0x00, 0x01, / bNumInterfaces: 2 interfaces / 0x01, / bConfigurationValue: / 0x00, / iConfiguration: / 0xC0, / bmAttributes: / 0x32, / MaxPower 100 mA /

/Interface Descriptor / 0x09, / bLength: Interface Descriptor size / USB_DESC_TYPE_INTERFACE, / bDescriptorType: Interface / / Interface descriptor type / 0x00, / bInterfaceNumber: Number of Interface / 0x00, / bAlternateSetting: Alternate setting / 0x02, / bNumEndpoints: One endpoints used / 0xff, / bInterfaceClass: Communication Interface Class / 0xff, / bInterfaceSubClass: Abstract Control Model / 0x00, / bInterfaceProtocol: Common AT commands / 0x00, / iInterface: /

/Endpoint OUT Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_OUT_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / 0x40, / wMaxPacketSize: / 0x00, 0x00, / bInterval: ignore for Bulk transfer /

/Endpoint IN Descriptor/ 0x07, / bLength: Endpoint Descriptor size / USB_DESC_TYPE_ENDPOINT, / bDescriptorType: Endpoint / CDC_IN_EP, / bEndpointAddress / 0x02, / bmAttributes: Bulk / 0x40, / wMaxPacketSize: / 0x00, 0x00 / bInterval / };

下面是我修改的 usb_cdc.H

define CDC_IN_EP 0x82U / EP1 for data IN /
define CDC_OUT_EP 0x01U / EP1 for data OUT /
define CDC_CMD_EP 0x83U / EP2 for CDC commands /
ifndef CDC_HS_BINTERVAL
define CDC_HS_BINTERVAL 0x10U
endif / CDC_HS_BINTERVAL /
ifndef CDC_FS_BINTERVAL
define CDC_FS_BINTERVAL 0x10U
endif / CDC_FS_BINTERVAL /
/ CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. /

define CDC_DATA_HS_MAX_PACKET_SIZE 512U / Endpoint IN & OUT Packet size /
define CDC_DATA_FS_MAX_PACKET_SIZE 64U / Endpoint IN & OUT Packet size /
define CDC_CMD_PACKET_SIZE 8U / Control Endpoint Packet size /
define USB_CDC_CONFIG_DESC_SIZ 32U//67U

使用特权

评论回复
沙发
laocuo1142| | 2024-8-20 19:27 | 只看该作者
设备描述符未正确更新

使用特权

评论回复
板凳
flycamelaaa| | 2024-8-20 19:27 | 只看该作者
USBD_LL_Init函数中硬编码的端点配置可能与你的设备描述符不匹配。

使用特权

评论回复
地板
powerantone| | 2024-8-20 19:28 | 只看该作者
在设备描述符中更改VID和PID,以避免与已知设备类型的冲突。

使用特权

评论回复
5
xionghaoyun| | 2024-8-21 09:05 | 只看该作者
一串英语看得头痛

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

941

主题

2672

帖子

5

粉丝