Setting-up cross compiler and build tools for STM32

Here is an ARM cross-compiler tutorial for STM32 and other ARM-based microcontrollers, like Microchip’s SAM’s.

What’s a cross compilation process? That is when you use a x86 machine to produce binary code for a different architecture, like ARM. The situation is similar when you use a Raspberry Pi (ARM CortexA) to build apps for STM32 (ARM CortexM).

cross compile for ARM target architecture

If you run Linux on your host and don’t have installed any cross compiler for ARM target, you can use this guide to get it working. With minor variations you can use it for a Raspberry Pi host as well.

Download software

First, you need to download the software. For that, go to https://developer.arm.com, ‘Tools and software’, ‘Open Source Software’, ‘GNU Toolchain’. There, search for ‘GNU-RM’, ‘Downloads’. ‘RM’ stands for Cortex-R and Cortex-M. The other section, ‘A’ is for Cortex-A, as you probably guessed, but this is not compatible with STM32.

From ‘Downloads’ section, search for gcc-arm-none-eabi-9-2019-q4-major-x86_64-linux.tar.bz2 or something similar, if your development machine is x86 or x64. If you are on a Raspberry Pi, search for aarch64-linux variant.

Unpack and install

After download, unpack the archive:

cd ~myusername/Downloads
tar -xjf gcc-arm-none-eabi-9-2019-q4-major-x86_64-linux.tar.bz2

Use z instead of j, if the ending of the file name is .gz.

Now, we create the /opt directory if it does not exist, then install the compiler and tools in the appropriate place:

udo mkdir /opt
cd /opt
sudo mv ~myusername/Downloads/gcc-arm-none-eabi-9-2019-q4-major /opt/gcc-arm

Set PATH

Additionally, we need to add the path to compiler to your operating system path. For that, open your preferred editor and browse for .bashrc file. It should be in your home directory. At the end of file, add:

export PATH="/opt/gcc-arm/bin:$PATH"

Then save and exit.

Alternatively, if you don’t want to have a lengthy PATH variable, there is another option: create a file named st-env.sh in /opt/gcc-arm and put this inside:

export PATH="/opt/gcc-arm/bin:$PATH"

then, at every STM32 session, just type in the console:

source /opt/gcc-arm/set-env.sh

Test

After setting path in either way, at this point, everything should be in place, and we can test the compiler. Open a new terminal console and type:

arm-none-eabi-gcc --version

It should return the version of the build, some copyright notice and a disclaimer. If you don’t see this, but “command not found” message, then either the path is not setup correctly or the installed tools are using a different prefix.

You can try to compile something dummy, to validate the gcc. So, open your editor, create a file main.c and type in:

#include <stdio.h>

void main(void)
{
    printf("Hello world");
}

After that, open a terminal console, cd to the path where main.c is, then type:

arm-none-eabi-gcc -c main.c

If you see a file named main.o in the same directory as main.c, then the compiler is installed correctly.

For sure, this ARM cross-compiler tutorial will be useful in the next step, the LED blink.

Share this:

No Responses

Leave a Reply

Your email address will not be published.