Take the Blue Pill the Story Ends

Ways to program the STM32F103 Blue Pill Module

Written by Guy Fernando

Last modified January 2020

Introduction

These Chinese manufactured STM32F103 boards are affectionately known as Blue Pills, the name being inspired from the Matrix movie line "You take the blue pill the story ends, you wake up in your bed and believe whatever you want to believe."

The STM32 Blue Pill module is incredibly cheap, and can be ordered for around £1.50 (2USD) including postage and packing to the UK. At the heart of the module sits an STM32F103C8T6, a 72MHz ARM Cortex-M3 CPU with 64kB of flash memory, and 10kB of RAM. Newer modules use the enhanced STM32F103CBT6 CPU which has 128kB of flash memory, 20kB of RAM and a few extra on-chip peripherals.

It is in theory possible to program the module just using a USB Micro B cable, assuming a bootloader program has been pre-loaded. But unfortunately Blue Pill modules leave the factory without any bootloader firmware being installed. Below describes how the maple bootloader specifically written for the Blue Pill module can be programmed using a suitable dongle. Once loaded, the onboard USB mini socket can be used to upload new firmware to the module with ease and convenience.

Blue Pill Module



When it comes to programming the Blue Pill module for the first time to flash a bootloader, there are two types of dongle that can be used.

  • USB to TTL dongle
  • ST-LINK dongle

The software package chosen here for programming is the STM32CubeProg, as it is free to download after giving your email address away. Once installed along with driver software it is very easy to use and works seamlessly with the other STM32Cube utilities. Download the maple bootloader, generic_boot20_pc13.bin which is designed to work with the Blue Pill module, including the module's red LED connected to the I/O port PC13.

Whenever the Blue Pill module is programmed using a dongle, the module must be set to PROGRAM mode, by setting the BOOT0 jumper to 1 (high).

Operate mode Program mode
Operate Mode Program Mode


USB to TTL Dongle to Program a Bootloader

Connect the USB to TTL dongle to the Blue Pill as shown below.

Blue Pill connected to USB-TTL dongle

Wiring between USB to TTL Dongle and Blue Pill
Cable USB to TTL Dongle STM32 Blue Pill
Green TXD (pin 2) A10
Yellow RXD (pin 3) A9
Black GND (pin 4) G
Red +5V (pin 5) 5V

The STM32F103 is a 3.3V powered device, however some pins are 5V tolerant, these include A9 and A10 and so it is safe to connect standard 5V TTL signals to these pins without damaging the microcontroller.

Note: The breadboard does not have to be used, as the connections can be made directly to the pins on the Blue Pill itself.

STM32CubeProgram connected to USB-TTL dongle

Plug the dongle into a USB port on your computer.

Launch the STMCubeProgrammer application. Select UART from the dropdown list, and click on the "Connect" button. Click the "Erasing & Programming" icon, click the "Browse" button and navigate to the location where generic_boot20_pc13.bin was saved earlier. To program the bootloader click on the "Start Programming" button.

When the programming is finished the log should contain the entry "File Download Complete".

ST-LINK Dongle to Program a Bootloader

Connect the ST-LINK dongle to the Blue Pill as shown below.

Blue Pill connected to USB-TTL dongle

Wiring between ST-LINK Dongle and Blue Pill
Cable ST-LINK Dongle STM32 Blue Pill
Orange SWDIO (pin 2) SWIO
Black GND (pin 4) GND
Brown SWCLK (pin 6) SWCLK
Red 3.3V (pin 8) 3V3





Note: The breadboard is not actually required, as the connections are be made directly to the pins on the Blue Pill itself.

STM32CubeProgram connected to ST-LINK dongle

Plug the dongle into a USB port on your computer.

Launch the STMCubeProgrammer application. Select ST-LINK from the dropdown list, and click on the "Connect" button. Click the "Erasing & Programming" icon, click the "Browse" button and navigate to the location where generic_boot20_pc13.bin was saved earlier. To program the bootloader click on the "Start Programming" button.

When the programming is finished the log should contain the entry "File Download Complete".

USB Forever More

Firmware for the Blue Pill module can now be written then compiled using toolchains such as the Arduino IDE or the STM32Cube IDE, and programmed into the Blue Pill module using just the USB port. The Arduino IDE is ideally suited for the novice and can be used when writing standard Arduino like sketch programs. The STM32Cube IDE is more suited to the power user, giving the developer the option of either writing bare metal code, or if some level of abstraction is required use of the STM32 HAL library.

Remember when the Blue Pill module is programmed using the USB port, the module must be set to OPERATE mode, by setting the BOOT0 jumper to 0 (low).