[Bug report]CubeMX generates wrong USB OTG FS macro names for STM32H743VITx
- July 28, 2026
- 3 replies
- 142 views
When enabling USB_OTG_FS peripheral in Device_Only mode for STM32H743VIT6, CubeMX generates usb_otg.c with incorrect HAL macros:
| Generated (wrong) | Should be (H7-native) |
| GPIO_AF10_OTG1_FS | GPIO_AF10_OTG2_FS |
| __HAL_RCC_USB_OTG_FS_CLK_ENABLE | __HAL_RCC_USB2_OTG_FS_CLK_ENABLE |
| __HAL_RCC_USB_OTG_FS_CLK_DISABLE | __HAL_RCC_USB2_OTG_FS_CLK_DISABLE |
Root cause:
On STM32H7, the FS USB peripheral is USB2_OTG_FS, not USB1_OTG_FS. The H7 HAL defines GPIO_AF10_OTG2_FS for the FS port and GPIO_AF10_OTG1_HS for the HS port. CubeMX, however, generates F4/F7-style macro names (e.g. GPIO_AF10_OTG1_FS) which are only valid for MCUs with a single USB OTG peripheral.
Moreover, GPIO_AF10_OTG1_FS is conditionally defined in stm32h7xx_hal_gpio_ex.h only when USB2_OTG_FS is not defined. Since STM32H743 does define USB2_OTG_FS, the macro is absent at compile time.
The legacy compatibility aliases in stm32_hal_legacy.h could resolve this, but CubeMX does not include that header in the generated code.
Note: This issue occurs regardless of whether ST's USB middleware stack is used or not. I am using a third-party USB library (CherryUSB) and only rely on CubeMX to generate correct HAL-level hardware initialization code (clock, GPIO, NVIC). The generated peripheral init macros should always be valid for the selected target MCU — the choice of USB protocol stack should not affect the correctness of the HAL-level code that CubeMX generates.
Workaround:
Manually add the following defines in the USER CODE section of usb_otg.c:
/* USER CODE BEGIN 0 */
#ifndef GPIO_AF10_OTG1_FS
#define GPIO_AF10_OTG1_FS GPIO_AF10_OTG2_FS
#endif
#ifndef __HAL_RCC_USB_OTG_FS_CLK_ENABLE
#define __HAL_RCC_USB_OTG_FS_CLK_ENABLE() __HAL_RCC_USB2_OTG_FS_CLK_ENABLE()
#define __HAL_RCC_USB_OTG_FS_CLK_DISABLE() __HAL_RCC_USB2_OTG_FS_CLK_DISABLE()
#endif
/* USER CODE END 0 */Expected fix:
CubeMX should generate the H7-native macro names when the target MCU has dual USB OTG peripherals (e.g. STM32H743).
Environment:
- MCU: STM32H743VIT6
- CubeMX: V6.18.0
- STM32H7 series MCU package version: V1.13.0
- Third-party USB stack: CherryUSB (ST's USB middleware not used)
