Skip to content

Commit fec542c

Browse files
committed
ports/stm32: Fix USB VBUS sensing for newer STM32F4/F7 HAL versions.
The USB_OTG_GCCFG register bit definitions changed between older and newer STM32F4/F7 HAL versions: - Older chips use: NOVBUSSENS, VBUSBSEN, VBUSASEN - Newer chips use: VBDEN This adds conditional compilation to support both register naming schemes, allowing TinyUSB builds to compile on all STM32F4/F7 variants. Signed-off-by: Andrew Leech <[email protected]>
1 parent a3d87f8 commit fec542c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ports/stm32/usbd_conf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
181181
// Configure VBUS sensing for TinyUSB on STM32F4/F7 which have separate GCCFG register
182182
// The DWC2 PHY init only sets PWRDWN bit, but doesn't configure VBUS sensing
183183
#if defined(STM32F4) || defined(STM32F7)
184+
#if defined(USB_OTG_GCCFG_VBDEN)
185+
// Newer STM32F4/F7 with VBDEN register bit
186+
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
187+
// Enable VBUS detection
188+
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN;
189+
#else
190+
// Disable VBUS detection (force VBUS valid)
191+
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
192+
#endif
193+
#else
194+
// Older STM32F4 with separate VBUSASEN/VBUSBSEN/NOVBUSSENS register bits
184195
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
185196
// Enable VBUS sensing in "B device" mode (using PA9)
186197
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
@@ -192,6 +203,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
192203
#endif
193204
#endif
194205
#endif
206+
#endif
195207

196208
#if MICROPY_HW_STM_USB_STACK
197209
return;

0 commit comments

Comments
 (0)