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 = blinkywill generateblinky.elf,blinky.binandblinky.hex. -
OPTFLAGS: This sets the gcc optimization level.
I suggest using-O2to optimize for speed or-Osto 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.
- Write your main function inside
main.c - Add as many other source files as you want
- If you're using interrupts, add your ISRs to the vector table in
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:
- *.o: intermediate object files
- *.size: size of the .text, .data and .bss sections
- *.su: stack usage statistics (
gcc -fstack-usage) - *.map: link map (
ld -Map) - *.lst: disassembled program with sections, symbols and source files (
objdump -x -S) - *.dis: complete disassembly of all sections (
objdump -D)
The Makefile included in this template can start a debugging session using st-util and gdb:
- Open a terminal and
cdinto the source folder. - Run
make serverto open the st-util server. - Open another terminal and
cdinto the source folder. - Run
make gdbto run gdb. - Inside gdb run
loadto load your program into flash. - Good luck!
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.
- When i run
makeI get/bin/sh: arm-none-eabi-gcc: command not found
Check that the ARM version of gcc is correctly installed.
If your gcc is installed using a different name prefix or in a user-defined folder you must change theCROSS_COMPILEvariable inside the Makefile.
e.g.,CROSS_COMPILE = another-prefix-will runanother-prefix-gcc
e.g.,CROSS_COMPILE = /home/john/arm/arm-none-eabi-will run/home/john/arm/arm-none-eabi-gcc
- When I run
make serverI getbind: Address already in use
By default st-util communicates with gdb through port 4242.
If you get this error it means that port 4242 is already in use by another program.
ChangeSTUTIL_PORTinside the Makefile to use a different port.
e.g.,STUTIL_PORT = 5555will use port 5555
- When I run
make serverI getbind: Permission denied
You selected a privileged port number. Try another port >=1024.
- When I run
make serverI getmake: st-util: Command not found
Check that st-util is correctly installed.
If st-util is installed in a user-defined folder you must change theSTUTILvariable inside the Makefile.
e.g.,STUTIL = /home/john/stlink/st-utilwill run/home/john/stlink/st-util
- When I run
make flashormake eraseI getmake: st-flash: Command not found
Check that st-flash is correctly installed.
If st-flash is installed in a user-defined folder you must change theSTFLASHvariable inside the Makefile.
e.g.,STFLASH = /home/john/stlink/st-flashwill run/home/john/stlink/st-flash
- When I use optimization levels higher than
-O0my program doesn't work correctly / interrupts don't work.
Make sure that every global variable that gets changed by an ISR is declared as volatile.
- Can I enable optimization for the debug build?
Yes, but be aware that debugging an optimized program is often a bad idea.
You can set the debug optimization level using theDBG_OPTFLAGSvariable inside the Makefile.
e.g.,DBG_OPTFLAGS = -O2will compile using-O2
- Can I see what
makeis doing?
Yes, by changing theVERBOSEvariable inside the Makefile.
VERBOSE = Nis the default option andVERBOSE = Ywill enable the verbose mode.
- When i run
makeI getregion 'RAM' overflowed by ... bytes
This means you're using too much RAM.
The linker script is set to keep 2KB of free RAM to be used by the stack and the heap.
This is accomplished by defining two fake empty memory sections in the linker script.
If you're sure that your program will work with less free RAM, you can lower the amount of free memory by changing the_heap_sizeand_stack_sizevariables in the linker script.
By default they are set to 1KB each.