Setting-up cross compiler and build tools for STM32
Posted in Embedded By Grig On December 12, 2019Here 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).
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.
Later edit: Clarification – in this tutorial I’ve been using Linux Mint 18, 64-bit, x86 machine.
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:
sudo 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.
After installation and setting the path , when I run “arm-none-eabi-gcc —version” command in terminal, it shows “bash: /opt/gcc-arm/bin/arm-none-eabi-gcc : cannot execute binary file: Exec format error”. What should I do now?
And actually I am doing these set up in my raspberry pi “Raspbian Buster OS”
Hari, what I understood is you ran a x86 compiler binary on a Raspberry Pi machine, which is an ARM machine. Either try to use a x86 machine for this tutorial, or install appropriate binaries for ARM.
For Raspberry PI this should be the compiler: https://developer.arm.com/-/media/Files/downloads/gnu-rm/9-2020q2/gcc-arm-none-eabi-9-2020-q2-update-aarch64-linux.tar.bz2?revision=7166404a-b4f5-4598-ac75-e5f8b90abb09&la=en&hash=01D713C1174E80C856385F5732E9BDC466DB729B
Let me know if that works for you.
Hi Grig,
Thanks for the reply. But I did downloaded the above mentioned zip which is intended for aarch64 architecture. But I couldn’t not make it work.
But I can able to install the GCC compiler from terminal window of raspberrypi with the following commands
Binutils:
sudo apt-get install binutils-arm-none-eabi
GCC Arm cross-compiler
sudo apt-get install gcc-arm-none-eabi
GDB debugger:
sudo apt-get install gdb-multiarch
Now it’s working fine.😊
Great!