Programming STM32F103 Blue Pill using USB Bootloader and PlatformIO

This is the infamous Blue Pill board – a $2 ARM STM32F103 development board with all the capabilities of a Teensy 3.x at a fraction of the price of an Arduino. So what’s the catch?

I’ll tell you – software support.

A couple weeks ago I decided to invest some time learning this platform because I was sick of paying 20+ dollars for a Teensy. While the PJRC platforms are fantastic, they are expensive and need a proprietary boot loader in order to work. I want a small and powerful arm chip which I can integrate INTO my own PCBs and the Teensy does not easily or cheaply allow this. The Blue Pill and it’s derivatives appear to be just the thing I need!

But how do you use one of these things? It should be simple! Just buy one, connect it to your computer over USB, and upload code from the Arduino IDE or PlatformIO just like you would with an Arduino!… WRONG! It appears the Blue Pills are not quite there yet. The main problem is lack of a USB boot loader installed at the factory as well as poor documentation and poor support in PlatformIO.

As a side note, if you are not using the PlatformIO framework with either ATOM or Visual Studio Code as your prototype embedded development environment then you need to get with the program! Just download VS Code and install the PlatformIO plugin – it’s fantastic! Way better than Arduino IDE!

Now, I’ll tell you exactly how to get your blue pill working like an Arduino:

  1. Get a Blue Pill STM32F103C8T6 board AND EITHER a 3.3V USB/UART adapter, a knockoff ST-LINK programmer, or a REAL ARM programmer such as a J-Link. You only need one of those three things. I’ve verified this tutorial works fine with all 3.
  2. Change resistor R10 from 10k Ohms to 1.5k Ohms on the Blue pill board. This is an 0603 size surface mount resistor on the bottom side of the Blue Pill. Read up elsewhere on this problem and make sure you need to do it for your specific board. I know it’s a PITA, but just do it – it’s not that hard and will make USB work properly.
  3. Flash the latest USB bootloader onto your Blue Pill STM32F103
    1. You need to get the latest STM32duino bootloader from rogerclarkmelbourne’s GITHUB here: https://github.com/rogerclarkmelbourne/STM32duino-bootloader Go into the “binaries” folder and download the bootloader.bin file that matches the LED pin on your actual Blue Pill board! My Blue Pill boards have the LED on pin PC13 so I selected the file “generic_boot20_PC13.bin”.
    2. OPTION 1: Use J-Flash Lite program with your J-LINK ARM programmer to flash the bootloader file to the Blue Pill
      1. Wire up the Programmer as I did in my picture below, or look up how to connect your specific ARM programmer to an ARM processor over SWD pins.
      2. In J-Flash lite, select the “STM32F103CB” processor from the dropdown, select the bootloader.bin file from earlier, and hit write. J-Flash Lite should say success. Plug in your Blue Pill to PC via USB and it should register as a com port or something and the led should blink a few times.
    3. OPTION 2: The Blue Pill does come from factory with a UART bootloader pre-programmed to operate on a couple of the pins. This allows you to upload the USB bootloader from earlier with just a USB to UART adapter. This is more annoying because you must use a weird Python Program. See this dude’s tutorial for info. 
    4. OPTION 3: Use the knockoff STLINK V2 programmer you purchased to burn the bootloader. Honestly I lied and haven’t done this method yet, but you should just need to download some STLINK flasher app and upload our USB bootloader to the Blue Pill just as we did with J-Flash in option 1.
    5.  You’re USB bootloader should now be on the Blue Pill STM32F103 – make sure both jumpers on Blue Pill are set to default of 0 and the LED should blink fast after pressing reset and then continue blinking slowly to indicate no user program is found yet.
  4. Install DFU drivers to allow Windows to program the Blue Pill
    1. Go to rogerclarkmelbourne’s github repo for Arduino_STM32 here: https://github.com/rogerclarkmelbourne/Arduino_STM32
    2. Download the repo, extract all the stuff, and go into “/drivers/win”, and run the “install_drivers.bat” as administrator. This seems super sketchy but it actually worked for me every time.
  5. Make sure you have GIT and JAVA installed on your PC – trust me – PlatformIO needs both to work with the Blue Pill apparently
  6. Make sure you have the PlatformIO plugin installed and updated in either ATOM or Visual Studio Code
  7. Make a NEW project using PlatformIO and selecting board: bluepill & framework:Arduino
  8. Edit platformio.ino file with a couple custom settings
    1. Change the text after platform to be “platform = https://github.com/platformio/platform-ststm32.git” This is why we needed GIT – it will allow PlatformIO to pull the most recent framework and board info directly from GITHUB.
    2. Add or Change “upload_port = anything”
    3. Add or Change “upload_protocol = dfu”
    4. See my screenshot of completed PlatformIO.ini
  9. You should now be able to upload a sketch/program from PlatformIO onto the Blue Pill Board with ONLY a USB cable!
    1. If you’re testing with Arduino “Blink” style program, make sure to change the LED pin from 13 or whatever to PB13 for the Blue Pill!
    2. You’ll likely need to press the reset button on the Blue Pill to get the program to upload

I really hope this helps some people out. This took more hours than I’d care to admit to figure out. Please let me know if anything needs to be changed or updated. I’ll try to keep it current – everything here worked as of 12/16/17.