2025-09-04 12:06 AM
Hi,
I tried to bring up the USB host on the USB-A connector, but without success.
As a starting point, I took the example project:
STM32CubeN6\Projects\STM32N6570-DK\Applications\USBX\Ux_Host_DualClass (for the USB Type-C connector), replaced USB1 with USB2 throughout the code, and enabled the USB power switch via GPIO.
The device receives 5V, but no USB2 interrupt is triggered.
Below is my USB2 initialization. In line 18, I tried
RCC_USBPHY2REFCLKSOURCE_HSE_DIRECT, and
RCC_USBOTGHS2CLKSOURCE_HSE_DIRECT,
but neither case works.
It seems I’m missing something else. What could be wrong on my side?
void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
{
if (hhcd->Instance == USB2_OTG_HS)
{
/* USER CODE BEGIN USB_OTG_HS_MspInit 0 */
/* USER CODE END USB_OTG_HS_MspInit 0 */
/* Enable VDDUSB */
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWREx_EnableVddUSBVMEN();
while(__HAL_PWR_GET_FLAG(PWR_FLAG_USB33RDY));
HAL_PWREx_EnableVddUSB();
/** Initializes the peripherals clock
*/
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USBOTGHS2;
PeriphClkInitStruct.UsbOtgHs2ClockSelection = RCC_USBOTGHS2CLKSOURCE_HSE_DIRECT;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/** Set USB OTG HS PHY2 Reference Clock Source */
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USBPHY2;
PeriphClkInitStruct.UsbPhy2ClockSelection = RCC_USBPHY2REFCLKSOURCE_HSE_DIRECT;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
__HAL_RCC_GPIOA_CLK_ENABLE();
LL_AHB5_GRP1_ForceReset(RCC_AHB5RSTR_OTG2PHYCTLRST);
__HAL_RCC_USB2_OTG_HS_FORCE_RESET();
__HAL_RCC_USB2_OTG_HS_PHY_FORCE_RESET();
LL_RCC_HSE_SelectHSEDiv2AsDiv2Clock();
LL_AHB5_GRP1_ReleaseReset(RCC_AHB5RSTR_OTG2PHYCTLRST);
/* Peripheral clock enable */
__HAL_RCC_USB2_OTG_HS_CLK_ENABLE();
/* Required few clock cycles before accessing USB PHY Controller Registers */
HAL_Delay(1);
USB2_HS_PHYC->USBPHYC_CR &= ~(0x7 << 0x4);
USB2_HS_PHYC->USBPHYC_CR |= (0x1 << 16) |
(0x2 << 4) |
(0x1 << 2) |
0x1U;
__HAL_RCC_USB2_OTG_HS_PHY_RELEASE_RESET();
/* Required few clock cycles before Releasing Reset */
HAL_Delay(1);
__HAL_RCC_USB2_OTG_HS_RELEASE_RESET();
/* Peripheral PHY clock enable */
__HAL_RCC_USB2_OTG_HS_PHY_CLK_ENABLE();
/* USB_OTG_HS interrupt Init */
HAL_NVIC_SetPriority(USB2_OTG_HS_IRQn, 7, 0);
HAL_NVIC_EnableIRQ(USB2_OTG_HS_IRQn);
/* USER CODE BEGIN USB_OTG_HS_MspInit 1 */
/* USER CODE END USB_OTG_HS_MspInit 1 */
}
}