Skip to main content
Senior III
June 9, 2025
Question

USBX ThreadX doesn't recognize device attached

  • June 9, 2025
  • 6 replies
  • 622 views

I'm trying to implement the USBX Audio class using the board STM32U5A9J-DK and the USBC port present on the board.

I've opened the default project from TouchGFX selecting the ThreadX one. I then opened the .ioc file in the CubeMX and activated the USB_OTG_HS in Device_Only Mode. I then went into the Middleware section and activated the USBX Core System, Device CoreStack HS and Device Controllers HS and then, under the Device Class HS I've turned ON the Audio Class.

I then copied the code in this example (usbx/Projects/STM32H743I-EVAL/Applications/USBX/Ux_Device_Audio).

The code correctly compiles and execute (the GUI still works and entering into the debug mode I'm seeing the USBX has been initialize).

The issue comes when I connect the board using the USBC to the PC. I'm seeing the LED in the board turning ON, basically confirming the USBC port is connected but, the callback that should execute the function USBD_AUDIO_Activate when the USB Audio class is attached doesn't fire. The PC seems to not recognize anything attached to it

What I'm doing wrong?

6 replies

Technical Moderator
June 11, 2025

Hi @nico23 

Does HAL_PCD_IRQHandler is even called? Did you enable USB HS global interrupt?

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.Best regards,FBL
nico23Author
Senior III
June 14, 2025

Hi @FBL ,

thanks for answering. Yes, I've it ON in the .ioc file

nico23_0-1749912963758.png

and I'm seeing that CubeMX has added the code inside stm32u5xx_it.c

/**
 * @brief This function handles USB OTG HS global interrupt.
 */
void OTG_HS_IRQHandler(void)
{
 /* USER CODE BEGIN OTG_HS_IRQn 0 */

 /* USER CODE END OTG_HS_IRQn 0 */
 HAL_PCD_IRQHandler(&hpcd_USB_OTG_HS);
 /* USER CODE BEGIN OTG_HS_IRQn 1 */

 /* USER CODE END OTG_HS_IRQn 1 */
}

and inside the HAL_PCD_MspInit I've the

 HAL_NVIC_SetPriority(OTG_HS_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(OTG_HS_IRQn);

I've checked that the code runs the EnableIRQ line with the debug as the codebase correctly compile and run but, everytime II attach an USB cable to the board and the PC, nothing happens

The OTG_HS_IRQHandler is not fired even if the LED in the USB-C port lights up (I think it says there's the 5V connected). [the cable is a data cable]

 

nico23Author
Senior III
June 18, 2025

Is there something I'm missing out?

nico23Author
Senior III
June 21, 2025

Looking into the code, I've found that the code generated by the CubeMX for the HAL_PCD_MspInit states

/**USB_OTG_HS GPIO Configuration
 PA12 ------> USB_OTG_HS_DP
 PA11 ------> USB_OTG_HS_DM
 PA9 ------> USB_OTG_HS_VBUS
 */
 GPIO_InitStruct.Pin = GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

But, for some reason, it initializes only PA9. If I search for PIN 11 and 12 I found that there's an inactive code in HAL_XSPI_MspInit where

#if 0 // Disable the HSPI initialization code generated by CubeMX
GPIO_InitStruct.Pin = GPIO_PIN_15|GPIO_PIN_14|GPIO_PIN_13|GPIO_PIN_12
 |GPIO_PIN_11|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8
 |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_0;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF8_HSPI1;
 HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

Strangely in HAL_PCD_MspDeInit they're correctly deactivated

void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd)
{
 if(hpcd->Instance==USB_OTG_HS)
 {
 /* USER CODE BEGIN USB_OTG_HS_MspDeInit 0 */

 /* USER CODE END USB_OTG_HS_MspDeInit 0 */
 /* Peripheral clock disable */
 __HAL_RCC_USB_OTG_HS_CLK_DISABLE();
 __HAL_RCC_USBPHYC_CLK_DISABLE();

 /**USB_OTG_HS GPIO Configuration
 PA12 ------> USB_OTG_HS_DP
 PA11 ------> USB_OTG_HS_DM
 PA9 ------> USB_OTG_HS_VBUS
 */
 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_9);

 /* USB_OTG_HS interrupt DeInit */
 HAL_NVIC_DisableIRQ(OTG_HS_IRQn);
 /* USER CODE BEGIN USB_OTG_HS_MspDeInit 1 */

 /* USER CODE END USB_OTG_HS_MspDeInit 1 */
 }

}

Looking at the code of the STM32H7 example

/** Enable USB Voltage detector
 */
 HAL_PWREx_EnableUSBVoltageDetector();

 __HAL_RCC_GPIOB_CLK_ENABLE();
 __HAL_RCC_GPIOI_CLK_ENABLE();
 __HAL_RCC_GPIOC_CLK_ENABLE();
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**USB_OTG_HS GPIO Configuration
 PB5 ------> USB_OTG_HS_ULPI_D7
 PI11 ------> USB_OTG_HS_ULPI_DIR
 PC0 ------> USB_OTG_HS_ULPI_STP
 PH4 ------> USB_OTG_HS_ULPI_NXT
 PB10 ------> USB_OTG_HS_ULPI_D3
 PB11 ------> USB_OTG_HS_ULPI_D4
 PA5 ------> USB_OTG_HS_ULPI_CK
 PB1 ------> USB_OTG_HS_ULPI_D2
 PB12 ------> USB_OTG_HS_ULPI_D5
 PA3 ------> USB_OTG_HS_ULPI_D0
 PB0 ------> USB_OTG_HS_ULPI_D1
 PB13 ------> USB_OTG_HS_ULPI_D6
 */
 GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_1
 |GPIO_PIN_12|GPIO_PIN_0|GPIO_PIN_13;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_11;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
 HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_0;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_4;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_3;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 /* USB_OTG_HS clock enable */
 __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
 __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();

 /* USB_OTG_HS interrupt Init */
 HAL_NVIC_SetPriority(OTG_HS_IRQn, 4, 0);
 HAL_NVIC_EnableIRQ(OTG_HS_IRQn);

Do I need to configure the two pins in the same way? Why doesn't CubeMX configure them properly?

nico23Author
Senior III
June 24, 2025

Just to have things straight, after having initialize the OTG_HS_IRQ interrupt, as soon as I attach the USB to the board, the function OTG_HS_IRQHandler() should be executed, am I right?

 

nico23Author
Senior III
June 28, 2025

So, I've created a brand new project using CubeMX (latest version) just to check if the interrupt for the USB HS worked. It compiles and flash but, once I attach the USB C from my board to the PC, nothing happens and the interrupt is not fired.

This code is completely generated using CubeMX and no edit has been made but, still, USB OTG interrupt doesn't fire.

It doesn't use ThreadX or any other fancy Middleware, just plain/standard CubeMX generated code

Why?

https://github.com/NicoCaldo/USB_OTG_Test

Technical Moderator
June 30, 2025

Hi @nico23 

This product is ObsoleteWe recommend using STM32U5G9J-DK1 as a replacement.

As quick test, run this example on your board. If not working, you must have a hardware issue. Check solder bridges and jumpers. Check if your USB host and cable can supply at least 500mA. You can find more details in User Manual.

Which board revision do you have? Is it MB1829-U5G9xx-B01? 

USB IOs should be kept in analog mode. No need to reconfigure them as alternate function as detailed in datasheet.

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.Best regards,FBL