Skip to content

Commit 527c1f5

Browse files
vladislav-pejicmmahadevan108
authored andcommitted
driver: sensor: adxl362: Bug fix for q31_t conv
This is a bug fix for adxl362_temp_convert_q31 and adxl362_accel_convert_q31 functions. Functions are used to convert samples received from sensor to q31_t format when RTIO stream is used. Signed-off-by: Vladislav Pejic <[email protected]>
1 parent 28edb22 commit 527c1f5

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

drivers/sensor/adi/adxl362/adxl362_decoder.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
#ifdef CONFIG_ADXL362_STREAM
1111

12+
/* (2^31 / 2^8(shift) */
13+
#define ADXL362_TEMP_QSCALE 8388608
14+
#define ADXL362_TEMP_LSB_PER_C 15
15+
1216
#define ADXL362_COMPLEMENT 0xf000
1317

1418
static const uint32_t accel_period_ns[] = {
@@ -20,11 +24,20 @@ static const uint32_t accel_period_ns[] = {
2024
[ADXL362_ODR_400_HZ] = UINT32_C(1000000000) / 400,
2125
};
2226

23-
static const int32_t range_to_scale[] = {
24-
/* See table 1 in specifications section of datasheet */
25-
[ADXL362_RANGE_2G] = ADXL362_ACCEL_2G_LSB_PER_G,
26-
[ADXL362_RANGE_4G] = ADXL362_ACCEL_4G_LSB_PER_G,
27-
[ADXL362_RANGE_8G] = ADXL362_ACCEL_8G_LSB_PER_G,
27+
static const uint32_t range_to_shift[] = {
28+
[ADXL362_RANGE_2G] = 5,
29+
[ADXL362_RANGE_4G] = 6,
30+
[ADXL362_RANGE_8G] = 7,
31+
};
32+
33+
/* (1 / sensitivity) * (pow(2,31) / pow(2,shift)) * (unit_scaler) */
34+
static const uint32_t qscale_factor[] = {
35+
/* (1.0 / ADXL362_ACCEL_2G_LSB_PER_G) * (2^31 / 2^5) * SENSOR_G / 1000000 */
36+
[ADXL362_RANGE_2G] = UINT32_C(658338),
37+
/* (1.0 / ADXL362_ACCEL_4G_LSB_PER_G) * (2^31 / 2^6) * SENSOR_G / 1000000 */
38+
[ADXL362_RANGE_4G] = UINT32_C(658338),
39+
/* (1.0 / ADXL362_ACCEL_8G_LSB_PER_G) * (2^31 / 2^7) ) * SENSOR_G / 1000000 */
40+
[ADXL362_RANGE_8G] = UINT32_C(700360),
2841
};
2942

3043
static inline void adxl362_temp_convert_q31(q31_t *out, int16_t data_in)
@@ -36,10 +49,8 @@ static inline void adxl362_temp_convert_q31(q31_t *out, int16_t data_in)
3649
data_in |= ADXL362_COMPLEMENT;
3750
}
3851

39-
int64_t milli_c = (data_in - ADXL362_TEMP_BIAS_LSB) * ADXL362_TEMP_MC_PER_LSB +
40-
(ADXL362_TEMP_BIAS_TEST_CONDITION * 1000);
41-
42-
*out = CLAMP(((milli_c * 1000) + ((milli_c % 1000) * 1000)), INT32_MIN, INT32_MAX);
52+
*out = ((data_in - ADXL362_TEMP_BIAS_LSB) / ADXL362_TEMP_LSB_PER_C
53+
+ ADXL362_TEMP_BIAS_TEST_CONDITION) * ADXL362_TEMP_QSCALE;
4354
}
4455

4556
static inline void adxl362_accel_convert_q31(q31_t *out, int16_t data_in, int32_t range)
@@ -50,9 +61,7 @@ static inline void adxl362_accel_convert_q31(q31_t *out, int16_t data_in, int32_
5061
data_in |= ADXL362_COMPLEMENT;
5162
}
5263

53-
int64_t micro_ms2 = data_in * SENSOR_G / range_to_scale[range];
54-
55-
*out = CLAMP((micro_ms2 + (micro_ms2 % 1000000)), INT32_MIN, INT32_MAX);
64+
*out = data_in * qscale_factor[range];
5665
}
5766

5867
static int adxl362_decode_stream(const uint8_t *buffer, struct sensor_chan_spec chan_spec,
@@ -102,6 +111,7 @@ static int adxl362_decode_stream(const uint8_t *buffer, struct sensor_chan_spec
102111
memset(data, 0, sizeof(struct sensor_three_axis_data));
103112
data->header.base_timestamp_ns = enc_data->timestamp;
104113
data->header.reading_count = 1;
114+
data->shift = 8;
105115

106116
data->readings[count].timestamp_delta =
107117
period_ns * sample_num;
@@ -121,6 +131,7 @@ static int adxl362_decode_stream(const uint8_t *buffer, struct sensor_chan_spec
121131
memset(data, 0, sizeof(struct sensor_three_axis_data));
122132
data->header.base_timestamp_ns = enc_data->timestamp;
123133
data->header.reading_count = 1;
134+
data->shift = range_to_shift[enc_data->selected_range];
124135

125136
switch (chan_spec.chan_type) {
126137
case SENSOR_CHAN_ACCEL_X:

0 commit comments

Comments
 (0)