Skip to content

Commit 057ee44

Browse files
committed
drivers: sensor: adi: adxl345: take selected range into account in conversion
When running the samples/sensor/accel_trig sample with a adxl345 sensor the logged output values were doubled compared to the expected values. The sensor_channel_get() path calls adxl345_accel_convert which did not handle the configured g-range, resulting in invalid sample values except for the +/-16g range. To fix this issue the configured range is taken into account and different conversion factors are applied. adxl345_accel_convert is also made static as it is not used by another file. Signed-off-by: Stefan Schmidt <[email protected]>
1 parent 923ece1 commit 057ee44

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

drivers/sensor/adi/adxl345/adxl345.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,22 @@ int adxl345_read_sample(const struct device *dev,
312312
return 0;
313313
}
314314

315-
void adxl345_accel_convert(struct sensor_value *val, int16_t sample)
315+
static void adxl345_accel_convert(struct sensor_value *val, int16_t sample,
316+
uint8_t selected_range)
316317
{
318+
const int32_t sensitivity[] = {
319+
[ADXL345_RANGE_2G] = INT32_C(SENSOR_G / 256),
320+
[ADXL345_RANGE_4G] = INT32_C(SENSOR_G / 128),
321+
[ADXL345_RANGE_8G] = INT32_C(SENSOR_G / 64),
322+
[ADXL345_RANGE_16G] = INT32_C(SENSOR_G / 32),
323+
};
324+
317325
if (sample & BIT(9)) {
318326
sample |= ADXL345_COMPLEMENT;
319327
}
320328

321-
val->val1 = ((sample * SENSOR_G) / 32) / 1000000;
322-
val->val2 = ((sample * SENSOR_G) / 32) % 1000000;
329+
val->val1 = (sample * sensitivity[selected_range]) / 1000000;
330+
val->val2 = (sample * sensitivity[selected_range]) % 1000000;
323331
}
324332

325333
static int adxl345_sample_fetch(const struct device *dev,
@@ -349,18 +357,24 @@ static int adxl345_channel_get(const struct device *dev,
349357

350358
switch (chan) {
351359
case SENSOR_CHAN_ACCEL_X:
352-
adxl345_accel_convert(val, data->samples.x);
360+
adxl345_accel_convert(val, data->samples.x,
361+
data->selected_range);
353362
break;
354363
case SENSOR_CHAN_ACCEL_Y:
355-
adxl345_accel_convert(val, data->samples.y);
364+
adxl345_accel_convert(val, data->samples.y,
365+
data->selected_range);
356366
break;
357367
case SENSOR_CHAN_ACCEL_Z:
358-
adxl345_accel_convert(val, data->samples.z);
368+
adxl345_accel_convert(val, data->samples.z,
369+
data->selected_range);
359370
break;
360371
case SENSOR_CHAN_ACCEL_XYZ:
361-
adxl345_accel_convert(val++, data->samples.x);
362-
adxl345_accel_convert(val++, data->samples.y);
363-
adxl345_accel_convert(val, data->samples.z);
372+
adxl345_accel_convert(val++, data->samples.x,
373+
data->selected_range);
374+
adxl345_accel_convert(val++, data->samples.y,
375+
data->selected_range);
376+
adxl345_accel_convert(val, data->samples.z,
377+
data->selected_range);
364378
break;
365379
default:
366380
return -ENOTSUP;

drivers/sensor/adi/adxl345/adxl345.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ int adxl345_read_sample(const struct device *dev, struct adxl345_sample *sample)
291291
#ifdef CONFIG_SENSOR_ASYNC_API
292292
void adxl345_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
293293
int adxl345_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder);
294-
void adxl345_accel_convert(struct sensor_value *val, int16_t sample);
295294
#endif /* CONFIG_SENSOR_ASYNC_API */
296295

297296
#ifdef CONFIG_ADXL345_STREAM

0 commit comments

Comments
 (0)