STM32U5A5 USB CDC issue
My project use STM32U595 chip. It will use HS USB to send large amount data to computer.
I use NUCLEO-U5A5ZJ-Q for testing.
The project use Ux_Device_CDC_ACM in NUCLEO-U5A5ZJ-Q of STM32Cube_FW_U5_V1.4.0.
I reference this post https://community.st.com/t5/stm32-mcus-products/nucleo-u5a5zj-q-usb-cdc-acm-issue-with-ux-device-class-cdc-acm/td-p/632086
I modify a little bit of Kannan1's code as:
#define MAX_PKT_SIZE 2048
//512 /* 200 */ /* 512 */ /* 1024 */
#define TOTALBYTE APP_TX_DATA_SIZE*1000
#define USEHAL
VOID usbx_cdc_acm_write_thread_entry(ULONG thread_input)
{
ULONG actual_length;
uint32_t i = 0;
int32_t total_bytes_to_send = TOTALBYTE;
uint32_t bytes_to_send = 0;
uint32_t buf_indx = 0;
UX_PARAMETER_NOT_USED(thread_input);
for (i = 0; i < APP_TX_DATA_SIZE; i++)
{
UserTxBufferFS[i] = i;
}
IOinit();
// tx_thread_sleep(MS_TO_TICK(10000));
while (1)
{
if(1==buttonState()){
total_bytes_to_send = TOTALBYTE;
while(1==buttonState());
LEDrOn();
while(total_bytes_to_send>0)
{
LEDbToggle();
if (total_bytes_to_send > MAX_PKT_SIZE)
{
bytes_to_send = MAX_PKT_SIZE;
}
else
{
bytes_to_send = total_bytes_to_send;
}
/* Send data over the class cdc_acm_write */
if (ux_device_class_cdc_acm_write(cdc_acm, (UCHAR *)(&UserTxBufferFS[buf_indx]),
bytes_to_send, &actual_length) == UX_SUCCESS)
{
total_bytes_to_send -= actual_length;
buf_indx += actual_length;
if (buf_indx >= APP_TX_DATA_SIZE)
{
buf_indx = 0;
}
}else{
tx_thread_sleep(MS_TO_TICK(10));
}
}
LEDrOff();
}else{
/* Sleep thread for 10ms */
tx_thread_sleep(MS_TO_TICK(10));
}
}
}
the LED and button code:
#define GPIO_Button_Pin GPIO_PIN_13
#define GPIO_Button_GPIO_Port GPIOC
#define GPIO_LEDr_Pin GPIO_PIN_2
#define GPIO_LEDr_GPIO_Port GPIOG
#define GPIO_LEDb_Pin GPIO_PIN_7
#define GPIO_LEDb_GPIO_Port GPIOC
void IOinit()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIO_LEDr_GPIO_Port, GPIO_LEDr_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIO_LEDb_GPIO_Port, GPIO_LEDb_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : GPIO_Button_Pin */
GPIO_InitStruct.Pin = GPIO_Button_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIO_Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : GPIO_LEDr_Pin */
GPIO_InitStruct.Pin = GPIO_LEDr_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIO_LEDr_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : GPIO_LEDb_Pin */
GPIO_InitStruct.Pin = GPIO_LEDb_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIO_LEDb_GPIO_Port, &GPIO_InitStruct);
}
void LEDrOn()
{
HAL_GPIO_WritePin(GPIO_LEDr_GPIO_Port, GPIO_LEDr_Pin, GPIO_PIN_SET);
}
void LEDrOff()
{
HAL_GPIO_WritePin(GPIO_LEDr_GPIO_Port, GPIO_LEDr_Pin, GPIO_PIN_RESET);
}
void LEDbToggle()
{
HAL_GPIO_TogglePin(GPIO_LEDb_GPIO_Port, GPIO_LEDb_Pin);
}
//push down 1;release 0
uint32_t buttonState()
{
uint32_t ret;
ret = (HAL_GPIO_ReadPin(GPIO_Button_GPIO_Port,GPIO_Button_Pin) == GPIO_PIN_RESET) ? 0:1;
return ret;
}
Also in function VOID USBX_APP_Device_Init(VOID):
// HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x100);
// HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x10);
// HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x20);
// HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 2, 0x10);
HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, USBD_MAX_EP0_SIZE/4);
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, USBD_CDCACM_EPIN_HS_MPS/4);
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 2, USBD_CDCACM_EPINCMD_HS_MPS/4);
So I only modify the function VOID usbx_cdc_acm_write_thread_entry(ULONG thread_input) and VOID USBX_APP_Device_Init(VOID). didn't touch other in original project.
when I set:
#define TOTALBYTE APP_TX_DATA_SIZE*100
it can successfully send the amount data to computer couple of times when I pushed the button. Then the code is locked.
when #define TOTALBYTE APP_TX_DATA_SIZE*1000
it locked at first time.
When locked, the debug show as:

Does anyone know what is the problem? Thanks.
Mark

