Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions doc/hardware/peripherals/sensor/accel_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <zephyr/drivers/sensor.h>

#define ACCEL_TRIGGERS \
{ SENSOR_TRIG_DRDY, SENSOR_STREAM_DATA_INCLUDE }, \
{ SENSOR_TRIG_TAP, SENSOR_STREAM_DATA_NOP }
#define ACCEL_TRIGGERS \
(SENSOR_TRIG_DRDY, SENSOR_STREAM_DATA_INCLUDE), (SENSOR_TRIG_TAP, SENSOR_STREAM_DATA_NOP)

#define ACCEL_ALIAS(id) DT_ALIAS(CONCAT(accel, id))
#define ACCEL_IODEV_SYM(id) CONCAT(accel_iodev, id)
#define ACCEL_IODEV_PTR(id) &ACCEL_IODEV_SYM(id)
Expand Down
4 changes: 1 addition & 3 deletions doc/hardware/peripherals/sensor/multiple_temp_polling.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

#include <zephyr/drivers/sensor.h>

#define TEMP_CHANNELS \
{ SENSOR_CHAN_AMBIENT_TEMP, 0 }, \
{ SENSOR_CHAN_AMBIENT_TEMP, 1 }
#define TEMP_CHANNELS (SENSOR_CHAN_AMBIENT_TEMP, 0), (SENSOR_CHAN_AMBIENT_TEMP, 1)
#define TEMP_ALIAS(id) DT_ALIAS(CONCAT(temp, id))
#define TEMP_IODEV_SYM(id) CONCAT(temp_iodev, id)
#define TEMP_IODEV_PTR(id) &TEMP_IODEV_SYM(id)
Expand Down
2 changes: 1 addition & 1 deletion doc/hardware/peripherals/sensor/temp_polling.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <zephyr/drivers/sensor.h>

#define TEMP_CHANNEL {SENSOR_CHAN_AMBIENT_TEMP, 0}
#define TEMP_CHANNEL (SENSOR_CHAN_AMBIENT_TEMP, 0)

const struct device *const temp0 = DEVICE_DT_GET(DT_ALIAS(temp0));

Expand Down
69 changes: 55 additions & 14 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,18 @@ struct sensor_chan_spec {
uint16_t chan_idx; /**< A sensor channel index */
};

/*
* Internal macro to generate SENSOR_CHAN_SPEC
*/
#define SENSOR_CHAN_SPEC(_chan_type, _idx) {.chan_type = (_chan_type), .chan_idx = (_idx)}

/*
* Each _item is a *parenthesized tuple*: (_chan_type, _idx)
* Unpack it and forward to SENSOR_CHAN_SPEC.
*/
#define _SENSOR_CHAN_SPEC_PREP(_chan_type, _idx) SENSOR_CHAN_SPEC(_chan_type, _idx)
#define SENSOR_CHAN_SPEC_PREP(_item) _SENSOR_CHAN_SPEC_PREP _item

/** @cond INTERNAL_HIDDEN */
/* Ensure sensor_chan_spec is sensibly sized to pass by value */
BUILD_ASSERT(sizeof(struct sensor_chan_spec) <= sizeof(uintptr_t),
Expand Down Expand Up @@ -631,15 +643,36 @@ enum sensor_stream_data_opt {
SENSOR_STREAM_DATA_DROP = 2,
};

/**
* @brief Sensor Stream Trigger
*
* A sensor stream trigger is a unique identifier per sensor device describing
* a trigger and the options for what to do with the associated data.
* It possible to select a sensor channel specification to apply the options on.
*
*/
struct sensor_stream_trigger {
enum sensor_trigger_type trigger;
enum sensor_stream_data_opt opt;
struct sensor_chan_spec chan_spec;
};

#define SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt) \
{ \
.trigger = (_trigger), .opt = (_opt), \
}
/*
* Internal macro to generate SENSOR_STREAM_TRIGGER
*/
#define SENSOR_STREAM_TRIGGER(_trigger, _opt, ...) \
{.trigger = (_trigger), \
.opt = (_opt), \
.chan_spec = COND_CODE_1(IS_EMPTY(__VA_ARGS__), ({ SENSOR_CHAN_ALL, 0 }), (__VA_ARGS__)) }

/*
* Each _item is a *parenthesized tuple*:
* (_trigger, _opt) or (_trigger, _opt, { chan, idx })
* Unpack it and forward to SENSOR_STREAM_TRIGGER.
*/
#define _SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt, ...) \
SENSOR_STREAM_TRIGGER(_trigger, _opt, __VA_ARGS__)
#define SENSOR_STREAM_TRIGGER_PREP(_item) _SENSOR_STREAM_TRIGGER_PREP _item

/*
* Internal data structure used to store information about the IODevice for async reading and
Expand All @@ -663,24 +696,28 @@ struct sensor_read_config {
*
* @code(.c)
* SENSOR_DT_READ_IODEV(icm42688_accelgyro, DT_NODELABEL(icm42688),
* { SENSOR_CHAN_ACCEL_XYZ, 0 },
* { SENSOR_CHAN_GYRO_XYZ, 0 });
* ( SENSOR_CHAN_ACCEL_XYZ, 0 ),
* ( SENSOR_CHAN_GYRO_XYZ, 0 ));
*
* int main(void) {
* sensor_read_async_mempool(&icm42688_accelgyro, &rtio);
* }
* @endcode
*/
#define SENSOR_DT_READ_IODEV(name, dt_node, ...) \
static struct sensor_chan_spec _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \
static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
/* clang-format off */
#define SENSOR_DT_READ_IODEV(name, dt_node, ...) \
static struct sensor_chan_spec _CONCAT(__channel_array_, name)[] = { \
FOR_EACH_NONEMPTY_TERM(SENSOR_CHAN_SPEC_PREP, (,), __VA_ARGS__) \
}; \
static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
.sensor = DEVICE_DT_GET(dt_node), \
.is_streaming = false, \
.channels = _CONCAT(__channel_array_, name), \
.count = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
.max = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
}; \
}; \
RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, _CONCAT(&__sensor_read_config_, name))
/* clang-format on */

/**
* @brief Define a stream instance of a sensor
Expand All @@ -701,16 +738,20 @@ struct sensor_read_config {
* }
* @endcode
*/
#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...) \
static struct sensor_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
/* clang-format off */
#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...) \
static struct sensor_stream_trigger _CONCAT(__trigger_array_, name)[] = { \
FOR_EACH_NONEMPTY_TERM(SENSOR_STREAM_TRIGGER_PREP, (,), __VA_ARGS__) \
}; \
static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
.sensor = DEVICE_DT_GET(dt_node), \
.is_streaming = true, \
.triggers = _CONCAT(__trigger_array_, name), \
.count = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
.max = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
}; \
}; \
RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, &_CONCAT(__sensor_read_config_, name))
/* clang-format on */

/* Used to submit an RTIO sqe to the sensor's iodev */
typedef void (*sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe);
Expand Down
6 changes: 3 additions & 3 deletions samples/sensor/accel_polling/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ static const struct device *const sensors[] = {LISTIFY(10, ACCELEROMETER_DEVICE,
#define ACCEL_IODEV_SYM(id) CONCAT(accel_iodev, id)
#define ACCEL_IODEV_PTR(id, _) &ACCEL_IODEV_SYM(id)

#define ACCEL_TRIGGERS \
{SENSOR_TRIG_FIFO_FULL, SENSOR_STREAM_DATA_INCLUDE}, \
{SENSOR_TRIG_FIFO_WATERMARK, SENSOR_STREAM_DATA_INCLUDE}
#define ACCEL_TRIGGERS \
(SENSOR_TRIG_FIFO_FULL, SENSOR_STREAM_DATA_INCLUDE), \
(SENSOR_TRIG_FIFO_WATERMARK, SENSOR_STREAM_DATA_INCLUDE)

#define ACCEL_DEFINE_IODEV(id, _) \
SENSOR_DT_STREAM_IODEV( \
Expand Down
5 changes: 2 additions & 3 deletions samples/sensor/bme280/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
const struct device *const dev = DEVICE_DT_GET_ANY(bosch_bme280);

SENSOR_DT_READ_IODEV(iodev, DT_COMPAT_GET_ANY_STATUS_OKAY(bosch_bme280),
{SENSOR_CHAN_AMBIENT_TEMP, 0},
{SENSOR_CHAN_HUMIDITY, 0},
{SENSOR_CHAN_PRESS, 0});
(SENSOR_CHAN_AMBIENT_TEMP, 0), (SENSOR_CHAN_HUMIDITY, 0),
(SENSOR_CHAN_PRESS, 0));

RTIO_DEFINE(ctx, 1, 1);

Expand Down
6 changes: 3 additions & 3 deletions samples/sensor/dht_polling/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
/* Support up to 10 temperature/humidity sensors */
static const struct device *const sensors[] = {LISTIFY(10, DHT_DEVICE, ())};

#define DHT_IODEV(i, _) \
#define DHT_IODEV(i, _) \
IF_ENABLED(DT_NODE_EXISTS(DHT_ALIAS(i)), \
(SENSOR_DT_READ_IODEV(_CONCAT(dht_iodev, i), DHT_ALIAS(i), \
{SENSOR_CHAN_AMBIENT_TEMP, 0}, \
{SENSOR_CHAN_HUMIDITY, 0})))
(SENSOR_CHAN_AMBIENT_TEMP, 0), \
(SENSOR_CHAN_HUMIDITY, 0))))

LISTIFY(10, DHT_IODEV, (;));

Expand Down
3 changes: 1 addition & 2 deletions samples/sensor/stream_drdy/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ static const struct device *const sensors[] = { LISTIFY(10, STREAMDEV_DEVICE, ()
#define STREAM_IODEV_SYM(id) CONCAT(accel_iodev, id)
#define STREAM_IODEV_PTR(id, _) &STREAM_IODEV_SYM(id)

#define STREAM_TRIGGERS \
{ SENSOR_TRIG_DATA_READY, SENSOR_STREAM_DATA_INCLUDE }
#define STREAM_TRIGGERS (SENSOR_TRIG_DATA_READY, SENSOR_STREAM_DATA_INCLUDE)

#define STREAM_DEFINE_IODEV(id, _) \
SENSOR_DT_STREAM_IODEV( \
Expand Down
6 changes: 3 additions & 3 deletions samples/sensor/stream_fifo/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ static const struct device *const sensors[] = { LISTIFY(10, STREAMDEV_DEVICE, ()
#define STREAM_IODEV_SYM(id) CONCAT(stream_iodev, id)
#define STREAM_IODEV_PTR(id, _) &STREAM_IODEV_SYM(id)

#define STREAM_TRIGGERS \
{ SENSOR_TRIG_FIFO_FULL, SENSOR_STREAM_DATA_NOP }, \
{ SENSOR_TRIG_FIFO_WATERMARK, SENSOR_STREAM_DATA_INCLUDE }
#define STREAM_TRIGGERS \
(SENSOR_TRIG_FIFO_FULL, SENSOR_STREAM_DATA_NOP), \
(SENSOR_TRIG_FIFO_WATERMARK, SENSOR_STREAM_DATA_INCLUDE)

#define STREAM_DEFINE_IODEV(id, _) \
SENSOR_DT_STREAM_IODEV( \
Expand Down