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).
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.
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:
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
sudo mv ~myusername/Downloads/gcc-arm-none-eabi-9-2019-q4-major /opt/gcc-arm
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:
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:
then, at every STM32 session, just type in the console:
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:
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: