Electronic – Makefile for STM32

compilerstm32stm32f10x

I want to write code in just a text editor (not an IDE) and then build it with a simple Makefile.

I've got:

I tried googling for such Makefiles, but most of them are written for older versions of CMSIS (for example, STM32Cube lacks stm32f10x.h and stm32f10x_conf.h which most build systems rely on).

Please, tell which compile commands should I use to compile code for STM32F103 and where to define the frequency, memory density other chip specific constants.

I would also be grateful if you share example of Makefile that uses texane's stlink utility to detect density and ram/rom size.

Best Answer

A makefile is actually a script for the 'make' utility and is not specific to any processor (GNU make atleast), neither has it got anything to do with CMSIS. You can, in fact, build nothing and simply create a folder using 'make' and 'recipies' written in the makefile. This is the user manual for GNU make for some background.

For the compiler commands again, you'll have to endure the GCC manual. It mentions the options that you need to enter for specifying the processor, optimization etc. For example, in the makefile recipe below:

build/%.o: source/%.c | build
    @echo " *** Building $@"
    arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -fsigned-char -ffreestanding -nostartfiles -Og -g3 -fno-move-loop-invariants -fdata-sections -ffunction-sections -I"include" -std=gnu11 -c -o $@ $^
    @echo " *** $@ built."
    @echo ""

it tells the 'make' utility that for making a file build\%.o, you'll need a file named source\%.c and the 'recipe' or method to do so is to run the arm-none-eabi-gcc compiler with the options specified in that line. The meaning of each compiler option is mentioned in the GCC manual. For example, -mcpu=cortex-m4 specifies the ARM cortex m4 processor. The stm32f103 would perhaps need -mcpu=cortex-m1.

As Arsenal said, a makefile cannot be used to specify frequency, memory size etc. While the frequency is configured in the application code, the memory size etc. would appear in the linker script used by the GNU linker 'ld'. Here is a reference to the linker manual. In fact, one of the options for the GNU compiler is -T"linker.ld" which specifies the linker file to be used. I found this blog post useful when I was trying to do something similar.