2025-11-15 5:20 AM - last edited on 2025-11-15 9:12 AM by Imen.D
We are working on a bare-metal application using USBX on an STM32N657X0H3Q device.
In CubeMX, we configured USB1_OTG_HS under Connectivity and enabled USBX in Middleware.
However, when we execute the following function:
static void MX_USB1_OTG_HS_PCD_Init(void)
{
/* USER CODE BEGIN USB1_OTG_HS_Init 0 */
/* USER CODE END USB1_OTG_HS_Init 0 */
/* USER CODE BEGIN USB1_OTG_HS_Init 1 */
/* USER CODE END USB1_OTG_HS_Init 1 */
hpcd_USB_OTG_HS1.Instance = USB1_OTG_HS;
hpcd_USB_OTG_HS1.Init.dev_endpoints = 9;
hpcd_USB_OTG_HS1.Init.speed = PCD_SPEED_HIGH;
hpcd_USB_OTG_HS1.Init.phy_itface = USB_OTG_HS_EMBEDDED_PHY;
hpcd_USB_OTG_HS1.Init.Sof_enable = DISABLE;
hpcd_USB_OTG_HS1.Init.low_power_enable = DISABLE;
hpcd_USB_OTG_HS1.Init.lpm_enable = DISABLE;
hpcd_USB_OTG_HS1.Init.use_dedicated_ep1 = DISABLE;
hpcd_USB_OTG_HS1.Init.vbus_sensing_enable = DISABLE;
hpcd_USB_OTG_HS1.Init.dma_enable = DISABLE;
if (HAL_PCD_Init(&hpcd_USB_OTG_HS1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB1_OTG_HS_Init 2 */
/* Configure FIFOs for HS USB */
HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS1, 0x200);
/* Set Tx FIFO 0 */
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS1, 0, 0x10);
/* Set Tx FIFO 2 */
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS1, 1, 0x10);
/* Set Tx FIFO 3 */
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS1, 2, 0x20);
/* USER CODE BEGIN USBX_Device_Init 2 */
ux_dcd_stm32_initialize((ULONG)USB1_OTG_HS, (ULONG)&hpcd_USB_OTG_HS1);
/* Start USB Peripheral */
HAL_PCD_Start(&hpcd_USB_OTG_HS1);
/* USER CODE END USBX_Device_Init 2 */
/* USER CODE END USB1_OTG_HS_Init 2 */
}
And after calling HAL_PCD_Start():
HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
{
__HAL_LOCK(hpcd);
__HAL_PCD_ENABLE(hpcd);
(void)USB_DevConnect(hpcd->Instance);
__HAL_UNLOCK(hpcd);
return HAL_OK;
}Windows immediately shows the error:
“USB Device Not Recognized – The last USB device you connected to this computer malfunctioned.”
We are unable to detect the USB device on the host PC and we would like assistance in identifying what is missing or incorrectly configured in our USB1_OTG_HS and USBX setup.
and the in the main() function is
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
HAL_Init();
/* USER CODE BEGIN Init */
SystemClock_Config();
/* USER CODE END Init */
/* USER CODE BEGIN SysInit */
MX_USBX_Init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB1_OTG_HS_PCD_Init();
SystemIsolation_Config();
/* USER CODE BEGIN 2 */
/* Now USB CDC is ready – transmit test message */
ULONG sent;
uint8_t msg[] = "USB CDC Ready!\r\n";
USBD_CDC_ACM_Transmit(msg, sizeof(msg) - 1, &sent);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ux_device_stack_tasks_run();
ULONG received = 0;
if (USBD_CDC_ACM_Receive(buffer, sizeof(buffer), &received) == UX_SUCCESS &&
received > 0)
{
USBD_CDC_ACM_Transmit(buffer, received, &sent);
}
}
/* USER CODE END 3 */
}
Edited by ST Moderator to apply source code formatting. Please read this post: How to insert source code.
2025-11-15 8:45 AM
Hello @Grace_04
Try plugging the device into a different USB port and use other USB cables as the problem might be caused by a damaged cable.
Try increasing stack and heap size to twice the default values.
Check also the power supply.
This issue might be related to a hardware problem. Therefore, I recommend testing the working application to determine whether the issue originates from your hardware or your code:
Please refer to the following article for guidance on USB development with STM32, which includes information on USB theory and hardware implementation: Guide to USB development resources on STM32.
2025-11-21 8:02 AM - edited 2025-11-21 8:04 AM
I had the same problem a while back and figured out that it was an MX cube code generation error. The problem: MX Cube doesn't generate the code that actually sets the USB clock at the correct rate.
We were using a 24MHz clock, and though we set that up in the MX Cube screen, it didn't generate the code. We had to add the lines
// Set 24MHz PHY ref clock
USB1_HS_PHYC->USBPHYC_CR &= ~(0x7 << 0x4);
USB1_HS_PHYC->USBPHYC_CR |= (0x2 << 4);At the end of HAL_PCD_MspInit, like so:
void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
{
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(pcdHandle->Instance==USB1_OTG_HS)
{
/* USER CODE BEGIN USB1_OTG_HS_MspInit 0 */
/* USER CODE END USB1_OTG_HS_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USBOTGHS1;
PeriphClkInitStruct.UsbOtgHs1ClockSelection = RCC_USBPHY1REFCLKSOURCE_HSE_DIRECT_DIV2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Enable VDDUSB */
HAL_PWREx_EnableVddUSB();
/* USB1_OTG_HS clock enable */
__HAL_RCC_USB1_OTG_HS_CLK_ENABLE();
__HAL_RCC_USB1_OTG_HS_PHY_CLK_ENABLE();
/* USB1_OTG_HS interrupt Init */
HAL_NVIC_SetPriority(USB1_OTG_HS_IRQn, 7, 0);
HAL_NVIC_EnableIRQ(USB1_OTG_HS_IRQn);
/* USER CODE BEGIN USB1_OTG_HS_MspInit 1 */
// Set 24MHz PHY ref clock
USB1_HS_PHYC->USBPHYC_CR &= ~(0x7 << 0x4);
USB1_HS_PHYC->USBPHYC_CR |= (0x2 << 4);
/* USER CODE END USB1_OTG_HS_MspInit 1 */
}
}
Your registers may differ depending on clock rate, but I hope this helps. It took us weeks to figure out. Also, make sure your dividers are set right. We have DIV2 in the periph clk structure, but I vaguely remember having to mess with that a bit. Either way, the main culprit for us was the lack of Reference Clock setup