99#include <zephyr/drivers/sensor_clock.h>
1010#include <zephyr/sys/util.h>
1111#include <zephyr/sys/byteorder.h>
12+ #include <zephyr/dt-bindings/sensor/rm3100.h>
1213#include "rm3100.h"
1314
1415uint8_t rm3100_encode_channel (enum sensor_channel chan )
@@ -33,12 +34,19 @@ int rm3100_encode(const struct device *dev,
3334 size_t num_channels ,
3435 uint8_t * buf )
3536{
37+ const struct rm3100_data * data = dev -> data ;
3638 struct rm3100_encoded_data * edata = (struct rm3100_encoded_data * )buf ;
3739 uint64_t cycles ;
3840 int err ;
3941
4042 edata -> header .channels = 0 ;
4143
44+ if (data -> settings .odr == RM3100_DT_ODR_600 ) {
45+ edata -> header .cycle_count = RM3100_CYCLE_COUNT_HIGH_ODR ;
46+ } else {
47+ edata -> header .cycle_count = RM3100_CYCLE_COUNT_DEFAULT ;
48+ }
49+
4250 for (size_t i = 0 ; i < num_channels ; i ++ ) {
4351 edata -> header .channels |= rm3100_encode_channel (channels [i ].chan_type );
4452 }
@@ -105,19 +113,30 @@ static int rm3100_decoder_get_frame_count(const uint8_t *buffer,
105113 return -1 ;
106114}
107115
108- static int rm3100_convert_raw_to_q31 (uint32_t raw_reading , q31_t * out , int8_t * shift )
116+ static int rm3100_convert_raw_to_q31 (uint16_t cycle_count , uint32_t raw_reading ,
117+ q31_t * out , int8_t * shift )
109118{
110119 int64_t value ;
120+ uint8_t divider ;
111121
112122 raw_reading = sys_be24_to_cpu (raw_reading );
113123 value = sign_extend (raw_reading , 23 );
114124
115125 /** Convert to Gauss, assuming 1 LSB = 75 uT, given default Cycle-Counting (200).
116126 * We can represent the largest sample (2^23 LSB) in Gauss with 11 bits.
117127 */
118- * shift = 11 ;
128+ if (cycle_count == RM3100_CYCLE_COUNT_DEFAULT ) {
129+ * shift = 11 ;
130+ divider = 75 ;
131+ } else {
132+ /** Otherwise, it's 1 LSB = 38 uT at Cycle-counting for 600 Hz ODR (100):
133+ * 12-bits max value.
134+ */
135+ * shift = 12 ;
136+ divider = 38 ;
137+ }
119138
120- int64_t micro_tesla_scaled = ((int64_t )value << (31 - * shift )) / 75 ;
139+ int64_t micro_tesla_scaled = ((int64_t )value << (31 - * shift )) / divider ;
121140 int64_t gauss_scaled = (int64_t )micro_tesla_scaled / 100 ;
122141
123142 * out = gauss_scaled ;
@@ -166,7 +185,8 @@ static int rm3100_decoder_decode(const uint8_t *buffer,
166185 raw_reading = edata -> magn .z ;
167186 }
168187
169- rm3100_convert_raw_to_q31 (raw_reading , & out -> readings -> value , & out -> shift );
188+ rm3100_convert_raw_to_q31 (
189+ edata -> header .cycle_count , raw_reading , & out -> readings -> value , & out -> shift );
170190
171191 * fit = 1 ;
172192 return 1 ;
@@ -182,9 +202,12 @@ static int rm3100_decoder_decode(const uint8_t *buffer,
182202 out -> header .base_timestamp_ns = edata -> header .timestamp ;
183203 out -> header .reading_count = 1 ;
184204
185- rm3100_convert_raw_to_q31 (edata -> magn .x , & out -> readings [0 ].x , & out -> shift );
186- rm3100_convert_raw_to_q31 (edata -> magn .y , & out -> readings [0 ].y , & out -> shift );
187- rm3100_convert_raw_to_q31 (edata -> magn .z , & out -> readings [0 ].z , & out -> shift );
205+ rm3100_convert_raw_to_q31 (
206+ edata -> header .cycle_count , edata -> magn .x , & out -> readings [0 ].x , & out -> shift );
207+ rm3100_convert_raw_to_q31 (
208+ edata -> header .cycle_count , edata -> magn .y , & out -> readings [0 ].y , & out -> shift );
209+ rm3100_convert_raw_to_q31 (
210+ edata -> header .cycle_count , edata -> magn .z , & out -> readings [0 ].z , & out -> shift );
188211
189212 * fit = 1 ;
190213 return 1 ;
0 commit comments