2025-11-08 2:33 AM
I am developing a custom board using STM32H523RET6.
It has a USB type C port, and I am trying to send some data from PC to the board through it, and decided to use USB CDC ACM with standalone mode. The code is based on this and this.
ux_device_class_cdc_acm_write_run worked perfectly, but ux_device_class_cdc_acm_read_run doesn't work.
No matter what kind of texts I send, ux_device_class_cdc_acm_read_run returns "T" (84).
The code I am using is this;
uint8_t UserRxBuffer[APP_RX_DATA_SIZE];
VOID USBX_DEVICE_CDC_ACM_Read_Task(VOID)
{
ULONG actual_length = 0;
UX_SLAVE_DEVICE *device;
device = &_ux_system_slave->ux_system_slave_device;
/* Check if device is configured */
if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (cdc_acm != UX_NULL))
{
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE
/* Set transmission_status to UX_FALSE for the first time */
cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_FALSE;
#endif /* UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE */
switch (TX_DATA_FLAG)
{
case 0:
/* Read the received data */
if (ux_device_class_cdc_acm_read_run(cdc_acm,
(UCHAR *)UserRxBuffer, 64,
&actual_length) == UX_STATE_NEXT)
{
if (actual_length != 0)
{
/* update TX_DATA_FLAG */
TX_DATA_FLAG = 1;
}
}
break;
case 1:
printf("Received data: %s\n", UserRxBuffer);
TX_DATA_FLAG = 0;
break;
default:
break;
}
}
}
VOID USBX_Device_Process(VOID *arg)
{
ux_device_stack_tasks_run();
}main.cpp
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
USBX_Device_Process(NULL);
USBX_DEVICE_CDC_ACM_Read_Task();
}
UXDevice memory pool size and USBX Device System Stack size are both set to 15*1024, and minimal heap and stack size are set to 0x1000 and 0x2000, so I don't think the lack of memory is not a problem.
I have no idea why it doesn't work. Any help is appreciated.
Thanks
2025-11-08 2:39 AM
Using debugger, I checked if ux_device_class_cdc_acm_read_run is actually called or not, and indeed it was called every time a text is sent from PC.
2025-11-11 6:58 AM
Hi @HaYa6174
Would you attach your project to debug the issue?
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-11-18 5:31 PM
Hi @FBL
Sorry for the late reply.
Here is my code
I written the code below in the different cpp file, and included its header file in main.cpp (symlink of main.c)
VOID USBX_DEVICE_CDC_ACM_Read_Task(VOID)
{
ULONG actual_length = 0;
UX_SLAVE_DEVICE *device;
device = &_ux_system_slave->ux_system_slave_device;
/* Check if device is configured */
if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (cdc_acm != UX_NULL))
{
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE
/* Set transmission_status to UX_FALSE for the first time */
cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_FALSE;
#endif /* UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE */
switch (TX_DATA_FLAG)
{
case 0:
/* Read the received data */
if (ux_device_class_cdc_acm_read_run(cdc_acm,
(UCHAR *)UserRxBuffer, 64,
&actual_length) == UX_STATE_NEXT)
{
if (actual_length != 0)
{
/* update TX_DATA_FLAG */
TX_DATA_FLAG = 1;
}
}
break;
case 1:
/* Send the data via UART */
printf("Received data: %s\n", UserRxBuffer);
TX_DATA_FLAG = 0;
// if (strcmp((char *)UserRxBuffer, "dfu") == 0)
// {
// TX_DATA_FLAG = 0;
// /* Set the device in DFU mode */
// __disable_irq();
// _reboot_cookie = 0xDEADBEEF;
// HAL_NVIC_SystemReset();
// /* reset TX_DATA_FLAG */
// }
// else
// {
// TX_DATA_FLAG = 0;
// }
break;
default:
break;
}
}
}
extern "C" {
int _write(int file, const char* ptr, unsigned long len) {
(void)file;
/* Avoid trying to write if the device is not configured */
if (cdc_acm == nullptr) {
return 0;
}
/* Write the data to the USBX CDC-ACM stream */
UINT status =
ux_device_class_cdc_acm_write_run(cdc_acm, (UCHAR*)ptr, len, &len);
if (status == UX_SUCCESS) {
return static_cast<int>(len);
}
return 0;
}
}