STM32 USB 复合设备 多路虚拟串口乱码问题
本帖最后由 Lex. 于 2018-5-3 08:48 编辑在STM32 F103C8T6 实现 USB 多路虚拟串口, 端点1的输出没问题,但是端点2的输出是乱码,@五哥1
/* 端点地址 */
/** @defgroup usbd_cdc_Exported_Defines
* @{
*/
#define CDC_IN_EP1 0x81/* EP1 for data IN */
#define CDC_OUT_EP1 0x01/* EP1 for data OUT */
#define CDC_CMD_EP1 0x82/* EP2 for CDC commands */
/* add */
#define CDC_IN_EP2 0x83/* EP2 for data IN */
#define CDC_OUT_EP2 0x03/* EP2 for data OUT */
#define CDC_CMD_EP2 0x84/* EP2 for CDC commands */
/* 配置描述符的*/
/* USB CDC device Configuration Descriptor */
__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc __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,
#ifdef DUAL_COM
0x04,
#else
0x02,/* bNumInterfaces: 2 interface */
#endif
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 0 mA */
/*---------------------------------------------------------------------------*/
#ifdef DUAL_COM
// IADInterface Association Descriptor
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x00, // bFirstInterface
0x02, // bInterfaceCount
0x02, // bFunctionClass: CDC
0x02, // bFunctionSubClass
0x01, // bFunctionProtocol
0x02, // iFunction
#endif
/*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 */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP1, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0x10, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE,/* bDescriptorType: */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP1, /* 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_EP1, /* 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 */
#ifdef DUAL_COM
/*---------------------------------------------------------------------------*/
// IADInterface Association Descriptor
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x02, // bFirstInterface
0x02, // bInterfaceCount
0x02, // bFunctionClass: CDC
0x02, // bFunctionSubClass
0x01, // bFunctionProtocol
0x02, // iFunction
/*Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE,/* bDescriptorType: Interface */
/* Interface descriptor type */
0x02, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP2, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0x10, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE,/* bDescriptorType: */
0x03, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP2, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),/* wMaxPacketSize: */
HIBYTE(CDC_DATA_HS_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_EP2, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),/* wMaxPacketSize: */
HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
0x00 /* bInterval: ignore for Bulk transfer */
#endif
} ;
/* 端点初始化 */
/**
* @briefUSBD_CDC_Init
* Initialize the CDC interface
* @parampdev: device instance
* @paramcfgidx: Configuration index
* @retval status
*/
static uint8_tUSBD_CDC_Init (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
uint8_t ret = 0;
USBD_CDC_HandleTypeDef *hcdc;
if(pdev->dev_speed == USBD_SPEED_HIGH)
{
/* Open EP IN */
USBD_LL_OpenEP(pdev,
CDC_IN_EP1,
USBD_EP_TYPE_BULK,
CDC_DATA_HS_IN_PACKET_SIZE);
/* Open EP OUT */
USBD_LL_OpenEP(pdev,
CDC_OUT_EP1,
USBD_EP_TYPE_BULK,
CDC_DATA_HS_OUT_PACKET_SIZE);
}
else
{
/* Open EP IN */
USBD_LL_OpenEP(pdev,
CDC_IN_EP1,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_IN_PACKET_SIZE);
/* Open EP OUT */
USBD_LL_OpenEP(pdev,
CDC_OUT_EP1,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_OUT_PACKET_SIZE);
#ifdef DUAL_COM
/* Open EP IN */
USBD_LL_OpenEP(pdev,
CDC_IN_EP2,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_IN_PACKET_SIZE);
/* Open EP OUT */
USBD_LL_OpenEP(pdev,
CDC_OUT_EP2,
USBD_EP_TYPE_BULK,
CDC_DATA_FS_OUT_PACKET_SIZE);
#endif
}
/* Open Command IN EP */
USBD_LL_OpenEP(pdev,
CDC_CMD_EP1,
USBD_EP_TYPE_INTR,
CDC_CMD_PACKET_SIZE);
///* Open Command IN EP */
USBD_LL_OpenEP(pdev,
CDC_CMD_EP2,
USBD_EP_TYPE_INTR,
CDC_CMD_PACKET_SIZE);
pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
if(pdev->pClassData == NULL)
{
ret = 1;
}
else
{
hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;
/* Initphysical Interface components */
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();
/* Init Xfer states */
hcdc->TxState =0;
hcdc->RxState =0;
if(pdev->dev_speed == USBD_SPEED_HIGH)
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP1,
hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
}
else
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP1,
hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP2,
hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
}
}
return ret;
}
设备描述符
发送函数
PC 上监控 端点1数据
端点2 数据:
两个端点发送相同的数据, 但是PC端接收的却不同 ~
多谢分享 :)
感谢分享; 问题解决了!!!
端点0 的Packet Buffer 的地址必须往后偏移
http://www.docin.com/p-760532785.html 楼主,这边也遇到乱码的问题,端点0 的Packet Buffer 的地址必须往后偏移 是在哪个地方改 你好,我遇到同样的问题,能否把你的例程发上来给我参考下,谢谢! 你好 打扰了,最近调试 stm32L476 第二路串口接收正常,发送乱码,并且发送几次后会导致串口工具异常,重置后仍是如此,看了你说的修改地址偏移,但是生成的HAL代码 好像没找到设置的位置,能指导一下吗,谢谢 Lex. 发表于 2018-5-3 08:48
问题解决了!!!
端点0 的Packet Buffer 的地址必须往后偏移
http://www.docin.com/p-760532785.html ...
请问楼主,这个偏移地址 新HAL生成的代码 好像没有这个地址设置,求指教, 我也遇到这个问题,一直找原因,原来是这样的。。。不过如何修改呢? 需要把断点fifo size设到最小,原因不明
页:
[1]
2