bmi160.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**
  2. * Copyright (C) 2015 - 2016 Bosch Sensortec GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the copyright holder nor the names of the
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23. * OR CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33. *
  34. * The information provided is believed to be accurate and reliable.
  35. * The copyright holder assumes no responsibility
  36. * for the consequences of use
  37. * of such information nor for any infringement of patents or
  38. * other rights of third parties which may result from its use.
  39. * No license is granted by implication or otherwise under any patent or
  40. * patent rights of the copyright holder.
  41. *
  42. * @file bmi160.h
  43. * @date 13 Apr 2017
  44. * @version 3.5.0
  45. * @brief
  46. *
  47. */
  48. /*!
  49. * @defgroup bmi160
  50. * @brief
  51. * @{*/
  52. #ifndef BMI160_H_
  53. #define BMI160_H_
  54. #ifdef __cplusplus
  55. extern "C"
  56. {
  57. #endif
  58. #include "bmi160_defs.h"
  59. #ifdef __KERNEL__
  60. #include <bmi160_math.h>
  61. #else
  62. #include <math.h>
  63. #include <string.h>
  64. #include <stdlib.h>
  65. #endif
  66. /*********************** User function prototypes ************************/
  67. /*!
  68. * @brief This API is the entry point for sensor.It performs
  69. * the selection of I2C/SPI read mechanism according to the
  70. * selected interface and reads the chip-id of bmi160 sensor.
  71. *
  72. * @param[in,out] dev : Structure instance of bmi160_dev
  73. * @note : Refer user guide for detailed info.
  74. *
  75. * @return Result of API execution status
  76. * @retval zero -> Success / -ve value -> Error
  77. */
  78. int8_t bmi160_init(struct bmi160_dev *dev);
  79. /*!
  80. * @brief This API reads the data from the given register address of sensor.
  81. *
  82. * @param[in] reg_addr : Register address from where the data to be read
  83. * @param[out] data : Pointer to data buffer to store the read data.
  84. * @param[in] len : No of bytes of data to be read.
  85. * @param[in] dev : Structure instance of bmi160_dev.
  86. *
  87. * @return Result of API execution status
  88. * @retval zero -> Success / -ve value -> Error
  89. */
  90. int8_t bmi160_get_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev);
  91. /*!
  92. * @brief This API writes the given data to the register address
  93. * of sensor.
  94. *
  95. * @param[in] reg_addr : Register address from where the data to be written.
  96. * @param[in] data : Pointer to data buffer which is to be written
  97. * in the sensor.
  98. * @param[in] len : No of bytes of data to write..
  99. * @param[in] dev : Structure instance of bmi160_dev.
  100. *
  101. * @return Result of API execution status
  102. * @retval zero -> Success / -ve value -> Error
  103. */
  104. int8_t bmi160_set_regs(uint8_t reg_addr, uint8_t *data, uint16_t len, const struct bmi160_dev *dev);
  105. /*!
  106. * @brief This API resets and restarts the device.
  107. * All register values are overwritten with default parameters.
  108. *
  109. * @param[in] dev : Structure instance of bmi160_dev.
  110. *
  111. * @return Result of API execution status
  112. * @retval zero -> Success / -ve value -> Error.
  113. */
  114. int8_t bmi160_soft_reset(const struct bmi160_dev *dev);
  115. /*!
  116. * @brief This API configures the power mode, range and bandwidth
  117. * of sensor.
  118. *
  119. * @param[in] dev : Structure instance of bmi160_dev.
  120. * @note : Refer user guide for detailed info.
  121. *
  122. * @return Result of API execution status
  123. * @retval zero -> Success / -ve value -> Error.
  124. */
  125. int8_t bmi160_set_sens_conf(struct bmi160_dev *dev);
  126. /*!
  127. * @brief This API sets the power mode of the sensor.
  128. *
  129. * @param[in] dev : Structure instance of bmi160_dev.
  130. *
  131. * @return Result of API execution status
  132. * @retval zero -> Success / -ve value -> Error.
  133. */
  134. int8_t bmi160_set_power_mode(struct bmi160_dev *dev);
  135. /*!
  136. * @brief This API reads sensor data, stores it in
  137. * the bmi160_sensor_data structure pointer passed by the user.
  138. * The user can ask for accel data ,gyro data or both sensor
  139. * data using bmi160_select_sensor enum
  140. *
  141. * @param[in] select_sensor : enum to choose accel,gyro or both sensor data
  142. * @param[out] accel : Structure pointer to store accel data
  143. * @param[out] gyro : Structure pointer to store gyro data
  144. * @param[in] dev : Structure instance of bmi160_dev.
  145. * @note : Refer user guide for detailed info.
  146. *
  147. * @return Result of API execution status
  148. * @retval zero -> Success / -ve value -> Error
  149. */
  150. int8_t bmi160_get_sensor_data(uint8_t select_sensor, struct bmi160_sensor_data *accel,
  151. struct bmi160_sensor_data *gyro, const struct bmi160_dev *dev);
  152. /*!
  153. * @brief This API configures the necessary interrupt based on
  154. * the user settings in the bmi160_int_settg structure instance.
  155. *
  156. * @param[in] int_config : Structure instance of bmi160_int_settg.
  157. * @param[in] dev : Structure instance of bmi160_dev.
  158. * @note : Refer user guide for detailed info.
  159. *
  160. * @return Result of API execution status
  161. * @retval zero -> Success / -ve value -> Error
  162. */
  163. int8_t bmi160_set_int_config(struct bmi160_int_settg *int_config, struct bmi160_dev *dev);
  164. /*!
  165. * @brief This API enables the step counter feature.
  166. *
  167. * @param[in] step_cnt_enable : value to enable or disable
  168. * @param[in] dev : Structure instance of bmi160_dev.
  169. * @note : Refer user guide for detailed info.
  170. *
  171. * @return Result of API execution status
  172. * @retval zero -> Success / -ve value -> Error
  173. */
  174. int8_t bmi160_set_step_counter(uint8_t step_cnt_enable, const struct bmi160_dev *dev);
  175. /*!
  176. * @brief This API reads the step counter value.
  177. *
  178. * @param[in] step_val : Pointer to store the step counter value.
  179. * @param[in] dev : Structure instance of bmi160_dev.
  180. * @note : Refer user guide for detailed info.
  181. *
  182. * @return Result of API execution status
  183. * @retval zero -> Success / -ve value -> Error
  184. */
  185. int8_t bmi160_read_step_counter(uint16_t *step_val, const struct bmi160_dev *dev);
  186. /*!
  187. * @brief This API reads the mention no of byte of data from the given
  188. * register address of auxiliary sensor.
  189. *
  190. * @param[in] reg_addr : Address of register to read.
  191. * @param[in] aux_data : Pointer to store the read data.
  192. * @param[in] len : No of bytes to read.
  193. * @param[in] dev : Structure instance of bmi160_dev.
  194. * @note : Refer user guide for detailed info.
  195. *
  196. * @return Result of API execution status
  197. * @retval zero -> Success / -ve value -> Error
  198. */
  199. int8_t bmi160_aux_read(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev);
  200. /*!
  201. * @brief This API writes the mention no of byte of data to the given
  202. * register address of auxiliary sensor.
  203. *
  204. * @param[in] reg_addr : Address of register to write.
  205. * @param[in] aux_data : Pointer to write data.
  206. * @param[in] len : No of bytes to write.
  207. * @param[in] dev : Structure instance of bmi160_dev.
  208. * @note : Refer user guide for detailed info.
  209. *
  210. * @return Result of API execution status
  211. * @retval zero -> Success / -ve value -> Error
  212. */
  213. int8_t bmi160_aux_write(uint8_t reg_addr, uint8_t *aux_data, uint16_t len, const struct bmi160_dev *dev);
  214. /*!
  215. * @brief This API initialize the auxiliary sensor
  216. * in order to access it.
  217. *
  218. * @param[in] dev : Structure instance of bmi160_dev.
  219. * @note : Refer user guide for detailed info.
  220. *
  221. * @return Result of API execution status
  222. * @retval zero -> Success / -ve value -> Error
  223. */
  224. int8_t bmi160_aux_init(const struct bmi160_dev *dev);
  225. /*!
  226. * @brief This API is used to setup the auxiliary sensor of bmi160 in auto mode
  227. * Thus enabling the auto update of 8 bytes of data from auxiliary sensor
  228. * to BMI160 register address 0x04 to 0x0B
  229. *
  230. * @param[in] data_addr : Starting address of aux. sensor's data register
  231. * (BMI160 registers 0x04 to 0x0B will be updated
  232. * with 8 bytes of data from auxiliary sensor
  233. * starting from this register address.)
  234. * @param[in] dev : Structure instance of bmi160_dev.
  235. *
  236. * @note : Set the value of auxiliary polling rate by setting
  237. * dev->aux_cfg.aux_odr to the required value from the table
  238. * before calling this API
  239. *
  240. * dev->aux_cfg.aux_odr | Auxiliary ODR (Hz)
  241. * -----------------------|-----------------------
  242. * BMI160_AUX_ODR_0_78HZ | 25/32
  243. * BMI160_AUX_ODR_1_56HZ | 25/16
  244. * BMI160_AUX_ODR_3_12HZ | 25/8
  245. * BMI160_AUX_ODR_6_25HZ | 25/4
  246. * BMI160_AUX_ODR_12_5HZ | 25/2
  247. * BMI160_AUX_ODR_25HZ | 25
  248. * BMI160_AUX_ODR_50HZ | 50
  249. * BMI160_AUX_ODR_100HZ | 100
  250. * BMI160_AUX_ODR_200HZ | 200
  251. * BMI160_AUX_ODR_400HZ | 400
  252. * BMI160_AUX_ODR_800HZ | 800
  253. *
  254. * @note : Other values of dev->aux_cfg.aux_odr are reserved and not for use
  255. *
  256. * @return Result of API execution status
  257. * @retval zero -> Success / -ve value -> Error
  258. */
  259. int8_t bmi160_set_aux_auto_mode(uint8_t *data_addr, struct bmi160_dev *dev);
  260. /*!
  261. * @brief This API configures the 0x4C register and settings like
  262. * Auxiliary sensor manual enable/ disable and aux burst read length.
  263. *
  264. * @param[in] dev : Structure instance of bmi160_dev.
  265. *
  266. * @return Result of API execution status
  267. * @retval zero -> Success / -ve value -> Error
  268. */
  269. int8_t bmi160_config_aux_mode(const struct bmi160_dev *dev);
  270. /*!
  271. * @brief This API is used to read the raw uncompensated auxiliary sensor
  272. * data of 8 bytes from BMI160 register address 0x04 to 0x0B
  273. *
  274. * @param[in] aux_data : Pointer to user array of length 8 bytes
  275. * Ensure that the aux_data array is of
  276. * length 8 bytes
  277. * @param[in] dev : Structure instance of bmi160_dev
  278. *
  279. * @return Result of API execution status
  280. * @retval zero -> Success / -ve value -> Error
  281. */
  282. int8_t bmi160_read_aux_data_auto_mode(uint8_t *aux_data, const struct bmi160_dev *dev);
  283. /*!
  284. * @brief This is used to perform self test of accel/gyro of the BMI160 sensor
  285. *
  286. * @param[in] select_sensor : enum to choose accel or gyro for self test
  287. * @param[in] dev : Structure instance of bmi160_dev
  288. *
  289. * @note self test can be performed either for accel/gyro at any instant.
  290. *
  291. * value of select_sensor | Inference
  292. *----------------------------------|--------------------------------
  293. * BMI160_ACCEL_ONLY | Accel self test enabled
  294. * BMI160_GYRO_ONLY | Gyro self test enabled
  295. * BMI160_BOTH_ACCEL_AND_GYRO | NOT TO BE USED
  296. *
  297. * @note The return value of this API gives us the result of self test.
  298. *
  299. * @note Performing self test does soft reset of the sensor, User can
  300. * set the desired settings after performing the self test.
  301. *
  302. * @return Result of API execution status
  303. * @retval zero -> Success / -ve value -> Error / +ve value -> Self-test fail
  304. *
  305. * Return value | Result of self test
  306. * --------------------------------|---------------------------------
  307. * BMI160_OK | Self test success
  308. * BMI160_W_GYRO_SELF_TEST_FAIL | Gyro self test fail
  309. * BMI160_W_ACCEl_SELF_TEST_FAIL | Accel self test fail
  310. */
  311. int8_t bmi160_perform_self_test(uint8_t select_sensor, struct bmi160_dev *dev);
  312. /*!
  313. * @brief This API reads the data from fifo buffer.
  314. *
  315. * @note User has to allocate the FIFO buffer along with
  316. * corresponding fifo length from his side before calling this API
  317. * as mentioned in the readme.md
  318. *
  319. * @note User must specify the number of bytes to read from the FIFO in
  320. * dev->fifo->length , It will be updated by the number of bytes actually
  321. * read from FIFO after calling this API
  322. *
  323. * @param[in] dev : Structure instance of bmi160_dev.
  324. *
  325. * @return Result of API execution status
  326. * @retval zero -> Success / -ve value -> Error
  327. *
  328. */
  329. int8_t bmi160_get_fifo_data(struct bmi160_dev const *dev);
  330. /*!
  331. * @brief This API writes fifo_flush command to command register.This
  332. * action clears all data in the Fifo without changing fifo configuration
  333. * settings.
  334. *
  335. * @param[in] dev : Structure instance of bmi160_dev
  336. *
  337. * @return Result of API execution status
  338. * @retval 0 -> Success
  339. * @retval Any non zero value -> Fail
  340. *
  341. */
  342. int8_t bmi160_set_fifo_flush(const struct bmi160_dev *dev);
  343. /*! @brief This API sets the FIFO configuration in the sensor.
  344. *
  345. * @param[in] config : variable used to specify the FIFO
  346. * configurations which are to be enabled or disabled in the sensor.
  347. *
  348. * @note : User can set either set one or more or all FIFO configurations
  349. * by ORing the below mentioned macros.
  350. * config | Value
  351. * ------------------------|---------------------------
  352. * BMI160_FIFO_TIME | 0x02
  353. * BMI160_FIFO_TAG_INT2 | 0x04
  354. * BMI160_FIFO_TAG_INT1 | 0x08
  355. * BMI160_FIFO_HEADER | 0x10
  356. * BMI160_FIFO_AUX | 0x20
  357. * BMI160_FIFO_ACCEL | 0x40
  358. * BMI160_FIFO_GYRO | 0x80
  359. *
  360. * @param[in] enable : Parameter used to enable or disable the above
  361. * FIFO configuration
  362. * @param[in] dev : Structure instance of bmi160_dev.
  363. *
  364. * @return status of bus communication result
  365. * @retval 0 -> Success
  366. * @retval Any non zero value -> Fail
  367. *
  368. */
  369. int8_t bmi160_set_fifo_config(uint8_t config, uint8_t enable, struct bmi160_dev const *dev);
  370. /*! @brief This API is used to configure the down sampling ratios of
  371. * the accel and gyro data for FIFO.Also, it configures filtered or
  372. * pre-filtered data for the fifo for accel and gyro.
  373. *
  374. * @param[in] fifo_down : variable used to specify the FIFO down
  375. * configurations which are to be enabled or disabled in the sensor.
  376. *
  377. * @note The user must select one among the following macros to
  378. * select down-sampling ratio for accel
  379. * config | Value
  380. * -------------------------------------|---------------------------
  381. * BMI160_ACCEL_FIFO_DOWN_ZERO | 0x00
  382. * BMI160_ACCEL_FIFO_DOWN_ONE | 0x10
  383. * BMI160_ACCEL_FIFO_DOWN_TWO | 0x20
  384. * BMI160_ACCEL_FIFO_DOWN_THREE | 0x30
  385. * BMI160_ACCEL_FIFO_DOWN_FOUR | 0x40
  386. * BMI160_ACCEL_FIFO_DOWN_FIVE | 0x50
  387. * BMI160_ACCEL_FIFO_DOWN_SIX | 0x60
  388. * BMI160_ACCEL_FIFO_DOWN_SEVEN | 0x70
  389. *
  390. * @note The user must select one among the following macros to
  391. * select down-sampling ratio for gyro
  392. *
  393. * config | Value
  394. * -------------------------------------|---------------------------
  395. * BMI160_GYRO_FIFO_DOWN_ZERO | 0x00
  396. * BMI160_GYRO_FIFO_DOWN_ONE | 0x01
  397. * BMI160_GYRO_FIFO_DOWN_TWO | 0x02
  398. * BMI160_GYRO_FIFO_DOWN_THREE | 0x03
  399. * BMI160_GYRO_FIFO_DOWN_FOUR | 0x04
  400. * BMI160_GYRO_FIFO_DOWN_FIVE | 0x05
  401. * BMI160_GYRO_FIFO_DOWN_SIX | 0x06
  402. * BMI160_GYRO_FIFO_DOWN_SEVEN | 0x07
  403. *
  404. * @note The user can enable filtered accel data by the following macro
  405. * config | Value
  406. * -------------------------------------|---------------------------
  407. * BMI160_ACCEL_FIFO_FILT_EN | 0x80
  408. *
  409. * @note The user can enable filtered gyro data by the following macro
  410. * config | Value
  411. * -------------------------------------|---------------------------
  412. * BMI160_GYRO_FIFO_FILT_EN | 0x08
  413. *
  414. * @note : By ORing the above mentioned macros, the user can select
  415. * the required FIFO down config settings
  416. *
  417. * @param[in] dev : Structure instance of bmi160_dev.
  418. *
  419. * @return status of bus communication result
  420. * @retval 0 -> Success
  421. * @retval Any non zero value -> Fail
  422. *
  423. */
  424. int8_t bmi160_set_fifo_down(uint8_t fifo_down, const struct bmi160_dev *dev);
  425. /*!
  426. * @brief This API sets the FIFO watermark level in the sensor.
  427. *
  428. * @note The FIFO watermark is issued when the FIFO fill level is
  429. * equal or above the watermark level and units of watermark is 4 bytes.
  430. *
  431. * @param[in] fifo_wm : Variable used to set the FIFO water mark level
  432. * @param[in] dev : Structure instance of bmi160_dev
  433. *
  434. * @return Result of API execution status
  435. * @retval 0 -> Success
  436. * @retval Any non zero value -> Fail
  437. *
  438. */
  439. int8_t bmi160_set_fifo_wm(uint8_t fifo_wm, const struct bmi160_dev *dev);
  440. /*!
  441. * @brief This API parses and extracts the accelerometer frames from
  442. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  443. * the "accel_data" structure instance.
  444. *
  445. * @note The bmi160_extract_accel API should be called only after
  446. * reading the FIFO data by calling the bmi160_get_fifo_data() API.
  447. *
  448. * @param[out] accel_data : Structure instance of bmi160_sensor_data
  449. * where the accelerometer data in FIFO is stored.
  450. * @param[in,out] accel_length : Number of valid accelerometer frames
  451. * (x,y,z axes data) read out from fifo.
  452. * @param[in] dev : Structure instance of bmi160_dev.
  453. *
  454. * @note accel_length is updated with the number of valid accelerometer
  455. * frames extracted from fifo (1 accel frame = 6 bytes) at the end of
  456. * execution of this API.
  457. *
  458. * @return Result of API execution status
  459. * @retval 0 -> Success
  460. * @retval Any non zero value -> Fail
  461. *
  462. */
  463. int8_t bmi160_extract_accel(struct bmi160_sensor_data *accel_data, uint8_t *accel_length, struct bmi160_dev const *dev);
  464. /*!
  465. * @brief This API parses and extracts the gyro frames from
  466. * FIFO data read by the "bmi160_get_fifo_data" API and stores it in
  467. * the "gyro_data" structure instance.
  468. *
  469. * @note The bmi160_extract_accel API should be called only after
  470. * reading the FIFO data by calling the bmi160_get_fifo_data() API.
  471. *
  472. * @param[out] gyro_data : Structure instance of bmi160_sensor_data
  473. * where the gyro data in FIFO is stored.
  474. * @param[in,out] gyro_length : Number of valid gyro frames
  475. * (x,y,z axes data) read out from fifo.
  476. * @param[in] dev : Structure instance of bmi160_dev.
  477. *
  478. * @note gyro_length is updated with the number of valid gyro
  479. * frames extracted from fifo (1 gyro frame = 6 bytes) at the end of
  480. * execution of this API.
  481. *
  482. * @return Result of API execution status
  483. * @retval 0 -> Success
  484. * @retval Any non zero value -> Fail
  485. *
  486. */
  487. int8_t bmi160_extract_gyro(struct bmi160_sensor_data *gyro_data, uint8_t *gyro_length, struct bmi160_dev const *dev);
  488. #ifdef __cplusplus
  489. }
  490. #endif
  491. #endif /* BMI160_H_ */
  492. /** @}*/