Thursday, 7 September 2017

Setting up GCC ARM Coding Environment for STMicrocontroller


1. Installing compiler and stlink

To compile C and/or C++ source code of your firmware you will need gcc-arm-none-eabi compiler and stlink.

Installing gcc-arm-none-eabi

What is extremely useful, there are complete and easy to install packages for all major platforms (https://launchpad.net/~team-gcc-arm-embedded/+archive/ubuntu/ppa)
sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi

Installing stlink

At first, we need install dependencies then build it from sources (https://github.com/texane/stlink/blob/master/doc/compiling.md#build-from-sources).
sudo apt-get install git build-essential libusb-1.0.0-dev cmake
cd $HOME
git clone git@github.com:texane/stlink.git
cd stlink
make release
cd build/Release && make install DESTDIR=_install
echo "export PATH=\$PATH:$HOME/stlink/build/Release/_install/usr/local/bin" >> $HOME/.bashrc

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)
$ arm-none-eabi-gcc -std=gnu99 -g -O2 -Wall -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 -fsingle-precision-constant -Wdouble-promotion main.c -o main.elf
After performing successful compilation, you can check program and data memory size with this command.
$ arm-none-eabi-size -tA main.elf 
main.elf  :
section              size        addr
.isr_vector           192   134217728
.text                6404   134217920
.rodata                60   134224324
.ARM                    8   134224384
.init_array             8   134224392
.fini_array             4   134224400
.data                1092   536870912
.jcr                    4   536872004
.bss                   32   536872008
._user_heap_stack    1536   536872040
.ARM.attributes        40           0
.comment               31           0
.debug_line          7416           0
.debug_info         22917           0
.debug_abbrev        6837           0
.debug_aranges        744           0
.debug_loc           6584           0
.debug_ranges         472           0
.debug_str           5717           0
.debug_frame         2004           0
Total               62102

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.
TARGET=main

CC=arm-none-eabi-gcc
LD=arm-none-eabi-gcc
AR=arm-none-eabi-ar
AS=arm-none-eabi-as
CP=arm-none-eabi-objcopy
OD=arm-none-eabi-objdump
SE=arm-none-eabi-size
SF=st-flash

CFLAGS  = -std=gnu99 -g -O2 -Wall
CFLAGS += -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0
CFLAGS += -fsingle-precision-constant -Wdouble-promotion

SRCS =  main.c

.PHONY: $(TARGET)

$(TARGET): $(TARGET).elf

$(TARGET).elf: $(SRCS)
        $(CC) $(INCLUDE) $(CFLAGS) $^ -o $@
        $(CP) -O binary $(TARGET).elf $(TARGET).bin

clean:
        rm -f *.o $(TARGET).elf $(TARGET).bin

flash:
        $(SF) write $(TARGET).bin 0x8000000
If you launch a simple make in the terminal, only label “all” will be executed. When you launch make flash label “flash” will be executed, and so on.

4. Summary

Essentially, assuming that our program is in main.c, only those three things are needed to compile and burn the code to STM32 chip.
$ arm-none-eabi-gcc -std=gnu99 -g -O2 -Wall -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 -fsingle-precision-constant -Wdouble-promotion main.c -o main.elf
$ arm-none-eabi-objcopy -O binary main.elf main.bin
$ st-flash write main.bin 0x8000000


It’s important to highlight that we can easily automate whole process with Makefiles. Sooner or later you will need it!



ref:
http://blog.podkalicki.com/how-to-compile-and-burn-the-code-to-stm32-chip-on-linux-ubuntu/
https://startingelectronics.org/tutorials/STM32-microcontrollers/programming-STM32-flash-in-Linux/
http://fishpepper.de/2016/09/16/installing-using-st-link-v2-to-flash-stm32-on-linux/

No comments:

Post a Comment