你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 USB 复合设备 多路虚拟串口乱码问题

[复制链接]
Lex. 提问时间:2018-5-2 14:40 /
本帖最后由 Lex. 于 2018-5-3 08:48 编辑

在STM32 F103C8T6 实现 USB 多路虚拟串口, 端点1的输出没问题,但是端点2的输出是乱码,@五哥1  


/* 端点地址 */
  1. /** @defgroup usbd_cdc_Exported_Defines
  2.   * @{
  3.   */
  4. #define CDC_IN_EP1                                   0x81  /* EP1 for data IN */
  5. #define CDC_OUT_EP1                                  0x01  /* EP1 for data OUT */
  6. #define CDC_CMD_EP1                                  0x82  /* EP2 for CDC commands */

  7. /*        add        */
  8. #define CDC_IN_EP2                                   0x83  /* EP2 for data IN */
  9. #define CDC_OUT_EP2                                  0x03  /* EP2 for data OUT */
  10. #define CDC_CMD_EP2                                  0x84  /* EP2 for CDC commands */
复制代码





/* 配置描述符的  */

  1. /* USB CDC device Configuration Descriptor */
  2. __ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
  3. {
  4.   /*Configuration Descriptor*/
  5.   0x09,   /* bLength: Configuration Descriptor size */
  6.   USB_DESC_TYPE_CONFIGURATION,      /* bDescriptorType: Configuration */
  7.   USB_CDC_CONFIG_DESC_SIZ,                /* wTotalLength:no of returned bytes */
  8.   0x00,
  9.         
  10. #ifdef DUAL_COM        
  11.   0x04,
  12. #else        
  13.   0x02,  /* bNumInterfaces: 2 interface */
  14. #endif        
  15.         
  16.   0x01,   /* bConfigurationValue: Configuration value */
  17.   0x00,   /* iConfiguration: Index of string descriptor describing the configuration */
  18.   0xC0,   /* bmAttributes: self powered */
  19.   0x32,   /* MaxPower 0 mA */

  20.   /*---------------------------------------------------------------------------*/

  21. #ifdef DUAL_COM        
  22.   // IAD  Interface Association Descriptor
  23.   0x08,        // bLength: Interface Descriptor size
  24.   0x0B,        // bDescriptorType: IAD
  25.   0x00,        // bFirstInterface
  26.   0x02,        // bInterfaceCount
  27.   0x02,        // bFunctionClass: CDC
  28.   0x02,        // bFunctionSubClass
  29.   0x01,        // bFunctionProtocol
  30.   0x02,        // iFunction        
  31. #endif        

  32.   /*Interface Descriptor */
  33.   0x09,   /* bLength: Interface Descriptor size */
  34.   USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: Interface */
  35.   /* Interface descriptor type */
  36.   0x00,   /* bInterfaceNumber: Number of Interface */
  37.   0x00,   /* bAlternateSetting: Alternate setting */
  38.   0x01,   /* bNumEndpoints: One endpoints used */
  39.   0x02,   /* bInterfaceClass: Communication Interface Class */
  40.   0x02,   /* bInterfaceSubClass: Abstract Control Model */
  41.   0x01,   /* bInterfaceProtocol: Common AT commands */
  42.   0x00,   /* iInterface: */

  43.   /*Header Functional Descriptor*/
  44.   0x05,   /* bLength: Endpoint Descriptor size */
  45.   0x24,   /* bDescriptorType: CS_INTERFACE */
  46.   0x00,   /* bDescriptorSubtype: Header Func Desc */
  47.   0x10,   /* bcdCDC: spec release number */
  48.   0x01,

  49.   /*Call Management Functional Descriptor*/
  50.   0x05,   /* bFunctionLength */
  51.   0x24,   /* bDescriptorType: CS_INTERFACE */
  52.   0x01,   /* bDescriptorSubtype: Call Management Func Desc */
  53.   0x00,   /* bmCapabilities: D0+D1 */
  54.   0x01,   /* bDataInterface: 1 */

  55.   /*ACM Functional Descriptor*/
  56.   0x04,   /* bFunctionLength */
  57.   0x24,   /* bDescriptorType: CS_INTERFACE */
  58.   0x02,   /* bDescriptorSubtype: Abstract Control Management desc */
  59.   0x02,   /* bmCapabilities */

  60.   /*Union Functional Descriptor*/
  61.   0x05,   /* bFunctionLength */
  62.   0x24,   /* bDescriptorType: CS_INTERFACE */
  63.   0x06,   /* bDescriptorSubtype: Union func desc */
  64.   0x00,   /* bMasterInterface: Communication class interface */
  65.   0x01,   /* bSlaveInterface0: Data Class Interface */

  66.   /*Endpoint 2 Descriptor*/
  67.   0x07,                           /* bLength: Endpoint Descriptor size */
  68.   USB_DESC_TYPE_ENDPOINT,   /* bDescriptorType: Endpoint */
  69.   CDC_CMD_EP1,                     /* bEndpointAddress */
  70.   0x03,                           /* bmAttributes: Interrupt */
  71.   LOBYTE(CDC_CMD_PACKET_SIZE),     /* wMaxPacketSize: */
  72.   HIBYTE(CDC_CMD_PACKET_SIZE),
  73.   0x10,                           /* bInterval: */
  74.   /*---------------------------------------------------------------------------*/

  75.   /*Data class interface descriptor*/
  76.   0x09,   /* bLength: Endpoint Descriptor size */
  77.   USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: */
  78.   0x01,   /* bInterfaceNumber: Number of Interface */
  79.   0x00,   /* bAlternateSetting: Alternate setting */
  80.   0x02,   /* bNumEndpoints: Two endpoints used */
  81.   0x0A,   /* bInterfaceClass: CDC */
  82.   0x00,   /* bInterfaceSubClass: */
  83.   0x00,   /* bInterfaceProtocol: */
  84.   0x00,   /* iInterface: */

  85.   /*Endpoint OUT Descriptor*/
  86.   0x07,   /* bLength: Endpoint Descriptor size */
  87.   USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  88.   CDC_OUT_EP1,                        /* bEndpointAddress */
  89.   0x02,                              /* bmAttributes: Bulk */
  90.   LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  91.   HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  92.   0x00,                              /* bInterval: ignore for Bulk transfer */

  93.   /*Endpoint IN Descriptor*/
  94.   0x07,   /* bLength: Endpoint Descriptor size */
  95.   USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  96.   CDC_IN_EP1,                         /* bEndpointAddress */
  97.   0x02,                              /* bmAttributes: Bulk */
  98.   LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  99.   HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
  100.   0x00,                               /* bInterval: ignore for Bulk transfer */

  101. #ifdef DUAL_COM
  102. /*---------------------------------------------------------------------------*/
  103.   // IAD  Interface Association Descriptor
  104.   0x08,        // bLength: Interface Descriptor size
  105.   0x0B,                // bDescriptorType: IAD
  106.   0x02,        // bFirstInterface
  107.   0x02,        // bInterfaceCount
  108.   0x02,        // bFunctionClass: CDC
  109.   0x02,        // bFunctionSubClass
  110.   0x01,        // bFunctionProtocol
  111.   0x02,        // iFunction

  112. /*Interface Descriptor */
  113.   0x09,   /* bLength: Interface Descriptor size */
  114.   USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: Interface */
  115.   /* Interface descriptor type */
  116.   0x02,   /* bInterfaceNumber: Number of Interface */
  117.   0x00,   /* bAlternateSetting: Alternate setting */
  118.   0x01,   /* bNumEndpoints: One endpoints used */
  119.   0x02,   /* bInterfaceClass: Communication Interface Class */
  120.   0x02,   /* bInterfaceSubClass: Abstract Control Model */
  121.   0x01,   /* bInterfaceProtocol: Common AT commands */
  122.   0x00,   /* iInterface: */

  123.   /*Header Functional Descriptor*/
  124.   0x05,   /* bLength: Endpoint Descriptor size */
  125.   0x24,   /* bDescriptorType: CS_INTERFACE */
  126.   0x00,   /* bDescriptorSubtype: Header Func Desc */
  127.   0x10,   /* bcdCDC: spec release number */
  128.   0x01,

  129.   /*Call Management Functional Descriptor*/
  130.   0x05,   /* bFunctionLength */
  131.   0x24,   /* bDescriptorType: CS_INTERFACE */
  132.   0x01,   /* bDescriptorSubtype: Call Management Func Desc */
  133.   0x00,   /* bmCapabilities: D0+D1 */
  134.   0x01,   /* bDataInterface: 1 */

  135.   /*ACM Functional Descriptor*/
  136.   0x04,   /* bFunctionLength */
  137.   0x24,   /* bDescriptorType: CS_INTERFACE */
  138.   0x02,   /* bDescriptorSubtype: Abstract Control Management desc */
  139.   0x02,   /* bmCapabilities */

  140.   /*Union Functional Descriptor*/
  141.   0x05,   /* bFunctionLength */
  142.   0x24,   /* bDescriptorType: CS_INTERFACE */
  143.   0x06,   /* bDescriptorSubtype: Union func desc */
  144.   0x00,   /* bMasterInterface: Communication class interface */
  145.   0x01,   /* bSlaveInterface0: Data Class Interface */

  146.   /*Endpoint 2 Descriptor*/
  147.   0x07,                           /* bLength: Endpoint Descriptor size */
  148.   USB_DESC_TYPE_ENDPOINT,   /* bDescriptorType: Endpoint */
  149.   CDC_CMD_EP2,                     /* bEndpointAddress */
  150.   0x03,                           /* bmAttributes: Interrupt */
  151.   LOBYTE(CDC_CMD_PACKET_SIZE),     /* wMaxPacketSize: */
  152.   HIBYTE(CDC_CMD_PACKET_SIZE),
  153.   0x10,                           /* bInterval: */
  154.   /*---------------------------------------------------------------------------*/

  155.   /*Data class interface descriptor*/
  156.   0x09,   /* bLength: Endpoint Descriptor size */
  157.   USB_DESC_TYPE_INTERFACE,  /* bDescriptorType: */
  158.   0x03,   /* bInterfaceNumber: Number of Interface */
  159.   0x00,   /* bAlternateSetting: Alternate setting */
  160.   0x02,   /* bNumEndpoints: Two endpoints used */
  161.   0x0A,   /* bInterfaceClass: CDC */
  162.   0x00,   /* bInterfaceSubClass: */
  163.   0x00,   /* bInterfaceProtocol: */
  164.   0x00,   /* iInterface: */

  165.   /*Endpoint OUT Descriptor*/
  166.   0x07,   /* bLength: Endpoint Descriptor size */
  167.   USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  168.   CDC_OUT_EP2,                        /* bEndpointAddress */
  169.   0x02,                              /* bmAttributes: Bulk */
  170.   LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  171.   HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  172.   0x00,                              /* bInterval: ignore for Bulk transfer */

  173.   /*Endpoint IN Descriptor*/
  174.   0x07,   /* bLength: Endpoint Descriptor size */
  175.   USB_DESC_TYPE_ENDPOINT,      /* bDescriptorType: Endpoint */
  176.   CDC_IN_EP2,                         /* bEndpointAddress */
  177.   0x02,                              /* bmAttributes: Bulk */
  178.   LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),  /* wMaxPacketSize: */
  179.   HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),
  180.   0x00                               /* bInterval: ignore for Bulk transfer */

  181. #endif

  182. } ;
复制代码



/* 端点初始化 */
  1. /**
  2.   * @brief  USBD_CDC_Init
  3.   *         Initialize the CDC interface
  4.   * @param  pdev: device instance
  5.   * @param  cfgidx: Configuration index
  6.   * @retval status
  7.   */
  8. static uint8_t  USBD_CDC_Init (USBD_HandleTypeDef *pdev,
  9.                                uint8_t cfgidx)
  10. {
  11.   uint8_t ret = 0;
  12.   USBD_CDC_HandleTypeDef   *hcdc;

  13.   if(pdev->dev_speed == USBD_SPEED_HIGH  )
  14.   {  
  15.     /* Open EP IN */
  16.     USBD_LL_OpenEP(pdev,
  17.                    CDC_IN_EP1,
  18.                    USBD_EP_TYPE_BULK,
  19.                    CDC_DATA_HS_IN_PACKET_SIZE);

  20.     /* Open EP OUT */
  21.     USBD_LL_OpenEP(pdev,
  22.                    CDC_OUT_EP1,
  23.                    USBD_EP_TYPE_BULK,
  24.                    CDC_DATA_HS_OUT_PACKET_SIZE);

  25.   }
  26.   else
  27.   {
  28.     /* Open EP IN */
  29.     USBD_LL_OpenEP(pdev,
  30.                    CDC_IN_EP1,
  31.                    USBD_EP_TYPE_BULK,
  32.                    CDC_DATA_FS_IN_PACKET_SIZE);

  33.     /* Open EP OUT */
  34.     USBD_LL_OpenEP(pdev,
  35.                    CDC_OUT_EP1,
  36.                    USBD_EP_TYPE_BULK,
  37.                    CDC_DATA_FS_OUT_PACKET_SIZE);

  38. #ifdef DUAL_COM         
  39.     /* Open EP IN */
  40.     USBD_LL_OpenEP(pdev,
  41.                    CDC_IN_EP2,
  42.                    USBD_EP_TYPE_BULK,
  43.                    CDC_DATA_FS_IN_PACKET_SIZE);

  44.     /* Open EP OUT */
  45.     USBD_LL_OpenEP(pdev,
  46.                    CDC_OUT_EP2,
  47.                    USBD_EP_TYPE_BULK,
  48.                    CDC_DATA_FS_OUT_PACKET_SIZE);         
  49. #endif                                   
  50.   }
  51.   /* Open Command IN EP */
  52.   USBD_LL_OpenEP(pdev,
  53.                  CDC_CMD_EP1,
  54.                  USBD_EP_TYPE_INTR,
  55.                  CDC_CMD_PACKET_SIZE);

  56. //  /* Open Command IN EP */
  57.   USBD_LL_OpenEP(pdev,
  58.                  CDC_CMD_EP2,
  59.                  USBD_EP_TYPE_INTR,
  60.                  CDC_CMD_PACKET_SIZE);


  61.   pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));

  62.   if(pdev->pClassData == NULL)
  63.   {
  64.     ret = 1;
  65.   }
  66.   else
  67.   {
  68.     hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData;

  69.     /* Init  physical Interface components */
  70.     ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();

  71.     /* Init Xfer states */
  72.     hcdc->TxState =0;
  73.     hcdc->RxState =0;

  74.     if(pdev->dev_speed == USBD_SPEED_HIGH  )
  75.     {      
  76.       /* Prepare Out endpoint to receive next packet */
  77.       USBD_LL_PrepareReceive(pdev,
  78.                              CDC_OUT_EP1,
  79.                              hcdc->RxBuffer,
  80.                              CDC_DATA_HS_OUT_PACKET_SIZE);
  81.     }
  82.     else
  83.     {
  84.       /* Prepare Out endpoint to receive next packet */
  85.       USBD_LL_PrepareReceive(pdev,
  86.                              CDC_OUT_EP1,
  87.                              hcdc->RxBuffer,
  88.                              CDC_DATA_FS_OUT_PACKET_SIZE);

  89.       USBD_LL_PrepareReceive(pdev,
  90.                              CDC_OUT_EP2,
  91.                              hcdc->RxBuffer,
  92.                              CDC_DATA_FS_OUT_PACKET_SIZE);               
  93.     }


  94.   }
  95.   return ret;
  96. }
复制代码




TIM图片20180502142936.png

设备描述符
TIM图片20180502143023.png


发送函数
TIM图片20180502143532.png

PC 上监控 端点1数据
TIM图片20180502143842.png

端点2 数据:
TIM图片20180502143819.png

两个端点发送相同的数据, 但是PC端接收的却不同   ~



TIM图片20180502143048.png

点评

付费主题?  发表于 2018-5-2 15:46
2 收藏 1 评论13 发布时间:2018-5-2 14:40

举报

13个回答
llp474129014 回答时间:2018-5-2 16:39:07
多谢分享

评分

参与人数 1ST金币 -1 收起 理由
zero99 -1 求助,禁止灌水

查看全部评分

mmuuss586 回答时间:2018-5-2 16:46:34

感谢分享;

评分

参与人数 1ST金币 -1 收起 理由
zero99 -1 哪来的分享

查看全部评分

Lex. 回答时间:2018-5-3 08:48:15
问题解决了!!!
端点0 的Packet Buffer 的地址必须往后偏移
http://www.docin.com/p-760532785.html

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2 结贴奖励

查看全部评分

笑尘一梦 回答时间:2018-9-12 19:35:41
楼主,这边也遇到乱码的问题,端点0 的Packet Buffer 的地址必须往后偏移 是在哪个地方改
John-2049691 回答时间:2019-12-31 17:25:48
你好,我遇到同样的问题,能否把你的例程发上来给我参考下,谢谢!
星——星 回答时间:2020-3-23 15:35:16
你好 打扰了,最近调试 stm32L476 第二路串口接收正常,发送乱码,并且发送几次后会导致串口工具异常,重置后仍是如此,看了你说的修改地址偏移,但是生成的HAL代码 好像没找到设置的位置,能指导一下吗,谢谢
星——星 回答时间:2020-3-23 15:46:13
Lex. 发表于 2018-5-3 08:48
问题解决了!!!
端点0 的Packet Buffer 的地址必须往后偏移
http://www.docin.com/p-760532785.html ...

请问楼主,这个偏移地址 新HAL生成的代码 好像没有这个地址设置,求指教,
lw5413 回答时间:2020-4-6 23:24:08
我也遇到这个问题,一直找原因,原来是这样的。。。不过如何修改呢?
lw5413 回答时间:2020-4-12 11:38:31
需要把断点fifo size设到最小,原因不明
icewater1234 回答时间:2020-7-8 16:11:55
棒棒棒
carmelo 回答时间:2020-10-10 10:30:21
输入你做了吗?
dubutian 回答时间:2020-10-11 10:46:35
多谢分享
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版