cancel
Showing results for 
Search instead for 
Did you mean: 

OTG_HS_IRQHandler isn't fired on STM32U5A9J-DK when USBC attached

nico23
Senior II

So, the situation has been pretty uncomfortable and frustrating.

I'm trying to implement the code to make the USB-C on the STM32U5A9J-DK work so that my PC recognizes the board as a Device.

I first started to try to implement it with my currently running code with ThreadX and TouchGFX, but it didn't work.

So, I started with a clean project, directly from CubeMX with just the USB OTG switched ON. No custom code, just the one generated by CubeMX.

Tried to attach the board to my PC, no device appeared on my PC, and, despite the breakpoint set on the only line of code inside OTG_HS_IRQHandler, when attaching the board to the USB, the interrupt doesn't fire.

I've checked step by step if the initialization and HAL_NVIC_SetPriority(OTG_HS_IRQn, 0, 0); HAL_NVIC_EnableIRQ(OTG_HS_IRQn); are executed, and everything seemed ok.

So, my question is, because there's no real documentation on how to implement the USB handling with the STM32 microcontroller, how am I supposed to make it work?

This is the github base project with just the USB OTG enabled https://github.com/NicoCaldo/USB_OTG_Test

5 REPLIES 5
FBL
ST Employee

Hi @nico23 

First, the GitHub link is not working!

Second, CubeMX generates only MX_USB_OTG_HS_PCD_Init()  which you need to call to initialize the USB device. Then, you should set RX and TX FIFO and initialize the controller driver. After that, by calling HAL_PCD_Start (), you will enable the global USB interrupt internally.

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.


nico23
Senior II

Hi @FBL ,

the project was private, it's now public and you can see it.

Where can I find the documentation on all the steps I have to do to correctly implement it? Also, I'm not using ThreadX here so, I'm not understanding the part about "RX and TX FIFO and initialize the controller driver"

Hi @nico23 

Let me clarify the situation regarding USB implementation on STM32U5.

You essentially have two options for USB device middleware:

  1. ST’s Classic USB Middleware (the traditional USB Device library)
  2. USBX Stack (part of Azure RTOS and CubeU5, natively supported on STM32U5 by CubeMX)

Currently, CubeMX for STM32U5 supports USBX middleware only and does not import the classic USB middleware libraries. That's why you don’t see the classic middleware generated in your CubeMX project.

  • Now, about USBX and USB Peripheral Initialization (Standalone, without RTOS)

When using USBX standalone (without ThreadX or any RTOS), you must manually initialize the USB peripheral and configure the USB controller driver. This includes setting the RX and TX FIFO sizes before starting the USB peripheral. This article can be helpful in this case.

Here is a typical initialization sequence you should follow in your code:

/* USER CODE BEGIN MX_USBX_Device_Init1 */
/* Initialize the USB Peripheral */
MX_USB_OTG_HS_PCD_Init();
/* Set the RX Fifo */
HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);
/* Set the TX Fifo for the Control EP 0 */
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x40);
/* Set the TX Fifo for the HID Mouse EP 1 */
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x100);
/* Link the USB drivers with the USBX DCD and check if it return error */
if(ux_dcd_stm32_initialize((ULONG)USB_OTG_HS, (ULONG)&hpcd_USB_OTG_HS) != UX_SUCCESS)
{
          HAL_GPIO_WritePin(LD3_RED_GPIO_Port, LD3_RED_Pin, GPIO_PIN_SET);
          Error_Handler();
}
/* Start the PCD Peripheral */
HAL_PCD_Start(&hpcd_USB_OTG_HS);
/* USER CODE END MX_USBX_Device_Init1 */

 So, calling this line is not enough to initialize drivers for USB application.

I hope this helps!

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.


nico23
Senior II

Hi @FBL 

thanks for the headsup, and I've finally figured out what was wrong.

First, I've installed USBX and updated the MX_USBX_Device_Init with

UINT MX_USBX_Device_Init(VOID)
{
   UINT ret = UX_SUCCESS;

   /* USER CODE BEGIN MX_USBX_Device_Init1 */
  /* Initialize USBX Memory */
  if (ux_system_initialize(ux_device_byte_pool_buffer, UX_DEVICE_APP_MEM_POOL_SIZE, UX_NULL, 0) != UX_SUCCESS)
  {
    Error_Handler();
  }
   /* Initialize the USB Peripheral */
   MX_USB_OTG_HS_PCD_Init();
   /* Set the RX Fifo */
   HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);
   /* Set the TX Fifo for the Control EP 0 */
   HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x40);
   /* Set the TX Fifo for the HID Mouse EP 1 */
   HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x100);
   /* Link the USB drivers with the USBX DCD and check if it return error */
   if(ux_dcd_stm32_initialize((ULONG)USB_OTG_HS, (ULONG)&hpcd_USB_OTG_HS) != UX_SUCCESS)
   {
             HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
             Error_Handler();
   }
   /* Start the PCD Peripheral */
   HAL_PCD_Start(&hpcd_USB_OTG_HS);
   /* USER CODE END MX_USBX_Device_Init1 */

  return ret;
}

The problem wasn't here tho.

For some reason, disabling the VBUS sensing makes the interrupt fire correctly. Having  hpcd_USB_OTG_HS.Init.vbus_sensing_enable = ENABLE; was the issue. Changing to  hpcd_USB_OTG_HS.Init.vbus_sensing_enable=DISABLE; fixed it.

Now, the main issue remains.

Why does enabling the VBUS sensing make the interrupt on the USB-C port not work?

(The code on GitHub is updated; it doesn't work after attaching a USB to the USB-C port because it fires a hard fault as no descriptor for the USB device is configured but my main goal was to check if the interrupt was working)

 

FBL
ST Employee

Hi @nico23 

Did you check the pin used for vbus sensing in your software.  According to schematics, it is connected to PG1

FBL_0-1751905384751.png

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.