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