You can create your own examples and add them to the compilation for both SEGGER Embedded Studio and CMake. Do this only when you are familiar with the toolchain and how to build the stack and the examples.
The process of adding new examples is composed of the following stages:
- Creating custom example folder and CMakeLists file
- Defining application configuration
- Building and running custom examples
- Note
- The custom example to be added is referred to as
my_appon this page. Replace this name with the name of your application each time it is mentioned.
Creating custom example folder and CMakeLists file
To add folders and the CMakeLists.txt file for your custom example:
- In the
examplesfolder of your nRF5 SDK for Mesh installation path, add a newmy_appfolder. - Copy the contents of one of the existing example folders to this new folder. For example, copy the contents of
examples/beaconing. - In the
examples/CMakeLists.txtfile, depending on your platform (see Compatibility):- If your example supports the nRF52810 or nRF52820 SoC, add the new folder as a new
add_subdirectorycommand entry. For example:set(WEAK_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/common/src/nrf_mesh_weak.c" "${CMAKE_CURRENT_SOURCE_DIR}/common/src/app_error_weak.c" "${CMAKE_CURRENT_SOURCE_DIR}/common/src/assertion_handler_weak.c" CACHE INTERNAL "") add_subdirectory("common") add_subdirectory("beaconing") add_subdirectory("pb_remote") add_subdirectory("provisioner") add_subdirectory("my_app") - If your example does not support the nRF52810 or nRF52820 SoC, add the new folder as a new
add_subdirectorycommand entry in the platform-relatedifsection. For example:if ((NOT PLATFORM MATCHES "nrf52810") AND (NOT PLATFORM MATCHES "nrf52820")) add_subdirectory("my_app") add_subdirectory("sensor") add_subdirectory("scene") add_subdirectory("light_lightness") add_subdirectory("light_lc") add_subdirectory("light_ctl") add_subdirectory("enocean_switch") add_subdirectory("dimming") add_subdirectory("lpn") add_subdirectory("dfu") add_subdirectory("serial") add_subdirectory("light_switch") endif()
- If your example supports the nRF52810 or nRF52820 SoC, add the new folder as a new
- Modify the target name in the first line of the
examples/my_app/CMakeLists.txtfile toset(target "my_app").
Defining application configuration
When you created custom example folder and CMakeLists.txt file, you also copied the following files used for configuring the application:
nrf_mesh_config_app.happ_config.h
To define the example configuration:
- In the
examples/my_app/CMakeLists.txtfile, add or remove the application support modules based on your needs. - Edit the
include/app_config.hfile to match your application configuration and override the configuration of the nRF5 SDK (see SDK configuration header file). - Edit
nrf_mesh_config_app.hfile to modify:- the application support module section (see Application support module configuration and Application configuration)
- the Bluetooth mesh stack (see configuration pages for modules in API Reference, for example DFU configuration)
After defining the configuration, build the example with the following procedure.
Building and running custom examples
The building procedure for custom examples is similar to the standard building procedure, with the following differences:
- If you want to build examples with SEGGER Embedded Studio, you must generate new project files using CMake after you have configured the application.
- If you want to build examples with CMake using ninja, build your example with the following commands:
- The first time you build your custom example after adding it:
build $ ninja
- Each next time you build your custom example after building it for the first time:
build $ ninja my_app
- The first time you build your custom example after adding it:
When running the example in CMake-based environment, flash your example with the following command:
build $ ninja flash_my_app