From 01c66ec90d0ac23028a4435d10b062d4247750ed Mon Sep 17 00:00:00 2001 From: Tom Burdick Date: Sat, 6 Apr 2024 09:29:28 -0500 Subject: [PATCH] sensors: Add channel specifier Use a structured channel specifier rather than a single enum when specifying channels to read in the new read/decoder API. Replaces usages of a seperate channel and channel_index parameter where previously used with a struct sensor_chan_spec. Signed-off-by: Tom Burdick --- .../asahi_kasei/akm09918c/akm09918c_decoder.c | 18 ++- drivers/sensor/bosch/bma4xx/bma4xx.c | 8 +- drivers/sensor/default_rtio_sensor.c | 103 +++++++++--------- drivers/sensor/sensor_shell.c | 20 ++-- .../sensor/tdk/icm42688/icm42688_decoder.c | 47 ++++---- .../sensor/tdk/icm42688/icm42688_decoder.h | 2 +- drivers/sensor/tdk/icm42688/icm42688_rtio.c | 2 +- include/zephyr/drivers/sensor.h | 64 +++++++---- .../build_all/sensor/src/generic_test.c | 21 ++-- 9 files changed, 154 insertions(+), 131 deletions(-) diff --git a/drivers/sensor/asahi_kasei/akm09918c/akm09918c_decoder.c b/drivers/sensor/asahi_kasei/akm09918c/akm09918c_decoder.c index 9bd6b312b7a0e0..c05654f51670ed 100644 --- a/drivers/sensor/asahi_kasei/akm09918c/akm09918c_decoder.c +++ b/drivers/sensor/asahi_kasei/akm09918c/akm09918c_decoder.c @@ -5,22 +5,21 @@ #include "akm09918c.h" -static int akm09918c_decoder_get_frame_count(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint16_t *frame_count) +static int akm09918c_decoder_get_frame_count(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint16_t *frame_count) { ARG_UNUSED(buffer); - ARG_UNUSED(channel); - ARG_UNUSED(channel_idx); + ARG_UNUSED(chan_spec); /* This sensor lacks a FIFO; there will always only be one frame at a time. */ *frame_count = 1; return 0; } -static int akm09918c_decoder_get_size_info(enum sensor_channel channel, size_t *base_size, +static int akm09918c_decoder_get_size_info(struct sensor_chan_spec chan_spec, size_t *base_size, size_t *frame_size) { - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_MAGN_X: case SENSOR_CHAN_MAGN_Y: case SENSOR_CHAN_MAGN_Z: @@ -48,9 +47,8 @@ static int akm09918c_convert_raw_to_q31(int16_t reading, q31_t *out) return 0; } -static int akm09918c_decoder_decode(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint32_t *fit, - uint16_t max_count, void *data_out) +static int akm09918c_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint32_t *fit, uint16_t max_count, void *data_out) { const struct akm09918c_encoded_data *edata = (const struct akm09918c_encoded_data *)buffer; @@ -58,7 +56,7 @@ static int akm09918c_decoder_decode(const uint8_t *buffer, enum sensor_channel c return 0; } - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_MAGN_X: case SENSOR_CHAN_MAGN_Y: case SENSOR_CHAN_MAGN_Z: diff --git a/drivers/sensor/bosch/bma4xx/bma4xx.c b/drivers/sensor/bosch/bma4xx/bma4xx.c index 9d8656d67aa808..bd7f38f1071d48 100644 --- a/drivers/sensor/bosch/bma4xx/bma4xx.c +++ b/drivers/sensor/bosch/bma4xx/bma4xx.c @@ -343,7 +343,7 @@ static int bma4xx_submit_one_shot(const struct device *dev, struct rtio_iodev_sq struct bma4xx_data *bma4xx = dev->data; const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data; - const enum sensor_channel *const channels = cfg->channels; + const struct sensor_chan_spec *const channels = cfg->channels; const size_t num_channels = cfg->count; uint32_t min_buf_len = sizeof(struct bma4xx_encoded_data); @@ -370,7 +370,11 @@ static int bma4xx_submit_one_shot(const struct device *dev, struct rtio_iodev_sq /* Determine what channels we need to fetch */ for (int i = 0; i < num_channels; i++) { - switch (channels[i]) { + if (channels[i].chan_idx != 0) { + LOG_ERR("Only channel index 0 supported"); + return -ENOTSUP; + } + switch (channels[i].chan_type) { case SENSOR_CHAN_ALL: edata->has_accel = 1; #ifdef CONFIG_BMA4XX_TEMPERATURE diff --git a/drivers/sensor/default_rtio_sensor.c b/drivers/sensor/default_rtio_sensor.c index 3f9377c33b3591..683a2e1d95442d 100644 --- a/drivers/sensor/default_rtio_sensor.c +++ b/drivers/sensor/default_rtio_sensor.c @@ -46,12 +46,13 @@ const struct rtio_iodev_api __sensor_iodev_api = { * @param[in] num_channels Number of channels on the @p channels array * @return The number of samples required to read the given channels */ -static inline int compute_num_samples(const enum sensor_channel *channels, size_t num_channels) +static inline int compute_num_samples(const struct sensor_chan_spec *const channels, + size_t num_channels) { int num_samples = 0; for (size_t i = 0; i < num_channels; ++i) { - num_samples += SENSOR_CHANNEL_3_AXIS(channels[i]) ? 3 : 1; + num_samples += SENSOR_CHANNEL_3_AXIS(channels[i].chan_type) ? 3 : 1; } return num_samples; @@ -92,12 +93,12 @@ static inline uint32_t compute_min_buf_len(int num_output_samples) * @return Index of the @p channel if found or negative if not found */ static inline int check_header_contains_channel(const struct sensor_data_generic_header *header, - enum sensor_channel channel, int num_channels) + struct sensor_chan_spec chan_spec, int num_channels) { __ASSERT_NO_MSG(!SENSOR_CHANNEL_3_AXIS(channel)); for (int i = 0; i < num_channels; ++i) { - if (header->channels[i] == channel) { + if (sensor_chan_spec_eq(header->channels[i], chan_spec)) { return i; } } @@ -113,7 +114,7 @@ static inline int check_header_contains_channel(const struct sensor_data_generic static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe) { const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data; - const enum sensor_channel *const channels = cfg->channels; + const struct sensor_chan_spec *const channels = cfg->channels; const int num_output_samples = compute_num_samples(channels, cfg->count); uint32_t min_buf_len = compute_min_buf_len(num_output_samples); uint64_t timestamp_ns = k_ticks_to_ns_floor64(k_uptime_ticks()); @@ -148,24 +149,34 @@ static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_s /* Populate values, update shift, and set channels */ for (size_t i = 0, sample_idx = 0; i < cfg->count; ++i) { struct sensor_value value[3]; - const int num_samples = SENSOR_CHANNEL_3_AXIS(channels[i]) ? 3 : 1; + const int num_samples = SENSOR_CHANNEL_3_AXIS(channels[i].chan_type) ? 3 : 1; /* Get the current channel requested by the user */ - rc = sensor_channel_get(dev, channels[i], value); + rc = sensor_channel_get(dev, channels[i].chan_type, value); if (num_samples == 3) { - header->channels[sample_idx++] = - rc == 0 ? channels[i] - 3 : SENSOR_CHAN_MAX; - header->channels[sample_idx++] = - rc == 0 ? channels[i] - 2 : SENSOR_CHAN_MAX; - header->channels[sample_idx++] = - rc == 0 ? channels[i] - 1 : SENSOR_CHAN_MAX; + header->channels[sample_idx++] = (struct sensor_chan_spec) { + rc == 0 ? channels[i].chan_type - 3 : SENSOR_CHAN_MAX, + 0 + }; + header->channels[sample_idx++] = (struct sensor_chan_spec) { + rc == 0 ? channels[i].chan_type - 2 : SENSOR_CHAN_MAX, + 0 + }; + header->channels[sample_idx++] = (struct sensor_chan_spec) { + rc == 0 ? channels[i].chan_type - 1 : SENSOR_CHAN_MAX, + 0 + }; } else { - header->channels[sample_idx++] = rc == 0 ? channels[i] : SENSOR_CHAN_MAX; + header->channels[sample_idx++] = (struct sensor_chan_spec) { + rc == 0 ? channels[i].chan_type : SENSOR_CHAN_MAX, + 0 + }; } if (rc != 0) { - LOG_DBG("Failed to get channel %d, skipping", channels[i]); + LOG_DBG("Failed to get channel (type: %d, index %d), skipping", + channels[i].chan_type, channels[i].chan_idx); continue; } @@ -277,45 +288,40 @@ void sensor_processing_with_callback(struct rtio *ctx, sensor_processing_callbac * @param[out] frame_count The number of frames in the buffer (always 1) * @return 0 in all cases */ -static int get_frame_count(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx, - uint16_t *frame_count) +static int get_frame_count(const uint8_t *buffer, struct sensor_chan_spec channel, + uint16_t *frame_count) { struct sensor_data_generic_header *header = (struct sensor_data_generic_header *)buffer; - size_t count = 0; - - switch (channel) { + switch (channel.chan_type) { case SENSOR_CHAN_ACCEL_XYZ: - channel = SENSOR_CHAN_ACCEL_X; + channel.chan_type = SENSOR_CHAN_ACCEL_X; break; case SENSOR_CHAN_GYRO_XYZ: - channel = SENSOR_CHAN_GYRO_X; + channel.chan_type = SENSOR_CHAN_GYRO_X; break; case SENSOR_CHAN_MAGN_XYZ: - channel = SENSOR_CHAN_MAGN_X; + channel.chan_type = SENSOR_CHAN_MAGN_X; break; default: break; } for (size_t i = 0; i < header->num_channels; ++i) { - if (header->channels[i] == channel) { - if (channel_idx == count) { - *frame_count = 1; - return 0; - } - ++count; + if (sensor_chan_spec_eq(header->channels[i], channel)) { + *frame_count = 1; + return 0; } } return -ENOTSUP; } -int sensor_natively_supported_channel_size_info(enum sensor_channel channel, size_t *base_size, +int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size) { __ASSERT_NO_MSG(base_size != NULL); __ASSERT_NO_MSG(frame_size != NULL); - switch (channel) { + switch (channel.chan_type) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: @@ -391,19 +397,13 @@ int sensor_natively_supported_channel_size_info(enum sensor_channel channel, siz } static int get_q31_value(const struct sensor_data_generic_header *header, const q31_t *values, - enum sensor_channel channel, size_t channel_idx, q31_t *out) + struct sensor_chan_spec chan_spec, q31_t *out) { - size_t count = 0; - for (size_t i = 0; i < header->num_channels; ++i) { - if (channel != header->channels[i]) { - continue; - } - if (count == channel_idx) { + if (sensor_chan_spec_eq(chan_spec, header->channels[i])) { *out = values[i]; return 0; } - ++count; } return -EINVAL; } @@ -419,15 +419,15 @@ static int decode_three_axis(const struct sensor_data_generic_header *header, co data_out->shift = header->shift; data_out->readings[0].timestamp_delta = 0; - rc = get_q31_value(header, values, x, channel_idx, &data_out->readings[0].values[0]); + rc = get_q31_value(header, values, (struct sensor_chan_spec){x, channel_idx}, &data_out->readings[0].values[0]); if (rc < 0) { return rc; } - rc = get_q31_value(header, values, y, channel_idx, &data_out->readings[0].values[1]); + rc = get_q31_value(header, values, (struct sensor_chan_spec){y, channel_idx}, &data_out->readings[0].values[1]); if (rc < 0) { return rc; } - rc = get_q31_value(header, values, z, channel_idx, &data_out->readings[0].values[2]); + rc = get_q31_value(header, values, (struct sensor_chan_spec){z, channel_idx}, &data_out->readings[0].values[2]); if (rc < 0) { return rc; } @@ -435,8 +435,7 @@ static int decode_three_axis(const struct sensor_data_generic_header *header, co } static int decode_q31(const struct sensor_data_generic_header *header, const q31_t *values, - struct sensor_q31_data *data_out, enum sensor_channel channel, - size_t channel_idx) + struct sensor_q31_data *data_out, struct sensor_chan_spec chan_spec) { int rc; @@ -445,7 +444,7 @@ static int decode_q31(const struct sensor_data_generic_header *header, const q31 data_out->shift = header->shift; data_out->readings[0].timestamp_delta = 0; - rc = get_q31_value(header, values, channel, channel_idx, &data_out->readings[0].value); + rc = get_q31_value(header, values, chan_spec, &data_out->readings[0].value); if (rc < 0) { return rc; } @@ -469,7 +468,7 @@ static int decode_q31(const struct sensor_data_generic_header *header, const q31 * @return >0 the number of decoded frames * @return <0 on error */ -static int decode(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx, +static int decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec, uint32_t *fit, uint16_t max_count, void *data_out) { const struct sensor_data_generic_header *header = @@ -484,33 +483,33 @@ static int decode(const uint8_t *buffer, enum sensor_channel channel, size_t cha } /* Check for 3d channel mappings */ - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: case SENSOR_CHAN_ACCEL_XYZ: count = decode_three_axis(header, q, data_out, SENSOR_CHAN_ACCEL_X, - SENSOR_CHAN_ACCEL_Y, SENSOR_CHAN_ACCEL_Z, channel_idx); + SENSOR_CHAN_ACCEL_Y, SENSOR_CHAN_ACCEL_Z, chan_spec.chan_idx); break; case SENSOR_CHAN_GYRO_X: case SENSOR_CHAN_GYRO_Y: case SENSOR_CHAN_GYRO_Z: case SENSOR_CHAN_GYRO_XYZ: count = decode_three_axis(header, q, data_out, SENSOR_CHAN_GYRO_X, - SENSOR_CHAN_GYRO_Y, SENSOR_CHAN_GYRO_Z, channel_idx); + SENSOR_CHAN_GYRO_Y, SENSOR_CHAN_GYRO_Z, chan_spec.chan_idx); break; case SENSOR_CHAN_MAGN_X: case SENSOR_CHAN_MAGN_Y: case SENSOR_CHAN_MAGN_Z: case SENSOR_CHAN_MAGN_XYZ: count = decode_three_axis(header, q, data_out, SENSOR_CHAN_MAGN_X, - SENSOR_CHAN_MAGN_Y, SENSOR_CHAN_MAGN_Z, channel_idx); + SENSOR_CHAN_MAGN_Y, SENSOR_CHAN_MAGN_Z, chan_spec.chan_idx); break; case SENSOR_CHAN_POS_DX: case SENSOR_CHAN_POS_DY: case SENSOR_CHAN_POS_DZ: count = decode_three_axis(header, q, data_out, SENSOR_CHAN_POS_DX, - SENSOR_CHAN_POS_DY, SENSOR_CHAN_POS_DZ, channel_idx); + SENSOR_CHAN_POS_DY, SENSOR_CHAN_POS_DZ, chan_spec.chan_idx); break; case SENSOR_CHAN_DIE_TEMP: case SENSOR_CHAN_AMBIENT_TEMP: @@ -552,7 +551,7 @@ static int decode(const uint8_t *buffer, enum sensor_channel channel, size_t cha case SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE: case SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE: case SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT: - count = decode_q31(header, q, data_out, channel, channel_idx); + count = decode_q31(header, q, data_out, chan_spec); break; default: break; diff --git a/drivers/sensor/sensor_shell.c b/drivers/sensor/sensor_shell.c index 758e658816a948..e3c6051b5d0f64 100644 --- a/drivers/sensor/sensor_shell.c +++ b/drivers/sensor/sensor_shell.c @@ -217,7 +217,7 @@ static enum dynamic_command_context current_cmd_ctx = NONE; K_MUTEX_DEFINE(cmd_get_mutex); /* Crate a single common config for one-shot reading */ -static enum sensor_channel iodev_sensor_shell_channels[SENSOR_CHAN_ALL]; +static struct sensor_chan_spec iodev_sensor_shell_channels[SENSOR_CHAN_ALL]; static struct sensor_read_config iodev_sensor_shell_read_config = { .sensor = NULL, .is_streaming = false, @@ -346,7 +346,9 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len continue; } - rc = decoder->get_size_info(channel, &base_size, &frame_size); + struct sensor_chan_spec chan_spec = { channel, 0 }; + + rc = decoder->get_size_info(chan_spec, &base_size, &frame_size); if (rc != 0) { /* Channel not supported, skipping */ continue; @@ -360,12 +362,10 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len continue; } - while (decoder->get_frame_count(buf, channel, channel_idx, &frame_count) == 0) { + while (decoder->get_frame_count(buf, chan_spec, &frame_count) == 0) { fit = 0; memset(&accumulator_buffer, 0, sizeof(accumulator_buffer)); - while (decoder->decode(buf, channel, channel_idx, &fit, 1, decoded_buffer) > - 0) { - + while (decoder->decode(buf, chan_spec, &fit, 1, decoded_buffer) > 0) { switch (channel) { case SENSOR_CHAN_ACCEL_XYZ: case SENSOR_CHAN_GYRO_XYZ: @@ -521,12 +521,12 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[]) } if (argc == 2) { - /* read all channels */ + /* read all channel types */ for (int i = 0; i < ARRAY_SIZE(iodev_sensor_shell_channels); ++i) { if (SENSOR_CHANNEL_3_AXIS(i)) { continue; } - iodev_sensor_shell_channels[count++] = i; + iodev_sensor_shell_channels[count++] = (struct sensor_chan_spec){i, 0}; } } else { /* read specific channels */ @@ -538,7 +538,8 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[]) shell_error(sh, "Failed to read channel (%s)", argv[i]); continue; } - iodev_sensor_shell_channels[count++] = chan; + iodev_sensor_shell_channels[count++] = + (struct sensor_chan_spec){chan, 0}; } } @@ -570,7 +571,6 @@ static int cmd_get_sensor(const struct shell *sh, size_t argc, char *argv[]) return 0; } - static int cmd_sensor_attr_set(const struct shell *shell_ptr, size_t argc, char *argv[]) { const struct device *dev; diff --git a/drivers/sensor/tdk/icm42688/icm42688_decoder.c b/drivers/sensor/tdk/icm42688/icm42688_decoder.c index 3dc556ac61c7c3..d9f32421c15808 100644 --- a/drivers/sensor/tdk/icm42688/icm42688_decoder.c +++ b/drivers/sensor/tdk/icm42688/icm42688_decoder.c @@ -178,7 +178,7 @@ static uint8_t icm42688_encode_channel(enum sensor_channel chan) return encode_bmask; } -int icm42688_encode(const struct device *dev, const enum sensor_channel *const channels, +int icm42688_encode(const struct device *dev, const struct sensor_chan_spec *const channels, const size_t num_channels, uint8_t *buf) { struct icm42688_dev_data *data = dev->data; @@ -187,7 +187,7 @@ int icm42688_encode(const struct device *dev, const enum sensor_channel *const c edata->channels = 0; for (int i = 0; i < num_channels; i++) { - edata->channels |= icm42688_encode_channel(channels[i]); + edata->channels |= icm42688_encode_channel(channels[i].chan_type); } edata->header.is_fifo = false; @@ -345,9 +345,8 @@ static uint32_t gyro_period_ns[] = { [ICM42688_GYRO_ODR_32000] = UINT32_C(1000000) / 32, }; -static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint32_t *fit, uint16_t max_count, - void *data_out) +static int icm42688_fifo_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint32_t *fit, uint16_t max_count, void *data_out) { const struct icm42688_fifo_data *edata = (const struct icm42688_fifo_data *)buffer; const uint8_t *buffer_end = buffer + sizeof(struct icm42688_fifo_data) + edata->fifo_count; @@ -356,7 +355,7 @@ static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel chann int count = 0; int rc; - if ((uintptr_t)buffer_end <= *fit || channel_idx != 0) { + if ((uintptr_t)buffer_end <= *fit || chan_spec.chan_idx != 0) { return 0; } @@ -388,7 +387,7 @@ static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel chann buffer = frame_end; continue; } - if (channel == SENSOR_CHAN_DIE_TEMP) { + if (chan_spec.chan_type == SENSOR_CHAN_DIE_TEMP) { struct sensor_q31_data *data = (struct sensor_q31_data *)data_out; data->shift = 9; @@ -401,7 +400,7 @@ static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel chann } data->readings[count].temperature = icm42688_read_temperature_from_packet(buffer); - } else if (IS_ACCEL(channel) && has_accel) { + } else if (IS_ACCEL(chan_spec.chan_type) && has_accel) { /* Decode accel */ struct sensor_three_axis_data *data = (struct sensor_three_axis_data *)data_out; @@ -422,7 +421,7 @@ static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel chann buffer = frame_end; continue; } - } else if (IS_GYRO(channel) && has_gyro) { + } else if (IS_GYRO(chan_spec.chan_type) && has_gyro) { /* Decode gyro */ struct sensor_three_axis_data *data = (struct sensor_three_axis_data *)data_out; @@ -451,9 +450,8 @@ static int icm42688_fifo_decode(const uint8_t *buffer, enum sensor_channel chann return count; } -static int icm42688_one_shot_decode(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint32_t *fit, uint16_t max_count, - void *data_out) +static int icm42688_one_shot_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint32_t *fit, uint16_t max_count, void *data_out) { const struct icm42688_encoded_data *edata = (const struct icm42688_encoded_data *)buffer; const struct icm42688_decoder_header *header = &edata->header; @@ -467,11 +465,11 @@ static int icm42688_one_shot_decode(const uint8_t *buffer, enum sensor_channel c if (*fit != 0) { return 0; } - if (max_count == 0 || channel_idx != 0) { + if (max_count == 0 || chan_spec.chan_idx != 0) { return -EINVAL; } - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: @@ -570,31 +568,30 @@ static int icm42688_one_shot_decode(const uint8_t *buffer, enum sensor_channel c } } -static int icm42688_decoder_decode(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint32_t *fit, uint16_t max_count, - void *data_out) +static int icm42688_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint32_t *fit, uint16_t max_count, void *data_out) { const struct icm42688_decoder_header *header = (const struct icm42688_decoder_header *)buffer; if (header->is_fifo) { - return icm42688_fifo_decode(buffer, channel, channel_idx, fit, max_count, data_out); + return icm42688_fifo_decode(buffer, chan_spec, fit, max_count, data_out); } - return icm42688_one_shot_decode(buffer, channel, channel_idx, fit, max_count, data_out); + return icm42688_one_shot_decode(buffer, chan_spec, fit, max_count, data_out); } -static int icm42688_decoder_get_frame_count(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint16_t *frame_count) +static int icm42688_decoder_get_frame_count(const uint8_t *buffer, struct sensor_chan_spec chan_spec, + uint16_t *frame_count) { const struct icm42688_fifo_data *data = (const struct icm42688_fifo_data *)buffer; const struct icm42688_decoder_header *header = &data->header; - if (channel_idx != 0) { + if (chan_spec.chan_idx != 0) { return -ENOTSUP; } if (!header->is_fifo) { - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: @@ -643,10 +640,10 @@ static int icm42688_decoder_get_frame_count(const uint8_t *buffer, enum sensor_c return 0; } -static int icm42688_decoder_get_size_info(enum sensor_channel channel, size_t *base_size, +static int icm42688_decoder_get_size_info(struct sensor_chan_spec chan_spec, size_t *base_size, size_t *frame_size) { - switch (channel) { + switch (chan_spec.chan_type) { case SENSOR_CHAN_ACCEL_X: case SENSOR_CHAN_ACCEL_Y: case SENSOR_CHAN_ACCEL_Z: diff --git a/drivers/sensor/tdk/icm42688/icm42688_decoder.h b/drivers/sensor/tdk/icm42688/icm42688_decoder.h index 499cf3d0801554..9eadcbcecad574 100644 --- a/drivers/sensor/tdk/icm42688/icm42688_decoder.h +++ b/drivers/sensor/tdk/icm42688/icm42688_decoder.h @@ -36,7 +36,7 @@ struct icm42688_encoded_data { int16_t readings[7]; }; -int icm42688_encode(const struct device *dev, const enum sensor_channel *const channels, +int icm42688_encode(const struct device *dev, const struct sensor_chan_spec *const channels, const size_t num_channels, uint8_t *buf); int icm42688_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder); diff --git a/drivers/sensor/tdk/icm42688/icm42688_rtio.c b/drivers/sensor/tdk/icm42688/icm42688_rtio.c index ccb9532ee45a2c..ad00fd7abf25ad 100644 --- a/drivers/sensor/tdk/icm42688/icm42688_rtio.c +++ b/drivers/sensor/tdk/icm42688/icm42688_rtio.c @@ -46,7 +46,7 @@ static int icm42688_rtio_sample_fetch(const struct device *dev, int16_t readings static int icm42688_submit_one_shot(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe) { const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data; - const enum sensor_channel *const channels = cfg->channels; + const struct sensor_chan_spec *const channels = cfg->channels; const size_t num_channels = cfg->count; uint32_t min_buf_len = sizeof(struct icm42688_encoded_data); int rc; diff --git a/include/zephyr/drivers/sensor.h b/include/zephyr/drivers/sensor.h index f110a47989648a..ba76b51670c065 100644 --- a/include/zephyr/drivers/sensor.h +++ b/include/zephyr/drivers/sensor.h @@ -418,6 +418,32 @@ typedef int (*sensor_channel_get_t)(const struct device *dev, enum sensor_channel chan, struct sensor_value *val); +/** + * @brief Sensor Channel Specification + * + * A sensor channel specification is a unique identifier per sensor device describing + * a measurement channel. + */ +struct sensor_chan_spec { + uint16_t chan_type; /**< A sensor channel type */ + uint16_t chan_idx; /**< A sensor channel index */ +}; + +/** + * @brief Check if channel specs are equavalent + * + * @param chan_spec0 First chan spec + * @param chan_spec1 Second chan spec + * @retval true If equivalent + * @retval false If not equivalent + */ + static inline bool sensor_chan_spec_eq(struct sensor_chan_spec chan_spec0, + struct sensor_chan_spec chan_spec1) + { + return chan_spec0.chan_type == chan_spec1.chan_type && + chan_spec0.chan_idx == chan_spec1.chan_idx; + } + /** * @brief Decodes a single raw data buffer * @@ -430,13 +456,12 @@ struct sensor_decoder_api { * * @param[in] buffer The buffer provided on the @ref rtio context. * @param[in] channel The channel to get the count for - * @param[in] channel_idx The index of the channel * @param[out] frame_count The number of frames on the buffer (at least 1) * @return 0 on success * @return -ENOTSUP if the channel/channel_idx aren't found */ - int (*get_frame_count)(const uint8_t *buffer, enum sensor_channel channel, - size_t channel_idx, uint16_t *frame_count); + int (*get_frame_count)(const uint8_t *buffer, struct sensor_chan_spec channel, + uint16_t *frame_count); /** * @brief Get the size required to decode a given channel @@ -450,7 +475,8 @@ struct sensor_decoder_api { * @return 0 on success * @return -ENOTSUP if the channel is not supported */ - int (*get_size_info)(enum sensor_channel channel, size_t *base_size, size_t *frame_size); + int (*get_size_info)(struct sensor_chan_spec channel, size_t *base_size, + size_t *frame_size); /** * @brief Decode up to @p max_count samples from the buffer @@ -470,7 +496,6 @@ struct sensor_decoder_api { * * @param[in] buffer The buffer provided on the @ref rtio context * @param[in] channel The channel to decode - * @param[in] channel_idx The index of the channel * @param[in,out] fit The current frame iterator * @param[in] max_count The maximum number of channels to decode. * @param[out] data_out The decoded data @@ -478,8 +503,8 @@ struct sensor_decoder_api { * @return >0 the number of decoded frames * @return <0 on error */ - int (*decode)(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx, - uint32_t *fit, uint16_t max_count, void *data_out); + int (*decode)(const uint8_t *buffer, struct sensor_chan_spec channel, uint32_t *fit, + uint16_t max_count, void *data_out); /** * @brief Check if the given trigger type is present @@ -518,20 +543,18 @@ struct sensor_decoder_api { struct sensor_decode_context { const struct sensor_decoder_api *decoder; const uint8_t *buffer; - enum sensor_channel channel; - size_t channel_idx; + struct sensor_chan_spec channel; uint32_t fit; }; /** * @brief Initialize a sensor_decode_context */ -#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_, channel_index_) \ +#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_type_, channel_index_) \ { \ .decoder = (decoder_), \ .buffer = (buffer_), \ - .channel = (channel_), \ - .channel_idx = (channel_index_), \ + .channel = {.chan_type = (channel_type_), .chan_idx = (channel_index_)} \ .fit = 0, \ } @@ -545,11 +568,10 @@ struct sensor_decode_context { */ static inline int sensor_decode(struct sensor_decode_context *ctx, void *out, uint16_t max_count) { - return ctx->decoder->decode(ctx->buffer, ctx->channel, ctx->channel_idx, &ctx->fit, - max_count, out); + return ctx->decoder->decode(ctx->buffer, ctx->channel, &ctx->fit, max_count, out); } -int sensor_natively_supported_channel_size_info(enum sensor_channel channel, size_t *base_size, +int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size); /** @@ -582,6 +604,7 @@ struct sensor_stream_trigger { { \ .trigger = (_trigger), .opt = (_opt), \ } + /* * Internal data structure used to store information about the IODevice for async reading and * streaming sensor data. @@ -590,7 +613,7 @@ struct sensor_read_config { const struct device *sensor; const bool is_streaming; union { - enum sensor_channel *const channels; + struct sensor_chan_spec *const channels; struct sensor_stream_trigger *const triggers; }; size_t count; @@ -604,7 +627,8 @@ struct sensor_read_config { * * @code(.c) * SENSOR_DT_READ_IODEV(icm42688_accelgyro, DT_NODELABEL(icm42688), - * SENSOR_CHAN_ACCEL_XYZ, SENSOR_CHAN_GYRO_XYZ); + * { SENSOR_CHAN_ACCEL_XYZ, 0 }, + * { SENSOR_CHAN_GYRO_XYZ, 0 }); * * int main(void) { * sensor_read(&icm42688_accelgyro, &rtio); @@ -612,7 +636,7 @@ struct sensor_read_config { * @endcode */ #define SENSOR_DT_READ_IODEV(name, dt_node, ...) \ - static enum sensor_channel _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \ + static struct sensor_chan_spec _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \ static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \ .sensor = DEVICE_DT_GET(dt_node), \ .is_streaming = false, \ @@ -886,10 +910,10 @@ struct __attribute__((__packed__)) sensor_data_generic_header { int8_t shift; /* This padding is needed to make sure that the 'channels' field is aligned */ - int8_t _padding[sizeof(enum sensor_channel) - 1]; + int8_t _padding[sizeof(struct sensor_chan_spec) - 1]; /* Channels present in the frame */ - enum sensor_channel channels[0]; + struct sensor_chan_spec channels[0]; }; /** diff --git a/tests/drivers/build_all/sensor/src/generic_test.c b/tests/drivers/build_all/sensor/src/generic_test.c index 2d5e0c1df86a9e..514b2673359173 100644 --- a/tests/drivers/build_all/sensor/src/generic_test.c +++ b/tests/drivers/build_all/sensor/src/generic_test.c @@ -30,7 +30,7 @@ union sensor_data_union { * Set up an RTIO context that can be shared for all sensors */ -static enum sensor_channel iodev_all_channels[SENSOR_CHAN_ALL]; +static struct sensor_chan_spec iodev_all_channels[SENSOR_CHAN_ALL]; static struct sensor_read_config iodev_read_config = { .channels = iodev_all_channels, .max = SENSOR_CHAN_ALL, @@ -120,7 +120,7 @@ static void run_generic_test(const struct device *dev) channel_table[ch].epsilon, shift); /* Add to the list of channels to read */ - iodev_all_channels[iodev_read_config.count++] = ch; + iodev_all_channels[iodev_read_config.count++].chan_type = ch; /* Generate a set of CONFIG_GENERIC_SENSOR_TEST_NUM_EXPECTED_VALS test * values. @@ -155,16 +155,17 @@ static void run_generic_test(const struct device *dev) /* Set this iteration's expected values in emul for every supported channel */ for (size_t i = 0; i < iodev_read_config.count; i++) { - enum sensor_channel ch = iodev_all_channels[i]; + struct sensor_chan_spec ch = iodev_all_channels[i]; rv = emul_sensor_backend_set_channel( - emul, ch, &channel_table[ch].expected_values[iteration], - channel_table[ch].expected_value_shift); - zassert_ok( - rv, - "Cannot set value 0x%08x on channel %d (error %d, iteration %d/%d)", - channel_table[i].expected_values[iteration], ch, rv, iteration + 1, - CONFIG_GENERIC_SENSOR_TEST_NUM_EXPECTED_VALS); + emul, ch, &channel_table[ch.chan_type].expected_values[iteration], + channel_table[ch.chan_type].expected_value_shift); + zassert_ok(rv, + "Cannot set value 0x%08x on channel (type: %d, index: %d) " + "(error %d, iteration %d/%d)", + channel_table[i].expected_values[iteration], ch.chan_type, + ch.chan_idx, rv, iteration + 1, + CONFIG_GENERIC_SENSOR_TEST_NUM_EXPECTED_VALS); } /* Perform the actual sensor read */