CMakeLists.txt:
# Can be setted in cli
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../nrf52.cmake)
cmake_minimum_required(VERSION 3.8)
project(example C ASM)
add_executable(example main.c)
set_target_properties(example PROPERTIES NRF52_CHIP NRF52832)
nrf52_target(example)
main.c:
int main()
{
for(;;);
return 0;
}
configure:
$ cmake ./
-- No TOOLCHAIN_PATH specified, using default: /usr
-- No TOOLCHAIN_TRIPLET specified, using default: arm-none-eabi
-- No NRF5_SDK_PATH specified, using default: /opt/nRF5_SDK
-- The C compiler identification is GNU 9.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /bin/arm-none-eabi-gcc
-- Check for working C compiler: /bin/arm-none-eabi-gcc
-- Check for working C compiler: /bin/arm-none-eabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to:
build:
$ cmake --build ./
Scanning dependencies of target example
[ 25%] Building C object CMakeFiles/example.dir/main.c.obj
[ 50%] Building ASM object CMakeFiles/example.dir/opt/nRF5_SDK/modules/nrfx/mdk/gcc_startup_nrf52.S.obj
[ 75%] Building C object CMakeFiles/example.dir/opt/nRF5_SDK/modules/nrfx/mdk/system_nrf52.c.obj
[100%] Linking C executable example
[100%] Built target example
The main piece of nrf52-cmake is toolchain file nrf52.cmake.
It setups cross-compiling enviroment and defines some useful functions.
This file should be passed to cmake using CMAKE_TOOLCHAIN_FILE
variable,
either using command line or in CMakeLists.txt.
This toolchain file uses following variables:
-
TOOLCHAIN_PATH
(default:/usr
) - path to ARM GCC toolchain, compiler should be located in${TOOLCHAIN_PATH}/bin
-
TOOLCHAIN_TRIPLET
(default:arm-none-eabi
) - compiler target triplet, cmake will search for${TOOLCHAIN_TRIPLET}-gcc
executable -
NRF5_SDK_PATH
(default:/opt/nRF5_SDK
) - optional path to NRF5 SDK. Required if you're going to use startup code and linker script from SDK (most common scenario)
Besides toolchain setup nrf52-cmake defines some useful functions, which works on cmake targets and target properties. Some properties can (or should) be setted on target to use nrf52-cmake functions:
-
NRF52_CHIP
- mandatory property to specify NRF52 device. Can be either NRF52xxx or NRF52xxx-xxxx form. Currently onlyNRF52840
,NRF52832
,NRF52811
,NRF52810
are supported. -
NRF52_NO_SDK
- optional tells nrf52-cmake not to use any source code from NRF5 SDK. You'll have to provide all startup sources by yourself. -
NRF52_NO_LINKER_SCRIPT
- optional tells nrf52-cmake not to use common autogenerated linker script.
nrf52-cmake adds following useful functions:
-
nrf52_get_chip(<target> <chip> <variant>)
- parsesNRF52_CHIP
property on<target>
and outputs some useful information:<chip>
- specific chip code (840, 832, etc.)<variant>
- chip variant (AA, AB, etc), default will be AA.
-
nrf52_add_sdk_startup(<target>)
- search and adds startup sources to target from nRF5 SDK. -
nrf52_linker_sizes_from_device(<chip> <variant> <ram_size> <flash_size>)
- get flash and ram size from chip name and variant. -
nrf52_linker_generate_script(<target> <script> [RAM_SIZE <size>] [FLASH_SIZE <size>] [RAM_START <addr>] [FLASH_START <addr>])
- generate linker script. -
nrf52_add_linker_script(<target> <script>)
- add (custom) linker script file to target. -
nrf52_generate_linker_script(<target>)
- generate (usingnrf52_linker_sizes_from_device
andnrf52_linker_generate_script
) and add linker script for target. -
nrf52_configure_compiler(<target>)
- configure all compiler options -
nrf52_target(<target>)
- process target and and make everything listed above.
nrf52-cmake provides support for SoftDevice firmware. Minimal example can be found here
SoftDevice support implemented as cmake package, so you'll need to use
find_package
first:
find_package(SoftDevice COMPONENTS <variant> REQUIRED)
where <variant>
- SoftDevice variant(s) (S112, S132, S140, etc.)
Package will add following IMPORTED
targets, that can be linked using
target_link_libraries
:
-
SoftDevice::<variant>
- header only library that adds include directories and compiler definitions -
SoftDevice::<variant>Firmware
- library that will link SoftDevice firmware (extracted from .hex file) into final executable. Link those if you want complete image with SoftDevice included.
SoftDevice package also defines some functions:
-
softdevice_generate_linker_script(<target> SOFTDEVICE <variant> RAM <size>)
- generates correct linker script for using with SoftDevice.RAM
argument specifies amount of RAM that will be used by SoftDevice firmware. -
softdevice_add_flash_target(<target> SOFTDEVICE <variant> [ADAPTER <adapter>])
- add custom target<target>
that will flash SoftDevice firmware to device using openocd and<adapter>
hardware adapter (jlink by default). SoftDevice variant (S140, S132, S332, etc) should be specified bySOFTDEVICE
parameter.
nrf52-cmake provides support for ChibiOS RTOS building. Example located here
You'll need both ChibiOS (version 19.1.x) and ChibiOS-Contrib to run ChibiOS on NRF52 platform. Only NRF52832 is currently supported by ChibiOS.
ChibiOS support implemented as cmake package:
find_package(ChibiOS COMPONENTS Core HAL NRF5 REQUIRED)
To use this package you'll need to specify ChibiOS location:
$ cmake -DChibiOS_ROOT=<path> -DChibiOS_Contrib_ROOT=<path> ../
Package will add many IMPORTED
libraries with ChibiOS modules' source code:
-
ChibiOS::RT
andChibiOS::NIL
- kernel core (NIL or RT) -
ChibiOS::HAL
- ChibiOS HAL -
ChibiOS::NRF52832
- kernel support for NRF52832 -
ChibiOS::HAL::NRF52832
- HAL support for NRF52832 -
ChibiOS::HAL::*
andChibiOS::HAL::NRF52832::*
- HAL drivers (e.g.ChibiOS::HAL::I2C
andChibiOS::HAL::NRF52832::I2C
- support for I2C driver)
To help with HAL modules there is function
nrf52_linker_generate_script(<target> PLATFORM NRF52832 CONFIG <path to halconf.h>)
which links <target>
with HAL drivers listed in halconf.h
Currently, nrf52-cmake does not generates linker script for ChibiOS, you'll need to provide own (see examples/chibios).
Also, there is function ChibiOS_set_stack_sizes(<target> [DEFAULT] [MAIN <size> PROCESS <size>])
which sets stack sizes for ChibiOS linker script. Calling ChibiOS_set_stack_sizes
is mandatory for now.