diff --git a/doc/hardware/peripherals/sensor/accel_stream.c b/doc/hardware/peripherals/sensor/accel_stream.c index aac2e99318b5d..8445304009ead 100644 --- a/doc/hardware/peripherals/sensor/accel_stream.c +++ b/doc/hardware/peripherals/sensor/accel_stream.c @@ -6,9 +6,9 @@ #include -#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) diff --git a/doc/hardware/peripherals/sensor/multiple_temp_polling.c b/doc/hardware/peripherals/sensor/multiple_temp_polling.c index cdcb43d8b569c..724a8d3b39e89 100644 --- a/doc/hardware/peripherals/sensor/multiple_temp_polling.c +++ b/doc/hardware/peripherals/sensor/multiple_temp_polling.c @@ -6,9 +6,7 @@ #include -#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) diff --git a/doc/hardware/peripherals/sensor/temp_polling.c b/doc/hardware/peripherals/sensor/temp_polling.c index 8c61a41f14690..98cb8437a8f5f 100644 --- a/doc/hardware/peripherals/sensor/temp_polling.c +++ b/doc/hardware/peripherals/sensor/temp_polling.c @@ -6,7 +6,7 @@ #include -#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)); diff --git a/include/zephyr/drivers/sensor.h b/include/zephyr/drivers/sensor.h index 2e92f100542f4..1192a1ec6355b 100644 --- a/include/zephyr/drivers/sensor.h +++ b/include/zephyr/drivers/sensor.h @@ -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), @@ -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 @@ -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 @@ -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); diff --git a/samples/sensor/accel_polling/src/main.c b/samples/sensor/accel_polling/src/main.c index cdcc9bc55613b..1bbc8800fb96d 100644 --- a/samples/sensor/accel_polling/src/main.c +++ b/samples/sensor/accel_polling/src/main.c @@ -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( \ diff --git a/samples/sensor/bme280/src/main.c b/samples/sensor/bme280/src/main.c index 433339c88055c..707428da63a38 100644 --- a/samples/sensor/bme280/src/main.c +++ b/samples/sensor/bme280/src/main.c @@ -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); diff --git a/samples/sensor/dht_polling/src/main.c b/samples/sensor/dht_polling/src/main.c index fcbe0ca929523..c18cc2d4d3149 100644 --- a/samples/sensor/dht_polling/src/main.c +++ b/samples/sensor/dht_polling/src/main.c @@ -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, (;)); diff --git a/samples/sensor/stream_drdy/src/main.c b/samples/sensor/stream_drdy/src/main.c index d46048981f589..7253c5209e042 100644 --- a/samples/sensor/stream_drdy/src/main.c +++ b/samples/sensor/stream_drdy/src/main.c @@ -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( \ diff --git a/samples/sensor/stream_fifo/src/main.c b/samples/sensor/stream_fifo/src/main.c index 45dd3cab8f4f7..4ccc16dff9b80 100644 --- a/samples/sensor/stream_fifo/src/main.c +++ b/samples/sensor/stream_fifo/src/main.c @@ -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( \