CMake-based STM32F4 Template
This repository contains a CMake-based template project as a starting point for STM32 F4 projects. ST's official Standard Peripheral Library is already included in this repository and will automatically be linked against the target application.
Both C and C++ sources are supported. CMake will also try to find OpenOCD and will generate a flash target for easy programming of the microcontroller.
This template includes a sample application which turns on the blue LED on the STM32F4Discovery board for demonstration purposes.
Get CMake for your platform. You should use at least CMake 2.8.12.
For compiling the project, a GNU C/C++ compiler is required. I highly suggest the GNU ARM Embedded Toolchain.
Make sure that the toolchain executables are in your PATH environment. If you want to use OpenOCD, also make sure that it is in the PATH and the executable is called
build.bat (rename it to
build.sh for Linux ;-) ). This will create a new directory
build and will generate common Unix Makefiles for the STM32 project. If all goes well, you can enter
make for compiling the project.
This will also generate an Intel HEX file (.hex), a binary file (.bin), a map file (.map) generated by the linker and an assembler listing file intermixed with C code (.lss) - besides the obvious .elf file.
make flash for flashing via OpenOCD. In this example, the ST-Link on the STM32F4Discovery board is assumed. You might have to adapt this.
You may want to use
build-debug in the early development stage. This will generate Makefiles which compile the firmware with no optimization (-O0). Otherwise, optimization for size (-Os) will be used.
You can also let CMake generate project files for Eclipse. Try executing
build-eclipse which generates a project directory as a sibling to the root directory of this project. This is the way recommended by CMake. In Eclipse, just import the newly created project directory. All paths should have been automatically discovered so that the auto-completion feature works.
I tried to keep this template as clean as possible. The CMake settings I made should be self-explaining and you should be able to easily create your own project with this template as a base.
General project settings (the name, STM32 device family, important library defines, the linker and startup files) are made in the root CMakeLists.txt file.
In CMake/GNU-ARM-Toolchain.cmake, you can find the global C/C++ compiler and core flags. These should be sane for most STM32F4-based projects, but you might need to adjust them.
lib/startup_files subdirectory, you can find the startup files for all STM32F4 families. Select the correct one and copy it to the
You must also edit src/CMakeLists.txt as it contains your source files that should be compiled and linked.
Please also make sure that the linker file is correct. In most applications, this default linker file suffices and the only things you might have to change are the flash and RAM sizes in the
Last but not least: The included src/system_stm32f4xx.c works for the discovery board. Adapt it to your own needs (clock configuration, PLL, etc.). But this is beyond the scope of this project.
Other STM32 families
This template targets the STM32F4 family. But targeting other families (e.g. STM32F0) should be easy. Just adapt the core flags and exchange the standard peripheral library.
These projects helped me a lot while creating this template: