1 /* Bosch BMI160 inertial measurement unit header
2 *
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #ifndef ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
9 #define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_
10
11 #define DT_DRV_COMPAT bosch_bmi160
12
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/drivers/spi.h>
17 #include <zephyr/kernel.h>
18 #include <zephyr/sys/util.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* registers */
25 #define BMI160_REG_CHIPID 0x00
26 #define BMI160_REG_ERR 0x02
27 #define BMI160_REG_PMU_STATUS 0x03
28 #define BMI160_REG_DATA_MAG_X 0x04
29 #define BMI160_REG_DATA_MAG_Y 0x06
30 #define BMI160_REG_DATA_MAG_Z 0x08
31 #define BMI160_REG_DATA_RHALL 0x0A
32 #define BMI160_REG_DATA_GYR_X 0x0C
33 #define BMI160_REG_DATA_GYR_Y 0x0E
34 #define BMI160_REG_DATA_GYR_Z 0x10
35 #define BMI160_REG_DATA_ACC_X 0x12
36 #define BMI160_REG_DATA_ACC_Y 0x14
37 #define BMI160_REG_DATA_ACC_Z 0x16
38 #define BMI160_REG_SENSORTIME0 0x18
39 #define BMI160_REG_SENSORTIME1 0x19
40 #define BMI160_REG_SENSORTIME2 0x1A
41 #define BMI160_REG_STATUS 0x1B
42 #define BMI160_REG_INT_STATUS0 0x1C
43 #define BMI160_REG_INT_STATUS1 0x1D
44 #define BMI160_REG_INT_STATUS2 0x1E
45 #define BMI160_REG_INT_STATUS3 0x1F
46 #define BMI160_REG_TEMPERATURE0 0x20
47 #define BMI160_REG_TEMPERATURE1 0x21
48 #define BMI160_REG_FIFO_LENGTH0 0x22
49 #define BMI160_REG_FIFO_LENGTH1 0x23
50 #define BMI160_REG_FIFO_DATA 0x24
51 #define BMI160_REG_ACC_CONF 0x40
52 #define BMI160_REG_ACC_RANGE 0x41
53 #define BMI160_REG_GYR_CONF 0x42
54 #define BMI160_REG_GYR_RANGE 0x43
55 #define BMI160_REG_MAG_CONF 0x44
56 #define BMI160_REG_FIFO_DOWNS 0x45
57 #define BMI160_REG_FIFO_CONFIG0 0x46
58 #define BMI160_REG_FIFO_CONFIG1 0x47
59 #define BMI160_REG_MAG_IF0 0x4B
60 #define BMI160_REG_MAG_IF1 0x4C
61 #define BMI160_REG_MAG_IF2 0x4D
62 #define BMI160_REG_MAG_IF3 0x4E
63 #define BMI160_REG_MAG_IF4 0x4F
64 #define BMI160_REG_INT_EN0 0x50
65 #define BMI160_REG_INT_EN1 0x51
66 #define BMI160_REG_INT_EN2 0x52
67 #define BMI160_REG_INT_OUT_CTRL 0x53
68 #define BMI160_REG_INT_LATCH 0x54
69 #define BMI160_REG_INT_MAP0 0x55
70 #define BMI160_REG_INT_MAP1 0x56
71 #define BMI160_REG_INT_MAP2 0x57
72 #define BMI160_REG_INT_DATA0 0x58
73 #define BMI160_REG_INT_DATA1 0x59
74 #define BMI160_REG_INT_LOWHIGH0 0x5A
75 #define BMI160_REG_INT_LOWHIGH1 0x5B
76 #define BMI160_REG_INT_LOWHIGH2 0x5C
77 #define BMI160_REG_INT_LOWHIGH3 0x5D
78 #define BMI160_REG_INT_LOWHIGH4 0x5E
79 #define BMI160_REG_INT_MOTION0 0x5F
80 #define BMI160_REG_INT_MOTION1 0x60
81 #define BMI160_REG_INT_MOTION2 0x61
82 #define BMI160_REG_INT_MOTION3 0x62
83 #define BMI160_REG_INT_TAP0 0x63
84 #define BMI160_REG_INT_TAP1 0x64
85 #define BMI160_REG_INT_ORIENT0 0x65
86 #define BMI160_REG_INT_ORIENT1 0x66
87 #define BMI160_REG_INT_FLAT0 0x67
88 #define BMI160_REG_INT_FLAT1 0x68
89 #define BMI160_REG_FOC_CONF 0x69
90 #define BMI160_REG_CONF 0x6A
91 #define BMI160_REG_IF_CONF 0x6B
92 #define BMI160_REG_PMU_TRIGGER 0x6C
93 #define BMI160_REG_SELF_TEST 0x6D
94 #define BMI160_REG_NV_CONF 0x70
95 #define BMI160_REG_OFFSET_ACC_X 0x71
96 #define BMI160_REG_OFFSET_ACC_Y 0x72
97 #define BMI160_REG_OFFSET_ACC_Z 0x73
98 #define BMI160_REG_OFFSET_GYR_X 0x74
99 #define BMI160_REG_OFFSET_GYR_Y 0x75
100 #define BMI160_REG_OFFSET_GYR_Z 0x76
101 #define BMI160_REG_OFFSET_EN 0x77
102 #define BMI160_REG_STEP_CNT0 0x78
103 #define BMI160_REG_STEP_CNT1 0x79
104 #define BMI160_REG_STEP_CONF0 0x7A
105 #define BMI160_REG_STEP_CONF1 0x7B
106 #define BMI160_REG_CMD 0x7E
107
108 /* This is not a real register; reading it activates SPI on the BMI160 */
109 #define BMI160_SPI_START 0x7F
110
111 #define BMI160_REG_COUNT 0x80
112
113 /* Indicates a read operation; bit 7 is clear on write s*/
114 #define BMI160_REG_READ BIT(7)
115 #define BMI160_REG_MASK 0x7f
116
117 /* bitfields */
118
119 /* BMI160_REG_ERR */
120 #define BMI160_ERR_FATAL BIT(0)
121 #define BMI160_ERR_CODE BIT(1)
122 #define BMI160_ERR_CODE_MASK (0xf << 1)
123 #define BMI160_ERR_I2C_FAIL BIT(5)
124 #define BMI160_ERR_DROP_CMD BIT(6)
125 #define BMI160_ERR_MAG_DRDY BIT(7)
126
127 /* BMI160_REG_PMU_STATUS */
128 #define BMI160_PMU_STATUS_MAG_MASK 0x3
129 #define BMI160_PMU_STATUS_MAG_POS 0
130 #define BMI160_PMU_STATUS_GYR_POS 2
131 #define BMI160_PMU_STATUS_GYR_MASK (0x3 << 2)
132 #define BMI160_PMU_STATUS_ACC_POS 4
133 #define BMI160_PMU_STATUS_ACC_MASK (0x3 << 4)
134
135 #define BMI160_PMU_SUSPEND 0
136 #define BMI160_PMU_NORMAL 1
137 #define BMI160_PMU_LOW_POWER 2
138 #define BMI160_PMU_FAST_START 3
139
140 /* BMI160_REG_STATUS */
141 #define BMI160_STATUS_GYR_SELFTEST BIT(1)
142 #define BMI160_STATUS_MAG_MAN_OP BIT(2)
143 #define BMI160_STATUS_FOC_RDY BIT(3)
144 #define BMI160_STATUS_NVM_RDY BIT(4)
145 #define BMI160_STATUS_MAG_DRDY BIT(5)
146 #define BMI160_STATUS_GYR_DRDY BIT(6)
147 #define BMI160_STATUS_ACC_DRDY BIT(7)
148
149 /* BMI160_REG_INT_STATUS0 */
150 #define BMI160_INT_STATUS0_STEP BIT(0)
151 #define BMI160_INT_STATUS0_SIGMOT BIT(1)
152 #define BMI160_INT_STATUS0_ANYM BIT(2)
153 #define BMI160_INT_STATUS0_PMU_TRIG BIT(3)
154 #define BMI160_INT_STATUS0_D_TAP BIT(4)
155 #define BMI160_INT_STATUS0_S_TAP BIT(5)
156 #define BMI160_INT_STATUS0_ORIENT BIT(6)
157 #define BMI160_INT_STATUS0_FLAT BIT(7)
158
159 /* BMI160_REG_INT_STATUS1 */
160 #define BMI160_INT_STATUS1_HIGHG BIT(2)
161 #define BMI160_INT_STATUS1_LOWG BIT(3)
162 #define BMI160_INT_STATUS1_DRDY BIT(4)
163 #define BMI160_INT_STATUS1_FFULL BIT(5)
164 #define BMI160_INT_STATUS1_FWM BIT(6)
165 #define BMI160_INT_STATUS1_NOMO BIT(7)
166
167 /* BMI160_REG_INT_STATUS2 */
168 #define BMI160_INT_STATUS2_ANYM_FIRST_X BIT(0)
169 #define BMI160_INT_STATUS2_ANYM_FIRST_Y BIT(1)
170 #define BMI160_INT_STATUS2_ANYM_FIRST_Z BIT(2)
171 #define BMI160_INT_STATUS2_ANYM_SIGN BIT(3)
172 #define BMI160_INT_STATUS2_TAP_FIRST_X BIT(4)
173 #define BMI160_INT_STATUS2_TAP_FIRST_Y BIT(5)
174 #define BMI160_INT_STATUS2_TAP_FIRST_Z BIT(6)
175 #define BMI160_INT_STATUS2_TAP_SIGN BIT(7)
176
177 /* BMI160_REG_INT_STATUS3 */
178 #define BMI160_INT_STATUS3_HIGH_FIRST_X BIT(0)
179 #define BMI160_INT_STATUS3_HIGH_FIRST_Y BIT(1)
180 #define BMI160_INT_STATUS3_HIGH_FIRST_Z BIT(2)
181 #define BMI160_INT_STATUS3_HIGH_SIGN BIT(3)
182 #define BMI160_INT_STATUS3_ORIENT_1_0 BIT(4)
183 #define BMI160_INT_STATUS3_ORIENT_2 BIT(6)
184 #define BMI160_INT_STATUS3_FLAT BIT(7)
185
186 /* BMI160_REG_ACC_CONF */
187 #define BMI160_ACC_CONF_ODR_POS 0
188 #define BMI160_ACC_CONF_ODR_MASK 0xF
189 #define BMI160_ACC_CONF_BWP_POS 4
190 #define BMI160_ACC_CONF_BWP_MASK (0x7 << 4)
191 #define BMI160_ACC_CONF_US_POS 7
192 #define BMI160_ACC_CONF_US_MASK BIT(7)
193
194 /* BMI160_REG_GYRO_CONF */
195 #define BMI160_GYR_CONF_ODR_POS 0
196 #define BMI160_GYR_CONF_ODR_MASK 0xF
197 #define BMI160_GYR_CONF_BWP_POS 4
198 #define BMI160_GYR_CONF_BWP_MASK (0x3 << 4)
199
200 /* BMI160_REG_OFFSET_EN */
201 #define BMI160_GYR_OFS_EN_POS 7
202 #define BMI160_ACC_OFS_EN_POS 6
203 #define BMI160_GYR_MSB_OFS_Z_POS 4
204 #define BMI160_GYR_MSB_OFS_Z_MASK (BIT(4) | BIT(5))
205 #define BMI160_GYR_MSB_OFS_Y_POS 2
206 #define BMI160_GYR_MSB_OFS_Y_MASK (BIT(2) | BIT(3))
207 #define BMI160_GYR_MSB_OFS_X_POS 0
208 #define BMI160_GYR_MSB_OFS_X_MASK (BIT(0) | BIT(1))
209
210 /* BMI160_REG_CMD */
211 #define BMI160_CMD_START_FOC 3
212 #define BMI160_CMD_PMU_ACC 0x10
213 #define BMI160_CMD_PMU_GYR 0x14
214 #define BMI160_CMD_PMU_MAG 0x18
215 #define BMI160_CMD_SOFT_RESET 0xB6
216
217 #define BMI160_CMD_PMU_BIT 0x10
218 #define BMI160_CMD_PMU_MASK 0x0c
219 #define BMI160_CMD_PMU_SHIFT 2
220 #define BMI160_CMD_PMU_VAL_MASK 0x3
221
222 /* BMI160_REG_FOC_CONF */
223 #define BMI160_FOC_ACC_Z_POS 0
224 #define BMI160_FOC_ACC_Y_POS 2
225 #define BMI160_FOC_ACC_X_POS 4
226 #define BMI160_FOC_GYR_EN_POS 6
227
228 /* BMI160_REG_INT_MOTION0 */
229 #define BMI160_ANYM_DUR_POS 0
230 #define BMI160_ANYM_DUR_MASK 0x3
231
232 /* BMI160_REG_INT_EN0 */
233 #define BMI160_INT_FLAT_EN BIT(7)
234 #define BMI160_INT_ORIENT_EN BIT(6)
235 #define BMI160_INT_S_TAP_EN BIT(5)
236 #define BMI160_INT_D_TAP_EN BIT(4)
237 #define BMI160_INT_ANYM_Z_EN BIT(2)
238 #define BMI160_INT_ANYM_Y_EN BIT(1)
239 #define BMI160_INT_ANYM_X_EN BIT(0)
240 #define BMI160_INT_ANYM_MASK (BIT(0) | BIT(1) | BIT(2))
241
242 /* BMI160_REG_INT_EN1 */
243 #define BMI160_INT_FWM_EN BIT(6)
244 #define BMI160_INT_FFULL_EN BIT(5)
245 #define BMI160_INT_DRDY_EN BIT(4)
246 #define BMI160_INT_LOWG_EN BIT(3)
247 #define BMI160_INT_HIGHG_Z_EN BIT(2)
248 #define BMI160_INT_HIGHG_Y_EN BIT(1)
249 #define BMI160_INT_HIGHG_X_EN BIT(0)
250 #define BMI160_INT_HIGHG_MASK (BIT(0) | BIT(1) | BIT(2))
251
252 /* BMI160_REG_INT_EN2 */
253 #define BMI160_INT_STEP_DET_EN BIT(3)
254 #define BMI160_INT_STEP_NOMO_Z_EN BIT(2)
255 #define BMI160_INT_STEP_NOMO_Y_EN BIT(1)
256 #define BMI160_INT_STEP_NOMO_X_EN BIT(0)
257 #define BMI160_INT_STEP_NOMO_MASK (BIT(0) | BIT(1) | BIT(2))
258
259 /* BMI160_REG_INT_OUT_CTRL */
260 #define BMI160_INT2_OUT_EN BIT(7)
261 #define BMI160_INT2_OD BIT(6)
262 #define BMI160_INT2_LVL BIT(5)
263 #define BMI160_INT2_EDGE_CTRL BIT(4)
264 #define BMI160_INT1_OUT_EN BIT(3)
265 #define BMI160_INT1_OD BIT(2)
266 #define BMI160_INT1_LVL BIT(1)
267 #define BMI160_INT1_EDGE_CTRL BIT(0)
268
269 /* other */
270 #define BMI160_CHIP_ID 0xD1
271 #define BMI160_TEMP_OFFSET 23
272
273 /* allowed ODR values */
274 enum bmi160_odr {
275 BMI160_ODR_25_32 = 1,
276 BMI160_ODR_25_16,
277 BMI160_ODR_25_8,
278 BMI160_ODR_25_4,
279 BMI160_ODR_25_2,
280 BMI160_ODR_25,
281 BMI160_ODR_50,
282 BMI160_ODR_100,
283 BMI160_ODR_200,
284 BMI160_ODR_400,
285 BMI160_ODR_800,
286 BMI160_ODR_1600,
287 BMI160_ODR_3200,
288 };
289
290 /* Range values for accelerometer */
291 #define BMI160_ACC_RANGE_2G 0x3
292 #define BMI160_ACC_RANGE_4G 0x5
293 #define BMI160_ACC_RANGE_8G 0x8
294 #define BMI160_ACC_RANGE_16G 0xC
295
296 /* Range values for gyro */
297 #define BMI160_GYR_RANGE_2000DPS 0
298 #define BMI160_GYR_RANGE_1000DPS 1
299 #define BMI160_GYR_RANGE_500DPS 2
300 #define BMI160_GYR_RANGE_250DPS 3
301 #define BMI160_GYR_RANGE_125DPS 4
302
303 #define BMI160_ACC_SCALE_NUMERATOR(range_g) (2 * (range_g) * SENSOR_G)
304 #define BMI160_ACC_SCALE_DENOMINATOR UINT16_MAX
305
306 #define BMI160_GYR_SCALE_NUMERATOR(range_dps) (2 * (range_dps) * SENSOR_PI)
307 #define BMI160_GYR_SCALE_DENOMINATOR (UINT32_C(180) * UINT16_MAX)
308
309 /* default settings, based on menuconfig options */
310
311 /* make sure at least one sensor is active */
312 #if defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND) &&\
313 defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
314 #error "Error: You need to activate at least one sensor!"
315 #endif
316
317 #if defined(CONFIG_BMI160_ACCEL_PMU_RUNTIME) ||\
318 defined(CONFIG_BMI160_ACCEL_PMU_NORMAL)
319 # define BMI160_DEFAULT_PMU_ACC BMI160_PMU_NORMAL
320 #elif defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
321 # define BMI160_DEFAULT_PMU_ACC BMI160_PMU_SUSPEND
322 #else
323 # define BMI160_DEFAULT_PMU_ACC BMI160_PMU_LOW_POWER
324 #endif
325
326 #if defined(CONFIG_BMI160_GYRO_PMU_RUNTIME) ||\
327 defined(CONFIG_BMI160_GYRO_PMU_NORMAL)
328 # define BMI160_DEFAULT_PMU_GYR BMI160_PMU_NORMAL
329 #elif defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
330 # define BMI160_DEFAULT_PMU_GYR BMI160_PMU_SUSPEND
331 #else
332 # define BMI160_DEFAULT_PMU_GYR BMI160_PMU_FAST_START
333 #endif
334
335 #if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME) ||\
336 defined(CONFIG_BMI160_ACCEL_RANGE_2G)
337 # define BMI160_DEFAULT_RANGE_ACC BMI160_ACC_RANGE_2G
338 #elif defined(CONFIG_BMI160_ACCEL_RANGE_4G)
339 # define BMI160_DEFAULT_RANGE_ACC BMI160_ACC_RANGE_4G
340 #elif defined(CONFIG_BMI160_ACCEL_RANGE_8G)
341 # define BMI160_DEFAULT_RANGE_ACC BMI160_ACC_RANGE_8G
342 #else
343 # define BMI160_DEFAULT_RANGE_ACC BMI160_ACC_RANGE_16G
344 #endif
345
346 #if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME) ||\
347 defined(CONFIG_BMI160_GYRO_RANGE_2000DPS)
348 # define BMI160_DEFAULT_RANGE_GYR BMI160_GYR_RANGE_2000DPS
349 #elif defined(CONFIG_BMI160_GYRO_RANGE_1000DPS)
350 # define BMI160_DEFAULT_RANGE_GYR BMI160_GYR_RANGE_1000DPS
351 #elif defined(CONFIG_BMI160_GYRO_RANGE_500DPS)
352 # define BMI160_DEFAULT_RANGE_GYR BMI160_GYR_RANGE_500DPS
353 #elif defined(CONFIG_BMI160_GYRO_RANGE_250DPS)
354 # define BMI160_DEFAULT_RANGE_GYR BMI160_GYR_RANGE_250DPS
355 #else
356 # define BMI160_DEFAULT_RANGE_GYR BMI160_GYR_RANGE_125DPS
357 #endif
358
359 #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME) ||\
360 defined(CONFIG_BMI160_ACCEL_ODR_100)
361 # define BMI160_DEFAULT_ODR_ACC 8
362 #elif defined(CONFIG_BMI160_ACCEL_ODR_25_32)
363 # define BMI160_DEFAULT_ODR_ACC 1
364 #elif defined(CONFIG_BMI160_ACCEL_ODR_25_16)
365 # define BMI160_DEFAULT_ODR_ACC 2
366 #elif defined(CONFIG_BMI160_ACCEL_ODR_25_8)
367 # define BMI160_DEFAULT_ODR_ACC 3
368 #elif defined(CONFIG_BMI160_ACCEL_ODR_25_4)
369 # define BMI160_DEFAULT_ODR_ACC 4
370 #elif defined(CONFIG_BMI160_ACCEL_ODR_25_2)
371 # define BMI160_DEFAULT_ODR_ACC 5
372 #elif defined(CONFIG_BMI160_ACCEL_ODR_25)
373 # define BMI160_DEFAULT_ODR_ACC 6
374 #elif defined(CONFIG_BMI160_ACCEL_ODR_50)
375 # define BMI160_DEFAULT_ODR_ACC 7
376 #elif defined(CONFIG_BMI160_ACCEL_ODR_200)
377 # define BMI160_DEFAULT_ODR_ACC 9
378 #elif defined(CONFIG_BMI160_ACCEL_ODR_400)
379 # define BMI160_DEFAULT_ODR_ACC 10
380 #elif defined(CONFIG_BMI160_ACCEL_ODR_800)
381 # define BMI160_DEFAULT_ODR_ACC 11
382 #else
383 # define BMI160_DEFAULT_ODR_ACC 12
384 #endif
385
386 #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) ||\
387 defined(CONFIG_BMI160_GYRO_ODR_100)
388 # define BMI160_DEFAULT_ODR_GYR 8
389 #elif defined(CONFIG_BMI160_GYRO_ODR_25)
390 # define BMI160_DEFAULT_ODR_GYR 6
391 #elif defined(CONFIG_BMI160_GYRO_ODR_50)
392 # define BMI160_DEFAULT_ODR_GYR 7
393 #elif defined(CONFIG_BMI160_GYRO_ODR_200)
394 # define BMI160_DEFAULT_ODR_GYR 9
395 #elif defined(CONFIG_BMI160_GYRO_ODR_400)
396 # define BMI160_DEFAULT_ODR_GYR 10
397 #elif defined(CONFIG_BMI160_GYRO_ODR_800)
398 # define BMI160_DEFAULT_ODR_GYR 11
399 #elif defined(CONFIG_BMI160_GYRO_ODR_1600)
400 # define BMI160_DEFAULT_ODR_GYR 12
401 #else
402 # define BMI160_DEFAULT_ODR_GYR 13
403 #endif
404
405 /* end of default settings */
406
407 struct bmi160_range {
408 uint16_t range;
409 uint8_t reg_val;
410 };
411
412 #define BMI160_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
413 #define BMI160_BUS_I2C DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
414
415 union bmi160_bus {
416 #if BMI160_BUS_SPI
417 struct spi_dt_spec spi;
418 #endif
419 #if BMI160_BUS_I2C
420 struct i2c_dt_spec i2c;
421 #endif
422 };
423
424 typedef bool (*bmi160_bus_ready_fn)(const struct device *dev);
425 typedef int (*bmi160_reg_read_fn)(const struct device *dev,
426 uint8_t reg_addr, void *data, uint8_t len);
427 typedef int (*bmi160_reg_write_fn)(const struct device *dev,
428 uint8_t reg_addr, void *data, uint8_t len);
429
430 struct bmi160_bus_io {
431 bmi160_bus_ready_fn ready;
432 bmi160_reg_read_fn read;
433 bmi160_reg_write_fn write;
434 };
435
436 struct bmi160_cfg {
437 union bmi160_bus bus;
438 const struct bmi160_bus_io *bus_io;
439 #if defined(CONFIG_BMI160_TRIGGER)
440 struct gpio_dt_spec interrupt;
441 #endif
442 };
443
444 union bmi160_pmu_status {
445 uint8_t raw;
446 struct {
447 uint8_t mag : 2;
448 uint8_t gyr : 2;
449 uint8_t acc : 2;
450 uint8_t res : 2;
451 };
452 };
453
454 #define BMI160_AXES 3
455
456 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND) && \
457 !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
458 # define BMI160_SAMPLE_SIZE (2 * BMI160_AXES * sizeof(uint16_t))
459 #else
460 # define BMI160_SAMPLE_SIZE (BMI160_AXES * sizeof(uint16_t))
461 #endif
462
463 #if defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
464 # define BMI160_SAMPLE_BURST_READ_ADDR BMI160_REG_DATA_ACC_X
465 # define BMI160_DATA_READY_BIT_MASK (1 << 7)
466 #else
467 # define BMI160_SAMPLE_BURST_READ_ADDR BMI160_REG_DATA_GYR_X
468 # define BMI160_DATA_READY_BIT_MASK (1 << 6)
469 #endif
470
471 #define BMI160_BUF_SIZE (BMI160_SAMPLE_SIZE)
472
473 /* Each sample has X, Y and Z */
474 union bmi160_sample {
475 uint8_t raw[BMI160_BUF_SIZE];
476 uint16_t temperature;
477 struct {
478 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
479 uint16_t gyr[BMI160_AXES];
480 #endif
481 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
482 uint16_t acc[BMI160_AXES];
483 #endif
484 };
485 };
486
487 struct bmi160_scale {
488 /* numerator / denominator => micro m/s^2/lsb */
489 int32_t acc_numerator;
490 /* numerator / denominator => micro radians/s/lsb */
491 int64_t gyr_numerator;
492 };
493
494 struct bmi160_data {
495 const struct device *bus;
496 #if defined(CONFIG_BMI160_TRIGGER)
497 const struct device *dev;
498 const struct device *gpio;
499 struct gpio_callback gpio_cb;
500 #endif
501 union bmi160_pmu_status pmu_sts;
502 union bmi160_sample sample;
503 struct bmi160_scale scale;
504
505 #ifdef CONFIG_BMI160_TRIGGER_OWN_THREAD
506 struct k_sem sem;
507 #endif
508
509 #ifdef CONFIG_BMI160_TRIGGER_GLOBAL_THREAD
510 struct k_work work;
511 #endif
512
513 #ifdef CONFIG_BMI160_TRIGGER
514 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
515 sensor_trigger_handler_t handler_drdy_acc;
516 const struct sensor_trigger *trig_drdy_acc;
517 sensor_trigger_handler_t handler_anymotion;
518 const struct sensor_trigger *trig_anymotion;
519 #endif
520 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
521 sensor_trigger_handler_t handler_drdy_gyr;
522 const struct sensor_trigger *trig_drdy_gyr;
523 #endif
524 #endif /* CONFIG_BMI160_TRIGGER */
525 };
526
527 int bmi160_read(const struct device *dev, uint8_t reg_addr,
528 void *data, uint8_t len);
529 int bmi160_byte_read(const struct device *dev, uint8_t reg_addr,
530 uint8_t *byte);
531 int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
532 uint8_t byte);
533 int bmi160_word_write(const struct device *dev, uint8_t reg_addr,
534 uint16_t word);
535 int bmi160_reg_field_update(const struct device *dev, uint8_t reg_addr,
536 uint8_t pos, uint8_t mask, uint8_t val);
bmi160_reg_update(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t val)537 static inline int bmi160_reg_update(const struct device *dev,
538 uint8_t reg_addr,
539 uint8_t mask, uint8_t val)
540 {
541 return bmi160_reg_field_update(dev, reg_addr, 0, mask, val);
542 }
543 int bmi160_trigger_mode_init(const struct device *dev);
544 int bmi160_trigger_set(const struct device *dev,
545 const struct sensor_trigger *trig,
546 sensor_trigger_handler_t handler);
547 int bmi160_acc_slope_config(const struct device *dev,
548 enum sensor_attribute attr,
549 const struct sensor_value *val);
550 int32_t bmi160_acc_reg_val_to_range(uint8_t reg_val);
551 int32_t bmi160_gyr_reg_val_to_range(uint8_t reg_val);
552
553 #ifdef __cplusplus
554 }
555 #endif
556
557 #endif /* ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_ */
558