Search code examples
usbstm32cdcnucleo

STML4 USB virtual com port


I have the nucleo board (nucleo-L4R5ZI) and want to write a code to be able to send data from a uC to a PC via the USB. I followed some tutorials, used STM32CubeMx, other solutions found across the Internet, but anyways I failed. I can open the vcp on the PC side (using Hterm, TeraTerm and Realterm), but cannot get any data.

I use Eclipse and the buildin debugger, which I flashed to JLink.

The main loop:

int main(void)
{

  HAL_Init();    
  SystemClock_Config();    
  MX_GPIO_Init();    
  MX_USB_DEVICE_Init();    
  HAL_Delay(10000);

  uint8_t HiMsg[] = "0123456789987654321001234567899876543210\r\n";
  while (1)
  {    
       if( CDC_Transmit_FS(HiMsg, 20) == USBD_OK )
       {
           HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7); // blue LED
       }
       HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14); // red LED
       HAL_Delay(1000);
  }    
}

After executing this function, the blue LED lights only once and never changes its state (does not blink). It means that the CDC_Transmit_FS(...) returns USBD_OK only once, and next calls give USBD_Busy.

The MX_USB_DEVICE_Init() looks as follow:

void MX_USB_DEVICE_Init(void)
{
  USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
  USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
  USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
  USBD_Start(&hUsbDeviceFS);
  USBD_CDC_Init (&hUsbDeviceFS, &USBD_CDC); // I need to init it somewhere so I think here is a good place
}

The CDC_Transmit_FS looks like that:

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{

  uint8_t result = USBD_OK;
  /* USER CODE BEGIN 7 */
  CDC_Init_FS();
  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  if (hcdc->TxState != 0){
    return USBD_BUSY;
  }
  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  CDC_Init_FS();
  /* USER CODE END 7 */
  return result;
}

Does anyone know how to make it running? What do I miss?

Best,


Solution

  • SO... I found the solution! I can confirm that the code above works (just remove the CDC_Init_FS)!

    Acctully, it was a driver problem. for windows 10 you also need to install it despide what's written in the reference