@@ -19,13 +19,70 @@ static const uint32_t accel_period_ns[] = {
1919 [ADXL345_ODR_400HZ ] = UINT32_C (1000000000 ) / 400 ,
2020};
2121
22- static inline void adxl345_accel_convert_q31 (q31_t * out , uint16_t sample )
22+ static const uint32_t range_to_shift [] = {
23+ [ADXL345_RANGE_2G ] = 5 ,
24+ [ADXL345_RANGE_4G ] = 6 ,
25+ [ADXL345_RANGE_8G ] = 7 ,
26+ [ADXL345_RANGE_16G ] = 8 ,
27+ };
28+
29+ /* (1 / sensitivity) * (pow(2,31) / pow(2,shift)) * (unit_scaler) */
30+ static const uint32_t qscale_factor_no_full_res [] = {
31+ /* (1.0 / ADXL362_ACCEL_2G_LSB_PER_G) * (2^31 / 2^5) * SENSOR_G / 1000000 */
32+ [ADXL345_RANGE_2G ] = UINT32_C (2569011 ),
33+ /* (1.0 / ADXL362_ACCEL_4G_LSB_PER_G) * (2^31 / 2^6) * SENSOR_G / 1000000 */
34+ [ADXL345_RANGE_4G ] = UINT32_C (642253 ),
35+ /* (1.0 / ADXL362_ACCEL_8G_LSB_PER_G) * (2^31 / 2^7) ) * SENSOR_G / 1000000 */
36+ [ADXL345_RANGE_8G ] = UINT32_C (160563 ),
37+ /* (1.0 / ADXL362_ACCEL_8G_LSB_PER_G) * (2^31 / 2^8) ) * SENSOR_G / 1000000 */
38+ [ADXL345_RANGE_16G ] = UINT32_C (40141 ),
39+ };
40+
41+ /* (1 / sensitivity) * (pow(2,31) / pow(2,shift)) * (unit_scaler) */
42+ static const uint32_t qscale_factor_full_res [] = {
43+ /* (1.0 / ADXL362_ACCEL_2G_LSB_PER_G) * (2^31 / 2^5) * SENSOR_G / 1000000 */
44+ [ADXL345_RANGE_2G ] = UINT32_C (2569011 ),
45+ /* (1.0 / ADXL362_ACCEL_4G_LSB_PER_G) * (2^31 / 2^6) * SENSOR_G / 1000000 */
46+ [ADXL345_RANGE_4G ] = UINT32_C (1284506 ),
47+ /* (1.0 / ADXL362_ACCEL_8G_LSB_PER_G) * (2^31 / 2^7) ) * SENSOR_G / 1000000 */
48+ [ADXL345_RANGE_8G ] = UINT32_C (642253 ),
49+ /* (1.0 / ADXL362_ACCEL_8G_LSB_PER_G) * (2^31 / 2^8) ) * SENSOR_G / 1000000 */
50+ [ADXL345_RANGE_16G ] = UINT32_C (321126 ),
51+ };
52+
53+ static inline void adxl345_accel_convert_q31 (q31_t * out , int16_t sample , int32_t range ,
54+ uint8_t is_full_res )
2355{
24- if (sample & BIT (9 )) {
25- sample |= ADXL345_COMPLEMENT ;
56+ if (is_full_res ) {
57+ switch (range ) {
58+ case ADXL345_RANGE_2G :
59+ if (sample & BIT (9 )) {
60+ sample |= ADXL345_COMPLEMENT_MASK (10 );
61+ }
62+ break ;
63+ case ADXL345_RANGE_4G :
64+ if (sample & BIT (10 )) {
65+ sample |= ADXL345_COMPLEMENT_MASK (11 );
66+ }
67+ break ;
68+ case ADXL345_RANGE_8G :
69+ if (sample & BIT (11 )) {
70+ sample |= ADXL345_COMPLEMENT_MASK (12 );
71+ }
72+ break ;
73+ case ADXL345_RANGE_16G :
74+ if (sample & BIT (12 )) {
75+ sample |= ADXL345_COMPLEMENT_MASK (13 );
76+ }
77+ break ;
78+ }
79+ } else {
80+ if (sample & BIT (9 )) {
81+ sample |= ADXL345_COMPLEMENT ;
82+ }
2683 }
27- int32_t micro_ms2 = (( sample * SENSOR_G ) / 32 );
28- * out = CLAMP (((( int64_t ) micro_ms2 ) + ( micro_ms2 % 1000000 )), INT32_MIN , INT32_MAX ) ;
84+
85+ * out = sample * qscale_factor_no_full_res [ range ] ;
2986}
3087
3188static int adxl345_decode_stream (const uint8_t * buffer , struct sensor_chan_spec chan_spec ,
@@ -46,11 +103,13 @@ static int adxl345_decode_stream(const uint8_t *buffer, struct sensor_chan_spec
46103 memset (data , 0 , sizeof (struct sensor_three_axis_data ));
47104 data -> header .base_timestamp_ns = enc_data -> timestamp ;
48105 data -> header .reading_count = 1 ;
106+ data -> shift = range_to_shift [enc_data -> selected_range ];
49107
50108 buffer += sizeof (struct adxl345_fifo_data );
51109
52110 uint8_t sample_set_size = enc_data -> sample_set_size ;
53111 uint64_t period_ns = accel_period_ns [enc_data -> accel_odr ];
112+ uint8_t is_full_res = enc_data -> is_full_res ;
54113
55114 /* Calculate which sample is decoded. */
56115 if ((uint8_t * )* fit >= buffer ) {
@@ -73,13 +132,16 @@ static int adxl345_decode_stream(const uint8_t *buffer, struct sensor_chan_spec
73132 data -> readings [count ].timestamp_delta = sample_num * period_ns ;
74133 uint8_t buff_offset = 0 ;
75134
76- adxl345_accel_convert_q31 (& data -> readings [count ].x , * (int16_t * )buffer );
135+ adxl345_accel_convert_q31 (& data -> readings [count ].x , * (int16_t * )buffer ,
136+ enc_data -> selected_range , is_full_res );
77137 buff_offset = 2 ;
78138 adxl345_accel_convert_q31 (& data -> readings [count ].y ,
79- * (int16_t * )(buffer + buff_offset ));
139+ * (int16_t * )(buffer + buff_offset ),
140+ enc_data -> selected_range , is_full_res );
80141 buff_offset += 2 ;
81142 adxl345_accel_convert_q31 (& data -> readings [count ].z ,
82- * (int16_t * )(buffer + buff_offset ));
143+ * (int16_t * )(buffer + buff_offset ),
144+ enc_data -> selected_range , is_full_res );
83145 break ;
84146 default :
85147 return - ENOTSUP ;
0 commit comments