How to build and run ¶
This user journey describes how to build and run STM32Cube Software Examples on the STM32 development boards and Discovery kits.
Prerequisite ¶
Get an example project on the local disk by using one of the delivery channels described in the How to get STM32Cube Software Examples? section.
This page assumes that the example project is downloaded from the STM32 Example Library.
To build and run the example projects from the STM32Cube MCU Package, refer to the STM32Cube MCU package documentation for the selected STM32 series.
Note
Replace the element names in brackets (
[
]) with the actual names of the elements in use.
Step 1: Open the IDE project ¶
First, ensure that Visual Studio Code and the STM32CubeIDE for Visual Studio Code extension are installed on your machine. Refer to the STM32CubeIDE for Visual Studio Code documentation for more details about how to get and install this extension.
To import your cmake project, click on the
Open
Folder
button from the
file
menu bar and select the root folder of your example project (where the
CMakeLists.txt
file is located).
First, ensure that IAR Embedded Workbench for Arm (version 9.20 or higher) is installed on your machine.
Make sure the device you are using is supported in the IAR IDE.
For more information about adding device support, refer to the
DFP documentation of the selected STM32 series.
In your example project folder, locate the ewarm project with the
.eww
extension and double click on it to open the project in IAR IDE.
The project has been created with IAR EWARM version 9.20. It is possible to open it with IAR EWARM version 9.20 or higher. You will be asked to convert it if you are using a more recent version of IAR than 9.20.
First, ensure that Keil® MDK for Arm® (version 5.42 or higher) is installed on your computer. Verify that the device in use is supported in the Keil MDK integrated development environment. For more information about adding device support, refer to the DFP documentation of the selected STM32 series.
Important
The HAL2-based example projects are delivered in the
CMSIS Solution Project File Format
(open-cmsis projects), which is compatible with both Keil MDK5 (µVision) and MDK6 (Keil Studio).
The entry point of the
CMSIS solutions
is a
.csolution.yml
file.
No
.uvprojx
is delivered with the example.
When using Keil MDK version 5,
CMSIS-Toolbox, which is required to manage csolution files, is included in the installer
[1]. The tools are located in the MDK installation directory.
The
cmake
and
ninja
build systems must be available on your computer. All tools must be configured in the
PATH
environment variable.
The project has been created with STM32CubeMX2 for Keil MDK version 5.42. It is possible to open it with Keil MDK version 5.42 or higher [2].
In your example project folder, locate the
[example_name].csolution.yml
file and open it with Keil µVision. From the
Project
menu, choose
Open
project
and set the file filter to
Csolution
files
to see the
.csolution.yml
file and open it.
Note
For more information about the project structure and code organization, refer to the How to read an example section. To go further and understand the architecture of an example, refer to the Example architecture section.
Step 2: Configure the project ¶
Once the CMake project is open, the STM32Cube for VS Code extension recognizes it.
When the pop-up
Would
you
like
to
configure
discovered
CMake
project(s)
as
STM32CubeIDE
project(s)?
is displayed, click
Yes.
A configuration window appears if the settings are not automatically detected. In this window, select the board or device and the toolchain, then press
Configure.
The project is configured and ready for build
[3].
Important
The CMake projects are delivered with two build profiles:
Debug
and
Release.
You can switch between the
Debug
and
Release
profiles from the CMake configuration window (
PROJECT
STATUS>Configure).
The most demanding examples might not run in
Debug
mode due to the reduced optimization level.
Switch to the
Release
profile to run the example.
See
Debug build profile
for more information.
IDE-managed switches:
By default, PRINTF logs are disabled. In order to enable PRINTF logs, set the defined symbol
USE_TRACEto1in the profile options in the filecmake/flags.cmake.In the same file (
cmake/flags.cmake), you can adjust the other compiler switches specific to your example project if exist.Additionaly, some specific compiler switches can be set from the
user_modifiable/Pre_Include_Global.hfile.See Compiler switches for more information about the compiler switches.
Example:
target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC USE_TRACE=1 STM32C562xx _RTE_)
Linker script edition:
You can edit the
.ldGNU linker script of the project located underproject/user_modifiable/Device/to change:Flash and RAM sizes
Memory region placement (CODE, DATA, BSS, HEAP, STACK)
Special sections (such as DMA buffers, shared memory)
IDE-managed switches:
By default, PRINTF logs are disabled. In order to enable PRINTF logs, set the defined symbol
USE_TRACEto1in the project options through:Open the
Optionsof the project from theProjectmenu.In the
C/C++ compilercategory, select thePreprocessortab and setUSE_TRACEto1.
In this same
Preprocessortab, you can adjust the other compiler switches specific to your example project if exist.Additionaly, some specific compiler switches can be set from the
user_modifiable/Pre_Include_Global.hfile. Find this file withOptions>C/C++ compiler>Preinclude file.See Compiler switches for more information about the compiler switches.
Linker script edition:
You can edit the
.icfIAR linker script of the project located underproject/user_modifiable/Device/to change:Flash and RAM sizes
Memory region placement (CODE, DATA, BSS, HEAP, STACK)
Special sections (such as DMA buffers, shared memory)
IDE-managed switches:
By default, PRINTF logs are disabled. In order to enable PRINTF logs, set the defined symbol
USE_TRACEto1in the file[example_name].csolution.yml.
|
Switch |
Configuration |
|---|---|
|
|
In
# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/schemas/projmgr/2.11.0/tools/projmgr/schemas/csolution.schema.json
solution:
target-types:
- type: NUCLEO-C562RE
device: STM32C562RET6
define:
- USE_TRACE: 1
|
|
Pre-include (managed by STM32CubeMX2 and CMSIS-Toolbox) |
Configure
/*
* CSOLUTION generated file: DO NOT EDIT!
* Generated by: csolution version 2.11.0
*
* Project: 'hal_crc_user_defined_polynomial.release_IAR+NUCLEO-C562RE'
* Target: 'release_IAR+NUCLEO-C562RE'
*/
#ifndef PRE_INCLUDE_GLOBAL_H
#define PRE_INCLUDE_GLOBAL_H
/* STMicroelectronics::Device:STM32CubeMX2 Config:RCC@2.0.0 */
#define USE_EXTERNAL_ENV
#endif /* PRE_INCLUDE_GLOBAL_H */
|
See Compiler switches for more information about the compiler switches.
Important
The project configuration cannot be edited from the Keil MDK IDE project options.
All configuration must be done from the
.csolution.yml
file as described above.
Linker script edition:
You can edit the
.sctscatter file (linker script) of the project located underproject/user_modifiable/Device/to change:Flash and RAM sizes
Memory region placement (CODE, DATA, BSS, HEAP, STACK)
Special sections (such as DMA buffers, shared memory)
Debug probe:
By default, the project does not select the
ST-Link
debug probe.
Select the
ST-Link
debug probe in the project options to be able to flash and debug the project on the target board.
To do so, right-click on the
context
name in the
Project
Explorer
panel, select
Options
for
target..., and in the
Debug
category, select
ST-Link
Debugger
as the debug probe.
Note
The
USE_TRACE
define has some effect only if the example project implements console logs. See
Using the console
for more information about this feature.
Note
For more information about how to configure the projects, refer to the How to configure an example section.
Step 3: Build the project ¶
From the activity bar, click on the
CMake
icon. In
PROJECT
STATUS
section, under
Build, select your build profile.
Then click on the
Build
button to build the project.
The build output is displayed in the
OUTPUT
tab of the terminal panel.
From the
Project
menu, choose
Rebuild
All
to rebuild and relink all files in the current application.
You can also choose
Make
to respectively build only the files that have been changed since the last build.
From the
Project
menu, choose
Rebuild
csolution
context-set
to build and link all files in the current application.
You can also choose
Build
csolution
context-set
to respectively build only the files that have been changed since the last build.
Step 4: Flash and run the project ¶
This possibility is not available yet in the STM32CubeIDE for Visual Studio Code extension. Use the step-by-step debugging method described in Step 5: Step-by-step debugging to flash and run the project.
From the
Project
menu, choose
Download>Download
active
application
to download the project to the target, without launching a full debug session.
Then, reset your target to run the application.
From the
Flash
menu, choose
Download
to download the project to the target, without launching a full debug session.
Then, reset your target to run the application.
Note
Refer to the
README
file of the example project for more details about the expected behavior of the example once it is running on the target.
See
README
for more information about the content of the
README
files.
See
User interactions
for more information about how to interact with the example once it is running on the target.
Step 5: Step-by-step debugging ¶
From the activity bar, click on the
Run
and
Debug
icon.
In the
Run
and
Debug
view, click on the
Run
and
Debug
button and select
STM32Cube:
STM32
Launch
STLink
GDB
server.
When starting a new debug session, the startup might fail because the debugger firmware needs to be updated.
To do so, select the STLINK debug device under the
STM32CUBE
DEVICES
AND
BOARDS
section.
Click the
Upgrade
STLINK
firmware
button to update the firmware.
Then, launch the debug session again.
Important
The
release
profile does not enable the debug symbols (
-g3). Therefore the debugger cannot display the source code and variables content, and you cannot set breakpoints in the source code.
Switch to the
debug
profile to be able to debug the application and use all the debugging features such as breakpoints, variable inspection, and stepping through the code.
If you must debug the application in the
release
profile, you can edit the
cmake/flags.cmake
file to add the
-g3
switch to the
target_compile_options
of the
release
profile.
Example:
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC "SHELL:-g3 -Os -fdata-sections -ffunction-sections -std=gnu11 -Wall -fstack-usage --specs=nano.specs --specs=nosys.specs -Werror=implicit-function-declaration ${CC_SECURE}")
From the
Project
menu, choose
Download>Download
and
Debug
to download the project to the target and launch a debug session.
Or, from the
Project
menu, choose
Download>Debug
without
Downloading
to start a debug session without downloading the project.
From the
Debug
menu, choose
Start/Stop
Debug
Session
to launch a debug session and start debugging the application with the selected debug probe.
Then, you can set breakpoints, inspect variables, and step through the code.
Note
For more details about how to use the examples in debug mode, refer to the How to debug an example section.