stm32-rs / stm32f1xx-hal

A Rust embedded-hal HAL impl for the STM32F1 family based on japarics stm32f103xx-hal
Apache License 2.0
564 stars 177 forks source link

Enable USB Serial on Maple Mini #451

Closed olekhov closed 1 year ago

olekhov commented 1 year ago

Hello. I am trying to enable USB serial CDCACM device on Leaflabs Maple Mini board. It is very similar to Blue Pill board with few differences.

Board LED is connected to PB1.

There is no pull-up resistor on D+ pin, there is more complicated circuit: image

DISC pin is connected to PB9. Original examples configure PB9 to push-pull output and drive it low to enable USB CDCACM

Also, there is bootloader on the chip. It starts on reset and configures USB to enable DFU firmware update. I want to keep bootloader.

When I plug board to USB, first it is booted into bootloader, device is recognized as 1eaf:0003, the LED blinks several times and bootloader awaits for DFU. If no dfu is presented, board continues boot to firmware.

When firmware configures USB, it is not accepted by the host. syslog shows these messages:

дек 07 11:59:16 basilioe480 kernel: usb 1-3: new full-speed USB device number 2 using xhci_hcd
дек 07 11:59:16 basilioe480 kernel: usb 1-3: New USB device found, idVendor=1eaf, idProduct=0003, bcdDevice= 2.01
дек 07 11:59:16 basilioe480 kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
дек 07 11:59:16 basilioe480 kernel: usb 1-3: Product: Maple 003
дек 07 11:59:16 basilioe480 kernel: usb 1-3: Manufacturer: LeafLabs
дек 07 11:59:16 basilioe480 kernel: usb 1-3: SerialNumber: LLM 003
дек 07 11:59:16 basilioe480 mtp-probe[108342]: checking bus 1, device 2: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3"
дек 07 11:59:16 basilioe480 mtp-probe[108342]: bus: 1, device: 2 was not an MTP device
дек 07 11:59:16 basilioe480 mtp-probe[108344]: checking bus 1, device 2: "/sys/devices/pci0000:00/0000:00:14.0/usb1/1-3"
дек 07 11:59:16 basilioe480 mtp-probe[108344]: bus: 1, device: 2 was not an MTP device
дек 07 11:59:16 basilioe480 fwupd[106842]: 08:59:16:0295 FuPluginDfu          DFU version 0x0000 invalid, v1.1 assumed
дек 07 11:59:18 basilioe480 kernel: usb 1-3: USB disconnect, device number 2
дек 07 11:59:18 basilioe480 kernel: usb 1-3: new full-speed USB device number 7 using xhci_hcd
дек 07 11:59:19 basilioe480 kernel: usb 1-3: device descriptor read/64, error -71
дек 07 11:59:19 basilioe480 kernel: usb 1-3: device descriptor read/64, error -71
дек 07 11:59:19 basilioe480 kernel: usb 1-3: new full-speed USB device number 8 using xhci_hcd
дек 07 11:59:19 basilioe480 kernel: usb 1-3: device descriptor read/64, error -71
дек 07 11:59:19 basilioe480 kernel: usb 1-3: device descriptor read/64, error -71
дек 07 11:59:20 basilioe480 kernel: usb usb1-port3: attempt power cycle
дек 07 11:59:20 basilioe480 kernel: usb 1-3: new full-speed USB device number 9 using xhci_hcd
дек 07 11:59:20 basilioe480 kernel: usb 1-3: Device not responding to setup address.
дек 07 11:59:20 basilioe480 kernel: usb 1-3: Device not responding to setup address.
дек 07 11:59:21 basilioe480 kernel: usb 1-3: device not accepting address 9, error -71
дек 07 11:59:21 basilioe480 kernel: usb 1-3: new full-speed USB device number 10 using xhci_hcd
дек 07 11:59:21 basilioe480 kernel: usb 1-3: Device not responding to setup address.
дек 07 11:59:21 basilioe480 kernel: usb 1-3: Device not responding to setup address.
дек 07 11:59:21 basilioe480 kernel: usb 1-3: device not accepting address 10, error -71
дек 07 11:59:21 basilioe480 kernel: usb usb1-port3: unable to enumerate USB device

my current efforts are here

olekhov commented 1 year ago

Ok, I've found the fix.

For working USB one needs to configure proper clocks. The example configures clock at 48MHz.

Default value for USB Prescaler is 1.5. This results in invalid clock configuration. For 48MHz the prescaler should have value of 1. For 1.5 the running speed should be 72MHz.