How to configure includes of Zephyr Devicetree files via Kconfig or something similar

38 Views Asked by At

I am currently developing the Devicetree of my Board, but it has got 3 Extension ports with configureable GPIOs. Depending on the connected extension an io could be an I2C Port or just a gpio with input capabilities.

To configure this behaivour I tried including different devicetree (.dtsi) files. And depending on the boards connected the fitting firmware can be compiled. But I would like to do this via the graphical Kconfig Editor.

But if i try something like this:

board.dts

#if defined(CONFIG_IO_BOARD_XX)
    #include "ioboard-XX.dtsi"
#endif

main.c

#if defined(CONFIG_IO_BOARD_XX)
    #define LOCK_ALIAS DT_ALIAS(lock0)
    static const struct device* const gpio = DEVICE_DT_GET(LOCK_ALIAS);
#endif

I get an error message like that

In file included from C:/Users/ErFre/zephyrproject/zephyr/include/zephyr/toolchain/gcc.h:98,
                 from C:/Users/ErFre/zephyrproject/zephyr/include/zephyr/toolchain.h:50,
                 from C:/Users/ErFre/zephyrproject/zephyr/include/zephyr/kernel_includes.h:23,
                 from C:/Users/ErFre/zephyrproject/zephyr/include/zephyr/kernel.h:17,
                 from ../src/main.c:1:
C:/Users/ErFre/zephyrproject/zephyr/include/zephyr/device.h:89:41: error: '__device_dts_ord_DT_N_ALIAS_lock0_ORD' undeclared here (not in a function)
   89 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
      |                                         ^~~~~~~~~

This conversly means that the line #if defined(CONFIG_IO_BOARD_XX) worked in code, but not in the devicetree. I already read, that one shouldn't use Kconfig to specify fixed Hardware. But in this case the hardware isn't fixed. Options that specify fixed hardware configuration

Is there a way i can use Kconfig or some other central config file to configure my hardware and firmware state?

1

There are 1 best solutions below

2
On

As you have already seen, Kconfig cannot be used to help define the devicetree because the devicetree is processed by the build system before the Kconfig system is (so values from devicetree can actually help define the Kconfig system instead, not the other way around). So there's no way to use something like Kconfig unless you were to put your own config system around Zephyr's Kconfig (something like Zephyr's Sysbuild might work: https://docs.zephyrproject.org/latest/build/sysbuild/index.html)

Zephyr does provide some alternatives to achieve this. From your description, it sounds most like you what to adjust the configuration of your main board's extension header depending on the daughter board connected to it at compile time; this is typically done via shields where you define your daughter board via a boards/shields/<shield>/<shield>.overlay. You can then use a combination of pinctrl groups and a board-specific overlay for your shield in boards/shields/<shield>/boards/<your-board>/<your-board>.overlay to make sure the daughter board is connected to your board correctly. Then, you just need to specify the shield you are using via the command-line, which you can make cleaner via CMake, see more here: https://docs.zephyrproject.org/latest/hardware/porting/shields.html#shield-activation (if you definitely want to use Kconfig, it's possible you can use shields like above but pick the right shield/shield combinations with sysbuild from above)

Alternatively alternatively, you can explore doing the above with snippets instead but based on your description, it really sounds like the shield infrastructure most closely matches what you are trying to do.