23.5 STM32 Development Environment
STM32 is a 32-bit microcontroller (Microcontroller Unit, MCU) product line introduced by STMicroelectronics (ST) since 2007. This product line is based on the ARM Cortex-M processor core architecture and is one of the mainstream MCU platforms in the embedded development field.
Installing STM32CubeMX
STM32CubeMX is the official graphical hardware configuration and code generation tool provided by STMicroelectronics, which can be used to quickly complete hardware initialization for STM32 projects.
For developing STM32 embedded systems on the FreeBSD platform, it is recommended to use STM32CubeMX for initialization configuration. STM32CubeMX officially only supports Windows, Linux, and macOS. Running this software on FreeBSD requires the Linux compatibility layer.
For detailed tutorials on installing the Linux compatibility layer on FreeBSD, refer to other chapters in this book. The linux-rl9 compatibility layer maintained by FreeBSD is recommended.
Starting from STM32CubeMX V6.2.0, the installer includes a built-in Java Runtime Environment (JRE, Adoptium Temurin 21.0.3+9), eliminating the need for users to install Java separately. If you encounter issues running the installer under the FreeBSD Linux compatibility layer, you can try installing the system-level OpenJDK as an alternative. This section's example uses Port java/openjdk25.
After configuring the compatibility layer, download the archive from the STM32CubeMX official website and extract it. At the time of writing, downloading does not require registration or login; you can download as a guest. The download link will be sent via email, so ensure your email address can receive messages normally. The downloaded file is stm32cubemx-lin-v6-17-0.zip.
Extract the installer stm32cubemx-lin-v6-17-0.zip to the directory /home/ykla/stm:
$ unzip stm32cubemx-lin-v6-17-0.zip -d /home/ykla/stmTip
The
/home/yklapath in this section's example is for demonstration purposes; replace it with your actual home directory.
Navigate to the extracted folder and run the executable, which is SetupSTM32CubeMX-6.17.0 in this example. Launch the installer:
$ ./SetupSTM32CubeMX-6.17.0Begin installation:

Accept the license agreement, then click Next.

Accept the user terms, then click Next.

Enter the installation path. You should record this path; the example path is /home/ykla/STM32CubeMX.

Confirm using this path:

After confirmation, the program begins the installation:

Installation complete:

Exit the installer:

Create a desktop shortcut:
Create a STM32CubeMX.desktop file in the ~/Desktop directory, then write the following:
Replace the Exec and Icon paths above with your actual paths. Then grant executable permissions.

Installing Other Tools
In addition to STM32CubeMX, you also need to install the development toolchain and debugging tools. Install using pkg:
Or build using ports:
Building and Flashing
Create a project using STM32CubeMX. In Project Manager, under Project, select CMake in the Toolchain/IDE field. In Code Generator, select copy all used libraries into project folder and Generate peripheral initialization as a pair of '.c/.h' files per peripheral.
After generating the project, modify the CMakeLists.txt file:
Tip
When switching STM32 models, there are multiple placeholders in CMakeLists.txt that are bound to the specific MCU model and must be adjusted individually. It is recommended to regenerate the project in STM32CubeMX after switching the target chip, then modify the corresponding positions in CMakeLists.txt accordingly.
The linker script (
.ldfile) defines the starting addresses and sizes of Flash and RAM. Different models have different memory layouts; mixing them will cause the program to fail to run properly or to crash after flashing. STM32CubeMX automatically generates the correct linker script based on the selected chip. Never copy.ldfiles directly from other projects.
To use the gcc-arm-embedded toolchain in the terminal, add its binary files to PATH.
The configuration methods for each shell are as follows:
sh / Bash / Zsh
~/.profile
export PATH=/usr/local/gcc-arm-embedded-14.2.rel1/bin:$PATH
fish
~/.config/fish/config.fish
set -gx PATH /usr/local/gcc-arm-embedded-14.2.rel1/bin $PATH
csh / tcsh
~/.cshrc
setenv PATH /usr/local/gcc-arm-embedded-14.2.rel1/bin:$PATH
Begin building:
Finally, flash to the development board:
Last updated