1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "aos/kernel.h"
11 #include "sensor_drv_api.h"
12 #include "sensor_hal.h"
13
14
15 #define BMI055_ACC_I2C_ADDR1 (0x18)
16 #define BMI055_ACC_I2C_ADDR2 (0x19)
17 #define BMI055_ACC_I2C_ADDR3 (0x10)
18 #define BMI055_ACC_I2C_ADDR4 (0x11)
19 #define BMI055_ACC_I2C_ADDR_TRANS(n) ((n) << 1)
20 #define BMI055_ACC_I2C_ADDR BMI055_ACC_I2C_ADDR_TRANS(BMI055_ACC_I2C_ADDR1)
21
22
23 #define BMI055_ACC_INIT_VALUE (0)
24 #define BMI055_ACC_GEN_READ_WRITE_LENGTH (1)
25 #define BMI055_ACC_INTERFACE_IDLE_TIME_DELAY (1)
26 #define BMI055_ACC_LSB_MSB_READ_LENGTH (2)
27 #define BMI055_ACC_SHIFT_TWO_BITS (2)
28 #define BMI055_ACC_SHIFT_FOUR_BITS (4)
29 #define BMI055_ACC_SHIFT_FIVE_BITS (5)
30 #define BMI055_ACC_SHIFT_SIX_BITS (6)
31 #define BMI055_ACC_SHIFT_EIGHT_BITS (8)
32 #define BMI055_ACC_12_BIT_SHIFT (0xF0)
33
34 #define BMI055_ACC_FIFO_MODE_STATUS_RANGE (2)
35 #define BMI055_ACC_FIFO_DATA_SELECT_RANGE (4)
36 #define BMI055_ACC_FIFO_MODE_RANGE (4)
37 #define BMI055_ACC_FIFO_WML_RANGE (32)
38 #define BMI055_ACC_FIFO_XYZ_DATA_ENABLED (0x00)
39 #define BMI055_ACC_FIFO_X_DATA_ENABLED (0x01)
40 #define BMI055_ACC_FIFO_Y_DATA_ENABLED (0x02)
41 #define BMI055_ACC_FIFO_Z_DATA_ENABLED (0x03)
42 #define BMI055_ACC_FIFO_DATA_ENABLED_MASK (0x03)
43 #define BMI055_ACC_FIFO_XYZ_AXES_FRAME_SIZE (6)
44 #define BMI055_ACC_FIFO_SINGLE_AXIS_FRAME_SIZE (2)
45 #define BMI055_ACC_ACCEL_BW_MIN_RANGE (7)
46 #define BMI055_ACC_ACCEL_BW_1000HZ_RANGE (15)
47 #define BMI055_ACC_ACCEL_BW_MAX_RANGE (16)
48 #define BMI055_ACC_SLEEP_DURN_MIN_RANGE (4)
49 #define BMI055_ACC_SLEEP_TIMER_MODE_RANGE (2)
50 #define BMI055_ACC_SLEEP_DURN_MAX_RANGE (16)
51 #define BMI055_ACC_POWER_MODE_RANGE (6)
52 #define BMI055_ACC_SELF_TEST_AXIS_RANGE (4)
53 #define BMI055_ACC_SELF_TEST_SIGN_RANGE (2)
54
55 #define BMI055_ACC_EEP_OFFSET (0x16)
56 #define BMI055_ACC_IMAGE_BASE (0x38)
57 #define BMI055_ACC_IMAGE_LEN (22)
58 #define BMI055_ACC_CHIP_ID_ADDR (0x00)
59 #define BMI055_ACC_CHIP_ID_VALUE (0xFA)
60 #define BMI055_ACC_X_AXIS_LSB_ADDR (0x02)
61 #define BMI055_ACC_X_AXIS_MSB_ADDR (0x03)
62 #define BMI055_ACC_Y_AXIS_LSB_ADDR (0x04)
63 #define BMI055_ACC_Y_AXIS_MSB_ADDR (0x05)
64 #define BMI055_ACC_Z_AXIS_LSB_ADDR (0x06)
65 #define BMI055_ACC_Z_AXIS_MSB_ADDR (0x07)
66 #define BMI055_ACC_TEMP_ADDR (0x08)
67 #define BMI055_ACC_STAT1_ADDR (0x09)
68 #define BMI055_ACC_STAT2_ADDR (0x0A)
69 #define BMI055_ACC_STAT_TAP_SLOPE_ADDR (0x0B)
70 #define BMI055_ACC_STAT_ORIENT_HIGH_ADDR (0x0C)
71 #define BMI055_ACC_STAT_FIFO_ADDR (0x0E)
72 #define BMI055_ACC_RANGE_SELECT_ADDR (0x0F)
73 #define BMI055_ACC_BW_SELECT_ADDR (0x10)
74 #define BMI055_ACC_MODE_CTRL_ADDR (0x11)
75 #define BMI055_ACC_LOW_NOISE_CTRL_ADDR (0x12)
76 #define BMI055_ACC_DATA_CTRL_ADDR (0x13)
77 #define BMI055_ACC_RST_ADDR (0x14)
78 #define BMI055_ACC_INTR_ENABLE1_ADDR (0x16)
79 #define BMI055_ACC_INTR_ENABLE2_ADDR (0x17)
80 #define BMI055_ACC_INTR_SLOW_NO_MOTION_ADDR (0x18)
81 #define BMI055_ACC_INTR1_PAD_SELECT_ADDR (0x19)
82 #define BMI055_ACC_INTR_DATA_SELECT_ADDR (0x1A)
83 #define BMI055_ACC_INTR2_PAD_SELECT_ADDR (0x1B)
84 #define BMI055_ACC_INTR_SOURCE_ADDR (0x1E)
85 #define BMI055_ACC_INTR_SET_ADDR (0x20)
86 #define BMI055_ACC_INTR_CTRL_ADDR (0x21)
87 #define BMI055_ACC_LOW_DURN_ADDR (0x22)
88 #define BMI055_ACC_LOW_THRES_ADDR (0x23)
89 #define BMI055_ACC_LOW_HIGH_HYST_ADDR (0x24)
90 #define BMI055_ACC_HIGH_DURN_ADDR (0x25)
91 #define BMI055_ACC_HIGH_THRES_ADDR (0x26)
92 #define BMI055_ACC_SLOPE_DURN_ADDR (0x27)
93 #define BMI055_ACC_SLOPE_THRES_ADDR (0x28)
94 #define BMI055_ACC_SLOW_NO_MOTION_THRES_ADDR (0x29)
95 #define BMI055_ACC_TAP_PARAM_ADDR (0x2A)
96 #define BMI055_ACC_TAP_THRES_ADDR (0x2B)
97 #define BMI055_ACC_ORIENT_PARAM_ADDR (0x2C)
98 #define BMI055_ACC_THETA_BLOCK_ADDR (0x2D)
99 #define BMI055_ACC_THETA_FLAT_ADDR (0x2E)
100 #define BMI055_ACC_FLAT_HOLD_TIME_ADDR (0x2F)
101 #define BMI055_ACC_SELFTEST_ADDR (0x32)
102 #define BMI055_ACC_EEPROM_CTRL_ADDR (0x33)
103 #define BMI055_ACC_SERIAL_CTRL_ADDR (0x34)
104 #define BMI055_ACC_OFFSET_CTRL_ADDR (0x36)
105 #define BMI055_ACC_OFFSET_PARAMS_ADDR (0x37)
106 #define BMI055_ACC_OFFSET_X_AXIS_ADDR (0x38)
107 #define BMI055_ACC_OFFSET_Y_AXIS_ADDR (0x39)
108 #define BMI055_ACC_OFFSET_Z_AXIS_ADDR (0x3A)
109 #define BMI055_ACC_GP0_ADDR (0x3B)
110 #define BMI055_ACC_GP1_ADDR (0x3C)
111 #define BMI055_ACC_FIFO_MODE_ADDR (0x3E)
112 #define BMI055_ACC_FIFO_DATA_OUTPUT_ADDR (0x3F)
113 #define BMI055_ACC_FIFO_WML_TRIG (0x30)
114
115 #define BMI055_ACC_12_RESOLUTION (0)
116 #define BMI055_ACC_10_RESOLUTION (1)
117 #define BMI055_ACC_14_RESOLUTION (2)
118
119 #define BMI055_ACC_ENABLE_SOFT_RESET_VALUE (0xB6)
120 #define BMI055_ACC_RANGE_SELECT_POS (0)
121 #define BMI055_ACC_RANGE_SELECT_LEN (4)
122 #define BMI055_ACC_RANGE_SELECT_MSK (0x0F)
123 #define BMI055_ACC_RANGE_SELECT_REG BMI055_ACC_RANGE_SELECT_ADDR
124
125 #define BMI055_ACC_RANGE_2G (3)
126 #define BMI055_ACC_RANGE_4G (5)
127 #define BMI055_ACC_RANGE_8G (8)
128 #define BMI055_ACC_RANGE_16G (12)
129
130 #define BMI055_ACC_BW_15_63 (15)
131 #define BMI055_ACC_BW_31_25 (31)
132 #define BMI055_ACC_BW_62_5 (62)
133 #define BMI055_ACC_BW_125 (125)
134 #define BMI055_ACC_BW_250 (250)
135 #define BMI055_ACC_BW_500 (500)
136 #define BMI055_ACC_BW_1000 (1000)
137
138 #define BMI055_ACC_BW_7_81HZ (0x08)
139 #define BMI055_ACC_BW_15_63HZ (0x09)
140 #define BMI055_ACC_BW_31_25HZ (0x0A)
141 #define BMI055_ACC_BW_62_50HZ (0x0B)
142 #define BMI055_ACC_BW_125HZ (0x0C)
143 #define BMI055_ACC_BW_250HZ (0x0D)
144 #define BMI055_ACC_BW_500HZ (0x0E)
145 #define BMI055_ACC_BW_1000HZ (0x0F)
146 #define BMI055_ACC_BW_BIT_MASK (~0x0F)
147
148 #define BMI055_ACC_SLEEP_DURN_0_5MS (0x05)
149 #define BMI055_ACC_SLEEP_DURN_1MS (0x06)
150 #define BMI055_ACC_SLEEP_DURN_2MS (0x07)
151 #define BMI055_ACC_SLEEP_DURN_4MS (0x08)
152 #define BMI055_ACC_SLEEP_DURN_6MS (0x09)
153 #define BMI055_ACC_SLEEP_DURN_10MS (0x0A)
154 #define BMI055_ACC_SLEEP_DURN_25MS (0x0B)
155 #define BMI055_ACC_SLEEP_DURN_50MS (0x0C)
156 #define BMI055_ACC_SLEEP_DURN_100MS (0x0D)
157 #define BMI055_ACC_SLEEP_DURN_500MS (0x0E)
158 #define BMI055_ACC_SLEEP_DURN_1S (0x0F)
159 #define BMI055_ACC_SLEEP_DURN_POS (1)
160 #define BMI055_ACC_SLEEP_DURN_LEN (4)
161 #define BMI055_ACC_SLEEP_DURN_MSK (0x1E)
162 #define BMI055_ACC_SLEEP_DURN_REG BMI055_ACC_MODE_CTRL_ADDR
163 #define BMI055_ACC_SLEEP_MODE (0x40)
164 #define BMI055_ACC_DEEP_SUSPEND_MODE (0x20)
165 #define BMI055_ACC_SUSPEND_MODE (0x80)
166 #define BMI055_ACC_NORMAL_MODE (0x40)
167
168 #define BMI055_ACC_LOWPOWER_MODE (0x40)
169
170 #define BMI055_ACC_MODE_CTRL_POS (5)
171 #define BMI055_ACC_MODE_CTRL_LEN (3)
172 #define BMI055_ACC_MODE_CTRL_MSK (0xE0)
173 #define BMI055_ACC_MODE_CTRL_REG BMI055_ACC_MODE_CTRL_ADDR
174 #define BMI055_ACC_LOW_POWER_MODE_POS (6)
175 #define BMI055_ACC_LOW_POWER_MODE_LEN (1)
176 #define BMI055_ACC_LOW_POWER_MODE_MSK (0x40)
177 #define BMI055_ACC_LOW_POWER_MODE_REG BMI055_ACC_LOW_NOISE_CTRL_ADDR
178
179 #define BMI055_ACC_DEFAULT_ODR_100HZ (100)
180
181 #define BMI055_ACC_FIFO_DEPTH_MAX (32)
182 #define BMI055_ACC_FIFO_DATA_NUM (6)
183 #define BMI055_ACC_FIFO_BUFF_LEN (BMI055_ACC_FIFO_DEPTH_MAX * BMI055_ACC_FIFO_DATA_NUM)
184 // bmi055 sensitivity factor table, the unit is LSB/g
185 #define BMI055_ACC_IRQ_CLEAR_VAL (0x80)
186 #define BMI055_ACC_IRQ_LATCHED (0x0f)
187 #define BMI055_ACC_IRQ_NON_LATCHED (0)
188
189 #define BMI055_ACC_IRQ_FIFO_STAT (0x40)
190 #define BMI055_ACC_IRQ_DATA_READY_STAT (0x80)
191
192 #define BMI055_ACC_IRQ_DATA_READY_ENABLE (0x10)
193 #define BMI055_ACC_IRQ_FIFO_FULL_ENABLE (0x20)
194 #define BMI055_ACC_IRQ_FIFO_WML_ENABLE (0x40)
195
196 #define BMI055_ACC_IRQ_MAP_DATA_INT2 (0x80)
197 #define BMI055_ACC_IRQ_MAP_FIFO_WML_INT2 (0x40)
198 #define BMI055_ACC_IRQ_CONFIG_PUSH_PULL (0x04)
199 #define BMI055_ACC_IRQ_FIFO_WML_MODE (0x40)
200 #define BMI055_ACC_IRQ_PIN (62)
201 #define BMI055_ACC_FIFO_DEPTH_USD (20)
202 #define BMI055_ACC_DEFAULT_ODR (100)
203
204
205 #define BMI055_ACC_GET_BITSLICE(regvar, bitname) \
206 ((regvar & bitname##_MSK) >> bitname##_POS)
207
208 #define BMI055_ACC_SET_BITSLICE(regvar, bitname, val) \
209 ((regvar & ~bitname##_MSK) | ((val << bitname##_POS) & bitname##_MSK))
210
211
212
213 #define BMI055_GYRO_DEFAULT_ODR (200)
214
215 #define BMI055_GYRO_I2C_ADDR1 (0x68 << 1)
216 #define BMI055_GYRO_I2C_ADDR2 (0x69 << 1)
217
218 #define BMI055_GYRO_CHIP_ID_ADDR (0x00)
219 #define BMI055_GYRO_CHIP_ID_VALUE (0x0F)
220
221 #define BMI055_GYRO_RATE_X_LSB_ADDR (0x02)
222 #define BMI055_GYRO_RATE_X_MSB_ADDR (0x03)
223 #define BMI055_GYRO_RATE_Y_LSB_ADDR (0x04)
224 #define BMI055_GYRO_RATE_Y_MSB_ADDR (0x05)
225 #define BMI055_GYRO_RATE_Z_LSB_ADDR (0x06)
226 #define BMI055_GYRO_RATE_Z_MSB_ADDR (0x07)
227 #define BMI055_GYRO_TEMP_ADDR (0x08)
228 #define BMI055_GYRO_INTR_STAT0_ADDR (0x09)
229 #define BMI055_GYRO_INTR_STAT1_ADDR (0x0A)
230 #define BMI055_GYRO_INTR_STAT2_ADDR (0x0B)
231 #define BMI055_GYRO_INTR_STAT3_ADDR (0x0C)
232 #define BMI055_GYRO_FIFO_STAT_ADDR (0x0E)
233 #define BMI055_GYRO_RANGE_ADDR (0x0F)
234 #define BMI055_GYRO_BW_ADDR (0x10)
235 #define BMI055_GYRO_MODE_LPM1_ADDR (0x11)
236 #define BMI055_GYRO_MODE_LPM2_ADDR (0x12)
237 #define BMI055_GYRO_HIGH_BW_ADDR (0x13)
238 #define BMI055_GYRO_BGW_SOFT_RST_ADDR (0x14)
239 #define BMI055_GYRO_INTR_ENABLE0_ADDR (0x15)
240 #define BMI055_GYRO_INTR_ENABLE1_ADDR (0x16)
241 #define BMI055_GYRO_INTR_MAP_ZERO_ADDR (0x17)
242 #define BMI055_GYRO_INTR_MAP_ONE_ADDR (0x18)
243 #define BMI055_GYRO_INTR_MAP_TWO_ADDR (0x19)
244 #define BMI055_GYRO_INTR_ZERO_ADDR (0x1A)
245 #define BMI055_GYRO_INTR_ONE_ADDR (0x1B)
246 #define BMI055_GYRO_INTR_TWO_ADDR (0x1C)
247 #define BMI055_GYRO_INTR_4_ADDR (0x1E)
248 #define BMI055_GYRO_RST_LATCH_ADDR (0x21)
249 #define BMI055_GYRO_HIGHRATE_THRES_X_ADDR (0x22)
250 #define BMI055_GYRO_HIGHRATE_DURN_X_ADDR (0x23)
251 #define BMI055_GYRO_HIGHRATE_THRES_Y_ADDR (0x24)
252 #define BMI055_GYRO_HIGHRATE_DURN_Y_ADDR (0x25)
253 #define BMI055_GYRO_HIGHRATE_THRES_Z_ADDR (0x26)
254 #define BMI055_GYRO_HIGHRATE_DURN_Z_ADDR (0x27)
255 #define BMI055_GYRO_SOC_ADDR (0x31)
256 #define BMI055_GYRO_A_FOC_ADDR (0x32)
257 #define BMI055_GYRO_TRIM_NVM_CTRL_ADDR (0x33)
258 #define BMI055_GYRO_BGW_SPI3_WDT_ADDR (0x34)
259
260 #define BMI055_GYRO_OFFSET_OFC1_ADDR (0x36)
261 #define BMI055_GYRO_OFC2_ADDR (0x37)
262 #define BMI055_GYRO_OFC3_ADDR (0x38)
263 #define BMI055_GYRO_OFC4_ADDR (0x39)
264 #define BMI055_GYRO_TRIM_GP0_ADDR (0x3A)
265 #define BMI055_GYRO_TRIM_GP1_ADDR (0x3B)
266 #define BMI055_GYRO_SELFTEST_ADDR (0x3C)
267 #define BMI055_GYRO_FIFO_CGF1_ADDR (0x3D)
268 #define BMI055_GYRO_FIFO_CGF0_ADDR (0x3E)
269
270 #define BMI055_GYRO_FIFO_DATA_ADDR (0x3F)
271
272
273 #define BMI055_GYRO_RANGE_RANGE_2000DPS (0x00)
274 #define BMI055_GYRO_RANGE_RANGE_1000DPS (0x01)
275 #define BMI055_GYRO_RANGE_RANGE_500DPS (0x02)
276 #define BMI055_GYRO_RANGE_RANGE_250DPS (0x03)
277 #define BMI055_GYRO_RANGE_RANGE_125DPS (0x04)
278 #define BMI055_GYRO_RANGE_ADDR_RANGE_POS (0)
279 #define BMI055_GYRO_RANGE_ADDR_RANGE_LEN (3)
280 #define BMI055_GYRO_RANGE_ADDR_RANGE_MSK (0x07)
281 #define BMI055_GYRO_RANGE_ADDR_RANGE_REG (BMI055_GYRO_RANGE_ADDR)
282
283
284 #define BMI055_GYRO_BW_ADDR_POS (0)
285 #define BMI055_GYRO_BW_ADDR_LEN (3)
286 #define BMI055_GYRO_BW_ADDR_MSK (0x07)
287 #define BMI055_GYRO_BW_ADDR_REG (BMI055_GYRO_BW_ADDR)
288
289 #define BMI055_GYRO_MODE_LPM1_POS (5)
290 #define BMI055_GYRO_MODE_LPM1_LEN (3)
291 #define BMI055_GYRO_MODE_LPM1_MSK (0xA0)
292 #define BMI055_GYRO_MODE_LPM1_REG (BMI055_GYRO_MODE_LPM1_ADDR)
293
294
295 #define BMI055_GYRO_ODR_100 (100)
296 #define BMI055_GYRO_ODR_200 (200)
297 #define BMI055_GYRO_ODR_400 (400)
298 #define BMI055_GYRO_ODR_1000 (1000)
299 #define BMI055_GYRO_ODR_2000 (2000)
300
301 #define BMI055_GYRO_ODR_100_REG (0x07)
302 #define BMI055_GYRO_ODR_200_REG (0x06)
303 #define BMI055_GYRO_ODR_400_REG (0x03)
304 #define BMI055_GYRO_ODR_1000_REG (0x02)
305 #define BMI055_GYRO_ODR_2000_REG (0x01)
306
307 #define BMI055_GYRO_POWER_BIT_MASK (0xA0)
308 #define BMI055_GYRO_POWER_NORMAL_REG (0x00)
309 #define BMI055_GYRO_POWER_SLEEP_REG (0x80)
310 #define BMI055_GYRO_POWER_DEEP_SUSPEND_REG (0x20)
311
312 #define BMI055_GYRO_SHIFT_EIGHT_BITS (8)
313 #define BMI055_GYRO_DEFAULT_ODR (200)
314
315 static uint32_t bmi055_acc_factor[4] = { 1024, 512, 256, 128 };
316 static uint32_t acc_current_factor = 0;
317
318 static uint32_t bmi055_gyro_factor[5] = { 2624, 1312, 656, 328,
319 164 };
320 static uint32_t gyro_current_factor = 0;
321
322 i2c_dev_t bmi055_acc_ctx = {
323 .port = 3,
324 .config.dev_addr = BMI055_ACC_I2C_ADDR,
325 };
326
327 i2c_dev_t bmi055_gyro_ctx = {
328 .port = 3,
329 .config.dev_addr = BMI055_GYRO_I2C_ADDR1,
330 };
331
drv_acc_bosch_bmi055_soft_reset(i2c_dev_t * drv)332 int drv_acc_bosch_bmi055_soft_reset(i2c_dev_t *drv)
333 {
334 int ret = 0;
335 uint8_t value = BMI055_ACC_ENABLE_SOFT_RESET_VALUE;
336 ret = sensor_i2c_write(drv, BMI055_ACC_RST_ADDR, &value, I2C_DATA_LEN,
337 I2C_OP_RETRIES);
338 if (unlikely(ret)) {
339 return -1;
340 }
341 return 0;
342 }
343
drv_acc_bosch_bmi055_validate_id(i2c_dev_t * drv,uint8_t id_value)344 int drv_acc_bosch_bmi055_validate_id(i2c_dev_t *drv, uint8_t id_value)
345 {
346 uint8_t value = 0x00;
347 int ret = 0;
348
349 if (drv == NULL) {
350 return -1;
351 }
352
353 ret = sensor_i2c_read(drv, BMI055_ACC_CHIP_ID_ADDR, &value, I2C_DATA_LEN,
354 I2C_OP_RETRIES);
355 if (unlikely(ret)) {
356 return ret;
357 }
358
359 if (id_value != value) {
360 return -1;
361 }
362
363 return 0;
364 }
365
drv_acc_bosch_bmi055_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)366 int drv_acc_bosch_bmi055_set_power_mode(i2c_dev_t * drv,
367 dev_power_mode_e mode)
368 {
369 uint8_t value = 0x00;
370 int ret = 0;
371
372 ret = sensor_i2c_read(drv, BMI055_ACC_MODE_CTRL_REG, &value, I2C_DATA_LEN,
373 I2C_OP_RETRIES);
374 if (unlikely(ret)) {
375 return ret;
376 }
377
378 switch (mode) {
379 case DEV_POWER_ON: {
380 if ((value & BMI055_ACC_MODE_CTRL_MSK) == BMI055_ACC_NORMAL_MODE) {
381 return 0;
382 }
383 value |= BMI055_ACC_NORMAL_MODE;
384 ret = sensor_i2c_write(drv, BMI055_ACC_MODE_CTRL_REG, &value,
385 I2C_DATA_LEN, I2C_OP_RETRIES);
386 if (unlikely(ret)) {
387 return ret;
388 }
389 } break;
390
391 case DEV_POWER_OFF: {
392 if ((value & BMI055_ACC_MODE_CTRL_MSK) == BMI055_ACC_DEEP_SUSPEND_MODE) {
393 return 0;
394 }
395
396 value |= BMI055_ACC_DEEP_SUSPEND_MODE;
397 ret = sensor_i2c_write(drv, BMI055_ACC_MODE_CTRL_REG, &value,
398 I2C_DATA_LEN, I2C_OP_RETRIES);
399 if (unlikely(ret)) {
400 return ret;
401 }
402 } break;
403
404 case DEV_SLEEP: {
405 if ((value & BMI055_ACC_MODE_CTRL_MSK) == BMI055_ACC_SLEEP_MODE) {
406 return 0;
407 }
408
409 value |= BMI055_ACC_SLEEP_MODE;
410 ret = sensor_i2c_write(drv, BMI055_ACC_MODE_CTRL_REG, &value,
411 I2C_DATA_LEN, I2C_OP_RETRIES);
412 if (unlikely(ret)) {
413 return ret;
414 }
415
416 } break;
417
418 case DEV_SUSPEND: {
419 if ((value & BMI055_ACC_MODE_CTRL_MSK) == BMI055_ACC_SUSPEND_MODE) {
420 return 0;
421 }
422
423 value |= BMI055_ACC_SUSPEND_MODE;
424 ret = sensor_i2c_write(drv, BMI055_ACC_MODE_CTRL_REG, &value,
425 I2C_DATA_LEN, I2C_OP_RETRIES);
426 if (unlikely(ret)) {
427 return ret;
428 }
429
430 } break;
431
432 default:
433 break;
434 }
435 return 0;
436 }
437
drv_acc_bosch_bmi055_set_odr(i2c_dev_t * drv,uint32_t odr)438 int drv_acc_bosch_bmi055_set_odr(i2c_dev_t *drv, uint32_t odr)
439 {
440 int ret = 0;
441 uint8_t value = 0x00;
442 uint32_t bw = odr / 2;
443
444 ret = sensor_i2c_read(drv, BMI055_ACC_BW_SELECT_ADDR, &value, I2C_DATA_LEN,
445 I2C_OP_RETRIES);
446 if (unlikely(ret)) {
447 return ret;
448 }
449
450 if (bw >= BMI055_ACC_BW_1000) {
451 value &= BMI055_ACC_BW_BIT_MASK;
452 value |= BMI055_ACC_BW_1000HZ;
453 } else if (bw >= BMI055_ACC_BW_500) {
454 value &= BMI055_ACC_BW_BIT_MASK;
455 value |= BMI055_ACC_BW_500HZ;
456 } else if (bw >= BMI055_ACC_BW_250) {
457 value &= BMI055_ACC_BW_BIT_MASK;
458 value |= BMI055_ACC_BW_250HZ;
459 } else if (bw >= BMI055_ACC_BW_125) {
460 value &= BMI055_ACC_BW_BIT_MASK;
461 value |= BMI055_ACC_BW_125HZ;
462 } else if (bw >= BMI055_ACC_BW_62_5) {
463 value &= BMI055_ACC_BW_BIT_MASK;
464 value |= BMI055_ACC_BW_62_50HZ;
465 } else if (bw >= BMI055_ACC_BW_31_25) {
466 value &= BMI055_ACC_BW_BIT_MASK;
467 value |= BMI055_ACC_BW_31_25HZ;
468 } else {
469 value &= BMI055_ACC_BW_BIT_MASK;
470 value |= BMI055_ACC_BW_15_63HZ;
471 }
472
473 ret = sensor_i2c_write(drv, BMI055_ACC_BW_SELECT_ADDR, &value, I2C_DATA_LEN,
474 I2C_OP_RETRIES);
475 if (unlikely(ret)) {
476 return ret;
477 }
478 return 0;
479 }
480
drv_acc_bosch_bmi055_set_range(i2c_dev_t * drv,uint32_t range)481 int drv_acc_bosch_bmi055_set_range(i2c_dev_t *drv, uint32_t range)
482 {
483 int ret = 0;
484 uint8_t value = 0x00;
485
486 ret = sensor_i2c_read(drv, BMI055_ACC_RANGE_SELECT_REG, &value, I2C_DATA_LEN,
487 I2C_OP_RETRIES);
488 if (unlikely(ret)) {
489 return ret;
490 }
491
492 switch (range) {
493 case ACC_RANGE_2G: {
494 value =
495 BMI055_ACC_SET_BITSLICE(value, BMI055_ACC_RANGE_SELECT, BMI055_ACC_RANGE_2G);
496 } break;
497
498 case ACC_RANGE_4G: {
499 value =
500 BMI055_ACC_SET_BITSLICE(value, BMI055_ACC_RANGE_SELECT, BMI055_ACC_RANGE_4G);
501 } break;
502
503 case ACC_RANGE_8G: {
504 value =
505 BMI055_ACC_SET_BITSLICE(value, BMI055_ACC_RANGE_SELECT, BMI055_ACC_RANGE_8G);
506 } break;
507
508 case ACC_RANGE_16G: {
509 value =
510 BMI055_ACC_SET_BITSLICE(value, BMI055_ACC_RANGE_SELECT, BMI055_ACC_RANGE_16G);
511 } break;
512
513 default:
514 break;
515 }
516
517 ret = sensor_i2c_write(drv, BMI055_ACC_RANGE_SELECT_REG, &value, I2C_DATA_LEN,
518 I2C_OP_RETRIES);
519 if (unlikely(ret)) {
520 return ret;
521 }
522
523 if ((range >= ACC_RANGE_2G) && (range <= ACC_RANGE_16G)) {
524 acc_current_factor = bmi055_acc_factor[range];
525 }
526
527 return 0;
528 }
529
530
drv_acc_bosch_bmi055_open(void)531 int drv_acc_bosch_bmi055_open(void)
532 {
533 int ret = 0;
534
535 ret = drv_acc_bosch_bmi055_set_power_mode(&bmi055_acc_ctx, DEV_POWER_ON);
536 if (unlikely(ret)) {
537 return -1;
538 }
539 return 0;
540 }
541
drv_acc_bosch_bmi055_close(void)542 int drv_acc_bosch_bmi055_close(void)
543 {
544 int ret = 0;
545 ret = drv_acc_bosch_bmi055_set_power_mode(&bmi055_acc_ctx, DEV_POWER_OFF);
546 if (unlikely(ret)) {
547 return -1;
548 }
549 return 0;
550 }
551
drv_acc_bosch_bmi055_read(void * buf,size_t len)552 int drv_acc_bosch_bmi055_read(void *buf, size_t len)
553 {
554 int ret = 0;
555 size_t size;
556 uint8_t reg[6];
557 accel_data_t *accel = (accel_data_t *)buf;
558 if (buf == NULL) {
559 return -1;
560 }
561
562 size = sizeof(accel_data_t);
563 if (len < size) {
564 return -1;
565 }
566
567 ret = sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_X_AXIS_LSB_ADDR, ®[0],
568 I2C_REG_LEN, I2C_OP_RETRIES);
569 ret |= sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_X_AXIS_MSB_ADDR, ®[1],
570 I2C_REG_LEN, I2C_OP_RETRIES);
571 ret |= sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_Y_AXIS_LSB_ADDR, ®[2],
572 I2C_REG_LEN, I2C_OP_RETRIES);
573 ret |= sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_Y_AXIS_MSB_ADDR, ®[3],
574 I2C_REG_LEN, I2C_OP_RETRIES);
575 ret |= sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_Z_AXIS_LSB_ADDR, ®[4],
576 I2C_REG_LEN, I2C_OP_RETRIES);
577 ret |= sensor_i2c_read(&bmi055_acc_ctx, BMI055_ACC_Z_AXIS_MSB_ADDR, ®[5],
578 I2C_REG_LEN, I2C_OP_RETRIES);
579 if (unlikely(ret)) {
580 return -1;
581 }
582
583 accel->data[DATA_AXIS_X] =
584 (int32_t)((((int32_t)((int8_t)reg[1])) << BMI055_ACC_SHIFT_EIGHT_BITS) |
585 (reg[0] & BMI055_ACC_12_BIT_SHIFT));
586 accel->data[DATA_AXIS_X] =
587 accel->data[DATA_AXIS_X] >> BMI055_ACC_SHIFT_FOUR_BITS;
588
589 accel->data[DATA_AXIS_Y] =
590 (int32_t)((((int32_t)((int8_t)reg[3])) << BMI055_ACC_SHIFT_EIGHT_BITS) |
591 (reg[2] & BMI055_ACC_12_BIT_SHIFT));
592 accel->data[DATA_AXIS_Y] =
593 accel->data[DATA_AXIS_Y] >> BMI055_ACC_SHIFT_FOUR_BITS;
594
595 accel->data[DATA_AXIS_Z] =
596 (int32_t)((((int32_t)((int8_t)reg[5])) << BMI055_ACC_SHIFT_EIGHT_BITS) |
597 (reg[4] & BMI055_ACC_12_BIT_SHIFT));
598 accel->data[DATA_AXIS_Z] =
599 accel->data[DATA_AXIS_Z] >> BMI055_ACC_SHIFT_FOUR_BITS;
600
601 if (acc_current_factor != 0) {
602 // the unit of acc is mg, 1000 mg = 1 g.
603 accel->data[DATA_AXIS_X] = accel->data[DATA_AXIS_X] *
604 ACCELEROMETER_UNIT_FACTOR /
605 (int32_t)acc_current_factor;
606 accel->data[DATA_AXIS_Y] = accel->data[DATA_AXIS_Y] *
607 ACCELEROMETER_UNIT_FACTOR /
608 (int32_t)acc_current_factor;
609 accel->data[DATA_AXIS_Z] = accel->data[DATA_AXIS_Z] *
610 ACCELEROMETER_UNIT_FACTOR /
611 (int32_t)acc_current_factor;
612 }
613 accel->timestamp = aos_now_ms();
614
615 return (int)size;
616 }
617
618
drv_acc_bosch_bmi055_irq_handle(void)619 void drv_acc_bosch_bmi055_irq_handle(void)
620 {
621 }
622
drv_acc_bosch_bmi055_ioctl(int cmd,unsigned long arg)623 static int drv_acc_bosch_bmi055_ioctl(int cmd, unsigned long arg)
624 {
625 int ret = 0;
626
627 switch (cmd) {
628 case SENSOR_IOCTL_ODR_SET: {
629 ret = drv_acc_bosch_bmi055_set_odr(&bmi055_acc_ctx, arg);
630 if (unlikely(ret)) {
631 return -1;
632 }
633 } break;
634 case SENSOR_IOCTL_RANGE_SET: {
635 ret = drv_acc_bosch_bmi055_set_range(&bmi055_acc_ctx, arg);
636 if (unlikely(ret)) {
637 return -1;
638 }
639 } break;
640 case SENSOR_IOCTL_SET_POWER: {
641 ret = drv_acc_bosch_bmi055_set_power_mode(&bmi055_acc_ctx, arg);
642 if (unlikely(ret)) {
643 return -1;
644 }
645 } break;
646 case SENSOR_IOCTL_GET_INFO: {
647 /* fill the dev info here */
648 dev_sensor_info_t *info = (dev_sensor_info_t *)arg;
649 info->model = "BMI055";
650 info->range_max = 16;
651 info->range_min = 2;
652 info->unit = mg;
653 } break;
654
655 default:
656 break;
657 }
658
659 return 0;
660 }
661
drv_acc_bosch_bmi055_init(void)662 int drv_acc_bosch_bmi055_init(void)
663 {
664 int ret = 0;
665 sensor_obj_t sensor;
666
667 memset(&sensor, 0, sizeof(sensor));
668
669 /* fill the sensor obj parameters here */
670 sensor.io_port = I2C_PORT;
671 sensor.tag = TAG_DEV_ACC;
672 sensor.path = dev_acc_path;
673 sensor.open = drv_acc_bosch_bmi055_open;
674 sensor.close = drv_acc_bosch_bmi055_close;
675 sensor.read = drv_acc_bosch_bmi055_read;
676 sensor.write = NULL;
677 sensor.ioctl = drv_acc_bosch_bmi055_ioctl;
678 sensor.irq_handle = drv_acc_bosch_bmi055_irq_handle;
679 sensor.mode = DEV_POLLING;
680 sensor.data_len = sizeof(accel_data_t);
681
682 ret = sensor_create_obj(&sensor);
683 if (unlikely(ret)) {
684 return -1;
685 }
686
687 ret = drv_acc_bosch_bmi055_validate_id(&bmi055_acc_ctx, BMI055_ACC_CHIP_ID_VALUE);
688 if (unlikely(ret)) {
689 return -1;
690 }
691
692 ret = drv_acc_bosch_bmi055_soft_reset(&bmi055_acc_ctx);
693 if (unlikely(ret)) {
694 return -1;
695 }
696
697 aos_msleep(5);
698 ret = drv_acc_bosch_bmi055_set_range(&bmi055_acc_ctx, ACC_RANGE_8G);
699 if (unlikely(ret)) {
700 return -1;
701 }
702
703 // set odr is 100hz, and will update
704 ret = drv_acc_bosch_bmi055_set_odr(&bmi055_acc_ctx, BMI055_ACC_DEFAULT_ODR);
705 if (unlikely(ret)) {
706 return -1;
707 }
708 ret = drv_acc_bosch_bmi055_set_power_mode(&bmi055_acc_ctx, DEV_SLEEP);
709 if (unlikely(ret)) {
710 return -1;
711 }
712
713 /* update the phy sensor info to sensor hal */
714 LOG("%s %s successfully \n", SENSOR_STR, __func__);
715 return 0;
716 }
717
drv_gyro_bosch_bmi055_validate_id(i2c_dev_t * drv,uint8_t id_value)718 static int drv_gyro_bosch_bmi055_validate_id(i2c_dev_t *drv, uint8_t id_value)
719 {
720 uint8_t value = 0x00;
721 int ret = 0;
722
723 if (drv == NULL) {
724 return -1;
725 }
726
727 ret = sensor_i2c_read(drv, BMI055_GYRO_CHIP_ID_ADDR, &value, I2C_DATA_LEN,
728 I2C_OP_RETRIES);
729 if (unlikely(ret)) {
730 return ret;
731 }
732
733 if (id_value != value) {
734 return -1;
735 }
736
737 return 0;
738 }
739
drv_gyro_bosch_bmi055_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)740 static int drv_gyro_bosch_bmi055_set_power_mode(i2c_dev_t * drv,
741 dev_power_mode_e mode)
742 {
743 uint8_t value = 0x00;
744 int ret = 0;
745
746 ret = sensor_i2c_read(drv, BMI055_GYRO_MODE_LPM1_ADDR, &value, I2C_DATA_LEN,
747 I2C_OP_RETRIES);
748 if (unlikely(ret)) {
749 return ret;
750 }
751 switch (mode) {
752 case DEV_POWER_ON: {
753 if ((value & BMI055_GYRO_POWER_BIT_MASK) == BMI055_GYRO_POWER_NORMAL_REG) {
754 return 0;
755 }
756 value &= (~BMI055_GYRO_POWER_BIT_MASK);
757 value |= BMI055_GYRO_POWER_NORMAL_REG;
758 ret = sensor_i2c_write(drv, BMI055_GYRO_MODE_LPM1_ADDR, &value,
759 I2C_DATA_LEN, I2C_OP_RETRIES);
760 if (unlikely(ret)) {
761 return ret;
762 }
763 } break;
764
765 case DEV_POWER_OFF: {
766 if ((value & BMI055_GYRO_POWER_BIT_MASK) ==
767 BMI055_GYRO_POWER_DEEP_SUSPEND_REG) {
768 return 0;
769 }
770
771 value &= (~BMI055_GYRO_POWER_BIT_MASK);
772 value |= BMI055_GYRO_POWER_DEEP_SUSPEND_REG;
773 ret = sensor_i2c_write(drv, BMI055_GYRO_MODE_LPM1_ADDR, &value,
774 I2C_DATA_LEN, I2C_OP_RETRIES);
775 if (unlikely(ret)) {
776 return ret;
777 }
778 } break;
779
780 case DEV_SLEEP: {
781 if ((value & BMI055_GYRO_POWER_BIT_MASK) == BMI055_GYRO_POWER_SLEEP_REG) {
782 return 0;
783 }
784
785 value &= (~BMI055_GYRO_POWER_BIT_MASK);
786 value |= BMI055_GYRO_POWER_SLEEP_REG;
787 ret = sensor_i2c_write(drv, BMI055_GYRO_MODE_LPM1_ADDR, &value,
788 I2C_DATA_LEN, I2C_OP_RETRIES);
789 if (unlikely(ret)) {
790 return ret;
791 }
792
793 } break;
794
795 default:
796 break;
797 }
798 return 0;
799 }
800
drv_gyro_bosch_bmi055_set_odr(i2c_dev_t * drv,uint32_t odr)801 static int drv_gyro_bosch_bmi055_set_odr(i2c_dev_t *drv, uint32_t odr)
802 {
803 int ret = 0;
804 uint8_t value = 0x00;
805
806 ret = sensor_i2c_read(drv, BMI055_GYRO_BW_ADDR, &value, I2C_DATA_LEN,
807 I2C_OP_RETRIES);
808 if (unlikely(ret)) {
809 return ret;
810 }
811
812 if (odr >= BMI055_GYRO_ODR_2000) {
813 value &= BMI055_GYRO_BW_ADDR_MSK;
814 value |= BMI055_GYRO_ODR_2000_REG;
815 } else if (odr >= BMI055_GYRO_ODR_1000) {
816 value &= BMI055_GYRO_BW_ADDR_MSK;
817 value |= BMI055_GYRO_ODR_1000_REG;
818 } else if (odr >= BMI055_GYRO_ODR_400) {
819 value &= BMI055_GYRO_BW_ADDR_MSK;
820 value |= BMI055_GYRO_ODR_400_REG;
821 } else if (odr >= BMI055_GYRO_ODR_200) {
822 value &= BMI055_GYRO_BW_ADDR_MSK;
823 value |= BMI055_GYRO_ODR_200_REG;
824 } else {
825 value &= BMI055_GYRO_BW_ADDR_MSK;
826 value |= BMI055_GYRO_ODR_100_REG;
827 }
828
829 /* Write the range register 0x0F*/
830 ret = sensor_i2c_write(drv, BMI055_GYRO_BW_ADDR, &value, I2C_DATA_LEN,
831 I2C_OP_RETRIES);
832 if (unlikely(ret)) {
833 return ret;
834 }
835 return 0;
836 }
837
drv_gyro_bosch_bmi055_set_range(i2c_dev_t * drv,uint32_t range)838 static int drv_gyro_bosch_bmi055_set_range(i2c_dev_t *drv, uint32_t range)
839 {
840 int ret = 0;
841 uint8_t value = 0x00;
842
843 ret = sensor_i2c_read(drv, BMI055_GYRO_RANGE_ADDR, &value, I2C_DATA_LEN,
844 I2C_OP_RETRIES);
845 if (unlikely(ret)) {
846 return ret;
847 }
848
849 switch (range) {
850 case GYRO_RANGE_125DPS: {
851 value &= BMI055_GYRO_RANGE_ADDR_RANGE_MSK;
852 value |= BMI055_GYRO_RANGE_RANGE_125DPS;
853 } break;
854
855 case GYRO_RANGE_250DPS: {
856 value &= BMI055_GYRO_RANGE_ADDR_RANGE_MSK;
857 value |= BMI055_GYRO_RANGE_RANGE_250DPS;
858 } break;
859
860 case GYRO_RANGE_500DPS: {
861 value &= BMI055_GYRO_RANGE_ADDR_RANGE_MSK;
862 value |= BMI055_GYRO_RANGE_RANGE_500DPS;
863 } break;
864
865 case GYRO_RANGE_1000DPS: {
866 value &= BMI055_GYRO_RANGE_ADDR_RANGE_MSK;
867 value |= BMI055_GYRO_RANGE_RANGE_1000DPS;
868 } break;
869
870 case GYRO_RANGE_2000DPS: {
871 value &= BMI055_GYRO_RANGE_ADDR_RANGE_MSK;
872 value |= BMI055_GYRO_RANGE_RANGE_2000DPS;
873 } break;
874
875 default:
876 break;
877 }
878
879 /* Write the range register 0x0F*/
880 ret = sensor_i2c_write(drv, BMI055_GYRO_RANGE_ADDR, &value, I2C_DATA_LEN,
881 I2C_OP_RETRIES);
882 if (unlikely(ret)) {
883 return ret;
884 }
885
886 if ((range >= GYRO_RANGE_125DPS) && (range <= GYRO_RANGE_2000DPS)) {
887 gyro_current_factor = bmi055_gyro_factor[range];
888 }
889 return 0;
890 }
891
drv_gyro_bosch_bmi055_irq_handle(void)892 static void drv_gyro_bosch_bmi055_irq_handle(void)
893 {
894 /* no handle so far */
895 }
896
drv_gyro_bosch_bmi055_open(void)897 static int drv_gyro_bosch_bmi055_open(void)
898 {
899 int ret = 0;
900 ret = drv_gyro_bosch_bmi055_set_power_mode(&bmi055_gyro_ctx, DEV_POWER_ON);
901 if (unlikely(ret)) {
902 return -1;
903 }
904
905 LOG("%s %s successfully \n", SENSOR_STR, __func__);
906 return 0;
907 }
908
drv_gyro_bosch_bmi055_close(void)909 static int drv_gyro_bosch_bmi055_close(void)
910 {
911 int ret = 0;
912 ret = drv_gyro_bosch_bmi055_set_power_mode(&bmi055_gyro_ctx, DEV_POWER_OFF);
913 if (unlikely(ret)) {
914 return -1;
915 }
916 return 0;
917 }
918
drv_gyro_bosch_bmi055_read(void * buf,size_t len)919 static int drv_gyro_bosch_bmi055_read(void *buf, size_t len)
920 {
921 int ret = 0;
922 size_t size = 0;
923 uint8_t reg[6];
924 gyro_data_t *gyro = (gyro_data_t *)buf;
925 if (buf == NULL) {
926 return -1;
927 }
928
929 size = sizeof(gyro_data_t);
930 if (len < size) {
931 return -1;
932 }
933
934 ret = sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_X_LSB_ADDR, ®[0],
935 I2C_REG_LEN, I2C_OP_RETRIES);
936 ret |= sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_X_MSB_ADDR, ®[1],
937 I2C_REG_LEN, I2C_OP_RETRIES);
938 ret |= sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_Y_LSB_ADDR, ®[2],
939 I2C_REG_LEN, I2C_OP_RETRIES);
940 ret |= sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_Y_MSB_ADDR, ®[3],
941 I2C_REG_LEN, I2C_OP_RETRIES);
942 ret |= sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_Z_LSB_ADDR, ®[4],
943 I2C_REG_LEN, I2C_OP_RETRIES);
944 ret |= sensor_i2c_read(&bmi055_gyro_ctx, BMI055_GYRO_RATE_Z_MSB_ADDR, ®[5],
945 I2C_REG_LEN, I2C_OP_RETRIES);
946 if (unlikely(ret)) {
947 return -1;
948 }
949 gyro->data[DATA_AXIS_X] = (int32_t)(
950 (((int32_t)((int8_t)reg[1])) << BMI055_GYRO_SHIFT_EIGHT_BITS) | reg[0]);
951 gyro->data[DATA_AXIS_Y] = (int32_t)(
952 (((int32_t)((int8_t)reg[3])) << BMI055_GYRO_SHIFT_EIGHT_BITS) | reg[2]);
953 gyro->data[DATA_AXIS_Z] = (int32_t)(
954 (((int32_t)((int8_t)reg[5])) << BMI055_GYRO_SHIFT_EIGHT_BITS) | reg[4]);
955
956 if (gyro_current_factor != 0) {
957 // the unit of gyro is uDPS, 1000 000 uDPS = 1 DPS
958 gyro->data[DATA_AXIS_X] = (int32_t)((int64_t)gyro->data[DATA_AXIS_X] * GYROSCOPE_UNIT_FACTOR * 10 / gyro_current_factor);
959 gyro->data[DATA_AXIS_Y] = (int32_t)((int64_t)gyro->data[DATA_AXIS_Y] * GYROSCOPE_UNIT_FACTOR * 10 / gyro_current_factor);
960 gyro->data[DATA_AXIS_Z] = (int32_t)((int64_t)gyro->data[DATA_AXIS_Z] * GYROSCOPE_UNIT_FACTOR * 10 / gyro_current_factor);
961 }
962
963 gyro->timestamp = aos_now_ms();
964 return (int)size;
965 }
966
967
drv_gyro_bosch_bmi055_ioctl(int cmd,unsigned long arg)968 static int drv_gyro_bosch_bmi055_ioctl(int cmd, unsigned long arg)
969 {
970 int ret = 0;
971
972 switch (cmd) {
973 case SENSOR_IOCTL_ODR_SET: {
974 ret = drv_gyro_bosch_bmi055_set_odr(&bmi055_gyro_ctx, arg);
975 if (unlikely(ret)) {
976 return -1;
977 }
978 } break;
979 case SENSOR_IOCTL_RANGE_SET: {
980 ret = drv_gyro_bosch_bmi055_set_range(&bmi055_gyro_ctx, arg);
981 if (unlikely(ret)) {
982 return -1;
983 }
984 } break;
985 case SENSOR_IOCTL_SET_POWER: {
986 ret = drv_gyro_bosch_bmi055_set_power_mode(&bmi055_gyro_ctx, arg);
987 if (unlikely(ret)) {
988 return -1;
989 }
990 } break;
991 case SENSOR_IOCTL_GET_INFO: {
992 /* fill the dev info here */
993 dev_sensor_info_t *info = (dev_sensor_info_t *)arg;
994 info->model = "BMI055";
995 info->range_max = 2000;
996 info->range_min = 125;
997 info->unit = udps;
998 } break;
999
1000 default:
1001 break;
1002 }
1003
1004 return 0;
1005 }
1006
drv_gyro_bosch_bmi055_init(void)1007 int drv_gyro_bosch_bmi055_init(void)
1008 {
1009 int ret = 0;
1010 sensor_obj_t sensor;
1011 memset(&sensor, 0, sizeof(sensor));
1012
1013 /* fill the sensor obj parameters here */
1014 sensor.io_port = I2C_PORT;
1015 sensor.tag = TAG_DEV_GYRO;
1016 sensor.path = dev_gyro_path;
1017 sensor.open = drv_gyro_bosch_bmi055_open;
1018 sensor.close = drv_gyro_bosch_bmi055_close;
1019 sensor.read = drv_gyro_bosch_bmi055_read;
1020 sensor.write = NULL;
1021 sensor.ioctl = drv_gyro_bosch_bmi055_ioctl;
1022 sensor.irq_handle = drv_gyro_bosch_bmi055_irq_handle;
1023
1024 ret = sensor_create_obj(&sensor);
1025 if (unlikely(ret)) {
1026 return -1;
1027 }
1028
1029 ret = drv_gyro_bosch_bmi055_validate_id(&bmi055_gyro_ctx, BMI055_GYRO_CHIP_ID_VALUE);
1030 if (unlikely(ret)) {
1031 return -1;
1032 }
1033
1034 ret = drv_gyro_bosch_bmi055_set_range(&bmi055_gyro_ctx, GYRO_RANGE_2000DPS);
1035 if (unlikely(ret)) {
1036 return -1;
1037 }
1038
1039 ret = drv_gyro_bosch_bmi055_set_odr(&bmi055_gyro_ctx, BMI055_GYRO_DEFAULT_ODR);
1040 if (unlikely(ret)) {
1041 return -1;
1042 }
1043
1044 ret = drv_gyro_bosch_bmi055_set_power_mode(&bmi055_gyro_ctx, DEV_POWER_OFF);
1045 if (unlikely(ret)) {
1046 return -1;
1047 }
1048
1049 /* update the phy sensor info to sensor hal */
1050 LOG("%s %s successfully \n", SENSOR_STR, __func__);
1051 return 0;
1052 }
1053
1054 SENSOR_DRV_ADD(drv_acc_bosch_bmi055_init);
1055 SENSOR_DRV_ADD(drv_gyro_bosch_bmi055_init);
1056
1057