2020-04-17 07:16 AM
Cube MX 5.6.1 generated code:
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 */
}
to send data this is implemented
while (1)
{
/* USER CODE END WHILE */
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &buffer[0], sizeof(buffer));
USBD_CDC_TransmitPacket(&hUsbDeviceFS);
HAL_delay(1000);
/* USER CODE BEGIN 3 */
}
2020-04-17 04:46 PM
> Where is this class initialized?
pdev->pClassData is initialized when you call USBD_RegisterClass.
If it is 0 after successful return from USBD_RegisterClass, you probably have memory corruption.
-- pa
2020-04-18 07:02 AM
2020-04-18 08:22 PM
This field is only populated after the PC successfully enumerates the device, which is not instant. It also uses malloc, which is probably not what one would expect.
Typically putting HAL_Delay(500) prior to using the USB functions works, although it's not an elegant solution.
2020-04-18 08:39 PM
2020-04-19 05:14 AM
If you use freeRTOS, also take a look here (in case of memory corruption...) :
2020-04-19 06:31 AM
2020-04-19 06:32 AM
2020-04-19 06:33 AM