[ARM] Blue pill and STM32F on Linux

When I was surfing on the Internet, I found out this board that's a ARM Cortex-M3 for less than U$2,00 dollars, what's in my opinion a good deal for an all-in-one cheap solution and it's very simple and versatile to program and use. The blue pill is a reference for matrix movie, when Neo needs to take a decision to get in or out of the system. As it's presents on the film, you have the red pill, blue pill and an extra board called black pill, all compatible and ready to work on Arduino IDE, all the boards are the same schema, the only difference is in the black has a LED in another port of the MCU.
cheap ARM boards
Looking at the docs about this board we see some tips why the cheaper price, first there's a manufacturing problem with the USB D+ resistor on pin PA12, according to the schema, there's a 4.7k pull-up but as we usually expect from this price, the resistor soldered was wrong. As was not just wrong in the board itself, but also in the schema where as explained in this page the correct value is 1.5k in the USB D+ pin for full speed devices. Although this problem, the board is very good and easy to work, where the headers with 100 mils of pitch fits well in the breadboard. With two dollars in Brazil you cannot even just buy the headers of this board. From now, let's create a developing environment for the bluepill board on a Linux machine to make good things with this board.

Choosing an IDE

Some years ago I posted in my old blog a tutorial for a complete setup with eclipse + Tiva (ARM Cortex-M4) in Linux, where I used for a long time with my ARM projects, but as the things evolve it's important for me to have a more generic way to program others MCU because nowadays you could work with different types of ARM's in the same project. I found in the hackaday.io a post about the blue pill and 6 IDE's to develop some code and for who wants to testing some different IDE from Eclipse, it's good to read an see the opinion from the author. For me, I'll use the old and reliable Eclipse with some ARM plugins, that makes a powerful environment to develop firmware.
eclipse_ide

Installing the IDE

All information that'll write below comes from the GNU MCU Eclipse (formerly GNU ARM Eclipse) , this organized website has all links and tutorial for who wants to work with ARM/AArch64 or RISC-V on Linux, although this tutorial, go read this site to have a more precise information about the configurations. Start installing Eclipse CDT directly from the source, I'll not download by terminal because it depends of your operational system version. Go to Eclipse CDT page and download according to your Linux, after download, extract and copy to:

sudo mkdir -p /opt/eclipse
sudo cp -rf FOLDER-ECLIPSE-EXTRACTED /opt/eclipse
sudo ln -s /opt/eclipse/eclipse /usr/bin

You must have something like this, if you did right:
symbolic_link

Toolchain for ARM

As the main tool for our environment for compiling programs, we need a set of tools that provides us the way to construct programs for ARM microcontrollers. The tool set is usually called as toolchain, for ARM we obtain in the ARM developer website.
arm_toolchain
This file in the red square highlighted contains all the tools that we need to develop for ARM mcus. Download that, extract to a folder and do this:

tar xjf ~/Downloads/gcc-arm-none-eabi-6-2017-q1-update-linux.tar.bz2
mkdir ~/opt/
cp -rf ~/Downloads/gcc-arm-none-eabi-6-2017-q2-update-linux ~/opt/ #this is the recommended folder, eclipse'll look on this
sudo apt-get -y install lib32z1 lib32ncurses5 lib32bz2-1.0

The last line install all necessary tool to run the toolchain (x86) into a x64 OS system. One big warning that's in the reference of this tutorial is not to install que ARM-GCC toolchain that comes with your distribution because it'll maybe not support debug mode. Another recommendation is to not create a linker script for the toolchain because maybe in the future you'll need to use another set of tools/many tool in the same dev machine.Once up ready, you can check the toolchain version as the following:
arm_check_version

Eclipse plugins

After installed the eclipse and the toolchain you'll need a set of eclipse plugins that'll prepare your IDE to be ready with ARM tools. Open eclipse in your terminal (should work the linker script) and go to Help>>Install New Software and type GNU ARM Eclipse repository - https://dl.bintray.com/gnu-mcu-eclipse/updates-test/ on Work with input box.

Check all the boxes and click in next to install them all, once done, restart eclipse and you're done.

Eclipse preferences and finishing

As I wrote before, this tutorial is condensed from the original GNU MCU Eclipse website that has more complete tips and pictures for the tutorial. Another suggested recommendation is the workspace preferences and I think that's better you read the below links in this order and do the recommendations before I continue:

  1. Workspace preferences
  2. Create a blinky project
  3. Associate the device to a project

In the link 2, just remember to change the MCU to STM32F103C8T that's our microcontroller for bluepill. Another tip is to configure the PC13 pin of bluepill as the blink pin because in this pin we have a LED. : )

"My device doesn't show up"

If you have some problem in the target selection, you'll need to install the packs for the family that're working (on our case STM32F10x), to do this, go to eclipse and click in packs perspective to open a screen like this:

To install right-click on the version and select Install.

If you're working with bluepill or any other STM32F based boards, you can have a lot of advantages in using the CubeMX, this tool helps you with all the pre-configuration that we usually need to do before start developing the logic algorithm itself. Download this tool in CubeMX link and after unrar them, run the installer with:

cd FOLDER-CUBEMX-EXTRACTED/
sudo chmod +x SetupSTM32CubeMX-4.21.0.linux
./SetupSTM32CubeMX-4.21.0.linux

Follow the tutorial to install the tool and when done, open with:

cd ~/STM32CubeMX
sudo chmod +x STM32CubeMX
./STM32CubeMX

I'll not enter into the subject of how to use because ST has this excellent video that helps the new users to work with it.

"How I'll flash my target?"

As you imagined, bluepill needs a programmer to flash his memory and if you don't have enough money to buy a, original J-Link for ARM devices, the Segger provides a turn-key solution that helps you to convert a NUCLEO STM32 board into a universal J-Link Lite device. Take a look at the link and try to configure a NUCLEO board that's so much cheaper than a J-Link tool. In the end you'll have something like this image:
nucleo_blue_pill
This is not my image (I'm not with a camera right now), but you'll need to connect the SWDIO pins of the bluepill and your nucleo board, after configure the J-Link tool in the Eclipse with this:

  1. Configuring J-Link into Eclipse

There're another tools to program as openOCD but, J-Link is probably the best less headache way to program. To import the programming files generated by the CubeMX, Carmine Noviello has developed a great tutorial that'll help you with this, the only change that I would do on his tutorial is to start your project with STM32F1xx C/C++ Project template because you'll have all the standard IO's already adjusted. In this link I have an example of how to use the USB of the bluepill in CDC mode (Communication Device Class) to send some serial message in Linux. The only important setting that I've made to use the USB is the RCC Clock into the CubeMX because USB protocol requires a 48MHz PLL Clock (bluepill has two crystals, one for main MCU Speed Clock (8MHz) and another for precision reference ~32.768 kHz).
st_cube_mx_device
If you done alright, it's time to start developing and as Neo remember to dodge the bugs. :D
neo_ninja

References
[ARM] Blue pill and STM32F on Linux
Share this

Subscribe to @aignacio's