Skip to content

Commit

Permalink
llext: export DT devices to extensions
Browse files Browse the repository at this point in the history
This change adds a new configuration option, LLEXT_EXPORT_DEVICES, which
enables exporting all devices defined in the device tree to llext
extensions. When enabled, all devices are made available to extensions
via the standard DT_ / DEVICE_* macros.

Signed-off-by: Luca Burelli <[email protected]>
  • Loading branch information
pillo79 committed Sep 23, 2024
1 parent 5aae393 commit b9b55a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions include/zephyr/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <zephyr/sys/util.h>
#include <zephyr/toolchain.h>

#ifdef CONFIG_LLEXT
#include <zephyr/llext/symbol.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -99,6 +103,11 @@ typedef int16_t device_handle_t;
*/
#define Z_DEVICE_DT_DEV_ID(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))

#if defined(CONFIG_LLEXT_EXPORT_DEVICES)
/* Export device identifiers using the builtin name */
#define Z_DEVICE_EXPORT(node_id) EXPORT_SYMBOL(DEVICE_DT_NAME_GET(node_id))
#endif

/**
* @brief Create a device object and set it up for boot time initialization.
*
Expand Down Expand Up @@ -171,8 +180,9 @@ typedef int16_t device_handle_t;
*
* The device is declared with extern visibility, so a pointer to a global
* device object can be obtained with `DEVICE_DT_GET(node_id)` from any source
* file that includes `<zephyr/device.h>`. Before using the pointer, the
* referenced object should be checked using device_is_ready().
* file that includes `<zephyr/device.h>` (even from extensions, when
* @kconfig{CONFIG_LLEXT_EXPORT_DEVICES} is enabled). Before using the
* pointer, the referenced object should be checked using device_is_ready().
*
* @param node_id The devicetree node identifier.
* @param init_fn Pointer to the device's initialization function, which will be
Expand All @@ -197,7 +207,8 @@ typedef int16_t device_handle_t;
DEVICE_DT_NAME(node_id), init_fn, pm, data, config, \
level, prio, api, \
&Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
__VA_ARGS__)
__VA_ARGS__) \
IF_ENABLED(CONFIG_LLEXT_EXPORT_DEVICES, (; Z_DEVICE_EXPORT(node_id))) \

/**
* @brief Like DEVICE_DT_DEFINE(), but uses an instance of a `DT_DRV_COMPAT`
Expand Down
6 changes: 6 additions & 0 deletions subsys/llext/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ config LLEXT_STORAGE_WRITABLE
Select if LLEXT storage is writable, i.e. if extensions are stored in
RAM and can be modified in place

config LLEXT_EXPORT_DEVICES
bool "Export all DT devices to llexts"
help
When enabled, all Zephyr devices defined in the device tree are
made available to llexts via the standard DT_ / DEVICE_* macros.

config LLEXT_EXPORT_BUILTINS_BY_SLID
bool "Export built-in symbols to llexts via SLIDs"
help
Expand Down

0 comments on commit b9b55a8

Please sign in to comment.