USB Serial for Blue Pill (STM32) with PlatformIO

The Blue Pill (STM32) can be bought very cheaply on sites like eBay and Aliexpress, but they likely do not come shipped with a USB bootloader. You are then forced to use a USB to Serial/USB to TTL to interface with the BLue Pill, and that can be a nuisance sometimes, especially since you have to change the BOOT0 pin from 0 to 1 to upload, and then the reverse to run a sketch.

This post will show you how to upload a new bootloader to the Blue Pill and upload sketches over USB with PlatformIO in Windows.

Prerequisites

You will need a few things to get up and running:

To get the proper bootloader for your Blue Pill, read the pin number for the built-in LED of the board. The following example picture has its LED on pin PC13.

Then nagivate to Roger Clark's STM32duino-bootloader repo and download the appropriate .bin bootloader file that corresponds to your Blue Pill's LED pin. Since the example picture's LED pin is PC13, I would download generic_boot20_pc13.bin.

For the USB Maple serial driver, navigate to the drivers\win folder and run the install_drivers.bat batch file. The drivers will automatically install. Approve any Administrator requests.

Preparing the Blue Pill for flashing

Connect the USB to Serial/USB to TTL adapter to your PC and the Blue Pill. The reference pins are:

Adapter Pin Blue Pill Pin
3V3 3.3
GND GND
TXD A10
RXD A9

Move the jumper on BOOT0 from 0 to 1. Hit the RESET button.

Here's a reference pinout diagram.

Your PC should recognize the USB serial device. To double-check, you can open up Device Manager and looking for the device under "Ports".

Flash the bootloader

Open the Demonstrator GUI. Make sure the right COM port is selected.

Select "Next" The following screen should indicate that the Target is readable. If not, reset the Blue Pill again. Check your connections. Proceed to the next screen, where you'll see the the available pages for flashing. Proceed to the next screen.

Select the Download to Device radio button, and select the proper bootloader .bin file.

Then select "Next" to flash the bootloader.

Once done, close out of the program.

IMPORTANT: Before powering off the Blue Pill, return the BOOT0 jumper to 0.

Then you're finished! Disconnect the USB to Serial/USB to TTL adapter, and connect the Blue Pill with a Micro USB cable. You should see the serial device in Device Manager:

Uploading Sketches with PlatformIO

You can create a new PlatformIO project for the Blue Pill by selecting the proper board during project creation:

For a new project or an existing project, ensure you have the proper platformio.ini settings:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_port = COM8
upload_protocol = dfu

The most important entry is upload_protocol = dfu. PlatformIO will attempt to place the Blue Pill in DFU mode to upload the sketch, and then reset it.

If the upload process halts when "searching for DFU devices", you may need to manually reset the Blue Pill to upload the sketch.

Using PA11 and PA12

PA11 and PA12 are used for USB serial communication. They cannot be immediately used for regular pin interactions like IO, especially the PWM pin on PA11. To enable this functionality, we can disable USB serial by opening usb_serial.cpp found in .platformio\packages\framework-arduinoststm32\STM32F1\cores\maple

Then comment out everything inside void USBSerial::begin(void).

Resetting the Blue Pill via USB will no longer work and will require a manual reset.

Enjoy!

A Hackintosh Experience as a Long Time Windows User Dynamic Java Truststore for a JAX-WS Client
 

Add a comment

Comments

quasi quos alias quisquam sed velit voluptatem facere. repellat illo magnam tempora temporibus qui est iusto maxime quaerat natus quia facere sit quia nemo voluptatem reprehenderit.
Written on Sun, 23 Jun 2024 21:19:50 by sorgelakhanakp9z5 6u1ls6dilaef@gmail.com