2016-05-17 10:26 PM
stm32 USB VCP Receive problem.
hello. I need help. I use STM32F4 eval board. and I want use ROTS and USB_HS.my eval board has USB_OTG_FS and USB_OTG_HS. so I use USB_OTG_HS.I select in cubeMX, USB_OTG_HS's Exteranl Phy -> Device_Only.And select LWIP, FREERTOS, USB_DEVICE->Communication Device Class(Virtual Port Com).Code generate, and success to connect USB_HS. but, when I transmit from board to my windows10 OS computer, It work well.very good. but, Receive form computer to device, the data was not good. example , when I trans alphabet 'a', my board take it 'a\', when 'd', 'dAT', when 'c', 'c'..... I don't know why it works like this. This is my code. in main, init others pins, and devices. and at first RTOS defaultTask init USB HOST. ----------------------------------------------------------------------void StartDefaultTask(void const * argument){ /* init code for LWIP */ MX_LWIP_Init(); /* init code for USB_DEVICE */ MX_USB_DEVICE_Init(); /* USER CODE BEGIN 5 */ /* Infinite loop */ for(;;) { osDelay(1); } /* USER CODE END 5 */ }----------------------------------------------------------------------
and my other Task, I use USB code. like this. ----------------------------------------------------------------------void StartTask07(void const * argument){ /* USER CODE BEGIN StartTask07 */ /* Infinite loop */ uint8_t TxUsb; USBD_CDC_SetRxBuffer(&hUsbDeviceHS, RxUsb); USBD_CDC_SetTxBuffer(&hUsbDeviceHS , &TxUsb, 1);*/ osDelay(500); char MyRxBuff[16] = {'\0'}; char testBuff[32]; uint8_t size; uint32_t cnt = 0; for(;;) { if(hUsbDeviceHS.dev_state == USBD_STATE_CONFIGURED) { cnt++; sprintf(testBuff, '' myTest is %d \n'', cnt); Transmit(testBuff,strlen(testBuff)); USBD_CDC_TransmitPacket(&hUsbDeviceHS); if(Receive(MyRxBuff) == USBD_OK) { size = strlen(MyRxBuff); Transmit(MyRxBuff,strlen(MyRxBuff)); } else { USBD_CDC_SetTxBuffer(&hUsbDeviceHS , ''Hello\n'',7); USBD_CDC_TransmitPacket(&hUsbDeviceHS ); } } osDelay(500); }}----------------------------------------------------------------------int8_t Transmit (char* Buf, uint16_t Len){ /* USER CODE BEGIN 3 */ uint8_t result = USBD_OK; USBD_CDC_SetTxBuffer(&hUsbDeviceHS, Buf, Len); result = USBD_CDC_TransmitPacket(&hUsbDeviceHS); return (0); /* USER CODE END 3 */}int8_t Receive (char* Buf){ /* USER CODE BEGIN 3 */ uint8_t result = USBD_OK; USBD_CDC_SetRxBuffer(&hUsbDeviceHS, Buf); result = USBD_CDC_ReceivePacket(&hUsbDeviceHS); return (0); /* USER CODE END 3 */}----------------------------------------------------------------------this codes are in main.c.and next code is generated by CubeMx USBD_LL_INIT.--------------------------------------------------------------------------USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev){ /* Init USB_IP */ if (pdev->id == DEVICE_HS) { /* Link The driver to the stack */ hpcd_USB_OTG_HS.pData = pdev; pdev->pData = &hpcd_USB_OTG_HS; hpcd_USB_OTG_HS.Instance = USB_OTG_HS; hpcd_USB_OTG_HS.Init.dev_endpoints = 11; hpcd_USB_OTG_HS.Init.speed = PCD_SPEED_HIGH; hpcd_USB_OTG_HS.Init.dma_enable = DISABLE; hpcd_USB_OTG_HS.Init.ep0_mps = DEP0CTL_MPS_32; hpcd_USB_OTG_HS.Init.phy_itface = USB_OTG_ULPI_PHY; hpcd_USB_OTG_HS.Init.Sof_enable = DISABLE; hpcd_USB_OTG_HS.Init.low_power_enable = DISABLE; hpcd_USB_OTG_HS.Init.lpm_enable = ENABLE; hpcd_USB_OTG_HS.Init.vbus_sensing_enable = ENABLE; hpcd_USB_OTG_HS.Init.use_dedicated_ep1 = DISABLE; hpcd_USB_OTG_HS.Init.use_external_vbus = DISABLE; HAL_PCD_Init(&hpcd_USB_OTG_HS); HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200); HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x80); HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x174); } return USBD_OK;}--------------------------------------------------------------------------and next is my TeraTerm result. aW myTest is 23809aW myTest is 23810aW myTest is 23811aW myTest is 23812c myTest is 23813dAT myTest is 23814dAT myTest is 23815dAT myTest is 23816l@ myTest is 23817l@ myTest is 23818i myTest is 23819i myTest is 23820i myTest is 23821i myTest is 23822i myTest is 23823j&&sharp16; myTest is 23824j&&sharp16; myTest is 23825l@ myTest is 23826hAQ myTest is 23827hAQ myTest is 23828hAQ myTest is 23829o myTest is 23830o myTest is 23831o myTest is 23832pA[ myTest is 23833pA[ myTest is 23834pA[ myTest is 23835''myTest is %d'' are Trans from device. and front alphabet is wrong data. Thank you. #stm32-cubemx-usb-vcp-stm32f42016-09-01 10:28 AM
Hi,
It may be related to a bug in CubeMx which enable by default the Vbus sensing. It should be fixed in the last version.As a work around :Replace :hpcd_USB_OTG_HS.Init.vbus_sensing_enable = ENABLE;
byhpcd_USB_OTG_HS.Init.vbus_sensing_enable = DISABLE;
-Hannibal-2016-09-01 11:58 PM
do you still have the problem with receiving?
maybe this is helping:/fe800d0