🛠 USB OTG FS sur le port micro-AB : se faire passer pour un périphérique classe CDC
Objective
The STM32F4DISCOVERY has two independent USB connectors: the one for the ST-LINK/V2-A debugger (which programs the board) and a real USB OTG FS micro-AB port wired to the STM32F407's USB 2.0 OTG FS controller (integrated PHY). This tutorial configures this second port as a CDC device — a USB serial port that always works, unlike the ST-LINK's VCP, which is not wired on stock boards.
Exact Pinout
- DM/DP OTG FS: PA11 / PA12
- VBUS detection: PA9, ID pin: PA10 (the micro-AB connector distinguishes device and host via the ID pin)
- LEDs LD7/LD8: OTG VBUS and overcurrent detection — they inform you about the port's power supply
- The F407 also embeds an HS OTG controller (high-speed PHY + ULPI): not exposed on this board, only the FS is usable here
Hardware
- 1 STM32F4DISCOVERY board + micro-USB (or micro-AB) cable to the PC, plugged into the OTG connector
- STM32CubeF4: the USB Device library (CDC class) is provided in the package, with complete examples for the board
Procedure
- In the CubeF4 CDC example for STM32F4-Discovery, regenerate or write: 168 MHz clock, USB FS on PA11/PA12, pull-up on the DP line controlled by the USB stack.
- The USB Device stack (
STM32_USB_DEVICE_Librarymodule) providesUSBD_CDC: you implement reception in the class callback and send data withCDC_Transmit_FS. - Flash via the ST-LINK port (never via OTG on a blank board), then plug the cable into the OTG port: the PC enumerates a CDC serial device without drivers.
Application Skeleton
/* apres generation CDC sample CubeF4 — USB_DEVICE CDC */
USBD_HandleTypeDef husb;
USBD_Init(&husb, FS_PAD_VBUS, USB_OTG_FS_CORE_ID);
USBD_RegisterClass(&husb, USBD_CDC_CLASS);
USBD_CDC_RegisterInterface(&husb, &USBD_Interface_fops_FS);
USBD_Start(&husb);
/* Boucle principale : echo vers le terminal PC (port CDC, pas ST-Link) */
int main(void) {
SystemClock_Config; /* 48 MHz USB extraits de la PLL */
/* ... USBD init ci-dessus ... */
while (1) {
if (rxReady) { CDC_Transmit_FS(rxData, rxLen); rxReady = 0; }
}
}
Critical Points
- The USB clock requires exactly 48 MHz on the dedicated PLL output: with the board's 8 MHz HSE crystal, the standard 168 MHz configuration provides a correct 48 MHz — do not tweak the PLL without checking.
- The OTG port can also work as a host (host mass-storage demos exist in CubeF4): device/host detection comes from the ID pin PA10 and the type of micro-A/micro-B cable.
- MicroPython uses exactly this port (
MICROPY_HW_USB_FS) for its REPL: when MicroPython is flashed, the ST-LINK port is only used for re-flashing.
Expected Verification
The PC sees a CDC serial device (named according to the firmware's USB descriptors); echo works at 115200 and higher. LED LD7 lights up when VBUS is present on the OTG port.
Common Errors
- Plugging the cable into the ST-LINK port while looking for the new CDC device: the two ports are electrically and logically separate.
- Forgetting the 48 MHz configuration for the USB PLL: enumeration fails randomly or not at all.
- Using a standard micro-USB cable on the micro-AB connector without checking ID detection: on some cables/positions, role detection takes time — try the other orientation of the connector.
- Relying on the ST-LINK's VCP as a second serial port: not wired on stock boards, only this OTG port provides a real USB serial.
Other tutorials for this board
- Premier téléversement sur la STM32F4DISCOVERY avec l'Arduino STM32duino
- Zephyr RTOS sur la STM32F4DISCOVERY : west build et west flash
- MicroPython sur la STM32F4DISCOVERY : REPL interactif et pilotage des LED
- STM32Cube HAL et BSP officiel : LED, bouton et accéléromètre avec détection automatique
- Lecture audio sur le codec CS43L22 : I2S, classe D et les limitations du BSP
- Acquérir le micro PDM MP45DT02 : I2S2, filtre PDM-vers-PCM et contraintes de sortie