cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32h745Xi and USB Host CDC Data Transmit and receive Issue

kushal_parmar
Visitor

Hello,

i have implemented USB Host CDC in my controller using STM32CubeMX and also taken reference from the link : https://controllerstech.com/usb-cdc-device-and-host-in-stm32/#:~:text=HOST%20Setup,Leave%20everything%20here%20to%20default.aken the reference from the link .

My goal is to connect Arduino Uno to my controller's USB port. Software in arduino uno is written such that it will send data every second and retransmit the received data. Arduino code is verified by connection to my computer serial terminal. It is both transmitting and receiving the data.

But when i connect it to my stm32h745 board it gets detected and goes in ready state i.e. ApplicationState = APPLICATION_READY but not able to transmit or receive any data on the controller. When i transmit data from stm32h7 to arduino it is even not giving me failure but data is also not received on arduino. 

I have attached the portion of the code for reference. 

 

Arduino code :

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open the serial port at 9600 bps:
  pinMode(LED_BUILTIN, OUTPUT);
}

uint8_t Mode = 0;

void loop() {
  static uint8_t x = 0;
  // put your main code here, to run repeatedly:
  if(Serial.available())
  {
    Serial.print("Rcv Data: ");
    Serial.println(Serial.read());
  }


  Serial.println(x);
  x++;
  if(x > 230)
    x = 0;

    Mode = !Mode;
    digitalWrite(LED_BUILTIN, Mode);

    delay(1000);
}
 
STM32H7 Code:
 in main. c
int main(void)
{
MX_USB_HOST_Init();
 
while (1)
{
USBH_Process(&hUsbHostFS);
USB_HOST_TestFunction(&gOsControlApplication.mUsbCdcControl);
}
}
 
void MX_USB_HOST_Init(void)
{
  /* USER CODE BEGIN USB_HOST_Init_PreTreatment */
 
  /* USER CODE END USB_HOST_Init_PreTreatment */
 
  /* Init host Library, add supported class and start the library. */
  if (USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS) != USBH_OK)
  {
    Error_Handler();
  }
 
  if (USBH_RegisterClass(&hUsbHostFS, USBH_CDC_CLASS) != USBH_OK)
  {
    Error_Handler();
  }
 
  USB_DEBUGOUT("**** USB CDC Initialization Successfully Completed ****");
 
  if (USBH_Start(&hUsbHostFS) != USBH_OK)
  {
    Error_Handler();
  }
 
  /* USER CODE BEGIN USB_HOST_Init_PostTreatment */
 
ApplicationState = APPLICATION_IDLE;
 
  /* USER CODE END USB_HOST_Init_PostTreatment */
}
 
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  switch(id)
  {
  case HOST_USER_SELECT_CONFIGURATION:
  break;
 
  case HOST_USER_DISCONNECTION:
  ApplicationState = APPLICATION_DISCONNECT;
  break;
 
  case HOST_USER_CLASS_ACTIVE:
  ApplicationState = APPLICATION_READY;
USBH_CDC_Receive(&hUsbHostFS, (uint8_t *) CDC_RX_Buffer, RX_BUFF_SIZE);
  break;
 
  case HOST_USER_CONNECTION:
  ApplicationState = APPLICATION_START;
  break;
 
  default:
  break;
  }
  /* USER CODE END CALL_BACK_1 */
}
 
ErrorStatus USB_HOST_TransmitData(uint8_t *pData, uint32_t dataLength)
{
ErrorStatus ret = ERROR;
USBH_StatusTypeDef err = USBH_OK;
 
USB_DEBUGOUT("%s() %d ==> %s", __FUNCTION__, dataLength, pData);
 
USBH_CDC_Stop(&hUsbHostFS);
err = USBH_CDC_Transmit(&hUsbHostFS, pData, dataLength);
if(err == USBH_OK)
ret = SUCCESS;
else
USB_DEBUGOUT("Error USB Tx: %d", err);
 
return ret;
}
 
Regards,
Kushal
0 REPLIES 0