Image for post
Image for post

Juggling STM32 Blue Pill For Arduino Jugglers

Image for post
Image for post
STM32 Blue Pill with headers (left) and ST-Link V2 USB Debugger (right)
  1. ST-Link V2 USB Debugger (about US$ 2): Connects the Blue Pill to your computer via USB. We’ll be using the ST-Link to flash our compiled program into the Blue Pill.
  • st-link v2

Preparing The Blue Pill

For this tutorial there’s no need to solder the pointy yellow headers onto the Blue Pill. You don’t need any sensors, transceivers or even a breadboard — we’ll be using two components simulated in STM32 code: (1) Bosch BME280 sensor (for temperature, humidity and altitude) and (2) Wisol WSSFM10R4AT transceiver (for the Sigfox network).

Image for post
Image for post
Image for post
Image for post
Connecting the Blue Pill to the ST-Link USB Debugger
Image for post
Image for post
Both jumpers should be set to the “0” positions
Image for post
Image for post
Jumper settings for the Blue Pill

While You’re Downloading And Installing…

You’re now reading the third article in the “juggling” series of tutorials about crafting multitasking IoT programs on the Arduino Uno with the cocoOS task scheduler. The first article shows you how to read the temperature, humidity and altitude sensors of the BME280 as separate tasks, instead of using the normal single-threaded Arduino loop…


Installing OpenOCD and ST-Link USB Driver

For Windows:

  1. Unzip the downloaded file and copy the OpenOCD files into c:\openocd such that openocd.exe is located in the folder c:\openocd\bin
  2. Download the ST-Link USB driver from the
    ST-Link Driver Website (email registration required)
    Click Get Software
  3. Unzip the downloaded file. Double-click the driver installer:
    dpinst_amd64.exe
  1. Enter the following in a command prompt:
    brew install openocd
  1. Enter the following in a command prompt:
    sudo apt install openocd
  • ST-Link USB Driver is needed on Windows for OpenOCD to communicate with the ST-Link Debugger. It’s not needed on Mac and Ubuntu.

Downloading the source files

Open a Windows, Mac or Ubuntu command prompt and enter:
(For Windows, install git from Git For Windows)

git clone https://github.com/lupyuen/send_altitude_cocoos.git
cd send_altitude_cocoos
Image for post
Image for post
scripts\linksrc.cmd
chmod +x scripts/*.sh
scripts/linksrc.sh

Installing Visual Studio Code and PlatformIO

For Arduino we use the Arduino IDE to compile and upload programs. For the Blue Pill, we’ll use Visual Studio Code and the free, unregistered version of PlatformIO. Both tools are open source and they work together so well as the Blue Pill IDE that it might even be better than the Arduino IDE.

Image for post
Image for post
  1. C/C++ (by Microsoft — see below)
Image for post
Image for post
#define CONFIG_ARTICLE3  //  Uncomment to support Article #3
Image for post
Image for post
Our Blue Pill IDE — Visual Studio Code with PlatformIO

Really, it’s the same Arduino code running on Blue Pill!

Image for post
Image for post

Image for post
Image for post
Command buttons in the status bar

Build, Upload and Monitor your Blue Pill program

Connect the ST-Link USB Debugger to your computer. Notice the buttons at the lower left? (See screen above) We’ll click them now. If the buttons are not visible, click the menu Terminal → Run Task → PlatformIO… to run the commands instead.

Image for post
Image for post
Building your Blue Pill program
Image for post
Image for post
Uploading your program to the Blue Pill
Image for post
Image for post
Connect to view Blue Pill debug log
Image for post
Image for post
Your very first Blue Pill program!
Image for post
Image for post

Image for post
Image for post
Blue Pill Debug Log and the task actions that triggered the messages

Here’s proof that your Blue Pill is really juggling multiple tasks

In the debug log above I have labelled the messages (1 to 7) and the corresponding tasks that triggered the messages. And this is what exactly we would expect a multitasking IoT program to do— reading the sensors concurrently while aggregating the data in another task and sending the data in yet another task. We call this the Intent of the program.

Image for post
Image for post
Once again the mighty semaphore unfurls the true intent
  • So long as we control the sharing of resources: I2C Bus and Network Buffer.
  • There’s no need to transmit to the network every sample of sensor data that we read. The Network Task aggregates the data intelligently and transmits only when necessary. So we could throttle the transmission independently of the sensor polling. Or we could add more transceiver modules and Network Tasks to send data more frequently.

Hidden Intentions of Arduino Programs

https://github.com/lupyuen/unabiz-arduino/blob/master/examples/send-altitude-structured/send-altitude-structured.ino#L91:1

But if you really wish to migrate a classical Arduino program to the Blue Pill…

Here’s what you could do…

  1. Start with the send_altitude_cocoos code from this article and replace the main() function in main.cpp by your own. Replace all debugging code like Serial.print() / println() by debug_print() / debug_println(). Build the code for Blue Pill.
  2. You will encounter build errors because most Arduino APIs are not defined. Add stub functions to the folder stm32/porting until the Arduino APIs have been migrated to Blue Pill.

Highlights of the Blue Pill migration

bluepill Library

https://github.com/lupyuen/send_altitude_cocoos/blob/master/stm32/bluepill/bluepill.h
https://github.com/lupyuen/send_altitude_cocoos/blob/master/stm32/bluepill/led.cpp

Logging and Arm Semihosting

https://github.com/lupyuen/send_altitude_cocoos/blob/master/stm32/logger/logger.cpp
Image for post
Image for post
https://github.com/lupyuen/send_altitude_cocoos/blob/master/main.cpp

I2CInterface

https://github.com/lupyuen/send_altitude_cocoos/blob/master/stm32/i2cint/i2cint.cpp

UARTInterface

https://github.com/lupyuen/send_altitude_cocoos/blob/master/stm32/uartint/uartint.cpp

The End?

Many thanks to Peter Eckstrand for creating cocoOS, the amazing portable task scheduler that runs on Arduino, Blue Pill and many many more devices! I must also thank Peter for rescuing me from so many Blue Pill traps — hopefully we have eliminated the dangerous ones!

Wait… There’s More!

The next article in the series is here…

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Crypto News

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Lup Yuen Lee 李立源

Written by

Techie and Educator in IoT 物聯網教師

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Lup Yuen Lee 李立源

Written by

Techie and Educator in IoT 物聯網教師

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store