STM32 Cmake Quickstart Guide (Linux)
by alderon_creates in Circuits > Microcontrollers
18 Views, 0 Favorites, 0 Comments
STM32 Cmake Quickstart Guide (Linux)
This guide will provide details on how to create a build system for a STM32 microcontroller on Debian/Ubuntu Linux. This build system will utilise CMake, STM32 HAL and git as well as some other bits and pieces. I will be using a F4 series STM32, the NUCLEO-F446RE. I will also be using vim, but if you are more familiar with nano, use that in place of the vim commands. I highly recommend reading through the steps before attempting this for yourself.
This guide assumes:
- familiarity with using vim or nano
- familiarity with navigating a linux file system in terminal
- F4 series STM32 is being used
Supplies
- STM32 microcontroller board
- A cable for your STM32
- Have super user privelages
Install Required Packages
To begin, install the following packages using apt:
The installed versions can be checked using:
Setup Udev Rules for Flasing
It will be necessary to setup the udev rules on linux so that builds have permission to be flashed to the STM board. To create the rules, execute the following:
Add the following lines to the newly created file, write the changes and quit.
Connect your STM32 via the cable to your computer and complete the following to enable the rules and check that permissions are correct.
Create Workspace
Directory Setup
For this guide the home directory will be used (~/). Create a new directory to hold your build system and drivers, and download the F4 series drivers:
Your directory tree should now look like this (there are other folders also populated here, but these are the main ones we are concerned with currently):
Create an additional directory to hold your project:
Your directory tree should now look like this:
The working directory structure for the project now needs to be created. Once we are done it will look like the outline below:
Top Level CMakeLists
Create CmakeLists.txt and populate it with the below text:
Linker
In project/linker run the following command or manually copy the file in file explorer and rename it.
Startup File
In project/startup run the following command or manually copy the file in file explorer.
Toolchain File CMake
In project/cmake create arm-none-eabi.cmake and populate it with the below text:
Template main
In project/Core/Src create main.c and populate it with the below text:
At the same time in project/Core/Inc create an empty file called main.h.
Add system_stm32f4xx
Copy system_stm32f4xx.c and system_stm32f4xx.h into the src and inc directories:
Add Configuration Header
The stm32f4xx_hal_conf.h file will also need to be copied into the project. In project/Core/Inc run the following command or manually copy the file in file explorer and rename it.
Fix timebase issues
Before we can get started we need to resolve the fact that HAL comes with three timebase files, but can only use one. The simplest way to do this is to remove the two that you won't use.
Build and Flash
Test the Build
Test creating a build by running the following:
The following files should be created in the /build directory if successful:
- nucleo-f446re.elf
- nucleo-f446re.bin
- nucleo-f446re.hex
Flash the Board
To flash the build to the STM32 execute the following:
Adding a Build Script
No one wants to be continuously changing directories when developing code, building and flashing. We are going to add a build.sh file that can be called with arguments to do our builds for us.
In the project directory create a file called build.sh and populate with the following:
Builds and flashing can now be invoked from the top level using the following commands:
Blink the On-board LED
Edit your main.c file with the following code and then invoke the following to build and flash. If successful you should see the on board LED flash on and off every half a second. You may need to press the reset button on the board to reinitialise the flashed program.
Troubleshooting
- If you update the project name in cmake or build.sh you will need to update it in the other file too.
- If the program won't flash to the board you may need to update ST-Link firmware.
Conclusion
That's everything! You should now have a build system that uses CMake and STM32HAL. From here you can grow your project.
https://cmake.org/ provides resources to help you expand your CMake setup if you need to create a more complex multilevel src structure.