Arm Mbed OS support forum

Mbed cli 2: adding new library

I am trying to add code as new library which uses mbed os objects, i.e. it has to include mbed-os.h. I am struggling with how to set up a proper CMakeList.txt in this library so that the lib gets smoothly build along with the application.

Following the (somehow very condensed) mbed cli 2 migration guide I have to

  1. Create a “folder” and “and adding it to git version control”
  2. create a reponame.lib in the application

I interpret item 1. as requirement of 2.: The new library code has to be stored somewhere in a clonable git repository. Item 2. means just referencing to this new git repository.

Questions:

Which changes are now supposed to be done to the CMakeLists.txt of the (a) application and (b) of the new library? I suppose the new library must have a CMakeLists.txt as well, correct?

Is the mbed configure .. supposed to clone the library, i.e. it evaluates the .lib or must the library be connected to the application by git submodules for checking out?

Maybe a stupid general question, but how to inherit the mbed-os and target include paths of the application in the library? Is there a standard way in CMake (I am not a CMake expert…)

I had some success… and now I have other obstacles. But first to the success. But at very first I’ll summarize what I try to do:
Try supporting this hardware:

  • Board: Adafruit Feather nRF52840 Express
  • Child board: Adafruit FeatherWing OLED - 128x64 OLED

As both boards are not natively supported by MBED I have to invest same hacking. In MBED speak I have to support

  • a custom target “TARGET_NRF52840_FEATHER”: I think I managed doing that using “custom_targets.json” and inheriting from MBED board “MCU_NRF52840” + adapting some pins.
  • The OLED FeatherWing is a little bit tricky as I have to add and port 2 libraries Adafruit_GFX (for general graphics routines) and Adafruit_SH110x (for supporting the OLED chip) which are implemented for Arduino. With some hacking I am confident to getting that done as well. Among other steps I added appropriate (and simple) CMakeLists.txt to both.

So the project directory contains these directories:

...
mbed-os/
Adafruit_GFX/
Adafruit_SH110x/
TARGET_NRF52840_FEATHER/
...

My obstacle now: The hacked Adafruit libraries must support some MBED objects “I2C” because I exchanged the existing Arduino I2C support with MBED’s. For supporting that, the libraries have to include “mbed.h”. “mbed.h” includes a huge lot of stuff, among it is the whole target. But it seems that only the new custom target directory/library “TARGET_NRF52840_FEATHER” gets all the required other NRFxxx include directories and macros and not the other libraries.

In the Adafruit_xxx library directories where “mbed.h” is also required all the target include directories and sadly all the inherited target macros from “MCU_NRF52840” are NOT DEFINED and even if manually adding the target include directories I am lacking all the macro definitions and therefore get a lot of compiler errors.

The TARGET_NRF52840_FEATHER CMakeList.txt looks like this:

add_library(mbed-nrf52840-feather INTERFACE)

target_include_directories(mbed-nrf52840-feather
    INTERFACE
        .
        ${MBED_PATH}/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52
)

target_link_libraries(mbed-nrf52840-feather INTERFACE mbed-mcu-nrf52840)   

Does anybody have an idea how I could make the custom target’s include directories and macros visible to the other libraries?

I figured it out: Libs in a project should not be treated as libraries, i.e. shall not be built as libraries and linked to the project as libraries. Instead, the source files of the libraries must be added as sources to the current project. So the solution in my case was adding the sources to the target_sources():

target_sources(${APP_TARGET}
    PRIVATE
        main.cpp
        Adafruit_GFX/Adafruit_GFX.cpp
        Adafruit_GFX/Adafruit_GrayOLED.cpp
        Adafruit_SH110x/Adafruit_SH110X.cpp
        Adafruit_SH110x/Adafruit_SH1107.cpp
)