STM32F103C8 bare metal template
If you're new to the STM32 world, I strongly suggest you read all this README file before you start coding. Also there are comments in almost every source file. It's a good idea to read them.
To start programming using this template you need to follow these steps.
Clone this repo with git clone https://github.com/al95/STM32-Bare-Metal.git
or download a zip file from here.
At the beginning of the file you'll find two variables that you may want to change.
TARGET: This is the name of your program and will be used to name the "output" files.
e.g., TARGET = blinky
will generate blinky.elf
, blinky.bin
and blinky.hex
.
OPTFLAGS: This sets the gcc optimization level.
I suggest using -O2
to optimize for speed or -Os
to optimize for size, but be aware that you might get some unexpected results if you're not careful with your code.
If you're not sure just leave it at -O0
.
main.c
init.c
Add to OTHER_SRCS
all the source files that you want to compile.
e.g., OTHER_SRCS = main.c usarts.c timers.c
Just run make
.
You should get (at least) three files: an ELF, a BIN and an HEX.
The .elf file is the "final product" of the compilation.
If you don't know what an ELF file is, it probably means you don't need it.
The .bin and .hex files contain the actual data that will be written to the STM32 flash.
Which one you need depends on the programming software you want to use.
Flash your program to the STM32.
If you have texane's stlink installed you can just run make flash
.
Otherwise, use whatever programming software you prefer.
That's it!
To debug your program follow the first 4 steps of the Quick start guide then run make debug
.
This will disable any optimization (-O0
), add detailed debug information for gdb (-ggdb3
) and generate a few additional files. These are:
gcc -fstack-usage
)ld -Map
)objdump -x -S
)objdump -D
)The Makefile included in this template can start a debugging session using st-util and gdb:
cd
into the source folder.make server
to open the st-util server.cd
into the source folder.make gdb
to run gdb.load
to load your program into flash.You're on your own from here. If you don't know how to use gdb, Google is your friend. It's easier than it looks.
make
I get /bin/sh: arm-none-eabi-gcc: command not found
CROSS_COMPILE
variable inside the Makefile.CROSS_COMPILE = another-prefix-
will run another-prefix-gcc
CROSS_COMPILE = /home/john/arm/arm-none-eabi-
will run /home/john/arm/arm-none-eabi-gcc
make server
I get bind: Address already in use
STUTIL_PORT
inside the Makefile to use a different port.STUTIL_PORT = 5555
will use port 5555
make server
I get bind: Permission denied
make server
I get make: st-util: Command not found
STUTIL
variable inside the Makefile.STUTIL = /home/john/stlink/st-util
will run /home/john/stlink/st-util
make flash
or make erase
I get make: st-flash: Command not found
STFLASH
variable inside the Makefile.STFLASH = /home/john/stlink/st-flash
will run /home/john/stlink/st-flash
-O0
my program doesn't work correctly / interrupts don't work.
DBG_OPTFLAGS
variable inside the Makefile.DBG_OPTFLAGS = -O2
will compile using -O2
make
is doing?VERBOSE
variable inside the Makefile.VERBOSE = N
is the default option and VERBOSE = Y
will enable the verbose mode.
make
I get region 'RAM' overflowed by ... bytes
_heap_size
and _stack_size
variables in the linker script.