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

Courtecy of Hackaday.com

In case you'd like to learn how to program this board in C with debugging capability, thus completely purging Arduino ecosystem from your life, check my other blog post out.

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 v2 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
SWDIO (2) DIO
SWCLK(6) DCLK

Connections for in-circuit programming

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 the 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 ports, I decided to find which is which experimentally. First, I uploaded the following code to my Blue Pill: To get USB Serial working, I soldered a 1.5k resistor between 3.3V and PA12, while also desoldering R10 from PCB.

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
GND GND
RX A9 (TX)
TX A10 (RX)


UART-to-Serial connected to BluePill

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

Serial object works with pins A9 and A10

Aand USB serial on miniUSB connector where it should be.

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 A9 and A10

That's all you'll need to get a jump start on using Arduino Framework on Bluepill! From now on, you can increase software complexity of your Bluepill prototypes much faster thanks to Arduino API.

Comments

  1. It is really bad those bluepills come with the wrong resistor on R10 which fucks up the USB, and with a bootloader that does not support USB. It seems the BlackPill version has the USB fixed, and can be reflashed with a boot loader that supports USB, meaning those external flashers are not needed anymore.

    ReplyDelete
    Replies
    1. what problems are caused by the wrong resistor on R10 ?

      Delete
    2. Bluepill can't enumerate as an USB device because of that.

      Delete
    3. some computer are tolerant of that spec error, all of mine anyway.

      Delete
    4. some computer are tolerant of that spec error, all of mine.

      Delete
  2. So where and for how much can you get a blackpill?

    AliE doesn't seem to have it.

    ReplyDelete
    Replies
    1. Found it after scrolling almost to the bottom of search result page: http://ali.ski/rMBDJG
      Yeah, their USB connector does look more reliable, but the board itself is slightly wider, by the looks of it. Oh, and STM32 wiki has an article on those too:
      http://wiki.stm32duino.com/index.php?title=Black_Pill

      Delete
    2. Thanks.
      Any comparison of STM32 with ESP8266?

      Delete
    3. Uh, I'm not sure. How would they compare? ESP8266 has WiFi and quite a limited I/O, and while STM makes at least one eval. board with WiFi (http://www.st.com/en/ecosystems/x-nucleo-idw01m1.html), it's much more expensive that ESP8266 and BluePill.
      If I needed dev. board with pin count comparable to Arduino and Bluepill and with WiFi capability, I'd get Bluepill or Arduino to act as a host to ESP8266.

      Delete
  3. Hi Vadim,
    In fact Bluepill is a great board. I use this board to introduce NuttX for other people:
    https://www.youtube.com/c/NuttXChannel
    I hope you enjoy it as well.

    ReplyDelete
    Replies
    1. Hello, Alan!
      Glad you posted this link here! I didn't know about NuttX before, but flashing a full-fledged RTOS on Bluepill seems like a great step to make this board realize its full potential (multitasking etc.). Tutorials on your channel look really promising, I'll definitely take a look.

      Delete
    2. Thank you Vadim, I'm glad you liked it. Also I posted a small article about NuttX at Embedded.com: http://www.embedded.com/electronics-blogs/say-what-/4458729/What-is-the-NuttX-RTOS-and-why-should-you-care- This RTOS is used by many companies, but surprisingly many hackers/developers don't know about it.

      Delete
  4. Guess there is the a typo. Serial is on A9 & A10. And there is no big issue if this resistor is not correct. Just connect BluePill to USB-Uart converter and that's it. You have working USB serial interface that you're probably going to use just during development. This boards are really dirt chip with awesome chip on it.

    ReplyDelete
    Replies
    1. Whoops, thanks for pointing this out. The labels on my Bluepill are all bunched up together to the point where you can't always tell what pin is it - A8 or A9.
      As for USB Serial - well, it's always nice to have the debugging interface without needing an extra usb-serial converter. I remember how mine broke once, and I was forced to debug with USB Serial exclusively.

      Delete
  5. Since the blue pill's STM32F103C8 has officially 64KB of flash, couldn't the free version of Keil uVision be used? They say the C8 actually has 128KB but the programming utility will show only 64KB.

    The Keil software with the $3 ST-Link V2 programmer/debugger would seem like a good approach if using Windows.

    ReplyDelete
    Replies
    1. Really, why not? Personally, I've yet to see a stm32f102c8t6 with 128k of flash. And the datasheet indicates 64k, so there's that. Seems to me like the people who reported this memory bonus has some sort of limited factory f**kup edition chips...

      Delete
    2. AFAIK all STM32F103C8T6 microcontroller has a hidden 64KB, I explained how to unlock this 64KB here: https://www.youtube.com/watch?v=0eHpoPZvI3U

      Delete
  6. Hello, I am am very new with platform IO, how do I set up the com port and a programmer
    (FTDI). Or is it being done automatically

    ReplyDelete

Post a Comment