A clean (and very simple) base for stm32l4xx projects based on cmake
- Clone this repository:
git clone --recursive https://github.com/soundslikefrank/stm32l4-base.git my-cool-project
- Now we need to make a few adjustments. Use STM32CubeMX to generate code for your MCU/board (leave all default values), but use
Makefile
as Toolchain / IDE - Copy these files to the corresponding locations (adjust file names):
STM32CubeMXProject/startup_stm32l4xxxx.s
-> ./startup_stm32l4xxxx.s
STM32CubeMXProject/STM32L4xxxxxx_FLASH.ld
-> ./STM32L4xxxxxx_FLASH.ld
Then you can remove the old corresponding old files that come with this project.
- Adjust the references to these files in the
CMakeLists.txt
:
# Set startup file in project files:
set(PROJECT_FILES
# ...
startup_stm32l4xxxx.s
)
and
# If you're not sure which identifier to use, look into the STM32CubeMX generated Makefile
target_compile_definitions(${EXECUTABLE} PRIVATE
-DUSE_HAL_DRIVER
-DSTM32L4xxxx # change this!
)
and
target_link_options(${EXECUTABLE} PRIVATE
-T${CMAKE_SOURCE_DIR}/STM32L4xxxxxx_FLASH.ld
-
-optional- You might want to do some clock configuration (using STM32CubeMX or editing the file
system_stm32l4xx.c
directly), especially if you're using an external crystal -
-optional- Uncomment HAL modules that you don't need in
include/stm32l4xx_hal_conf.h
andCMakeLists.txt
-
To build (we are assuming an out-of-source build):
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../arm-none-eabi-gcc.cmake ..
make # or directly
make upload # use OpenOCD to flash!
You only ever need to run the cmake
command again after you made changes to the CMakeLists.txt.