How to use Platformio to develop for generic STM32F103C8 board (a.k.a. BluePill) with Arduino framework

A great wiki article about BluePill hardware that I heavily used while making this post:
stm32duino wiki

This post describes a step in moving on from Arduino ecosystem for Arduino-addicted fellows out there. After completing this guide, you'll almost break your crippling Arduino dependency by replacing Arduino IDE with PlatformIO plugin for Github Atom IDE, and Arduino boards with generic mass-produced board with STM32 microcontroller.

I couldn't find a decent one-piece manual online on the topic, so here goes:

You'll need

  • The Blue pill (can be procured on eBay, Aliexpress etc.).

    And at a reasonable price, as well!
  • STLink programmer clone (same place, almost the same price range).

  • A computer that can run Atom IDE , hopefully without too much lagging.

Step-by-step manual

Okay, now that you've got everything on the list, we can start uploading some sample code to the blue pill.

First off, follow the MANUAL (steps I. Atom and, optionally, II. Clang for Intelligent Code Completion) to install Github's Atom IDE and Platformio, which is a plugin for Atom. Code completion will just assist you while you type your code and is not essential.

Then do the Setting Up the Project paragraph. As soon as you've copy-pasted the example code, you should define LED_BUILTIN as PC13 - that's where the blue pill has its blinky thing connected.
The complete mapping of physical pins to Arduino pins can be seen on this handy pic:

All that's left now is to configure the project to use Arduino framework and STLink to upload the resulting code. For that, your platformio.ini should look like this:

Your Blink project should be able to successfully build now:

Build and upload buttons are located on the vertical pane on the left corner.

Try to upload now! Connect STLink to BluePill's programming pin header as follows:

STLink pin BluePill pin
GND (3 or 4) GND
3.3V (7 or 8) 3.3

Upload should be successful now and LED should blink. In some cases, you'll see this instead:

To fix that, short the BOOT0 pin to 3.3V with a jumper (consult with pinout diagram again) and reset the microcontroller. You'll be able to upload again after that.

The mode in which STM32 starts is determined by pins BOOT0 and BOOT1 :

BOOT1BOOT0Boot mode
X 0User Flash memory
0 1 System memory (bootloader)
1 1Embedded SRAM

You'll have to reset the board again in bootloader mode to be able to upload again. Therefore, my workflow with bluepill is like this:
  1. Set Bluepill to bootloader mode with jumpers.
  2. Upload my code, check if it works
  3. If it doesn't, reset Bluepill, fix the code and go to step 2.
  4. If everything is fine, Set Bluepill to run my code at startup with jumpers.

Libraries, serial monitor, and all the goodies we love in Arduino IDE

Blinking the LED isn't all we do with microcontrollers, and eventually you have to program more advanced peripherals that GPIO. That's where Arduino steps in with its huge ecosystem of libraries to every taste.

What do we have in Platformio? Turns out, almost as much libraries as in Arduino.

GUI interface in PlatformIO is available too!

Now, as for serial, I decided to find which is which experimentally. First, I uploaded the following code to my Blue Pill: Then I got my USB to Serial converter and connected it to Blue Pill's Serial lines like so:

USB to Serial pin BluePill pin

Now let's see if we can receive something in Putty:

Serial object works with pins A8 and A9

The result of investigation is that there are two ways to use serial communication on Blue Pill:

Arduino object Physical interface on BluePill
usb microUSB connector
Serial pins A8 and A9