Skip to content

Commit

Permalink
drivers: sensor: Add sensor info iterable section
Browse files Browse the repository at this point in the history
Adds an iterable section in ROM to hold constant information, such as
vendor and model name, for all enabled sensor driver instances. This
will be used by the future sensor subsystem to enumerate all available
sensors in the system.

Signed-off-by: Maureen Helm <[email protected]>
  • Loading branch information
MaureenHelm authored and carlescufi committed Oct 31, 2022
1 parent 217528f commit eee3d8f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmake/linker_script/common/common-rom.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ if(CONFIG_SETTINGS)
zephyr_iterable_section(NAME settings_handler_static KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
endif()

if(CONFIG_SENSOR_INFO)
zephyr_iterable_section(NAME sensor_info KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
endif()

zephyr_iterable_section(NAME k_p4wq_initparam KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)

if(CONFIG_EMUL)
Expand Down
3 changes: 3 additions & 0 deletions drivers/sensor/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ config SENSOR_SHELL_BATTERY
in a convenient format. It makes use of a fuel gauge to read its
information.

config SENSOR_INFO
bool "Sensor Info iterable section"

comment "Device Drivers"

source "drivers/sensor/adt7420/Kconfig"
Expand Down
46 changes: 44 additions & 2 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,50 @@ static inline int sensor_value_from_double(struct sensor_value *val, double inp)
return 0;
}

#ifdef CONFIG_SENSOR_INFO

struct sensor_info {
const struct device *dev;
const char *vendor;
const char *model;
const char *friendly_name;
};

#define SENSOR_INFO_INITIALIZER(_dev, _vendor, _model, _friendly_name) \
{ \
.dev = _dev, \
.vendor = _vendor, \
.model = _model, \
.friendly_name = _friendly_name, \
}

#define SENSOR_INFO_DEFINE(name, ...) \
static const STRUCT_SECTION_ITERABLE(sensor_info, name) = \
SENSOR_INFO_INITIALIZER(__VA_ARGS__)

#define SENSOR_INFO_DT_NAME(node_id) \
_CONCAT(__sensor_info, DEVICE_DT_NAME_GET(node_id))

#define SENSOR_INFO_DT_DEFINE(node_id) \
SENSOR_INFO_DEFINE(SENSOR_INFO_DT_NAME(node_id), \
DEVICE_DT_GET(node_id), \
DT_NODE_VENDOR_OR(node_id, NULL), \
DT_NODE_MODEL_OR(node_id, NULL), \
DT_PROP_OR(node_id, friendly_name, NULL)) \

#else

#define SENSOR_INFO_DEFINE(name, ...)
#define SENSOR_INFO_DT_DEFINE(node_id)

#endif /* CONFIG_SENSOR_INFO */

/**
* @brief Like DEVICE_DT_DEFINE() with sensor specifics.
*
* @details Defines a device which implements the sensor API.
* @details Defines a device which implements the sensor API. May define an
* element in the sensor info iterable section used to enumerate all sensor
* devices.
*
* @param node_id The devicetree node identifier.
*
Expand All @@ -721,7 +761,9 @@ static inline int sensor_value_from_double(struct sensor_value *val, double inp)
api_ptr, ...) \
DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
data_ptr, cfg_ptr, level, prio, \
api_ptr, __VA_ARGS__);
api_ptr, __VA_ARGS__); \
\
SENSOR_INFO_DT_DEFINE(node_id);

/**
* @brief Like SENSOR_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT
Expand Down
4 changes: 4 additions & 0 deletions include/zephyr/linker/common-rom/common-rom-misc.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
ITERABLE_SECTION_ROM(settings_handler_static, 4)
#endif

#if defined(CONFIG_SENSOR_INFO)
ITERABLE_SECTION_ROM(sensor_info, 4)
#endif

#if defined(CONFIG_EMUL)
SECTION_DATA_PROLOGUE(emulators_section,,)
{
Expand Down

0 comments on commit eee3d8f

Please sign in to comment.