How do you implement USB communication on STM32?
USB Communication Implementation on STM32 (Step-by-Step Guide)
1. Hardware Setup
Required Components:
USB connector (Type-C/Micro-B) + pull-up resistors (1.5kΩ on DP for Full Speed)
VBUS detection circuit (optional but recommended)
Pin Connections:
| STM32 Pin | USB Signal | Description |
|---|---|---|
| PA11 | DM (D-) | Data Minus |
| PA12 | DP (D+) | Data Plus |
| VBUS | 5V Input | Power Sensing (if used) |
2. Software Configuration (Using STM32CubeIDE)
Step 1: Enable USB in CubeMX
Open STM32CubeMX → Select your MCU
Enable USB (OTG_FS or OTG_HS) under "Connectivity"
Set mode:
Device Mode (for USB CDC/HID/Mass Storage)
Host Mode (for connecting to peripherals)
Configure Clock Settings:
Ensure 48MHz clock to USB peripheral (PLL must generate this)
Step 2: Select Middleware (USB Classes)
CDC (Virtual COM Port): For serial communication
HID: For keyboards/mice
MSC (Mass Storage): For USB drives
Custom Class: For vendor-specific protocols
3. Example: USB CDC (Virtual COM Port)
Code Implementation:
// 1. Initialize USB (generated by CubeMX) MX_USB_DEVICE_Init(); // 2. Send Data via USB CDC uint8_t data[] = "Hello USB!\r\n"; CDC_Transmit_FS(data, sizeof(data)); // 3. Receive Data (callback) void HAL_CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) { // Process received data (e.g., echo back) CDC_Transmit_FS(Buf, *Len); }
Descriptor Configuration (in usbd_cdc.c):
// Modify USB VID/PID (optional) #define USBD_VID 0x0483 // STM's VID #define USBD_PID 0x5740 // Custom PID // Adjust endpoint sizes (default is 64 bytes) #define CDC_DATA_FS_MAX_PACKET_SIZE 64
4. USB HID Example (Mouse Emulator)
// 1. Define HID report descriptor (in usbd_hid.c) __ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[] __ALIGN_END = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x02, // USAGE (Mouse) // ... (see USB HID specs) }; // 2. Send mouse movement uint8_t report[4] = {0, dx, dy, 0}; // Buttons, X, Y, Wheel USBD_HID_SendReport(&hUsbDeviceFS, report, sizeof(report));
5. Debugging Tips
Common Issues & Fixes:
| Symptom | Likely Cause | Solution |
|---|---|---|
| USB not detected | Missing pull-up on DP | Add 1.5kΩ resistor to 3.3V |
| Enumeration fails | Incorrect clock/PLL config | Verify 48MHz USB clock |
| Data corruption | Buffer overflow | Check endpoint sizes |
Debug Tools:
USBlyzer/Wireshark: Monitor USB traffic
ST-Link Debugger: Breakpoint analysis
USB Descriptor Validator: Check descriptor compliance
6. Advanced Topics
Dual-Role (OTG) Setup:
// Enable OTG mode in CubeMX USB_OTG_FS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD; // Force Device Mode
USB DFU (Firmware Update):
Set BOOT0=1 to enter DFU mode
Use
dfu-utilto flash.dfufiles:dfu-util -d 0483:df11 -a 0 -s 0x08000000 -D firmware.dfu
7. Example Projects
USB-to-UART Bridge (CDC):
Forward data between USB and UART (e.g., STM32 as a COM port)
Custom HID Device:
Send sensor data (e.g., accelerometer) as HID reports
USB Audio Interface:
Use USB Audio Class (UAC) for microphone/speaker

评论
发表评论