Skip to content

Commit

Permalink
sensors: Add channel specifiers
Browse files Browse the repository at this point in the history
Use a structured channel specifier rather than a single enum when
specifying channels to read in the new read/decoder API.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
  • Loading branch information
teburd committed Apr 5, 2024
1 parent bba8641 commit 1084abb
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 135 deletions.
8 changes: 6 additions & 2 deletions drivers/sensor/bma4xx/bma4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel_spec *const channels = cfg->channels;
const size_t num_channels = cfg->count;

uint32_t min_buf_len = sizeof(struct bma4xx_encoded_data);
Expand All @@ -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
Expand Down
28 changes: 15 additions & 13 deletions drivers/sensor/default_rtio_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel_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;
Expand Down Expand Up @@ -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_channel_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());
Expand Down Expand Up @@ -148,24 +149,26 @@ 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;
rc == 0 ? channels[i].chan_type - 3 : SENSOR_CHAN_MAX;
header->channels[sample_idx++] =
rc == 0 ? channels[i] - 2 : SENSOR_CHAN_MAX;
rc == 0 ? channels[i].chan_type - 2 : SENSOR_CHAN_MAX;
header->channels[sample_idx++] =
rc == 0 ? channels[i] - 1 : SENSOR_CHAN_MAX;
rc == 0 ? channels[i].chan_type - 1 : SENSOR_CHAN_MAX;
} else {
header->channels[sample_idx++] = rc == 0 ? channels[i] : SENSOR_CHAN_MAX;
header->channels[sample_idx++] =
rc == 0 ? channels[i].chan_type : SENSOR_CHAN_MAX;
}

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;
}

Expand Down Expand Up @@ -474,9 +477,8 @@ static int decode(const uint8_t *buffer, enum sensor_channel channel, size_t cha
{
const struct sensor_data_generic_header *header =
(const struct sensor_data_generic_header *)buffer;
const q31_t *q =
(const q31_t *)(buffer + sizeof(struct sensor_data_generic_header) +
header->num_channels * sizeof(enum sensor_channel));
const q31_t *q = (const q31_t *)(buffer + sizeof(struct sensor_data_generic_header) +
header->num_channels * sizeof(enum sensor_channel));
int count = 0;

if (*fit != 0 || max_count < 1) {
Expand Down
22 changes: 10 additions & 12 deletions drivers/sensor/sensor_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel_spec iodev_sensor_shell_channels[SENSOR_CHAN_ALL];
static struct sensor_read_config iodev_sensor_shell_read_config = {
.sensor = NULL,
.is_streaming = false,
Expand Down Expand Up @@ -444,8 +444,8 @@ void sensor_shell_processing_callback(int result, uint8_t *buf, uint32_t buf_len
shell_info(ctx->sh,
"channel idx=%d %s shift=%d num_samples=%d "
"value=%" PRIsensor_three_axis_data,
channel, sensor_channel_name[channel],
data->shift, accumulator_buffer.count,
channel, sensor_channel_name[channel], data->shift,
accumulator_buffer.count,
PRIsensor_three_axis_data_arg(*data, 0));
break;
}
Expand Down Expand Up @@ -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_channel_spec){i, 0};
}
} else {
/* read specific channels */
Expand All @@ -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_channel_spec){chan, 0};
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -975,10 +975,8 @@ static void data_ready_trigger_handler(const struct device *sensor,
value.val1 = micro_value / 1000000;
value.val2 = (int32_t)llabs(micro_value - (value.val1 * 1000000));
LOG_INF("sensor=%.*s, chan=%s, num_samples=%u, data=%d.%06d",
sensor_name_len_before_at, sensor_name,
sensor_channel_name[i],
stats[i].count,
value.val1, value.val2);
sensor_name_len_before_at, sensor_name, sensor_channel_name[i],
stats[i].count, value.val1, value.val2);

stats[i].accumulator = 0;
stats[i].count = 0;
Expand Down Expand Up @@ -1018,7 +1016,7 @@ static int cmd_trig_sensor(const struct shell *sh, size_t argc, char **argv)

if (sensor_idx < 0) {
shell_error(sh, "Unable to support more simultaneous sensor trigger"
" devices");
" devices");
err = -ENOTSUP;
} else {
struct sample_stats *stats = sensor_stats[sensor_idx];
Expand Down
4 changes: 2 additions & 2 deletions drivers/sensor/tdk/icm42688/icm42688_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel_spec *const channels,
const size_t num_channels, uint8_t *buf)
{
struct icm42688_dev_data *data = dev->data;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion drivers/sensor/tdk/icm42688/icm42688_rtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_channel_spec *const channels = cfg->channels;
const size_t num_channels = cfg->count;
uint32_t min_buf_len = sizeof(struct icm42688_encoded_data);
int rc;
Expand Down
Loading

0 comments on commit 1084abb

Please sign in to comment.