Skip to main content
FUceda
Visitor II
April 9, 2019
Question

How to set up an USB-CDC-Host and read Data from a Serial device firing values nonstop.

  • April 9, 2019
  • 0 replies
  • 1900 views

I use a STM32F429ZI-DISCO board that was programmed using STM32CubeMX's Code generation for a USB-CDC-Host on high speed. To generate some input data I use a Teensy3.6 as an arduino Board set up as a serial communication device. The data sent by the Teensy can be read correctly with a PC and a serial terminal (e.g. HTerm).

0690X000008A6vwQAC.png

However, I am not able to read the same Data with the STM-DISCO. I used the default installment from STM32CubeMX having tweaked the LineCoding to my specific serial port inside the init-function as shown below.

...
void MX_USB_HOST_Init(void)
{
 /* USER CODE BEGIN USB_HOST_Init_PreTreatment */
	// TODO Create LineCodingType for MM30
	CDC_LineCodingTypeDef my_Port_pref;
 
 /* USER CODE END USB_HOST_Init_PreTreatment */
 
 /* Init host Library, add supported class and start the library. */
 USBH_Init(&hUsbHostHS, USBH_UserProcess, HOST_HS);
 
 USBH_RegisterClass(&hUsbHostHS, USBH_CDC_CLASS);
 my_Port_pref.b.dwDTERate = 9600;
 my_Port_pref.b.bCharFormat = 0x00; // 0 - 1 SB | 1 - 1,5 SB | 2 - 2 SB
 my_Port_pref.b.bParityType = 0x00;
 my_Port_pref.b.bDataBits = 0x08;
 USBH_CDC_SetLineCoding(&hUsbHostHS, &my_Port_pref);
 
 USBH_Start(&hUsbHostHS);
 
 /* USER CODE BEGIN USB_HOST_Init_PostTreatment */
 
 /* USER CODE END USB_HOST_Init_PostTreatment */
}
...

After that, i used the USB_BulkRecieveData() function inside of the USBH_UserProcess to read some data as soon as a USB device is connected.

static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id)
{
 /* USER CODE BEGIN CALL_BACK_1 */
	uint16_t my_buffer_len = 512;
	uint8_t my_usb_buffer[my_buffer_len];
 switch(id)
 {
 case HOST_USER_SELECT_CONFIGURATION:
 break;
 
 case HOST_USER_DISCONNECTION:
 Appli_state = APPLICATION_DISCONNECT;
 HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, FALSE);
 HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, FALSE);
 break;
 
 case HOST_USER_CLASS_ACTIVE:
 Appli_state = APPLICATION_READY;
 HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, TRUE);
 break;
 
 case HOST_USER_CONNECTION:
 Appli_state = APPLICATION_START;
 HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, TRUE);
 USBH_BulkReceiveData(phost, my_usb_buffer, my_buffer_len, 0);
 break;
 
 default:
 break;
 }
 /* USER CODE END CALL_BACK_1 */
}

The data in the buffer after executing the receive-function does not look like the data that has been read by the PC. The first 200Bytes look random. Afterwards it reads a lot of zeros (00)h. Therefore I suspect that there is an fault in the initiation or the setup of the usb-serial host.

Do I need to use a different recieve function or do I need to set up the serial parameters at another position?

I tried using the USB-Host manual without success and the most tutorials go by using the DISCO board as a device and not as a master.

This topic has been closed for replies.