1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *
5 */
6 /*********************************************************************************************
7 *
8 *Copyright (C) 2016 - 2020 Bosch Sensortec GmbH
9
10 *Redistribution and use in source and binary forms, with or without
11 *modification, are permitted provided that the following conditions are met:
12
13 *Redistributions of source code must retain the above copyright
14 *notice, this list of conditions and the following disclaimer.
15
16 *Redistributions in binary form must reproduce the above copyright
17 *notice, this list of conditions and the following disclaimer in the
18 *documentation and/or other materials provided with the distribution.
19
20 *Neither the name of the copyright holder nor the names of the
21 *contributors may be used to endorse or promote products derived from
22 *this software without specific prior written permission.
23
24 *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 *CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
26 *IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 *WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 *DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
29 *OR CONTRIBUTORS BE LIABLE FOR ANY
30 *DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 *OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
32 *PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 *LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 *(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 *ANY WAY OUT OF THE USE OF THIS
38 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
39
40 *The information provided is believed to be accurate and reliable.
41 *The copyright holder assumes no responsibility
42 *for the consequences of use
43 *of such information nor for any infringement of patents or
44 *other rights of third parties which may result from its use.
45 *No license is granted by implication or otherwise under any patent or
46 *patent rights of the copyright holder.
47 *
48 *
49 *******************************************************************************************/
50
51 #include "aos/kernel.h"
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "sensor_drv_api.h"
56 #include "sensor_hal.h"
57
58 #define BMI120_I2C_ADDR_TRANS(n) ((n)<<1)
59 #define BMI120_I2C_SLAVE_ADDR_LOW 0x68
60 #define BMI120_I2C_ADDR BMI120_I2C_ADDR_TRANS(BMI120_I2C_SLAVE_ADDR_LOW)
61 /* COMMAND REGISTER */
62 #define BMI120_CMD_COMMANDS_ADDR (0X7E)
63 /* CHIP ID */
64 #define BMI120_USER_CHIP_ID_ADDR (0x00)
65 /* ACCEL CONFIG REGISTERS FOR ODR, BANDWIDTH AND UNDERSAMPLING */
66 #define BMI120_USER_ACCEL_CONFIG_ADDR (0X40)
67 /* ACCEL RANGE */
68 #define BMI120_USER_ACCEL_RANGE_ADDR (0X41)
69 /* ACCEL DATA REGISTERS */
70 #define BMI120_USER_DATA_14_ADDR (0X12)
71 #define BMI120_USER_DATA_15_ADDR (0X13)
72 #define BMI120_USER_DATA_16_ADDR (0X14)
73 #define BMI120_USER_DATA_17_ADDR (0X15)
74 #define BMI120_USER_DATA_18_ADDR (0X16)
75 #define BMI120_USER_DATA_19_ADDR (0X17)
76 /* GYRO DATA REGISTERS */
77 #define BMI120_USER_DATA_8_ADDR (0X0C)
78 #define BMI120_USER_DATA_9_ADDR (0X0D)
79 #define BMI120_USER_DATA_10_ADDR (0X0E)
80 #define BMI120_USER_DATA_11_ADDR (0X0F)
81 #define BMI120_USER_DATA_12_ADDR (0X10)
82 #define BMI120_USER_DATA_13_ADDR (0X11)
83
84 #define BMI120_MODE_SWITCHING_DELAY (30)
85
86 /* GYRO CONFIG REGISTERS FOR ODR AND BANDWIDTH */
87 #define BMI120_USER_GYRO_CONFIG_ADDR (0X42)
88 /* GYRO RANGE */
89 #define BMI120_USER_GYRO_RANGE_ADDR (0X43)
90
91 /* BMI120 CHIPID */
92 #define BMI120_CHIP_ID_VALUE (0xD3)
93 /* CMD REGISTERS DEFINITION START */
94 /* COMMAND REGISTER LENGTH, POSITION AND MASK */
95 /* Command description address - Reg Addr --> 0x7E, Bit --> 0....7 */
96 #define BMI120_CMD_COMMANDS__POS (0)
97 #define BMI120_CMD_COMMANDS__LEN (8)
98 #define BMI120_CMD_COMMANDS__MSK (0xFF)
99 #define BMI120_CMD_COMMANDS__REG (BMI120_CMD_COMMANDS_ADDR)
100
101 /* CMD */
102 #define BMI120_CMD_SOFTRESET (0xB6)
103 /* name ACCEL POWER MODE */
104 #define ACCEL_MODE_NORMAL (0x11)
105 #define ACCEL_LOWPOWER (0X12)
106 #define ACCEL_SUSPEND (0X10)
107 /* BMI120 Accel power modes */
108 #define BMI120_ACCEL_SUSPEND 0
109 #define BMI120_ACCEL_NORMAL_MODE 1
110 #define BMI120_ACCEL_LOW_POWER 2
111 /* GYRO POWER MODE */
112 #define GYRO_MODE_SUSPEND (0x14)
113 #define GYRO_MODE_NORMAL (0x15)
114 #define GYRO_MODE_FASTSTARTUP (0x17)
115 /* CHIP ID LENGTH, POSITION AND MASK */
116 /* Chip ID Description - Reg Addr --> (0x00), Bit --> 0...7 */
117 #define BMI120_USER_CHIP_ID__POS (0)
118 #define BMI120_USER_CHIP_ID__MSK (0xFF)
119 #define BMI120_USER_CHIP_ID__LEN (8)
120 #define BMI120_USER_CHIP_ID__REG (BMI120_USER_CHIP_ID_ADDR)
121 /* ACCEL CONFIGURATION LENGTH, POSITION AND MASK */
122 /* Acc_Conf Description - Reg Addr --> (0x40), Bit --> 0...3 */
123 #define BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__POS (0)
124 #define BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__LEN (4)
125 #define BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__MSK (0x0F)
126 #define BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__REG \
127 (BMI120_USER_ACCEL_CONFIG_ADDR)
128
129 /* Acc_Conf Description - Reg Addr --> (0x40), Bit --> 4...6 */
130 #define BMI120_USER_ACCEL_CONFIG_ACCEL_BW__POS (4)
131 #define BMI120_USER_ACCEL_CONFIG_ACCEL_BW__LEN (3)
132 #define BMI120_USER_ACCEL_CONFIG_ACCEL_BW__MSK (0x70)
133 #define BMI120_USER_ACCEL_CONFIG_ACCEL_BW__REG (BMI120_USER_ACCEL_CONFIG_ADDR)
134
135 /* Acc_Conf Description - Reg Addr --> (0x40), Bit --> 7 */
136 #define BMI120_USER_ACCEL_CONFIG_ACCEL_UNDER_SAMPLING__POS (7)
137 #define BMI120_USER_ACCEL_CONFIG_ACCEL_UNDER_SAMPLING__LEN (1)
138 #define BMI120_USER_ACCEL_CONFIG_ACCEL_UNDER_SAMPLING__MSK (0x80)
139 #define BMI120_USER_ACCEL_CONFIG_ACCEL_UNDER_SAMPLING__REG \
140 (BMI120_USER_ACCEL_CONFIG_ADDR)
141
142 /* Acc_Range Description - Reg Addr --> 0x41, Bit --> 0...3 */
143 #define BMI120_USER_ACCEL_RANGE__POS (0)
144 #define BMI120_USER_ACCEL_RANGE__LEN (4)
145 #define BMI120_USER_ACCEL_RANGE__MSK (0x0F)
146 #define BMI120_USER_ACCEL_RANGE__REG \
147 (BMI120_USER_ACCEL_RANGE_ADDR)
148
149 /* GYRO CONFIGURATION LENGTH, POSITION AND MASK */
150 /* Gyro_Conf Description - Reg Addr --> (0x42), Bit --> 0...3 */
151 #define BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__POS (0)
152 #define BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__LEN (4)
153 #define BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__MSK (0x0F)
154 #define BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__REG \
155 (BMI120_USER_GYRO_CONFIG_ADDR)
156
157 /* Gyro_Conf Description - Reg Addr --> (0x42), Bit --> 4...5 */
158 #define BMI120_USER_GYRO_CONFIG_BW__POS (4)
159 #define BMI120_USER_GYRO_CONFIG_BW__LEN (2)
160 #define BMI120_USER_GYRO_CONFIG_BW__MSK (0x30)
161 #define BMI120_USER_GYRO_CONFIG_BW__REG \
162 (BMI120_USER_GYRO_CONFIG_ADDR)
163
164 /* Gyr_Range Description - Reg Addr --> 0x43, Bit --> 0...2 */
165 #define BMI120_USER_GYRO_RANGE__POS (0)
166 #define BMI120_USER_GYRO_RANGE__LEN (3)
167 #define BMI120_USER_GYRO_RANGE__MSK (0x07)
168 #define BMI120_USER_GYRO_RANGE__REG (BMI120_USER_GYRO_RANGE_ADDR)
169
170 /* ACCEL ODR */
171 #define BMI120_ACCEL_OUTPUT_DATA_RATE_RESERVED (0x00)
172 #define BMI120_ACCEL_OUTPUT_DATA_RATE_0_78HZ (0x01)
173 #define BMI120_ACCEL_OUTPUT_DATA_RATE_1_56HZ (0x02)
174 #define BMI120_ACCEL_OUTPUT_DATA_RATE_3_12HZ (0x03)
175 #define BMI120_ACCEL_OUTPUT_DATA_RATE_6_25HZ (0x04)
176 #define BMI120_ACCEL_OUTPUT_DATA_RATE_12_5HZ (0x05)
177 #define BMI120_ACCEL_OUTPUT_DATA_RATE_25HZ (0x06)
178 #define BMI120_ACCEL_OUTPUT_DATA_RATE_50HZ (0x07)
179 #define BMI120_ACCEL_OUTPUT_DATA_RATE_100HZ (0x08)
180 #define BMI120_ACCEL_OUTPUT_DATA_RATE_200HZ (0x09)
181 #define BMI120_ACCEL_OUTPUT_DATA_RATE_400HZ (0x0A)
182 #define BMI120_ACCEL_OUTPUT_DATA_RATE_800HZ (0x0B)
183 #define BMI120_ACCEL_OUTPUT_DATA_RATE_1600HZ (0x0C)
184 #define BMI120_ACCEL_OUTPUT_DATA_RATE_RESERVED0 (0x0D)
185 #define BMI120_ACCEL_OUTPUT_DATA_RATE_RESERVED1 (0x0E)
186 #define BMI120_ACCEL_OUTPUT_DATA_RATE_RESERVED2 (0x0F)
187
188 /* GYRO ODR */
189 #define BMI120_GYRO_OUTPUT_DATA_RATE_RESERVED (0x00)
190 #define BMI120_GYRO_OUTPUT_DATA_RATE_25HZ (0x06)
191 #define BMI120_GYRO_OUTPUT_DATA_RATE_50HZ (0x07)
192 #define BMI120_GYRO_OUTPUT_DATA_RATE_100HZ (0x08)
193 #define BMI120_GYRO_OUTPUT_DATA_RATE_200HZ (0x09)
194 #define BMI120_GYRO_OUTPUT_DATA_RATE_400HZ (0x0A)
195 #define BMI120_GYRO_OUTPUT_DATA_RATE_800HZ (0x0B)
196 #define BMI120_GYRO_OUTPUT_DATA_RATE_1600HZ (0x0C)
197 #define BMI120_GYRO_OUTPUT_DATA_RATE_3200HZ (0x0D)
198 /* default HZ */
199 #define BMI120_ACC_DEFAULT_ODR_100HZ (100)
200 #define BMI120_ACC_DEFAULT_ODR_25HZ (25)
201 #define BMI120_GYRO_DEFAULT_ODR_100HZ (100)
202 #define BMI120_GYRO_DEFAULT_ODR_25HZ (25)
203 /* ACCEL RANGE */
204 #define BMI120_ACCEL_RANGE_2G (0X03)
205 #define BMI120_ACCEL_RANGE_4G (0X05)
206 #define BMI120_ACCEL_RANGE_8G (0X08)
207 #define BMI120_ACCEL_RANGE_16G (0X0C)
208
209 /* ACC sensitivity */
210 #define BMI120_ACC_SENSITIVITY_2G (16384)
211 #define BMI120_ACC_SENSITIVITY_4G (8192)
212 #define BMI120_ACC_SENSITIVITY_8G (4096)
213 #define BMI120_ACC_SENSITIVITY_16G (2048)
214
215 /* GYROSCOPE RANGE PARAMETER */
216 #define BMI120_GYRO_RANGE_2000_DEG_SEC (0x00)
217 #define BMI120_GYRO_RANGE_1000_DEG_SEC (0x01)
218 #define BMI120_GYRO_RANGE_500_DEG_SEC (0x02)
219 #define BMI120_GYRO_RANGE_250_DEG_SEC (0x03)
220 #define BMI120_GYRO_RANGE_125_DEG_SEC (0x04)
221
222 /* GYRO sensitivity */
223 #define BMI120_GYRO_SENSITIVITY_125DPS (262)
224 #define BMI120_GYRO_SENSITIVITY_250DPS (131)
225 #define BMI120_GYRO_SENSITIVITY_500DPS (66)
226 #define BMI120_GYRO_SENSITIVITY_1000DPS (33)
227 #define BMI120_GYRO_SENSITIVITY_2000DPS (16)
228
229 /* ACCEL DATA XYZ LENGTH, POSITION AND MASK */
230 /* ACC_X (LSB) Description - Reg Addr --> (0x12), Bit --> 0...7 */
231 #define BMI120_USER_DATA_14_ACCEL_X_LSB__POS (0)
232 #define BMI120_USER_DATA_14_ACCEL_X_LSB__LEN (8)
233 #define BMI120_USER_DATA_14_ACCEL_X_LSB__MSK (0xFF)
234 #define BMI120_USER_DATA_14_ACCEL_X_LSB__REG (BMI120_USER_DATA_14_ADDR)
235
236 /* ACC_X (MSB) Description - Reg Addr --> 0x13, Bit --> 0...7 */
237 #define BMI120_USER_DATA_15_ACCEL_X_MSB__POS (0)
238 #define BMI120_USER_DATA_15_ACCEL_X_MSB__LEN (8)
239 #define BMI120_USER_DATA_15_ACCEL_X_MSB__MSK (0xFF)
240 #define BMI120_USER_DATA_15_ACCEL_X_MSB__REG (BMI120_USER_DATA_15_ADDR)
241
242 /* ACC_Y (LSB) Description - Reg Addr --> (0x14), Bit --> 0...7 */
243 #define BMI120_USER_DATA_16_ACCEL_Y_LSB__POS (0)
244 #define BMI120_USER_DATA_16_ACCEL_Y_LSB__LEN (8)
245 #define BMI120_USER_DATA_16_ACCEL_Y_LSB__MSK (0xFF)
246 #define BMI120_USER_DATA_16_ACCEL_Y_LSB__REG (BMI120_USER_DATA_16_ADDR)
247
248 /* ACC_Y (MSB) Description - Reg Addr --> (0x15), Bit --> 0...7 */
249 #define BMI120_USER_DATA_17_ACCEL_Y_MSB__POS (0)
250 #define BMI120_USER_DATA_17_ACCEL_Y_MSB__LEN (8)
251 #define BMI120_USER_DATA_17_ACCEL_Y_MSB__MSK (0xFF)
252 #define BMI120_USER_DATA_17_ACCEL_Y_MSB__REG (BMI120_USER_DATA_17_ADDR)
253
254 /* ACC_Z (LSB) Description - Reg Addr --> 0x16, Bit --> 0...7 */
255 #define BMI120_USER_DATA_18_ACCEL_Z_LSB__POS (0)
256 #define BMI120_USER_DATA_18_ACCEL_Z_LSB__LEN (8)
257 #define BMI120_USER_DATA_18_ACCEL_Z_LSB__MSK (0xFF)
258 #define BMI120_USER_DATA_18_ACCEL_Z_LSB__REG (BMI120_USER_DATA_18_ADDR)
259
260 /* ACC_Z (MSB) Description - Reg Addr --> (0x17), Bit --> 0...7 */
261 #define BMI120_USER_DATA_19_ACCEL_Z_MSB__POS (0)
262 #define BMI120_USER_DATA_19_ACCEL_Z_MSB__LEN (8)
263 #define BMI120_USER_DATA_19_ACCEL_Z_MSB__MSK (0xFF)
264 #define BMI120_USER_DATA_19_ACCEL_Z_MSB__REG (BMI120_USER_DATA_19_ADDR)
265
266 /* GYRO DATA XYZ LENGTH, POSITION AND MASK */
267 /* GYR_X (LSB) Description - Reg Addr --> (0x0C), Bit --> 0...7 */
268 #define BMI120_USER_DATA_8_GYRO_X_LSB__POS (0)
269 #define BMI120_USER_DATA_8_GYRO_X_LSB__LEN (8)
270 #define BMI120_USER_DATA_8_GYRO_X_LSB__MSK (0xFF)
271 #define BMI120_USER_DATA_8_GYRO_X_LSB__REG (BMI120_USER_DATA_8_ADDR)
272
273 /* GYR_X (MSB) Description - Reg Addr --> (0x0D), Bit --> 0...7 */
274 #define BMI120_USER_DATA_9_GYRO_X_MSB__POS (0)
275 #define BMI120_USER_DATA_9_GYRO_X_MSB__LEN (8)
276 #define BMI120_USER_DATA_9_GYRO_X_MSB__MSK (0xFF)
277 #define BMI120_USER_DATA_9_GYRO_X_MSB__REG (BMI120_USER_DATA_9_ADDR)
278
279 /* GYR_Y (LSB) Description - Reg Addr --> 0x0E, Bit --> 0...7 */
280 #define BMI120_USER_DATA_10_GYRO_Y_LSB__POS (0)
281 #define BMI120_USER_DATA_10_GYRO_Y_LSB__LEN (8)
282 #define BMI120_USER_DATA_10_GYRO_Y_LSB__MSK (0xFF)
283 #define BMI120_USER_DATA_10_GYRO_Y_LSB__REG (BMI120_USER_DATA_10_ADDR)
284
285 /* GYR_Y (MSB) Description - Reg Addr --> (0x0F), Bit --> 0...7 */
286 #define BMI120_USER_DATA_11_GYRO_Y_MSB__POS (0)
287 #define BMI120_USER_DATA_11_GYRO_Y_MSB__LEN (8)
288 #define BMI120_USER_DATA_11_GYRO_Y_MSB__MSK (0xFF)
289 #define BMI120_USER_DATA_11_GYRO_Y_MSB__REG (BMI120_USER_DATA_11_ADDR)
290
291 /* GYR_Z (LSB) Description - Reg Addr --> (0x10), Bit --> 0...7 */
292 #define BMI120_USER_DATA_12_GYRO_Z_LSB__POS (0)
293 #define BMI120_USER_DATA_12_GYRO_Z_LSB__LEN (8)
294 #define BMI120_USER_DATA_12_GYRO_Z_LSB__MSK (0xFF)
295 #define BMI120_USER_DATA_12_GYRO_Z_LSB__REG (BMI120_USER_DATA_12_ADDR)
296
297 /* GYR_Z (MSB) Description - Reg Addr --> (0x11), Bit --> 0...7 */
298 #define BMI120_USER_DATA_13_GYRO_Z_MSB__POS (0)
299 #define BMI120_USER_DATA_13_GYRO_Z_MSB__LEN (8)
300 #define BMI120_USER_DATA_13_GYRO_Z_MSB__MSK (0xFF)
301 #define BMI120_USER_DATA_13_GYRO_Z_MSB__REG (BMI120_USER_DATA_13_ADDR)
302
303 /* SHIFT VALUE DEFINITION */
304 #define BMI120_SHIFT_BIT_POSITION_BY_01_BIT (1)
305 #define BMI120_SHIFT_BIT_POSITION_BY_02_BITS (2)
306 #define BMI120_SHIFT_BIT_POSITION_BY_03_BITS (3)
307 #define BMI120_SHIFT_BIT_POSITION_BY_04_BITS (4)
308 #define BMI120_SHIFT_BIT_POSITION_BY_05_BITS (5)
309 #define BMI120_SHIFT_BIT_POSITION_BY_06_BITS (6)
310 #define BMI120_SHIFT_BIT_POSITION_BY_07_BITS (7)
311 #define BMI120_SHIFT_BIT_POSITION_BY_08_BITS (8)
312 #define BMI120_SHIFT_BIT_POSITION_BY_09_BITS (9)
313 #define BMI120_SHIFT_BIT_POSITION_BY_12_BITS (12)
314 #define BMI120_SHIFT_BIT_POSITION_BY_13_BITS (13)
315 #define BMI120_SHIFT_BIT_POSITION_BY_14_BITS (14)
316 #define BMI120_SHIFT_BIT_POSITION_BY_15_BITS (15)
317 #define BMI120_SHIFT_BIT_POSITION_BY_16_BITS (16)
318
319 /* BIT SLICE GET AND SET FUNCTIONS */
320 #define BMI120_GET_BITSLICE(regvar, bitname)\
321 ((regvar & bitname##__MSK) >> bitname##__POS)
322
323
324 #define BMI120_SET_BITSLICE(regvar, bitname, val)\
325 ((regvar & ~bitname##__MSK) | \
326 ((val<<bitname##__POS)&bitname##__MSK))
327
328 static int32_t g_bmi120_acc_factor[ACC_RANGE_MAX] = { BMI120_ACC_SENSITIVITY_2G, BMI120_ACC_SENSITIVITY_4G,
329 BMI120_ACC_SENSITIVITY_8G, BMI120_ACC_SENSITIVITY_16G
330 };
331 static int32_t g_bmi120_gyro_factor[GYRO_RANGE_MAX] = {BMI120_GYRO_SENSITIVITY_125DPS, BMI120_GYRO_SENSITIVITY_250DPS, BMI120_GYRO_SENSITIVITY_500DPS,
332 BMI120_GYRO_SENSITIVITY_1000DPS, BMI120_GYRO_SENSITIVITY_2000DPS
333 };
334
335 static int32_t g_cur_acc_factor = 0;
336 static int32_t g_cur_gyro_factor = 0;
337 static int32_t g_bmi120flag = 0;
338
339 i2c_dev_t bmi120_ctx = {
340 .port = 3,
341 .config.address_width = 8,
342 .config.freq = 400000,
343 .config.dev_addr = BMI120_I2C_ADDR,
344 };
345
346 /**
347 * This function does the soft reset
348 *
349 * @param[in] drv pointer to the i2c dev
350 * @return the operation status, 0 is OK, others is error
351 */
drv_acc_gyro_bosch_bmi120_soft_reset(i2c_dev_t * drv)352 static int drv_acc_gyro_bosch_bmi120_soft_reset(i2c_dev_t* drv)
353 {
354 int ret = 0;
355 uint8_t value = 0;
356 value = BMI120_CMD_SOFTRESET;
357
358 ret = sensor_i2c_write(drv, BMI120_CMD_COMMANDS__REG, &value, I2C_DATA_LEN,
359 I2C_OP_RETRIES);
360 aos_msleep(2);
361 if(unlikely(ret) != 0) {
362 return -1;
363 }
364 return 0;
365 }
366
367 /**
368 * This function validates the chip ID of device
369 *
370 * @param[in] drv pointer to the i2c dev
371 * @param[in] id_value the expected CHIPID
372 * @return the operation status, 0 is OK, others is error
373 */
drv_acc_gyro_bosch_bmi120_validate_id(i2c_dev_t * drv,uint8_t id_value)374 static int drv_acc_gyro_bosch_bmi120_validate_id(i2c_dev_t* drv,
375 uint8_t id_value)
376 {
377 uint8_t value = 0x00;
378 int ret = 0;
379
380 if(drv == NULL) {
381 return -1;
382 }
383
384 ret = sensor_i2c_read(drv, BMI120_USER_CHIP_ID__REG, &value, I2C_DATA_LEN,
385 I2C_OP_RETRIES);
386 if(unlikely(ret) != 0) {
387 printf("read CHIPID failed \n");
388 return ret;
389 }
390 printf("read CHIPID %x\n", value);
391 if(id_value != value) {
392 printf("failed read CHIPID %x\n", value);
393 //return -1;
394 }
395
396 return 0;
397 }
398
399 /**
400 * This function sets the acc powermode
401 *
402 * @param[in] drv pointer to the i2c dev
403 * @param[in] mode the powermode to be setted
404 * @return the operation status, 0 is OK, others is error
405 */
drv_acc_bosch_bmi120_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)406 static int drv_acc_bosch_bmi120_set_power_mode(i2c_dev_t* drv,
407 dev_power_mode_e mode)
408 {
409 uint8_t value = 0x00;
410 int ret = 0;
411
412 switch(mode) {
413 case DEV_POWER_ON: {
414
415 value = ACCEL_MODE_NORMAL;
416 ret = sensor_i2c_write(drv, BMI120_CMD_COMMANDS__REG, &value, I2C_DATA_LEN,
417 I2C_OP_RETRIES);
418 aos_msleep(2);
419 if(unlikely(ret) != 0) {
420 return ret;
421 }
422 }
423 break;
424
425 case DEV_POWER_OFF:
426 case DEV_SLEEP: {
427 value = ACCEL_SUSPEND;
428 ret = sensor_i2c_write(drv, BMI120_CMD_COMMANDS__REG, &value, I2C_DATA_LEN,
429 I2C_OP_RETRIES);
430 aos_msleep(2);
431 if(unlikely(ret) != 0) {
432 return ret;
433 }
434 }
435 break;
436
437 default:
438 break;
439 }
440 return 0;
441 }
442
443 /**
444 * This function gets the acc ODR according to HZ
445 *
446 * @param[in] drv pointer to the i2c dev
447 * @param[in] hz the frequency required
448 * @return the conrresponding acc ODR
449 */
drv_acc_bosch_bmi120_hz2odr(uint32_t hz)450 static uint8_t drv_acc_bosch_bmi120_hz2odr(uint32_t hz)
451 {
452 if(hz > 800)
453 return BMI120_ACCEL_OUTPUT_DATA_RATE_1600HZ;
454 else if(hz > 400)
455 return BMI120_ACCEL_OUTPUT_DATA_RATE_800HZ;
456 else if(hz > 200)
457 return BMI120_ACCEL_OUTPUT_DATA_RATE_400HZ;
458 else if(hz > 100)
459 return BMI120_ACCEL_OUTPUT_DATA_RATE_200HZ;
460 else if(hz > 50)
461 return BMI120_ACCEL_OUTPUT_DATA_RATE_100HZ;
462 else if(hz > 25)
463 return BMI120_ACCEL_OUTPUT_DATA_RATE_50HZ;
464 else if(hz > 12)
465 return BMI120_ACCEL_OUTPUT_DATA_RATE_25HZ;
466 else if(hz > 6)
467 return BMI120_ACCEL_OUTPUT_DATA_RATE_12_5HZ;
468 else if(hz > 3)
469 return BMI120_ACCEL_OUTPUT_DATA_RATE_6_25HZ;
470 else if(hz >= 1)
471 return BMI120_ACCEL_OUTPUT_DATA_RATE_3_12HZ;
472 else
473 return BMI120_ACCEL_OUTPUT_DATA_RATE_1_56HZ;
474
475 }
476
477 /**
478 * This function sets the acc ODR
479 *
480 * @param[in] drv pointer to the i2c dev
481 * @param[in] hz the frequency required
482 * @return the operation status, 0 is OK, others is error
483 */
drv_acc_bosch_bmi120_set_odr(i2c_dev_t * drv,uint32_t hz)484 static int drv_acc_bosch_bmi120_set_odr(i2c_dev_t* drv, uint32_t hz)
485 {
486 int ret = 0;
487 uint8_t value = 0x00;
488 uint8_t odr = drv_acc_bosch_bmi120_hz2odr(hz);
489
490 ret = sensor_i2c_read(drv, BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__REG,
491 &value, I2C_DATA_LEN, I2C_OP_RETRIES);
492 if(unlikely(ret) != 0) {
493 return ret;
494 }
495
496 value = BMI120_SET_BITSLICE(value, BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE,
497 odr);
498
499 ret = sensor_i2c_write(drv, BMI120_USER_ACCEL_CONFIG_OUTPUT_DATA_RATE__REG,
500 &value, I2C_DATA_LEN, I2C_OP_RETRIES);
501 aos_msleep(2);
502 if(unlikely(ret) != 0) {
503 return ret;
504 }
505 return 0;
506 }
507
508 /**
509 * This function sets the acc range
510 *
511 * @param[in] drv pointer to the i2c dev
512 * @param[in] hz the range required
513 * @return the operation status, 0 is OK, others is error
514 */
drv_acc_bosch_bmi120_set_range(i2c_dev_t * drv,uint32_t range)515 static int drv_acc_bosch_bmi120_set_range(i2c_dev_t* drv, uint32_t range)
516 {
517 int ret = 0;
518 uint8_t value = 0x00;
519 uint8_t tmp = 0;
520
521 ret = sensor_i2c_read(drv, BMI120_USER_ACCEL_RANGE__REG, &value, I2C_DATA_LEN,
522 I2C_OP_RETRIES);
523 if(unlikely(ret) != 0) {
524 return ret;
525 }
526
527 switch(range) {
528 case ACC_RANGE_2G: {
529 tmp = BMI120_ACCEL_RANGE_2G;
530 }
531 break;
532
533 case ACC_RANGE_4G: {
534 tmp = BMI120_ACCEL_RANGE_4G;
535 }
536 break;
537
538 case ACC_RANGE_8G: {
539 tmp = BMI120_ACCEL_RANGE_8G;
540 }
541 break;
542
543 case ACC_RANGE_16G: {
544 tmp = BMI120_ACCEL_RANGE_16G;
545 }
546 break;
547
548 default:
549 break;
550 }
551
552 value = BMI120_SET_BITSLICE(value, BMI120_USER_ACCEL_RANGE, tmp);
553 ret = sensor_i2c_write(drv, BMI120_USER_ACCEL_RANGE__REG, &value, I2C_DATA_LEN,
554 I2C_OP_RETRIES);
555 aos_msleep(2);
556 if(unlikely(ret) != 0) {
557 return ret;
558 }
559
560 if((range >= ACC_RANGE_2G)&&(range <= ACC_RANGE_16G)) {
561 g_cur_acc_factor = g_bmi120_acc_factor[range];
562 }
563
564 return 0;
565 }
566
567 /**
568 * This function is the ISR
569 *
570 * @return
571 */
drv_acc_bosch_bmi120_irq_handle(void)572 static void drv_acc_bosch_bmi120_irq_handle(void)
573 {
574 /* no handle so far */
575 }
576
577 /**
578 * This function opens the acc
579 *
580 * @return the operation status, 0 is OK, others is error
581 */
drv_acc_bosch_bmi120_open(void)582 static int drv_acc_bosch_bmi120_open(void)
583 {
584 int ret = 0;
585
586 ret = drv_acc_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_ON);
587 if(unlikely(ret) != 0) {
588 return -1;
589 }
590
591 ret = drv_acc_bosch_bmi120_set_range(&bmi120_ctx, ACC_RANGE_8G);
592 if(unlikely(ret) != 0) {
593 return -1;
594 }
595
596 ret = drv_acc_bosch_bmi120_set_odr(&bmi120_ctx, BMI120_ACC_DEFAULT_ODR_25HZ);
597 if(unlikely(ret) != 0) {
598 return -1;
599 }
600 LOG("%s %s successfully \n", SENSOR_STR, __func__);
601 return 0;
602
603 }
604
605 /**
606 * This function closes the acc
607 *
608 * @return the operation status, 0 is OK, others is error
609 */
drv_acc_bosch_bmi120_close(void)610 static int drv_acc_bosch_bmi120_close(void)
611 {
612 int ret = 0;
613 ret = drv_acc_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_OFF);
614 if(unlikely(ret) != 0) {
615 return -1;
616 }
617 return 0;
618 }
619
620 /**
621 * This function reads the acc data and reports the data
622 *
623 * @param[in out] buf buffer for acc data
624 * @param[in out] len length of data
625 * @return the operation status, 0 is OK, others is error
626 */
drv_acc_bosch_bmi120_read(void * buf,size_t len)627 static int drv_acc_bosch_bmi120_read(void *buf, size_t len)
628 {
629 int ret = 0;
630 size_t size;
631 uint8_t reg[6];
632 accel_data_t *accel = (accel_data_t *)buf;
633 if(buf == NULL) {
634 return -1;
635 }
636
637 size = sizeof(accel_data_t);
638 if(len < size) {
639 return -1;
640 }
641
642 ret = sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_14_ACCEL_X_LSB__REG,
643 ®[0], I2C_REG_LEN, I2C_OP_RETRIES);
644 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_15_ACCEL_X_MSB__REG,
645 ®[1], I2C_REG_LEN, I2C_OP_RETRIES);
646 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_16_ACCEL_Y_LSB__REG,
647 ®[2], I2C_REG_LEN, I2C_OP_RETRIES);
648 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_17_ACCEL_Y_MSB__REG,
649 ®[3], I2C_REG_LEN, I2C_OP_RETRIES);
650 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_18_ACCEL_Z_LSB__REG,
651 ®[4], I2C_REG_LEN, I2C_OP_RETRIES);
652 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_19_ACCEL_Z_MSB__REG,
653 ®[5], I2C_REG_LEN, I2C_OP_RETRIES);
654 if(unlikely(ret) != 0) {
655 return -1;
656 }
657
658 accel->data[DATA_AXIS_X] = (int16_t)((((int32_t)((int8_t)reg[1])) <<
659 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[0]));
660 accel->data[DATA_AXIS_Y] = (int16_t)((((int32_t)((int8_t)reg[3])) <<
661 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[2]));
662 accel->data[DATA_AXIS_Z] = (int16_t)((((int32_t)((int8_t)reg[5])) <<
663 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[4]));
664
665 if(g_cur_acc_factor != 0) {
666 /* the unit of acc is mg, 1000 mg = 1 g */
667 accel->data[DATA_AXIS_X] = accel->data[DATA_AXIS_X] *
668 ACCELEROMETER_UNIT_FACTOR / (int32_t)g_cur_acc_factor;
669 accel->data[DATA_AXIS_Y] = accel->data[DATA_AXIS_Y] *
670 ACCELEROMETER_UNIT_FACTOR / (int32_t)g_cur_acc_factor;
671 accel->data[DATA_AXIS_Z] = accel->data[DATA_AXIS_Z] *
672 ACCELEROMETER_UNIT_FACTOR / (int32_t)g_cur_acc_factor;
673 }
674
675 accel->timestamp = aos_now_ms();
676
677 return (int)size;
678 }
679
680 /**
681 * This function is for the acc ioctl
682 *
683 * @param[in] cmd the ioctl command
684 * @param[in] arg the correspondding parameter
685 * @return the operation status, 0 is OK, others is error
686 */
drv_acc_bosch_bmi120_ioctl(int cmd,unsigned long arg)687 static int drv_acc_bosch_bmi120_ioctl(int cmd, unsigned long arg)
688 {
689 int ret = 0;
690
691 switch(cmd) {
692 case SENSOR_IOCTL_ODR_SET: {
693 ret = drv_acc_bosch_bmi120_set_odr(&bmi120_ctx, arg);
694 if(unlikely(ret) != 0) {
695 return -1;
696 }
697 }
698 break;
699 case SENSOR_IOCTL_RANGE_SET: {
700 ret = drv_acc_bosch_bmi120_set_range(&bmi120_ctx, arg);
701 if(unlikely(ret) != 0) {
702 return -1;
703 }
704 }
705 break;
706 case SENSOR_IOCTL_SET_POWER: {
707 ret = drv_acc_bosch_bmi120_set_power_mode(&bmi120_ctx, arg);
708 if(unlikely(ret) != 0) {
709 return -1;
710 }
711 }
712 break;
713 case SENSOR_IOCTL_GET_INFO: {
714 /* fill the dev info here */
715 dev_sensor_info_t *info = (dev_sensor_info_t*)arg;
716 info->model = "BMI120";
717 info->range_max = 16;
718 info->range_min = 2;
719 info->unit = mg;
720 }
721 break;
722
723 default:
724 break;
725 }
726
727 return 0;
728 }
729
730 /**
731 * This function is for the acc initialization
732 *
733 * @return the operation status, 0 is OK, others is error
734 */
drv_acc_bosch_bmi120_init(void)735 int drv_acc_bosch_bmi120_init(void) {
736 printf("drv_acc_bosch_bmi120_init started \n");
737 int ret = 0;
738 sensor_obj_t sensor;
739 memset(&sensor, 0, sizeof(sensor));
740
741 /* fill the sensor obj parameters here */
742 sensor.io_port = I2C_PORT;
743 sensor.tag = TAG_DEV_ACC;
744 sensor.path = dev_acc_path;
745 sensor.open = drv_acc_bosch_bmi120_open;
746 sensor.close = drv_acc_bosch_bmi120_close;
747 sensor.read = drv_acc_bosch_bmi120_read;
748 sensor.write = NULL;
749 sensor.ioctl = drv_acc_bosch_bmi120_ioctl;
750 sensor.irq_handle = drv_acc_bosch_bmi120_irq_handle;
751
752 ret = sensor_create_obj(&sensor);
753 if(unlikely(ret) != 0) {
754 printf("sensor_create_obj failed \n");
755 return -1;
756 }
757 ret = drv_acc_gyro_bosch_bmi120_validate_id(&bmi120_ctx, BMI120_CHIP_ID_VALUE);
758 if(unlikely(ret) != 0) {
759 printf("drv_acc_gyro_bosch_bmi120_validate_id failed \n");
760 return -1;
761 }
762
763 if(0 == g_bmi120flag) {
764 ret = drv_acc_gyro_bosch_bmi120_soft_reset(&bmi120_ctx);
765 if(unlikely(ret) != 0) {
766 printf("drv_acc_gyro_bosch_bmi120_soft_reset failed \n");
767 return -1;
768 }
769 ret = drv_acc_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_OFF);
770 if(unlikely(ret) != 0) {
771 printf("drv_acc_bosch_bmi120_set_power_mode failed \n");
772 return -1;
773 }
774 g_bmi120flag = 1;
775 } else {
776 LOG("%s %s acc do not need reset\n", SENSOR_STR, __func__);
777 }
778
779 LOG("%s %s successfully \n", SENSOR_STR, __func__);
780 printf("drv_acc_bosch_bmi120_init failed \n");
781 return 0;
782 }
783
784 /**
785 * This function sets the gyro powermode
786 *
787 * @param[in] drv pointer to the i2c dev
788 * @param[in] mode the powermode to be setted
789 * @return the operation status, 0 is OK, others is error
790 */
drv_gyro_bosch_bmi120_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)791 static int drv_gyro_bosch_bmi120_set_power_mode(i2c_dev_t* drv,
792 dev_power_mode_e mode)
793 {
794 uint8_t value = 0x00;
795 int ret = 0;
796
797 switch(mode) {
798 case DEV_POWER_ON: {
799
800 value = GYRO_MODE_NORMAL;
801 ret = sensor_i2c_write(drv, BMI120_CMD_COMMANDS__REG, &value, I2C_DATA_LEN,
802 I2C_OP_RETRIES);
803 aos_msleep(2);
804 if(unlikely(ret) != 0) {
805 return ret;
806 }
807 }
808 break;
809
810 case DEV_POWER_OFF:
811 case DEV_SLEEP: {
812 value = GYRO_MODE_SUSPEND;
813 ret = sensor_i2c_write(drv, BMI120_CMD_COMMANDS__REG, &value, I2C_DATA_LEN,
814 I2C_OP_RETRIES);
815 aos_msleep(2);
816 if(unlikely(ret) != 0) {
817 return ret;
818 }
819 }
820 break;
821 default:
822 break;
823 }
824 return 0;
825 }
826
827 /**
828 * This function gets the gyro ODR according to HZ
829 *
830 * @param[in] drv pointer to the i2c dev
831 * @param[in] hz the frequency required
832 * @return the conrresponding gyro ODR
833 */
drv_gyro_bosch_bmi120_hz2odr(uint32_t hz)834 static uint8_t drv_gyro_bosch_bmi120_hz2odr(uint32_t hz)
835 {
836 if(hz > 1600)
837 return BMI120_GYRO_OUTPUT_DATA_RATE_3200HZ;
838 else if(hz > 800)
839 return BMI120_GYRO_OUTPUT_DATA_RATE_1600HZ;
840 else if(hz > 400)
841 return BMI120_GYRO_OUTPUT_DATA_RATE_800HZ;
842 else if(hz > 200)
843 return BMI120_GYRO_OUTPUT_DATA_RATE_400HZ;
844 else if(hz > 100)
845 return BMI120_GYRO_OUTPUT_DATA_RATE_200HZ;
846 else if(hz > 50)
847 return BMI120_GYRO_OUTPUT_DATA_RATE_100HZ;
848 else if(hz > 25)
849 return BMI120_GYRO_OUTPUT_DATA_RATE_50HZ;
850 else
851 return BMI120_GYRO_OUTPUT_DATA_RATE_25HZ;
852
853 }
854
855 /**
856 * This function sets the gyro ODR
857 *
858 * @param[in] drv pointer to the i2c dev
859 * @param[in] hz the frequency required
860 * @return the operation status, 0 is OK, others is error
861 */
drv_gyro_bosch_bmi120_set_odr(i2c_dev_t * drv,uint32_t hz)862 static int drv_gyro_bosch_bmi120_set_odr(i2c_dev_t* drv, uint32_t hz)
863 {
864 int ret = 0;
865 uint8_t value = 0x00;
866 uint8_t odr = drv_gyro_bosch_bmi120_hz2odr(hz);
867
868 ret = sensor_i2c_read(drv, BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__REG,
869 &value, I2C_DATA_LEN, I2C_OP_RETRIES);
870 if(unlikely(ret) != 0) {
871 return ret;
872 }
873
874 value = BMI120_SET_BITSLICE(value, BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE,
875 odr);
876 ret = sensor_i2c_write(drv, BMI120_USER_GYRO_CONFIG_OUTPUT_DATA_RATE__REG,
877 &value, I2C_DATA_LEN, I2C_OP_RETRIES);
878 aos_msleep(2);
879 if(unlikely(ret) != 0) {
880 return ret;
881 }
882 return 0;
883 }
884
885 /**
886 * This function sets the gyro range
887 *
888 * @param[in] drv pointer to the i2c dev
889 * @param[in] hz the range required
890 * @return the operation status, 0 is OK, others is error
891 */
drv_gyro_bosch_bmi120_set_range(i2c_dev_t * drv,uint32_t range)892 static int drv_gyro_bosch_bmi120_set_range(i2c_dev_t* drv, uint32_t range)
893 {
894 int ret = 0;
895 uint8_t value = 0x00;
896 uint8_t tmp = 0;
897
898 ret = sensor_i2c_read(drv, BMI120_USER_GYRO_RANGE__REG, &value, I2C_DATA_LEN,
899 I2C_OP_RETRIES);
900 if(unlikely(ret) != 0) {
901 return ret;
902 }
903
904 switch(range) {
905 case GYRO_RANGE_125DPS: {
906 tmp = BMI120_GYRO_RANGE_125_DEG_SEC;
907 }
908 break;
909 case GYRO_RANGE_250DPS: {
910 tmp = BMI120_GYRO_RANGE_250_DEG_SEC;
911 }
912 break;
913
914 case GYRO_RANGE_500DPS: {
915 tmp = BMI120_GYRO_RANGE_500_DEG_SEC;
916 }
917 break;
918
919 case GYRO_RANGE_1000DPS: {
920 tmp = BMI120_GYRO_RANGE_1000_DEG_SEC;
921 }
922 break;
923
924 case GYRO_RANGE_2000DPS: {
925 tmp = BMI120_GYRO_RANGE_2000_DEG_SEC;
926 }
927 break;
928
929 default:
930 break;
931 }
932
933 value = BMI120_SET_BITSLICE(value, BMI120_USER_GYRO_RANGE, tmp);
934 ret = sensor_i2c_write(drv, BMI120_USER_GYRO_RANGE__REG, &value, I2C_DATA_LEN,
935 I2C_OP_RETRIES);
936 aos_msleep(2);
937 if(unlikely(ret) != 0) {
938 return ret;
939 }
940
941 if((range >= GYRO_RANGE_250DPS)&&(range <= GYRO_RANGE_2000DPS)) {
942 g_cur_gyro_factor = g_bmi120_gyro_factor[range];
943 }
944
945 return 0;
946 }
947
948 /**
949 * This function is ISR
950 *
951 * @return
952 */
drv_gyro_bosch_bmi120_irq_handle(void)953 static void drv_gyro_bosch_bmi120_irq_handle(void)
954 {
955 /* no handle so far */
956 }
957
958 /**
959 * This function opens the acc
960 *
961 * @return the operation status, 0 is OK, others is error
962 */
drv_gyro_bosch_bmi120_open(void)963 static int drv_gyro_bosch_bmi120_open(void)
964 {
965 int ret = 0;
966 ret = drv_gyro_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_ON);
967 if(unlikely(ret) != 0) {
968 return -1;
969 }
970
971 ret = drv_gyro_bosch_bmi120_set_range(&bmi120_ctx, GYRO_RANGE_1000DPS);
972 if(unlikely(ret) != 0) {
973 return -1;
974 }
975
976 ret = drv_gyro_bosch_bmi120_set_odr(&bmi120_ctx, BMI120_GYRO_DEFAULT_ODR_25HZ);
977 if(unlikely(ret) != 0) {
978 return -1;
979 }
980
981 return 0;
982
983 }
984
985 /**
986 * This function closes the gyro
987 *
988 * @return the operation status, 0 is OK, others is error
989 */
drv_gyro_bosch_bmi120_close(void)990 static int drv_gyro_bosch_bmi120_close(void)
991 {
992 int ret = 0;
993 ret = drv_gyro_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_OFF);
994 if(unlikely(ret) != 0) {
995 return -1;
996 }
997 return 0;
998 }
999
1000 /**
1001 * This function reads the gyro data and reports the data
1002 *
1003 * @param[in out] buf buffer for gyro data
1004 * @param[in out] len length of data
1005 * @return the operation status, 0 is OK, others is error
1006 */
drv_gyro_bosch_bmi120_read(void * buf,size_t len)1007 static int drv_gyro_bosch_bmi120_read(void *buf, size_t len)
1008 {
1009 int ret = 0;
1010 size_t size;
1011 uint8_t reg[6];
1012 gyro_data_t *gyro = (gyro_data_t *)buf;
1013 if(buf == NULL) {
1014 return -1;
1015 }
1016
1017 size = sizeof(gyro_data_t);
1018 if(len < size) {
1019 return -1;
1020 }
1021
1022 ret = sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_8_GYRO_X_LSB__REG,
1023 ®[0], I2C_REG_LEN, I2C_OP_RETRIES);
1024 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_9_GYRO_X_MSB__REG,
1025 ®[1], I2C_REG_LEN, I2C_OP_RETRIES);
1026 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_10_GYRO_Y_LSB__REG,
1027 ®[2], I2C_REG_LEN, I2C_OP_RETRIES);
1028 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_11_GYRO_Y_MSB__REG,
1029 ®[3], I2C_REG_LEN, I2C_OP_RETRIES);
1030 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_12_GYRO_Z_LSB__REG,
1031 ®[4], I2C_REG_LEN, I2C_OP_RETRIES);
1032 ret |= sensor_i2c_read(&bmi120_ctx, BMI120_USER_DATA_13_GYRO_Z_MSB__REG,
1033 ®[5], I2C_REG_LEN, I2C_OP_RETRIES);
1034 if(unlikely(ret) != 0) {
1035 return -1;
1036 }
1037
1038 gyro->data[DATA_AXIS_X] = (int16_t)((((int32_t)((int8_t)reg[1])) <<
1039 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[0]));
1040 gyro->data[DATA_AXIS_Y] = (int16_t)((((int32_t)((int8_t)reg[3])) <<
1041 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[2]));
1042 gyro->data[DATA_AXIS_Z] = (int16_t)((((int32_t)((int8_t)reg[5])) <<
1043 BMI120_SHIFT_BIT_POSITION_BY_08_BITS) | (reg[4]));
1044
1045 if(g_cur_gyro_factor != 0) {
1046 gyro->data[DATA_AXIS_X] = gyro->data[DATA_AXIS_X] * GYROSCOPE_UNIT_FACTOR /
1047 g_cur_gyro_factor;
1048 gyro->data[DATA_AXIS_Y] = gyro->data[DATA_AXIS_Y] * GYROSCOPE_UNIT_FACTOR /
1049 g_cur_gyro_factor;
1050 gyro->data[DATA_AXIS_Z] = gyro->data[DATA_AXIS_Z] * GYROSCOPE_UNIT_FACTOR /
1051 g_cur_gyro_factor;
1052 }
1053 gyro->timestamp = aos_now_ms();
1054
1055 return (int)size;
1056 }
1057
1058 /**
1059 * This function is for the gyro ioctl
1060 *
1061 * @param[in] cmd the ioctl command
1062 * @param[in] arg the correspondding parameter
1063 * @return the operation status, 0 is OK, others is error
1064 */
drv_gyro_bosch_bmi120_ioctl(int cmd,unsigned long arg)1065 static int drv_gyro_bosch_bmi120_ioctl(int cmd, unsigned long arg)
1066 {
1067 int ret = 0;
1068
1069 switch(cmd) {
1070 case SENSOR_IOCTL_ODR_SET: {
1071 ret = drv_gyro_bosch_bmi120_set_odr(&bmi120_ctx, arg);
1072 if(unlikely(ret) != 0) {
1073 return -1;
1074 }
1075 }
1076 break;
1077 case SENSOR_IOCTL_RANGE_SET: {
1078 ret = drv_gyro_bosch_bmi120_set_range(&bmi120_ctx, arg);
1079 if(unlikely(ret) != 0) {
1080 return -1;
1081 }
1082 }
1083 break;
1084 case SENSOR_IOCTL_SET_POWER: {
1085 ret = drv_gyro_bosch_bmi120_set_power_mode(&bmi120_ctx, arg);
1086 if(unlikely(ret) != 0) {
1087 return -1;
1088 }
1089 }
1090 break;
1091 case SENSOR_IOCTL_GET_INFO: {
1092 /* fill the dev info here */
1093 dev_sensor_info_t *info = (dev_sensor_info_t *)arg;
1094 info->model = "BMI120";
1095 info->range_max = 2000;
1096 info->range_min = 125;
1097 info->unit = udps;
1098 }
1099 break;
1100
1101 default:
1102 break;
1103 }
1104
1105 return 0;
1106 }
1107
1108 /**
1109 * This function is for the gyro initialization
1110 *
1111 * @return the operation status, 0 is OK, others is error
1112 */
drv_gyro_bosch_bmi120_init(void)1113 int drv_gyro_bosch_bmi120_init(void) {
1114 int ret = 0;
1115 sensor_obj_t sensor;
1116 memset(&sensor, 0, sizeof(sensor));
1117
1118 /* fill the sensor obj parameters here */
1119 sensor.io_port = I2C_PORT;
1120 sensor.tag = TAG_DEV_GYRO;
1121 sensor.path = dev_gyro_path;
1122 sensor.open = drv_gyro_bosch_bmi120_open;
1123 sensor.close = drv_gyro_bosch_bmi120_close;
1124 sensor.read = drv_gyro_bosch_bmi120_read;
1125 sensor.write = NULL;
1126 sensor.ioctl = drv_gyro_bosch_bmi120_ioctl;
1127 sensor.irq_handle = drv_gyro_bosch_bmi120_irq_handle;
1128
1129 ret = sensor_create_obj(&sensor);
1130 if(unlikely(ret) != 0) {
1131 return -1;
1132 }
1133
1134 ret = drv_acc_gyro_bosch_bmi120_validate_id(&bmi120_ctx, BMI120_CHIP_ID_VALUE);
1135 if(unlikely(ret) != 0) {
1136 return -1;
1137 }
1138
1139 if(0 == g_bmi120flag) {
1140 ret = drv_acc_gyro_bosch_bmi120_soft_reset(&bmi120_ctx);
1141 if(unlikely(ret) != 0) {
1142 return -1;
1143 }
1144
1145 ret = drv_gyro_bosch_bmi120_set_power_mode(&bmi120_ctx, DEV_POWER_OFF);
1146 if(unlikely(ret) != 0) {
1147 return -1;
1148 }
1149 g_bmi120flag = 1;
1150 } else {
1151 LOG("%s %s gyro do not need reset\n", SENSOR_STR, __func__);
1152 }
1153
1154 /* update the phy sensor info to sensor hal */
1155 LOG("%s %s successfully \n", SENSOR_STR, __func__);
1156 return 0;
1157 }
1158
1159 SENSOR_DRV_ADD(drv_acc_bosch_bmi120_init);
1160 SENSOR_DRV_ADD(drv_gyro_bosch_bmi120_init);