🛠 USB OTG FS sur le port micro-AB : se faire passer pour un périphérique classe CDC

STM32F4DISCOVERY (STM32F407VGT6 Discovery kit) · intermediaire · Schéma : les deux mondes USB de la STM32F4DISCOVERY — à gauche le port ST-LINK/V2-A (programmation), à droite le connecteur micro-AB OTG FS (PA11/PA12, VBUS PA9, ID PA10) reliant la carte à un PC qui voit un port série CDC ; les LED LD7/LD8 signalent VBUS et surintensité.

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

Hardware

Procedure

  1. 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.
  2. The USB Device stack (STM32_USB_DEVICE_Library module) provides USBD_CDC: you implement reception in the class callback and send data with CDC_Transmit_FS.
  3. 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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials