Menu

How to compile and burn the code to STM32 chip on Linux (Ubuntu)

This is tutorial for beginners that shows how to install tools, compile the code with gcc-arm-none-eabi and send it to the STM32 using st-flash. It also introduce basics of automation of this task by putting all instructions into Makefile.

2. Compiling and burning the code

Now that you have the toolchain installed, a next step is to compile the source code into a .ELF, then generate .BIN file and finally burn this this binary file to STM32 chip using ST-Link v2 programmer.

Example code

Here is an example content of main.c file. The code does nothing except getting stuck in an endless loop but it’s always something!

int
main(void)
{
while (1);
}

Compiling

The command below will compile your code. It’s GCC so I assume it looks familiar to you and no additional explanations are needed. If you want perform compilation for some other MCU then you need specify at least appropriate -mcpu, .LD and .S files (not provided in this tutorial)

Generating .BIN

Most programmers will not accept a GNU executable as an input file, so we need to do a little more processing. So, the next step is about converting the information form .ELF into .BIN file. The GNU utility that does this is called arm-none-eabi-objcopy.

$ arm-none-eabi-objcopy -O binary main.elf main.bin

Burning

The utility called st-flash can program processors using the content of the .BIN files specified on the command line. With the command below, the file main.bin will be burned into the flash memory.

$ st-flash write main.bin 0x8000000

Voila! Chip is programmed.

3. Make and Makefiles

Now, we can automate this process by creating a Makefile and putting our commands there. The structure of a Makefile is very simple, and more information about it can be found here. Utility make reads automatically a Makefile file in the folder where you launch it. Take a look at simple Makefile presented bellow.

Hi, thanks. Sorry for late response. If it says “command not found” then the bash doesn’t know the path of st-flash. As a first, I would check if st-flash is available under destination path ($HOME/stlink/build/Release/_install/usr/local/bin). Sometimes it needs to reopen/refresh terminal to load new set of environment variables. L