Modifying an application
For simplicity, this guide will refer to both samples and applications as “applications”.
After programming and testing an application, you probably want to make some modifications to the application, for example, add your own files with additional functionality, change compilation options, or update the default configuration.
Adding files and changing compiler settings
The nRF Connect SDK build system is based on Zephyr, whose build system is based on CMake. For more information about how the build system works in Zephyr, see Build and Configuration Systems and Application Development in the Zephyr documentation.
Role of CMakeLists.txt
In the nRF Connect SDK, the application is a CMake project.
As such, the application controls the configuration and build process of itself, Zephyr, and sourced libraries.
The application’s CMakeLists.txt
file is the main CMake project file and the source of the build process configuration.
Zephyr provides a CMake package that must be loaded by the application into its CMakeLists.txt
file.
When loaded, the application can reference items provided by both Zephyr and the nRF Connect SDK.
Loading Zephyr’s CMake package creates the app
CMake target.
You can add application source files to this target from the application CMakeLists.txt
file.
Updating CMakeLists.txt
The recommended method to update the CMakeLists.txt
file is to use the nRF Connect for VS Code extension to maintain it.
You can also edit the file directly.
Editing CMakeLists.txt
You can add source files to the app
CMake target with the target_sources()
function provided by CMake.
Pay attention to the following configuration options:
If your application is complex, you can split it into subdirectories. These subdirectories can provide their own
CMakeLists.txt
files.The build system searches for header files in include directories. Add additional include directories for your application with the
target_include_directories()
function provided by CMake. For example, if you want to include aninc
directory, the code would look like the following:target_include_directories(app PRIVATE inc)
See the CMake documentation and Build System (CMake) in the Zephyr documentation for more information about how to edit CMakeLists.txt
.
Advanced compiler settings
The application has full control over the build process.
Using Zephyr’s configuration options is the standard way of controlling how the system is built.
These options can be found under Zephyr’s menuconfig Build and Link Features > Compiler Options.
For example, to turn off optimizations, select CONFIG_NO_OPTIMIZATIONS
.
Compiler options not controlled by the Zephyr build system can be controlled through the CONFIG_COMPILER_OPT
Kconfig option.
Configuring your application
You might want to change the default options of the application. There are different ways of doing this, but not all will store your configuration permanently.
Configuration system overview
Zephyr and the nRF Connect SDK use several configuration systems, each system with a specialized syntax and purpose.
The following tables summarizes information about the configuration sources in the nRF Connect SDK.
Source |
File types |
Usage |
Available editing tools |
Additional information |
---|---|---|---|---|
|
Hardware-related options |
Devicetree Visual Editor is part of the nRF Connect for VS Code extension. You still need to be familiar with the devicetree language to use it. |
||
|
Software-related options |
Kconfig GUI is part of the nRF Connect for VS Code extension. |
||
|
Memory layout configuration |
Partition Manager script |
Partition Manager is an nRF Connect SDK configuration system that is not available in Zephyr. |
See the following sections for more information. To read more about Zephyr’s configuration system, see Build and Configuration Systems in the Zephyr documentation.
Memory layout configuration
The Partition Manager is specific to the nRF Connect SDK. If enabled, it provides the memory layout configuration. The layout is impacted by various elements, such as Kconfig configuration options or the presence of child images. Partition Manager ensures that all required partitions are in the correct place and have the correct size.
If enabled, the memory layout can be controlled in the following ways:
Dynamically (default) - In this scenario, the layout is impacted by various elements, such as Kconfig configuration options or the presence of child images. Partition Manager ensures that all required partitions are in the correct place and have the correct size.
Statically - In this scenario, you need to provide the static configuration. See Static configuration for information about how to do this.
After CMake has run, a single partitions.yml
file with the complete memory layout will have been created in the build
directory.
This process also creates a set of header files that provides defines, which can be used to refer to memory layout elements.
For more information, see Partition Manager.
Child images
The nRF Connect SDK build system allows the application project to become a root for the sub-applications known in the nRF Connect SDK as child images. Examples of child images are bootloader images, network core images, or security-related images. Each child image is a separate application.
For more information, see Multi-image builds.
Changing the hardware configuration
To correctly edit the .dts
and .overlay
files for your project, you need to be familiar with the Devicetree language and syntax.
The nRF Connect for VS Code extension offers several layers of Devicetree integration, ranging from summarizing devicetree settings in a menu and providing devicetree language support in the editor, to the Devicetree Visual Editor, a GUI tool for editing devicetree files.
Follow the steps in How to create devicetree files and use one of the following options to edit the .dts
and .overlay
files:
Changing the software configuration
You can change the software-related configuration temporarily or permanently.
The temporary build files are deleted when you clean the build directory with the pristine
target (see Zephyr’s Rebuilding an Application for more information).
Temporary changes
When building your application, the different .config
, *_defconfig
files and the prj.conf
file are merged together and then processed by Kconfig.
The resulting configuration is written to the zephyr/.config
file in your build
directory.
This means that this file is available when building the application until you clean the build directory pristinely.
The documentation for each configuration option lists the menu path where the option can be found.
Use the nRF Kconfig GUI in the nRF Connect for VS Code extension to select the desired options. The GUI organizes the Kconfig options in a hierarchical list and lets you view and manage your selection.
To locate a specific configuration option, use the Search modules field. Read the Configuring with nRF Kconfig page in the nRF Connect for VS Code extension documentation for more information.
Alternatively, you can configure your application in the nRF Connect for VS Code extension using menuconfig. Open the More actions.. menu next to Kconfig action in the Actions View to start menuconfig in the extension.
To quickly test different configuration options, or to build your application in different variants, you can update the .config
file in the build directory.
Changes are picked up immediately.
Note
While it is possible to edit the .config
file directly, you should use the nRF Kconfig GUI in the nRF Connect for VS Code extension or a tool like menuconfig or guiconfig to update it.
These tools present all available options and allow you to select the ones that you need.
Alternatively, you can configure your application using menuconfig. For this purpose, run the following command when Building on the command line.
west build -t menuconfig
See Interactive Kconfig interfaces in the Zephyr documentation for instructions on how to run menuconfig or guiconfig. To locate a specific configuration option, use the Jump to field.
Permanent changes
To configure your application and maintain the configuration after you clean the build directory pristinely, you need to specify the configuration in one of the permanent configuration files.
This will be either the default prj.conf
file of the application or an extra Kconfig fragment.
In these files, you can specify different values for configuration options that are defined by a library or board, and you can add configuration options that are specific to your application.
See Setting Kconfig configuration values in the Zephyr documentation for information on how to change the configuration permanently.
Tip
Reconfiguring through menuconfig only changes the specific setting and the invisible options that are calculated from it.
It does not adjust visible symbols that have already defaulted to a value even if this default calculation is supposed to be dependent on the changed setting.
This may result in a bloated configuration compared to changing the setting directly in prj.conf
.
See the section Stuck symbols in menuconfig and guiconfig on the Kconfig - Tips and Best Practices in the Zephyr documentation for more information.
If you work with nRF Connect for VS Code extension, you can use one of the following options:
Select an extra Kconfig fragment file when you build an application.
Edit the Kconfig options using the nRF Kconfig GUI and save changes permanently to an existing or new
prj.conf
file.
See the extension’s documentation about Kconfig for more information.
If you work on the command line, pass the additional options to the west build
command.
The options must be added after a --
at the end of the command.
See Permanent CMake Arguments for more information.
The prj.conf
file is read when you open a project.
The file will be reloaded when CMake re-runs.
This will happen automatically when the application is rebuilt.
Providing CMake options
You can provide additional options for building your application to the CMake process, which can be useful, for example, to switch between different build scenarios. These options are specified when CMake is run, thus not during the actual build, but when configuring the build.
For information about what variables can be set and how to add these variables in your project, see Important Build System Variables in the Zephyr documentation.
If you work with the nRF Connect for VS Code extension, you can specify project-specific CMake options when you add the build configuration for a new nRF Connect SDK project. See How to build an application in the nRF Connect for VS Code extension documentation.
If you work on the command line, pass the additional options to the west build
command.
The options must be added after a --
at the end of the command.
See One-Time CMake Arguments for more information.
Configuring build types
When the CONF_FILE
variable contains a single file and this file follows the naming pattern prj_<buildtype>.conf
, then the build type will be inferred to be <buildtype>.
The build type cannot be set explicitly.
The <buildtype> can be any string, but it is common to use release
and debug
.
For information about how to set variables, see Important Build System Variables in the Zephyr documentation.
The Partition Manager’s static configuration can also be made dependent on the build type.
When the build type has been inferred, the file pm_static_<buildtype>.yml
will have precedence over pm_static.yml
.
The child image Kconfig configuration can also be made dependent on the build type.
The child image Kconfig overlay file is named child_image/<child_image_name>.conf
instead of prj.conf
, but otherwise follows the same pattern as the parent Kconfig.
Alternatively, the child image Kconfig configuration file can be introduced as child_image/<child_image_name>/prj.conf
and follow the same pattern as the parent Kconfig.
For example, child_image/mcuboot/prj_release.conf
can be used to define release
build type for mcuboot
child image.
The Devicetree configuration is not affected by the build type.
Note
For an example of an application that is using build types, see the nRF Desktop application (nRF Desktop build types) or the nRF Machine Learning application (nRF Machine Learning build types).
To select the build type in the nRF Connect for VS Code extension:
When building an application as described in the nRF Connect for VS Code extension documentation, follow the steps for setting up the build configuration.
In the Add Build Configuration screen, select the desired
.conf
file from the Configuration drop-down menu.Fill in other configuration options, if applicable, and click Build Configuration.
To select the build type when building the application from command line, specify the build type by adding the following parameter to the west build
command:
-- -DCONF_FILE=prj_selected_build_type.conf
For example, you can replace the selected_build_type variable to build the release
firmware for nrf52840dk_nrf52840
by running the following command in the project directory:
west build -b nrf52840dk_nrf52840 -d build_nrf52840dk_nrf52840 -- -DCONF_FILE=prj_release.conf
The build_nrf52840dk_nrf52840
parameter specifies the output directory for the build files.