Skip to content

kernel/device: Update to multi API device model #49374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions samples/multi_api_shell/sensor_shell/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(multi_api_shell)

target_sources(app PRIVATE src/main.c)
19 changes: 19 additions & 0 deletions samples/multi_api_shell/sensor_shell/app.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2022 Trackunit A/S
*
* SPDX-License-Identifier: Apache-2.0
*/

&i2c2 {
lis2dh@0b {
compatible = "st,lis2dh";
reg = <0x0b>;
label = "sensor2";
};

lis2dh@0c {
compatible = "bosch,bme280";
reg = <0x0c>;
label = "sensor3";
};
};
8 changes: 8 additions & 0 deletions samples/multi_api_shell/sensor_shell/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# nothing here
# Board b_u585i_iot02a

CONFIG_SENSOR=y
CONFIG_HTS221=y
CONFIG_LIS2DH=y
CONFIG_BME280=y
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is? where else is it defined?

CONFIG_LEGACY_DEVICE_MODEL=n
3 changes: 3 additions & 0 deletions samples/multi_api_shell/sensor_shell/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sample:
description: Simple dummy shell
name: Multi API sensor shell
79 changes: 79 additions & 0 deletions samples/multi_api_shell/sensor_shell/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2022 Trackunit A/S
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>

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



/*
* Note that DEVICE_DT_PROPERTY and DT_PROP use the same node so they
* can be mixed
*/
#ifdef CONFIG_LEGACY_DEVICE_MODEL

#define SENSOR_INFO_INITIALIZER(node_id) \
{ \
.dev = DEVICE_DT_GET(node_id), \
.vendor = DEVICE_DT_PROPERTY(node_id, vendor), \
.model = DEVICE_DT_PROPERTY(node_id, model), \
.friendly_name = DT_PROP_OR(node_id, label, "") \
},

#else

#define SENSOR_INFO_INITIALIZER(node_id) \
{ \
.dev = DEVICE_DT_API_GET(node_id, sensor), \
.vendor = DEVICE_DT_PROPERTY(node_id, vendor), \
.model = DEVICE_DT_PROPERTY(node_id, model), \
.friendly_name = DT_PROP_OR(node_id, label, "") \
},

#endif /* CONFIG_LEGACY_DEVICE_MODEL */

/*
* It is known at compile time if any sensor exists in the system, save
* memory by warning user to exclude the shell using KConfig.
*/
#if (DEVICE_DT_API_SUPPORTED_ANY(sensor) == 0)
#warning "Sensor shell is enabled without any sensor in system"
#endif

/*
* Every sensor with status OK in devicetree will be filled into this
* list automatically during compile time
*/
static struct sensor_info sensors[] =
{
DEVICE_DT_API_FOREACH(SENSOR_INFO_INITIALIZER, sensor)
};

/*
* Yes, this is not actually a shell, it is an example showing how to
* identify and include all sensors in the system.
*/
void main(void)
{
/* Print sensor info header */
printk("Sensor info:\n");
printk("Sensor count: %u\n\n", ARRAY_SIZE(sensors));

/* Print sensor info for each sensor */
for (int i = 0; i < ARRAY_SIZE(sensors); i++) {
printk("device: %u, vendor: %s, model: %s, friendly_name: %s\n",
(size_t)sensors[i].dev, sensors[i].vendor, sensors[i].model,
sensors[i].friendly_name);
}
}