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:
- 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.
- 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.
- Flash the latest USB bootloader onto your Blue Pill STM32F103
- 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”.
- OPTION 1: Use J-Flash Lite program with your J-LINK ARM programmer to flash the bootloader file to the Blue Pill
- 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.
- 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.
- 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.
- 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.
- 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.
- Install DFU drivers to allow Windows to program the Blue Pill
- Go to rogerclarkmelbourne’s github repo for Arduino_STM32 here: https://github.com/rogerclarkmelbourne/Arduino_STM32
- 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.
- Make sure you have GIT and JAVA installed on your PC – trust me – PlatformIO needs both to work with the Blue Pill apparently
- Make sure you have the PlatformIO plugin installed and updated in either ATOM or Visual Studio Code
- Make a NEW project using PlatformIO and selecting board: bluepill & framework:Arduino
- Edit platformio.ino file with a couple custom settings
- 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.
- Add or Change “upload_port = anything”
- Add or Change “upload_protocol = dfu”
- See my screenshot of completed PlatformIO.ini
- You should now be able to upload a sketch/program from PlatformIO onto the Blue Pill Board with ONLY a USB cable!
- 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!
- 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.
Kiran
Great info, Coyt! Looks like it was a pain in the ass to figure out
Winston
Awesome work! Love those cheap ARM boards…
Daniel
Why people insist to use bootloaders and waste precious flash for the ability to use a proprietary tool to flash a new (and special!) firmware is completely beyond me. STM32F103 has *excellent* tooling support in every regard, but if you restrict yourself to the (rather crappy) STM32duino environment you might never see the beautiful world that is out there.
PlatformIO itself supports three more environments for the STM32F103: libOpenCM3, mbed and STM32Cube but there’re plenty more out there…
coyt
Mainly familiarity and ease of use (quickness).
Not sure what you mean by “ability to use a proprietary tool”… pretty sure the bootloader is open source and actually allows more freedom because you no longer need a real JTAG adapter…
I honestly really like the Arduino style HALs that many people have ported to many different MCUs these days (ESP8266 & ESP32 are other examples – they have plenty of support under their own toolchains as well as Arduino HAL spinoffs). Because there are so many, I can grab one of a ton of different options and immediately know how to program it. Because of this, I can pick a MCU based on the hardware I need instead of it’s software.
Furthermore, many people are not professional embedded software developers (myself included). I learn as I build projects, and I’d rather not have to learn and do low level register manipulation for every micro I want to use – it’s honestly just not fun for me. I may eventually check out the other environments you mentioned, but STM32duino has so far done everything I needed very easily (once I got it setup as in my tutorial above). Were there specific problems you had with it?
Rob
Articolo utilissimo
Molte grazie
Pingback: Programming STM32F103 Blue Pill using USB Bootloader and PlatformIO | 懒得折腾
Frans
Thank you!
Pingback: How to convert a BluePill into an Teensy | Fuzzy Hypothesis Online
Evangelos
You dont have to replace the R10 resistor to have usb working. Replacing a 0603 part is not a task that everyone can do.
Just solder a common 1.8K resistor on A12 and 3V3 pins, in the upper side of module, leaving the 10K resistor untouched. The resulting 1.8K//10K=1525 Ohms is working very well
Kai Liu
Your solution is great.
I heard a lot about USB issue with this pull up resistor. Does it really matter? Since the USB also works with 10KOhm in my bluepill boards, and I also think maybe 10KB is placed here for CAN bus purposes as well.
Luis Malavé
Me has ayudado muchisimo.. Gracias!!!
Leonel
Great!
Finally an article that got my uploading over USB working!
This helped for me:
– Use upload protocol dfu instead of serial.
– PlatformIO has a bluepill specific board definition (instead of just generic stm32f103…).
– platformio.ini
Tip:
The bootloader can be easily flashed using UART/USB-to-serial (pins A9/A10) with ‘STM32 Flash loader demonstrator’. It is a Win32 executable and does not require Python.
Get it here: http://www.st.com/content/st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-programmers/flasher-stm32.html
Leonel
Spec-wise R10 should be a 1.5k resistor, however in practice the 10k version that is installed on many bluepill’s will be sufficient in many(/most) cases.
Replace a 10k version R10 only when you can’t get the board to work with USB. This will save you from unnecessary (precision soldering) work.
I changed the resistor because I could not get the board to work with USB, but the issue turned out not to be caused by the value of R10.
I have all boards working now, the board where I replaced the 10k resistor with a 1.5k, but also the other boards that still have the 10k resistor they were shipped with.
Maurizio Butti
It worked! Apparently though the builtin LED is on PC13, not PB13, at least on my board
http://wiki.stm32duino.com/index.php?title=Blue_Pill
Robert
Yeah, the rest of the article has it OK, only at
“… make sure to change the LED pin from 13 or whatever to PB13 for the Blue Pill! ”
should be PC13
Rando
Thanks for this great post, very useful for me today!!!
Pingback: STM32F103 Mikrocontroller | Andreas' Blog
Abe
Appreciate it. A lot of data!
https://www.gpsies.com/mapUser.do?username=adimge83
Mark
Hi
I just integrated the Arduio Blue Pill in the uTasker project including USB-MSD loader:
http://www.utasker.com/stm32/BluePill.html
Allows development in all popular IDEs (IAR, Keil; Atollic) and simulation in Visual Studio, plus project compatibility with various STM32 board and parts, as well as most Kinetis board/parts.
Regards
Mark
saeed
Thank you !
1.8k Resistance resolve my problem.
Andy
Great Article, this and a decent USB cable sorted my probl ems with the Blue pills, they are so fussy, cables that i know work on other devices refused to work with the blue pills USB connector, even after replacing R10.
Isambard
I would recommend you buy a cheap stlink v/2 clone from Aliexpress and use SWD to flash and debug.
Talha KTE
Hello,
I have stm32f103c8t6 and st-link v2(USB programmer). I am trying to put the hex file inside the board. I am able to connect to the device and able to see the memory of it but I am not able to put the hex file inside the board. Can you help me?
Best regards