2025-02-24 04:02 PM - edited 2025-02-24 04:04 PM
I am using a STM32F103 and having issues to make an USB application.
I generated the project using CubeMX and I am not being able to transmitt a message.
Here is the configuration:
Main is like this (It is C++ in case it matters):
int main(void) {
HAL_Init();
SystemClock_Config();
MX_USB_DEVICE_Init();
while (true) {
HAL_Delay(100);
volatile uint8_t result = CDC_Transmit_FS((uint8_t*) secondLine, strlen(secondLine));
if (result != USBD_OK) {
}
}
}
After inspecting in debug mode the CDC_Transmit_FS function I saw it was returning "USBD_BUSY":
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 7 */
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);
/* USER CODE END 7 */
return result;
}
I saw that hcdc structure is NULL and that is why function is returning USBD_BUSY.
Also when connected to Windows10 computer, no device is detected.
Just in case the (autogenerated) MX_USB_DEVICE_Init function looks like this:
/**
* Init USB device Library, add supported class and start the library
* @retval None
*/
void MX_USB_DEVICE_Init(void)
{
/* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
/* USER CODE END USB_DEVICE_Init_PreTreatment */
/* Init Device Library, add supported class and start the library. */
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
{
Error_Handler();
}
if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
/* USER CODE END USB_DEVICE_Init_PostTreatment */
}