# USB on STM32F107RCT and Stm32CubeMX

I have STM32f107 MCU soldered on custom board. I would like to use USB peripheral in CDC mode. I connected pins PA11 (D-), PA12 (D+) and GND directly to a USB cable which leads to the computer. The device is self-powered, thus I don't use VDD pin.

In STM32Cube I created a new project, configured only USB_OTG_FS device and USB_DEVICE in middleware section. Also I configured High-Speed Clock in RCC section so that USB peripheral has 48 MHz clock.

Then I generated the program skeleton and uploaded it to the MCU. Unfortunately it does not work. I think the main problem is that there is no voltage on D+ pin so that the computer is not able to recognize a new device. When I execute the CDC_Transmit_FS function, the processor falls into the Hard fault.

The strangest thing is that when I configure STM32F429 MCU on Discovery kit in the similar way, the USB works out of the box. I tried to diff the source code, but I haven't found any crucial difference (except the STMF4 MCU uses USB High speed OTG in full speed mode).

Please does anybody have some experience on using USB with STM32F107 and STM32CubeMX program skeleton?

EDIT: When I run the MCU in bootloader mode, the D+ line is pulled to 3V3 - therefore I think the MCU really does not need any external pull-up (also datasheet says this).

EDIT2: Does anybody know what Activate VBUS option in CubeMX exactly do? I can understand it in Host mode, but what does it do in Device-Only mode?

• With ST parts you typically need the pullup resistor on a USB data line that indicates the device speed to be external - the usually do not include it within the chip. – Chris Stratton Aug 11 '16 at 17:39
• @ChrisStratton Other ST parts may require this, but not this specific microcontroller. I have a working project with this IC and no data pullups. – bitsmack Aug 11 '16 at 18:04

Even though the device is self-powered, you still need to connect the USB's Vbus (pin 1) to the microcontroller's OTG_FS_VBUS (pin PA9).

This isn't to provide power, but to allow the microcontroller to know when to start up the USB subsystem.

• Are you sure? I tried the 3-wire connection on STMF429I Discovery kit (I bypassed the official microUSB connector and connected USB cable directly to appropriate MCU pins) and it worked without problems. I think the most important thing is that D+ data line is pulled to 3V3 and this is signal for the computer to start USB communication. – klasyc Aug 12 '16 at 7:29
• @klasyc I see by your answer that Vbus sensing is required in Device mode, but can be disabled in Host mode. Thanks for coming back and letting us know! – bitsmack Aug 12 '16 at 19:05

Thank you for both answers, you are right. To sum it up I will make another answer. I hope someone will find this useful.

• USB connection requires GND, D+ and D- lines. VBUS is used only for USB cable detection.
• VBUS sensing can be disabled at least on some MCUs. In this case, VUSB wire is not necessary.
• When you enable USB OTG peripheral in CubeMX, it does not light up the VBUS pin even it is necessary (unless you check the Activate VBUS checkbox).
• I cannot say what does Activate VBUS option do in Device mode. I tried to generate sources both with and without this option and both projects required VBUS line to make USB working.
• When you generate code from CubeMX (I am using HAL version 1.31) with USB CDC in Device mode, the generated code really works on both stm32f1 and stm32f4 cores out of the box. Even if you don't implement send/receive handlers, the MCU is still recognized as VCP.
• I wanted to disable VBUS sensing on STM32F107 by writting zero to VBUSBSEN bit in GCCFG register, but I didn't succeed or the value has been overwritten
by the usb library (the library is quite complicated, thus I don't know where exactly put the register assignment). Also it is interesting that reference manual on the page 926, section 28.17.3 says that in Device mode the VBUS sensing must be on.
• STM32F107 MCU does not require any pull-up resistor to make the USB working.

If I have stated anything incorrect, please correct me in the comments.

Klasyc

That pull-up R on D+ (or D-) is used to indicate the USB speed of the device. While host can detect the plugged device by this pull-up, in your case your have USB Device (Slave). And since it is self powered, as @bitsmack answered it shall detect connection by monitoring VBUS and start USB operation.

If you configured USB peripheral as dual role/OTG, you have to also connect USB_OTG_xx_ID pin to ID/OTG pin in your micro-USB connector.

For CDC, in CubeMX you can configure your device as Device_Only, connect VBUS (connector <-> MCU). If using micro USB connector then also wire connectors ID/OTG <-> GND.

• I still cannot figure out why STM32F429 works without VBUS wire and STM32F107 not. Do you think it is a hardware difference of both MCUs? – klasyc Aug 12 '16 at 8:33
• You didn't mentioned your USB connector type. If yours is USB micro then you have to ground ID/OTG pin. STMF4 board surely handles ID/OTG pin correctly by software. Regarding VBUS, in STMF4 it is already connected, somehow it 'thinks' that device is connected, even if you using other 3 pins. Maybe you can turn off VBUS detection in software. – Flanker Aug 12 '16 at 9:38
• I use USB A connector connected to the computer and on the other side I connect the white, green and black wire directly to the discovery kit's pins (the same circuit as for stm32f107). Therefore I think the VBUS pin is not necessary. – klasyc Aug 12 '16 at 9:42
• Since you didn't have VBUS connected, you have to disable VBUS sensing: electronics.stackexchange.com/questions/124063/… – Flanker Aug 12 '16 at 9:53
• I haven't succeeded in disabling VBUS sensing, but connecting VBUS pin really helped. Since I have to much observations, I wrote them in separate answer. Thank you for your assistance. – klasyc Aug 12 '16 at 11:54