Hi
I have been studying the nRF Connect SDK and taking the courses at Nordic Dev Academy. In the Zephyr Fundamentals course, as I don't have the sensors used in lesson 6 about I2C, I decided to use the sensors I have with me: the BNO055 IMU sensor.
But I'm also trying to dig even deeper and explore CMake at the same time.
I solve the exercise using the Bosch BNO055 IMU sensor, but I believe this question applies to any type of open-source library that I want to integrate into my projects. First, I downloaded the bno055.c and bno055.h files from the Bosch BNO055 IMU sensor github repository, added the files to my project directory and in the main.c file I included the header file:
#include "bno055.h"
I then integrated the bno055 driver into my project using functions from the Zephyr I2C API such as i2c_write(), i2c_read(), and i2c_write_read(). I was able to build the project without any problems and flash my nRF52840DK.
However, I want to set up a build system using the CMakeLists.txt file to make sure I can download and integrate the sensor files at build time and thus ensure that the project is always tracking the sensor's github repository. This is important in situations where the library has a bug fix or some improvement and automatically the project is updated with the new improvements in the next build process.
So, for this I configured the following CMakeLists.txt file:
make_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(my_project) # Add your application source files target_sources(app PRIVATE src/main.c) # Include ExternalProject module include(ExternalProject) # Set the directory where the external project will be downloaded and built set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external) # Define the ExternalProject for the external repository ExternalProject_Add( BNO055_driver PREFIX ${EXTERNAL_INSTALL_LOCATION}/BNO055_driver GIT_REPOSITORY "https://github.com/boschsensortec/BNO055_driver.git" GIT_TAG master UPDATE_COMMAND "" # Skip update step CONFIGURE_COMMAND "" # Skip configure step BUILD_COMMAND "" # Skip build step INSTALL_COMMAND "" # Skip install step ) # Get the source directory of the external project ExternalProject_Get_Property(BNO055_driver SOURCE_DIR) # Include the directory containing the header file include_directories(${SOURCE_DIR}) # Optionally, you can add a dependency on the external project to ensure it's built before your application add_dependencies(app BNO055_driver)
When I build the project, the build system is able to download the files from the github repository to my laptop but then the build system stops and have the following error:
-- Configuring done (4.4s) -- Generating done (0.1s) -- Build files have been written to: /Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build -- west build: building application [1/183] Preparing syscall dependency handling [4/183] Generating include/generated/version.h -- Zephyr version: 3.3.99 (/opt/nordic/ncs/v2.4.0/zephyr), build: v3.3.99-ncs1 [159/183] Performing download step (git clone) for 'BNO055_driver' Cloning into 'BNO055_driver'... Already on 'master' Your branch is up to date with 'origin/master'. [173/183] Linking C executable zephyr/zephyr_pre0.elf FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map /Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build/zephyr/zephyr_pre0.map : && ccache /opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -gdwarf-4 zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr/zephyr_pre0.elf zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj -fuse-ld=bfd -Wl,-T zephyr/linker_zephyr_pre0.cmd -Wl,-Map=/Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build/zephyr/zephyr_pre0.map -Wl,--whole-archive app/libapp.a zephyr/libzephyr.a zephyr/arch/common/libarch__common.a zephyr/arch/arch/arm/core/aarch32/libarch__arm__core__aarch32.a zephyr/arch/arch/arm/core/aarch32/cortex_m/libarch__arm__core__aarch32__cortex_m.a zephyr/arch/arch/arm/core/aarch32/mpu/libarch__arm__core__aarch32__mpu.a zephyr/lib/libc/minimal/liblib__libc__minimal.a zephyr/soc/arm/common/cortex_m/libsoc__arm__common__cortex_m.a zephyr/soc/arm/nordic_nrf/nrf52/libsoc__arm__nordic_nrf__nrf52.a zephyr/drivers/clock_control/libdrivers__clock_control.a zephyr/drivers/console/libdrivers__console.a zephyr/drivers/gpio/libdrivers__gpio.a zephyr/drivers/i2c/libdrivers__i2c.a zephyr/drivers/pinctrl/libdrivers__pinctrl.a zephyr/drivers/serial/libdrivers__serial.a zephyr/drivers/timer/libdrivers__timer.a modules/nrf/lib/fatal_error/lib..__nrf__lib__fatal_error.a modules/nrf/drivers/hw_cc310/lib..__nrf__drivers__hw_cc310.a modules/hal_nordic/nrfx/libmodules__hal_nordic__nrfx.a modules/segger/libmodules__segger.a -Wl,--no-whole-archive zephyr/kernel/libkernel.a -L"/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/thumb/v7e-m/nofp" -L/Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build/zephyr -lgcc zephyr/arch/common/libisr_tables.a -mcpu=cortex-m4 -mthumb -mabi=aapcs -Wl,--gc-sections -Wl,--build-id=none -Wl,--sort-common=descending -Wl,--sort-section=alignment -Wl,-u,_OffsetAbsSyms -Wl,-u,_ConfigAbsSyms -nostdlib -static -Wl,-X -Wl,-N -Wl,--orphan-handling=warn -Wl,-no-pie /opt/nordic/ncs/v2.4.0/nrfxlib/crypto/nrf_cc310_platform/lib/cortex-m4/soft-float/no-interrupts/libnrf_cc310_platform_0.9.17.a && cd /Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build/zephyr && /opt/homebrew/Cellar/cmake/3.26.3/bin/cmake -E echo /opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: app/libapp.a(main.c.obj): in function `main': /Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/src/main.c:35: undefined reference to `bno055_init' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed. FATAL ERROR: command exited with status 1: /opt/homebrew/bin/cmake --build /Users/my_user/Documents/Projects/nRFConnectSDK-ZephyrRTOS-Fundamentals/fund_less6_exer1_exer2/fund_less6_exer2/build
Bellow you can see the directory of main.c and the directory where the CMake download the bno055.c and bno55.h files:
fund_less6_exer1_exer2/fund_less6_exer2/build/external/BNO055_driver/src/BNO055_driver/bno055.h fund_less6_exer1_exer2/fund_less6_exer2/build/external/BNO055_driver/src/BNO055_driver/bno055.c fund_less6_exer1_exer2/fund_less6_exer2/src/main.c