Skip to content

Commit b249535

Browse files
teburdcarlescufi
authored andcommitted
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 <[email protected]>
1 parent a2087be commit b249535

File tree

17 files changed

+295
-237
lines changed

17 files changed

+295
-237
lines changed

drivers/sensor/amd_sb_tsi/sb_tsi_emul.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ static int sb_tsi_emul_init(const struct emul *target, const struct device *pare
9999
return 0;
100100
}
101101

102-
static int sb_tsi_emul_set_channel(const struct emul *target, enum sensor_channel chan,
102+
static int sb_tsi_emul_set_channel(const struct emul *target, struct sensor_chan_spec ch,
103103
const q31_t *value, int8_t shift)
104104
{
105105
struct sb_tsi_emul_data *data = target->data;
106106
int64_t scaled_value;
107107
int32_t millicelsius;
108108
int32_t reg_value;
109109

110-
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
110+
if (ch.chan_type != SENSOR_CHAN_AMBIENT_TEMP && ch.chan_idx != 0) {
111111
return -ENOTSUP;
112112
}
113113

@@ -121,10 +121,10 @@ static int sb_tsi_emul_set_channel(const struct emul *target, enum sensor_channe
121121
return 0;
122122
}
123123

124-
static int sb_tsi_emul_get_sample_range(const struct emul *target, enum sensor_channel chan,
124+
static int sb_tsi_emul_get_sample_range(const struct emul *target, struct sensor_chan_spec ch,
125125
q31_t *lower, q31_t *upper, q31_t *epsilon, int8_t *shift)
126126
{
127-
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
127+
if (ch.chan_type != SENSOR_CHAN_AMBIENT_TEMP || ch.chan_idx != 0) {
128128
return -ENOTSUP;
129129
}
130130

drivers/sensor/asahi_kasei/akm09918c/akm09918c_decoder.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55

66
#include "akm09918c.h"
77

8-
static int akm09918c_decoder_get_frame_count(const uint8_t *buffer, enum sensor_channel channel,
9-
size_t channel_idx, uint16_t *frame_count)
8+
static int akm09918c_decoder_get_frame_count(const uint8_t *buffer,
9+
struct sensor_chan_spec chan_spec,
10+
uint16_t *frame_count)
1011
{
1112
ARG_UNUSED(buffer);
12-
ARG_UNUSED(channel);
13-
ARG_UNUSED(channel_idx);
13+
ARG_UNUSED(chan_spec);
1414

1515
/* This sensor lacks a FIFO; there will always only be one frame at a time. */
1616
*frame_count = 1;
1717
return 0;
1818
}
1919

20-
static int akm09918c_decoder_get_size_info(enum sensor_channel channel, size_t *base_size,
20+
static int akm09918c_decoder_get_size_info(struct sensor_chan_spec chan_spec, size_t *base_size,
2121
size_t *frame_size)
2222
{
23-
switch (channel) {
23+
switch (chan_spec.chan_type) {
2424
case SENSOR_CHAN_MAGN_X:
2525
case SENSOR_CHAN_MAGN_Y:
2626
case SENSOR_CHAN_MAGN_Z:
@@ -48,17 +48,16 @@ static int akm09918c_convert_raw_to_q31(int16_t reading, q31_t *out)
4848
return 0;
4949
}
5050

51-
static int akm09918c_decoder_decode(const uint8_t *buffer, enum sensor_channel channel,
52-
size_t channel_idx, uint32_t *fit,
53-
uint16_t max_count, void *data_out)
51+
static int akm09918c_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec chan_spec,
52+
uint32_t *fit, uint16_t max_count, void *data_out)
5453
{
5554
const struct akm09918c_encoded_data *edata = (const struct akm09918c_encoded_data *)buffer;
5655

5756
if (*fit != 0) {
5857
return 0;
5958
}
6059

61-
switch (channel) {
60+
switch (chan_spec.chan_type) {
6261
case SENSOR_CHAN_MAGN_X:
6362
case SENSOR_CHAN_MAGN_Y:
6463
case SENSOR_CHAN_MAGN_Z:

drivers/sensor/asahi_kasei/akm09918c/akm09918c_emul.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static int akm09918c_emul_init(const struct emul *target, const struct device *p
133133
return 0;
134134
}
135135

136-
static int akm09918c_emul_backend_set_channel(const struct emul *target, enum sensor_channel ch,
136+
static int akm09918c_emul_backend_set_channel(const struct emul *target, struct sensor_chan_spec ch,
137137
const q31_t *value, int8_t shift)
138138
{
139139
if (!target || !target->data) {
@@ -143,7 +143,7 @@ static int akm09918c_emul_backend_set_channel(const struct emul *target, enum se
143143
struct akm09918c_emul_data *data = target->data;
144144
uint8_t reg;
145145

146-
switch (ch) {
146+
switch (ch.chan_type) {
147147
case SENSOR_CHAN_MAGN_X:
148148
reg = AKM09918C_REG_HXL;
149149
break;
@@ -178,7 +178,7 @@ static int akm09918c_emul_backend_set_channel(const struct emul *target, enum se
178178
}
179179

180180
static int akm09918c_emul_backend_get_sample_range(const struct emul *target,
181-
enum sensor_channel ch, q31_t *lower,
181+
struct sensor_chan_spec ch, q31_t *lower,
182182
q31_t *upper, q31_t *epsilon, int8_t *shift)
183183
{
184184
ARG_UNUSED(target);
@@ -187,7 +187,7 @@ static int akm09918c_emul_backend_get_sample_range(const struct emul *target,
187187
return -EINVAL;
188188
}
189189

190-
switch (ch) {
190+
switch (ch.chan_type) {
191191
case SENSOR_CHAN_MAGN_X:
192192
case SENSOR_CHAN_MAGN_Y:
193193
case SENSOR_CHAN_MAGN_Z:

drivers/sensor/bosch/bma4xx/bma4xx.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static int bma4xx_submit_one_shot(const struct device *dev, struct rtio_iodev_sq
343343
struct bma4xx_data *bma4xx = dev->data;
344344

345345
const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
346-
const enum sensor_channel *const channels = cfg->channels;
346+
const struct sensor_chan_spec *const channels = cfg->channels;
347347
const size_t num_channels = cfg->count;
348348

349349
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
370370

371371
/* Determine what channels we need to fetch */
372372
for (int i = 0; i < num_channels; i++) {
373-
switch (channels[i]) {
373+
if (channels[i].chan_idx != 0) {
374+
LOG_ERR("Only channel index 0 supported");
375+
return -ENOTSUP;
376+
}
377+
switch (channels[i].chan_type) {
374378
case SENSOR_CHAN_ALL:
375379
edata->has_accel = 1;
376380
#ifdef CONFIG_BMA4XX_TEMPERATURE
@@ -389,7 +393,8 @@ static int bma4xx_submit_one_shot(const struct device *dev, struct rtio_iodev_sq
389393
break;
390394
#endif /* CONFIG_BMA4XX_TEMPERATURE */
391395
default:
392-
LOG_ERR("Requested unsupported channel ID %d", channels[i]);
396+
LOG_ERR("Requested unsupported channel type %d, idx %d",
397+
channels[i].chan_type, channels[i].chan_idx);
393398
return -ENOTSUP;
394399
}
395400
}
@@ -436,18 +441,18 @@ static int bma4xx_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_
436441
* RTIO decoder
437442
*/
438443

439-
static int bma4xx_decoder_get_frame_count(const uint8_t *buffer, enum sensor_channel channel,
440-
size_t channel_idx, uint16_t *frame_count)
444+
static int bma4xx_decoder_get_frame_count(const uint8_t *buffer, struct sensor_chan_spec ch,
445+
uint16_t *frame_count)
441446
{
442447
const struct bma4xx_encoded_data *edata = (const struct bma4xx_encoded_data *)buffer;
443448
const struct bma4xx_decoder_header *header = &edata->header;
444449

445-
if (channel_idx != 0) {
450+
if (ch.chan_idx != 0) {
446451
return -ENOTSUP;
447452
}
448453

449454
if (!header->is_fifo) {
450-
switch (channel) {
455+
switch (ch.chan_type) {
451456
case SENSOR_CHAN_ACCEL_X:
452457
case SENSOR_CHAN_ACCEL_Y:
453458
case SENSOR_CHAN_ACCEL_Z:
@@ -467,10 +472,10 @@ static int bma4xx_decoder_get_frame_count(const uint8_t *buffer, enum sensor_cha
467472
return -ENOTSUP;
468473
}
469474

470-
static int bma4xx_decoder_get_size_info(enum sensor_channel channel, size_t *base_size,
475+
static int bma4xx_decoder_get_size_info(struct sensor_chan_spec ch, size_t *base_size,
471476
size_t *frame_size)
472477
{
473-
switch (channel) {
478+
switch (ch.chan_type) {
474479
case SENSOR_CHAN_ACCEL_X:
475480
case SENSOR_CHAN_ACCEL_Y:
476481
case SENSOR_CHAN_ACCEL_Z:
@@ -487,9 +492,9 @@ static int bma4xx_decoder_get_size_info(enum sensor_channel channel, size_t *bas
487492
}
488493
}
489494

490-
static int bma4xx_get_shift(enum sensor_channel channel, uint8_t accel_fs, int8_t *shift)
495+
static int bma4xx_get_shift(struct sensor_chan_spec ch, uint8_t accel_fs, int8_t *shift)
491496
{
492-
switch (channel) {
497+
switch (ch.chan_type) {
493498
case SENSOR_CHAN_ACCEL_X:
494499
case SENSOR_CHAN_ACCEL_Y:
495500
case SENSOR_CHAN_ACCEL_Z:
@@ -562,9 +567,8 @@ static void bma4xx_convert_raw_temp_to_q31(int8_t raw_val, q31_t *out)
562567
}
563568
#endif /* CONFIG_BMA4XX_TEMPERATURE */
564569

565-
static int bma4xx_one_shot_decode(const uint8_t *buffer, enum sensor_channel channel,
566-
size_t channel_idx, uint32_t *fit, uint16_t max_count,
567-
void *data_out)
570+
static int bma4xx_one_shot_decode(const uint8_t *buffer, struct sensor_chan_spec ch,
571+
uint32_t *fit, uint16_t max_count, void *data_out)
568572
{
569573
const struct bma4xx_encoded_data *edata = (const struct bma4xx_encoded_data *)buffer;
570574
const struct bma4xx_decoder_header *header = &edata->header;
@@ -573,11 +577,11 @@ static int bma4xx_one_shot_decode(const uint8_t *buffer, enum sensor_channel cha
573577
if (*fit != 0) {
574578
return 0;
575579
}
576-
if (max_count == 0 || channel_idx != 0) {
580+
if (max_count == 0 || ch.chan_idx != 0) {
577581
return -EINVAL;
578582
}
579583

580-
switch (channel) {
584+
switch (ch.chan_type) {
581585
case SENSOR_CHAN_ACCEL_X:
582586
case SENSOR_CHAN_ACCEL_Y:
583587
case SENSOR_CHAN_ACCEL_Z:
@@ -590,7 +594,9 @@ static int bma4xx_one_shot_decode(const uint8_t *buffer, enum sensor_channel cha
590594

591595
out->header.base_timestamp_ns = edata->header.timestamp;
592596
out->header.reading_count = 1;
593-
rc = bma4xx_get_shift(SENSOR_CHAN_ACCEL_XYZ, header->accel_fs, &out->shift);
597+
rc = bma4xx_get_shift((struct sensor_chan_spec){.chan_type = SENSOR_CHAN_ACCEL_XYZ,
598+
.chan_idx = 0},
599+
header->accel_fs, &out->shift);
594600
if (rc != 0) {
595601
return -EINVAL;
596602
}
@@ -628,8 +634,8 @@ static int bma4xx_one_shot_decode(const uint8_t *buffer, enum sensor_channel cha
628634
}
629635
}
630636

631-
static int bma4xx_decoder_decode(const uint8_t *buffer, enum sensor_channel channel,
632-
size_t channel_idx, uint32_t *fit, uint16_t max_count,
637+
static int bma4xx_decoder_decode(const uint8_t *buffer, struct sensor_chan_spec ch,
638+
uint32_t *fit, uint16_t max_count,
633639
void *data_out)
634640
{
635641
const struct bma4xx_decoder_header *header = (const struct bma4xx_decoder_header *)buffer;
@@ -639,7 +645,7 @@ static int bma4xx_decoder_decode(const uint8_t *buffer, enum sensor_channel chan
639645
return -ENOTSUP;
640646
}
641647

642-
return bma4xx_one_shot_decode(buffer, channel, channel_idx, fit, max_count, data_out);
648+
return bma4xx_one_shot_decode(buffer, ch, fit, max_count, data_out);
643649
}
644650

645651
SENSOR_DECODER_API_DT_DEFINE() = {

drivers/sensor/bosch/bma4xx/bma4xx_emul.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void bma4xx_emul_set_accel_data(const struct emul *target, q31_t value, int8_t s
216216
data->regs[reg + 1] = FIELD_GET(GENMASK(11, 4), reg_val);
217217
}
218218

219-
static int bma4xx_emul_backend_set_channel(const struct emul *target, enum sensor_channel ch,
219+
static int bma4xx_emul_backend_set_channel(const struct emul *target, struct sensor_chan_spec ch,
220220
const q31_t *value, int8_t shift)
221221
{
222222

@@ -226,7 +226,7 @@ static int bma4xx_emul_backend_set_channel(const struct emul *target, enum senso
226226

227227
struct bma4xx_emul_data *data = target->data;
228228

229-
switch (ch) {
229+
switch (ch.chan_type) {
230230
case SENSOR_CHAN_ACCEL_X:
231231
bma4xx_emul_set_accel_data(target, value[0], shift, BMA4XX_REG_DATA_8);
232232
break;
@@ -250,15 +250,15 @@ static int bma4xx_emul_backend_set_channel(const struct emul *target, enum senso
250250
return 0;
251251
}
252252

253-
static int bma4xx_emul_backend_get_sample_range(const struct emul *target, enum sensor_channel ch,
254-
q31_t *lower, q31_t *upper, q31_t *epsilon,
255-
int8_t *shift)
253+
static int bma4xx_emul_backend_get_sample_range(const struct emul *target,
254+
struct sensor_chan_spec ch, q31_t *lower,
255+
q31_t *upper, q31_t *epsilon, int8_t *shift)
256256
{
257257
if (!lower || !upper || !epsilon || !shift) {
258258
return -EINVAL;
259259
}
260260

261-
switch (ch) {
261+
switch (ch.chan_type) {
262262
case SENSOR_CHAN_ACCEL_X:
263263
case SENSOR_CHAN_ACCEL_Y:
264264
case SENSOR_CHAN_ACCEL_Z:

0 commit comments

Comments
 (0)