bmi160_support.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. ****************************************************************************
  3. * Copyright (C) 2014 Bosch Sensortec GmbH
  4. *
  5. * bmi160_support.c
  6. * Date: 2014/12/12
  7. * Revision: 1.0.5 $
  8. *
  9. * Usage: Sensor Driver support file for BMI160 sensor
  10. *
  11. ****************************************************************************
  12. * License:
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * Neither the name of the copyright holder nor the names of the
  25. * contributors may be used to endorse or promote products derived from
  26. * this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  30. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  31. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  33. * OR CONTRIBUTORS BE LIABLE FOR ANY
  34. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  35. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  36. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  39. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. * ANY WAY OUT OF THE USE OF THIS
  42. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  43. *
  44. * The information provided is believed to be accurate and reliable.
  45. * The copyright holder assumes no responsibility
  46. * for the consequences of use
  47. * of such information nor for any infringement of patents or
  48. * other rights of third parties which may result from its use.
  49. * No license is granted by implication or otherwise under any patent or
  50. * patent rights of the copyright holder.
  51. **************************************************************************/
  52. #include "bmi160_support.h"
  53. #include "bmi160.h"
  54. /* Mapping the structure*/
  55. struct bmi160_t s_bmi160;
  56. /* Read the sensor data of accel, gyro and mag*/
  57. struct bmi160_gyro_t gyroxyz;
  58. struct bmi160_accel_t accelxyz;
  59. struct bmi160_mag_t magxyzr;
  60. struct trim_data *mag_trim;
  61. /*!
  62. * @brief This function used for initialize the sensor
  63. *
  64. *
  65. * @return results of bus communication function
  66. * @retval 0 -> Success
  67. * @retval 1 -> Error
  68. *
  69. *
  70. */
  71. BMI160_RETURN_FUNCTION_TYPE bmi160_initialize_sensor(u8 v_running_mode_u8)
  72. {
  73. BMI160_RETURN_FUNCTION_TYPE com_rslt = C_BMI160_ZERO_U8X;
  74. /* Based on the user need configure I2C or SPI interface.
  75. * It is sample code to explain how to use the bmi160 API*/
  76. #ifdef INCLUDE_BMI160API
  77. com_rslt = spi_routine();
  78. /*SPI_routine(); */
  79. #endif
  80. /*
  81. * This function used to assign the value/reference of
  82. * the following parameters
  83. * I2C address
  84. * Bus Write
  85. * Bus read
  86. * company_id
  87. */
  88. com_rslt += bmi160_init(&s_bmi160);
  89. if(s_bmi160.chip_id == 0)return -1;
  90. uint8_t acc_mode = 0xFF,gryo_mode = 0xFF;
  91. uint32_t count = 0;
  92. do{
  93. com_rslt += bmi160_set_command_register(0xB6);//reset
  94. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  95. bmi160_get_accel_power_mode_stat(&acc_mode);
  96. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  97. bmi160_get_gyro_power_mode_stat(&gryo_mode);
  98. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  99. if(count++ > 10)break;
  100. }while(acc_mode != 0x00 && gryo_mode != 0x00);
  101. /**** STANDARD_UI_IMU output****/
  102. com_rslt += bmi160_config_running_mode(v_running_mode_u8);
  103. return com_rslt;
  104. }
  105. /*!
  106. * @brief This Function used to read the sensor data using
  107. * different running mode
  108. * @param v_running_mode_u8 : The value of running mode
  109. * Description | value
  110. * --------------------------------|----------
  111. * STANDARD_UI_9DOF_FIFO | 0
  112. * STANDARD_UI_IMU_FIFO | 1
  113. * STANDARD_UI_IMU | 2
  114. * STANDARD_UI_ADVANCEPOWERSAVE | 3
  115. * ACCEL_PEDOMETER | 4
  116. * ACCEL_PEDOMETER_FIFO | 5
  117. * APPLICATION_HEAD_TRACKING | 6
  118. * APPLICATION_NAVIGATION | 7
  119. * APPLICATION_REMOTE_CONTROL | 8
  120. * APPLICATION_INDOOR_NAVIGATION | 9
  121. *
  122. *
  123. * @return results of bus communication function
  124. * @retval 0 -> Success
  125. * @retval 1 -> Error
  126. *
  127. *
  128. */
  129. BMI160_RETURN_FUNCTION_TYPE bmi160_config_running_mode(
  130. u8 v_running_mode_u8)
  131. {
  132. struct gyro_sleep_setting gyr_setting;
  133. struct bmi160_mag_xyz_s32_t magxyz;
  134. /* Variable used for get the status of mag interface*/
  135. u8 v_mag_interface_u8 = C_BMI160_ZERO_U8X;
  136. BMI160_RETURN_FUNCTION_TYPE com_rslt = ERROR;
  137. /* Configure the gyro sleep setting based on your need*/
  138. if (v_running_mode_u8 == STANDARD_UI_ADVANCEPOWERSAVE) {
  139. gyr_setting. sleep_trigger = BMI160_WRITE_FOUR;
  140. gyr_setting. wakeup_trigger = BMI160_WRITE_TWO;
  141. gyr_setting. sleep_state = BMI160_WRITE_ZERO;
  142. gyr_setting. wakeup_int = BMI160_WRITE_ZERO;
  143. }
  144. /* The below code used for enable and
  145. disable the secondary mag interface*/
  146. com_rslt = bmi160_get_if_mode(&v_mag_interface_u8);
  147. if (((v_running_mode_u8 == STANDARD_UI_IMU_FIFO) ||
  148. (v_running_mode_u8 == STANDARD_UI_IMU) ||
  149. (v_running_mode_u8 == STANDARD_UI_ADVANCEPOWERSAVE) ||
  150. (v_running_mode_u8 == APPLICATION_NAVIGATION) ||
  151. (v_running_mode_u8 == ACCEL_PEDOMETER) ||
  152. (v_running_mode_u8 == APPLICATION_REMOTE_CONTROL) ||
  153. (v_running_mode_u8 == APPLICATION_INDOOR_NAVIGATION))
  154. && (v_mag_interface_u8 == BMI160_MAG_INTERFACE_ON_PRIMARY_ON)) {
  155. com_rslt +=
  156. bmi160_set_bmm150_mag_and_secondary_if_power_mode(
  157. MAG_SUSPEND_MODE);
  158. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  159. com_rslt += bmi160_set_if_mode(
  160. BMI160_MAG_INTERFACE_OFF_PRIMARY_ON);
  161. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  162. }
  163. if (((v_running_mode_u8 == STANDARD_UI_9DOF_FIFO)
  164. || (v_running_mode_u8 == APPLICATION_HEAD_TRACKING) ||
  165. (v_running_mode_u8 == APPLICATION_NAVIGATION)) &&
  166. (v_mag_interface_u8 == BMI160_MAG_INTERFACE_OFF_PRIMARY_ON)) {
  167. /* Init the magnetometer */
  168. com_rslt += bmi160_bmm150_mag_interface_init();
  169. /* bmi160_delay_ms in ms*/
  170. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  171. }
  172. switch (v_running_mode_u8) {
  173. case STANDARD_UI_9DOF_FIFO:
  174. /*Set the accel mode as Normal write in the register 0x7E*/
  175. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  176. /* bmi160_delay_ms in ms*/
  177. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  178. /*Set the gyro mode as Normal write in the register 0x7E*/
  179. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  180. /* bmi160_delay_ms in ms*/
  181. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  182. /* Set the accel bandwidth as Normal */
  183. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  184. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  185. /* Set the gryo bandwidth as Normal */
  186. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  187. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  188. /* set gyro data rate as 100Hz*/
  189. com_rslt += bmi160_set_gyro_output_data_rate(
  190. BMI160_GYRO_OUTPUT_DATA_RATE_100HZ);
  191. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  192. /* set accel data rate as 100Hz*/
  193. com_rslt += bmi160_set_accel_output_data_rate(
  194. BMI160_ACCEL_OUTPUT_DATA_RATE_100HZ);
  195. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  196. /***** read FIFO data based on interrupt*****/
  197. com_rslt += bmi160_interrupt_configuration();
  198. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  199. /* Enable the FIFO header*/
  200. com_rslt += bmi160_set_fifo_header_enable(FIFO_HEADER_ENABLE);
  201. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  202. /* Enable the FIFO mag*/
  203. com_rslt += bmi160_set_fifo_mag_enable(FIFO_MAG_ENABLE);
  204. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  205. /* Enable the FIFO accel*/
  206. com_rslt += bmi160_set_fifo_accel_enable(FIFO_ACCEL_ENABLE);
  207. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  208. /* Enable the FIFO gyro*/
  209. com_rslt += bmi160_set_fifo_gyro_enable(FIFO_GYRO_ENABLE);
  210. /* Enable the FIFO time*/
  211. com_rslt += bmi160_set_fifo_time_enable(FIFO_TIME_ENABLE);
  212. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  213. /* Enable the FIFO stop on full*/
  214. com_rslt += bmi160_set_fifo_stop_on_full(
  215. FIFO_STOPONFULL_ENABLE);
  216. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  217. /* Enable the FIFO water mark interrupt1*/
  218. com_rslt += bmi160_set_intr_fifo_wm(C_BMI160_ZERO_U8X,
  219. FIFO_WM_INTERRUPT_ENABLE);
  220. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  221. /* Enable the FIFO water mark interrupt2*/
  222. com_rslt += bmi160_set_intr_fifo_wm(C_BMI160_ONE_U8X,
  223. FIFO_WM_INTERRUPT_ENABLE);
  224. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  225. /* set the fifo water mark*/
  226. com_rslt += bmi160_set_fifo_wm(BMI160_WRITE_TWO);
  227. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  228. /* read the FIFO data*/
  229. com_rslt += bmi160_read_fifo_header_data(FIFO_FRAME);
  230. break;
  231. case STANDARD_UI_IMU_FIFO:
  232. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  233. /* bmi160_delay_ms in ms*/
  234. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  235. /*Set the gyro mode as Normal write in the register 0x7E*/
  236. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  237. /* bmi160_delay_ms in ms*/
  238. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  239. /* Set the accel bandwidth as Normal */
  240. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  241. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  242. /* Set the gryo bandwidth as Normal */
  243. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  244. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  245. /* set gyro data rate as 100Hz*/
  246. com_rslt += bmi160_set_gyro_output_data_rate(
  247. BMI160_GYRO_OUTPUT_DATA_RATE_100HZ);
  248. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  249. /* set accel data rate as 100Hz*/
  250. com_rslt += bmi160_set_accel_output_data_rate(
  251. BMI160_ACCEL_OUTPUT_DATA_RATE_100HZ);
  252. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  253. /***** read FIFO data based on interrupt*****/
  254. com_rslt += bmi160_interrupt_configuration();
  255. /* Enable the FIFO header*/
  256. com_rslt += bmi160_set_fifo_header_enable(FIFO_HEADER_ENABLE);
  257. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  258. /* Enable the FIFO accel*/
  259. com_rslt += bmi160_set_fifo_accel_enable(FIFO_ACCEL_ENABLE);
  260. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  261. /* Enable the FIFO gyro*/
  262. com_rslt += bmi160_set_fifo_gyro_enable(FIFO_GYRO_ENABLE);
  263. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  264. /* Enable the FIFO time*/
  265. com_rslt += bmi160_set_fifo_time_enable(FIFO_TIME_ENABLE);
  266. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  267. /* Enable the FIFO stop on full*/
  268. com_rslt += bmi160_set_fifo_stop_on_full(
  269. FIFO_STOPONFULL_ENABLE);
  270. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  271. /* Enable the FIFO water mark interrupt1*/
  272. com_rslt += bmi160_set_intr_fifo_wm(C_BMI160_ZERO_U8X,
  273. FIFO_WM_INTERRUPT_ENABLE);
  274. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  275. /* Enable the FIFO water mark interrupt2*/
  276. com_rslt += bmi160_set_intr_fifo_wm(C_BMI160_ONE_U8X,
  277. FIFO_WM_INTERRUPT_ENABLE);
  278. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  279. /* set the fifo water mark as 10*/
  280. com_rslt += bmi160_set_fifo_wm(BMI160_WRITE_TWO);
  281. // /* read the FIFO data*/
  282. // com_rslt += bmi160_read_fifo_header_data(FIFO_FRAME);
  283. break;
  284. case STANDARD_UI_IMU:
  285. /*Set the accel mode as Normal write in the register 0x7E*/
  286. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  287. /* bmi160_delay_ms in ms*/
  288. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  289. /*Set the gyro mode as Normal write in the register 0x7E*/
  290. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  291. /* bmi160_delay_ms in ms*/
  292. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  293. /* Set the accel range as BMI160_ACCEL_RANGE_16G */
  294. com_rslt += bmi160_set_accel_range(BMI160_ACCEL_RANGE_16G);
  295. /* bmi160_delay_ms in ms*/
  296. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  297. com_rslt += bmi160_set_gyro_range(BMI160_GYRO_RANGE_2000_DEG_SEC);
  298. /* bmi160_delay_ms in ms*/
  299. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  300. /* Set the accel bandwidth as Normal */
  301. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  302. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  303. /* Set the gryo bandwidth as Normal */
  304. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  305. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  306. /* set gyro data rate as 100Hz*/
  307. com_rslt += bmi160_set_gyro_output_data_rate(
  308. BMI160_GYRO_OUTPUT_DATA_RATE_100HZ);
  309. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  310. /* set accel data rate as 100Hz*/
  311. com_rslt += bmi160_set_accel_output_data_rate(
  312. BMI160_ACCEL_OUTPUT_DATA_RATE_100HZ);
  313. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  314. /* read gyro data*/
  315. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  316. /* read accel data*/
  317. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  318. u8 acc_range,gryo_range;
  319. com_rslt += bmi160_get_gyro_range(&gryo_range);
  320. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  321. com_rslt += bmi160_get_accel_range(&acc_range);
  322. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  323. if(acc_range != BMI160_ACCEL_RANGE_16G || gryo_range != BMI160_GYRO_RANGE_2000_DEG_SEC)com_rslt += -1;
  324. if(gyroxyz.x == 0 && gyroxyz.y == 0 && gyroxyz.z == 0)com_rslt += -1;
  325. if(accelxyz.x == 0 && accelxyz.y == 0 && accelxyz.z == 0)com_rslt += -1;
  326. break;
  327. case STANDARD_UI_ADVANCEPOWERSAVE:
  328. /*Set the accel mode as Normal write in the register 0x7E*/
  329. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  330. /* bmi160_delay_ms in ms*/
  331. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  332. /* Set the accel bandwidth as Normal */
  333. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  334. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  335. /* Set the gryo bandwidth as Normal */
  336. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  337. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  338. /* set gyro data rate as 100Hz*/
  339. com_rslt += bmi160_set_gyro_output_data_rate(
  340. BMI160_GYRO_OUTPUT_DATA_RATE_100HZ);
  341. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  342. /* set accel data rate as 100Hz*/
  343. com_rslt += bmi160_set_accel_output_data_rate(
  344. BMI160_ACCEL_OUTPUT_DATA_RATE_100HZ);
  345. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  346. /* Enable any motion interrupt - x axis*/
  347. com_rslt += bmi160_set_intr_enable_0(C_BMI160_ZERO_U8X,
  348. BMI160_WRITE_ONE);
  349. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  350. /* Enable any motion interrupt - y axis*/
  351. com_rslt += bmi160_set_intr_enable_0(C_BMI160_ONE_U8X,
  352. BMI160_WRITE_ONE);
  353. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  354. /* Enable any motion interrupt - z axis*/
  355. com_rslt += bmi160_set_intr_enable_0(C_BMI160_TWO_U8X,
  356. BMI160_WRITE_ONE);
  357. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  358. /* Enable no motion interrupt - x axis*/
  359. com_rslt += bmi160_set_intr_enable_2(C_BMI160_ZERO_U8X,
  360. BMI160_WRITE_ONE);
  361. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  362. /* Enable no motion interrupt - y axis*/
  363. com_rslt += bmi160_set_intr_enable_2(C_BMI160_ONE_U8X,
  364. BMI160_WRITE_ONE);
  365. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  366. /* Enable no motion interrupt - z axis*/
  367. com_rslt += bmi160_set_intr_enable_2(C_BMI160_TWO_U8X,
  368. BMI160_WRITE_ONE);
  369. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  370. /* set the gyro sleep trigger*/
  371. com_rslt += bmi160_set_gyro_sleep_trigger(
  372. gyr_setting.sleep_trigger);
  373. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  374. /* set the gyro wakeup trigger*/
  375. com_rslt += bmi160_set_gyro_wakeup_trigger(
  376. gyr_setting.wakeup_trigger);
  377. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  378. /* set the gyro sleep state*/
  379. com_rslt += bmi160_set_gyro_sleep_state(
  380. gyr_setting.sleep_state);
  381. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  382. /* set the gyro wakeup interrupt*/
  383. com_rslt += bmi160_set_gyro_wakeup_intr(gyr_setting.wakeup_int);
  384. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  385. /* read gyro data*/
  386. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  387. /* read accel data*/
  388. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  389. break;
  390. case ACCEL_PEDOMETER:
  391. com_rslt = bmi160_set_accel_under_sampling_parameter(BMI160_ENABLE);//¨°a?a??1y2¨¦?¨´?¡ê¨º?¡ê?2?¨¨???2?¨¢?¦Ì¨ª1|o??¡ê¨º?
  392. /*Set the accel mode as Normal write in the register 0x7E*/
  393. com_rslt = bmi160_set_command_register(ACCEL_LOWPOWER);
  394. /* bmi160_delay_ms in ms*/
  395. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  396. com_rslt += bmi160_set_accel_range(BMI160_ACCEL_RANGE_16G);
  397. /* bmi160_delay_ms in ms*/
  398. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  399. com_rslt = bmi160_set_accel_under_sampling_parameter(BMI160_ENABLE);//¨°a?a??1y2¨¦?¨´?¡ê¨º?¡ê?2?¨¨???2?¨¢?¦Ì¨ª1|o??¡ê¨º?
  400. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  401. /*Set the gyro mode as SUSPEND write in the register 0x7E*/
  402. com_rslt += bmi160_set_command_register(GYRO_MODE_SUSPEND);
  403. /* bmi160_delay_ms in ms*/
  404. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  405. /* Set the accel bandwidth as OSR4 */
  406. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_OSR4_AVG1);
  407. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  408. /* set accel data rate as 25Hz*/
  409. com_rslt += bmi160_set_accel_output_data_rate(
  410. BMI160_ACCEL_OUTPUT_DATA_RATE_25HZ);
  411. /* 10 not available*/
  412. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  413. /* read accel data*/
  414. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  415. if(accelxyz.x == 0 && accelxyz.y == 0 && accelxyz.z == 0)com_rslt += -1;
  416. break;
  417. case ALL_SUSPEND:
  418. com_rslt += bmi160_set_command_register(GYRO_MODE_SUSPEND);
  419. /* bmi160_delay_ms in ms*/
  420. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  421. com_rslt += bmi160_set_command_register(ACCEL_SUSPEND);
  422. /* bmi160_delay_ms in ms*/
  423. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  424. break;
  425. case ACCEL_PEDOMETER_FIFO:
  426. /*Set the accel mode as Normal write in the register 0x7E*/
  427. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  428. /* bmi160_delay_ms in ms*/
  429. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  430. /*Set the gyro mode as SUSPEND write in the register 0x7E*/
  431. com_rslt += bmi160_set_command_register(GYRO_MODE_SUSPEND);
  432. /* bmi160_delay_ms in ms*/
  433. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  434. /* Set the accel range as BMI160_ACCEL_RANGE_16G */
  435. com_rslt += bmi160_set_accel_range(BMI160_ACCEL_RANGE_16G);
  436. /* bmi160_delay_ms in ms*/
  437. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  438. /* Set the accel bandwidth as Normal */
  439. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  440. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  441. /* set accel data rate as 100Hz*/
  442. com_rslt += bmi160_set_accel_output_data_rate(
  443. BMI160_ACCEL_OUTPUT_DATA_RATE_100HZ);
  444. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  445. /* Enable the FIFO header*/
  446. com_rslt += bmi160_set_fifo_header_enable(FIFO_HEADER_ENABLE);
  447. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  448. /* Enable the FIFO accel*/
  449. com_rslt += bmi160_set_fifo_accel_enable(FIFO_ACCEL_ENABLE);
  450. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  451. /* Enable the FIFO stop on full*/
  452. com_rslt += bmi160_set_fifo_stop_on_full(
  453. FIFO_STOPONFULL_ENABLE);
  454. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  455. // /* read the FIFO data*/
  456. // com_rslt += bmi160_read_fifo_header_data(FIFO_FRAME);
  457. break;
  458. case APPLICATION_HEAD_TRACKING:
  459. /*Set the accel mode as Normal write in the register 0x7E*/
  460. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  461. /* bmi160_delay_ms in ms*/
  462. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  463. /*Set the gyro mode as Normal write in the register 0x7E*/
  464. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  465. /* bmi160_delay_ms in ms*/
  466. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  467. /* Set the accel bandwidth as Normal */
  468. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_NORMAL_AVG4);
  469. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  470. /* Set the gryo bandwidth as Normal */
  471. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  472. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  473. /* set gyro data rate as 1600Hz*/
  474. com_rslt += bmi160_set_gyro_output_data_rate(
  475. BMI160_GYRO_OUTPUT_DATA_RATE_1600HZ);
  476. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  477. /* set accel data rate as 1600Hz*/
  478. com_rslt += bmi160_set_accel_output_data_rate(
  479. BMI160_ACCEL_OUTPUT_DATA_RATE_1600HZ);
  480. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  481. /* read gyro data*/
  482. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  483. /* read accel data */
  484. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  485. /* read mag data */
  486. com_rslt += bmi160_bmm150_mag_compensate_xyz(&magxyz);
  487. break;
  488. case APPLICATION_NAVIGATION:
  489. /*Set the accel mode as Normal write in the register 0x7E*/
  490. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  491. /* bmi160_delay_ms in ms*/
  492. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  493. /*Set the gyro mode as Normal write in the register 0x7E*/
  494. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  495. /* bmi160_delay_ms in ms*/
  496. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  497. /* Set the accel bandwidth as OSRS4 */
  498. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_OSR4_AVG1);
  499. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  500. /* Set the gryo bandwidth as Normal */
  501. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  502. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  503. /* set gyro data rate as 200Hz*/
  504. com_rslt += bmi160_set_gyro_output_data_rate(
  505. BMI160_GYRO_OUTPUT_DATA_RATE_200HZ);
  506. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  507. /* set accel data rate as 200Hz*/
  508. com_rslt += bmi160_set_accel_output_data_rate(
  509. BMI160_ACCEL_OUTPUT_DATA_RATE_200HZ);
  510. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  511. /* read gyro data*/
  512. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  513. /* read accel data */
  514. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  515. /* read mag data*/
  516. com_rslt += bmi160_bmm150_mag_compensate_xyz(&magxyz);
  517. break;
  518. case APPLICATION_REMOTE_CONTROL:
  519. /*Set the accel mode as Normal write in the register 0x7E*/
  520. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  521. /* bmi160_delay_ms in ms*/
  522. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  523. /*Set the gyro mode as Normal write in the register 0x7E*/
  524. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  525. /* bmi160_delay_ms in ms*/
  526. s_bmi160.delay_msec(C_BMI160_THIRTY_U8X);
  527. /* Set the accel bandwidth as OSRS4 */
  528. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_OSR4_AVG1);
  529. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  530. /* Set the gryo bandwidth as OSR4 */
  531. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_OSR4_MODE);
  532. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  533. /* set gyro data rate as 200Hz*/
  534. com_rslt += bmi160_set_gyro_output_data_rate(
  535. BMI160_GYRO_OUTPUT_DATA_RATE_200HZ);
  536. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  537. /* set accel data rate as 200Hz*/
  538. com_rslt += bmi160_set_accel_output_data_rate(
  539. BMI160_ACCEL_OUTPUT_DATA_RATE_200HZ);
  540. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  541. /* read gyro data */
  542. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  543. /* read accel data*/
  544. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  545. break;
  546. case APPLICATION_INDOOR_NAVIGATION:
  547. /*Set the accel mode as Normal write in the register 0x7E*/
  548. com_rslt = bmi160_set_command_register(ACCEL_MODE_NORMAL);
  549. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  550. /*Set the gyro mode as Normal write in the register 0x7E*/
  551. com_rslt += bmi160_set_command_register(GYRO_MODE_NORMAL);
  552. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  553. /* Set the accel bandwidth as OSRS4 */
  554. com_rslt += bmi160_set_accel_bw(BMI160_ACCEL_OSR4_AVG1);
  555. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  556. /* Set the gryo bandwidth as OSR4 */
  557. com_rslt += bmi160_set_gyro_bw(BMI160_GYRO_OSR4_MODE);
  558. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  559. /* set gyro data rate as 200Hz*/
  560. com_rslt += bmi160_set_gyro_output_data_rate(
  561. BMI160_GYRO_OUTPUT_DATA_RATE_400HZ);
  562. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  563. /* set accel data rate as 200Hz*/
  564. com_rslt += bmi160_set_accel_output_data_rate(
  565. BMI160_ACCEL_OUTPUT_DATA_RATE_400HZ);
  566. s_bmi160.delay_msec(C_BMI160_ONE_U8X);/* bmi160_delay_ms in ms*/
  567. /* read gyro data*/
  568. com_rslt += bmi160_read_gyro_xyz(&gyroxyz);
  569. /* read accel data */
  570. com_rslt += bmi160_read_accel_xyz(&accelxyz);
  571. break;
  572. }
  573. return com_rslt;
  574. }
  575. /*!
  576. * @brief This function used for interrupt configuration
  577. *
  578. *
  579. * @return results of bus communication function
  580. * @retval 0 -> Success
  581. * @retval 1 -> Error
  582. *
  583. *
  584. */
  585. BMI160_RETURN_FUNCTION_TYPE bmi160_interrupt_configuration(void)
  586. {
  587. /* This variable used for provide the communication
  588. results*/
  589. BMI160_RETURN_FUNCTION_TYPE com_rslt = ERROR;
  590. /* Configure the in/out control of interrupt1*/
  591. com_rslt = bmi160_set_output_enable(C_BMI160_ZERO_U8X,
  592. BMI160_WRITE_ONE);
  593. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  594. /* Configure the in/out control of interrupt2*/
  595. com_rslt += bmi160_set_output_enable(C_BMI160_ONE_U8X,
  596. BMI160_WRITE_ONE);
  597. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  598. /* Configure the interrupt1 active high
  599. 0x00 - Active low
  600. 0x01 - Active high*/
  601. com_rslt += bmi160_set_intr_level(C_BMI160_ZERO_U8X,
  602. BMI160_WRITE_ONE);
  603. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  604. /* Configure the interrupt2 active high
  605. 0x00 - Active low
  606. 0x01 - Active high*/
  607. com_rslt += bmi160_set_intr_level(C_BMI160_ONE_U8X,
  608. BMI160_WRITE_ONE);
  609. s_bmi160.delay_msec(C_BMI160_ONE_U8X);
  610. return com_rslt;
  611. }
  612. #ifdef INCLUDE_BMI160API
  613. #define MASK_DATA1 0xFF
  614. #define MASK_DATA2 0x80
  615. #define MASK_DATA3 0x7F
  616. /*!
  617. * @brief Used for SPI initialization
  618. * @note
  619. * The following function is used to map the
  620. * SPI bus read, write and bmi160_delay_ms
  621. * with global structure bmi160
  622. */
  623. s8 spi_routine(void)
  624. {
  625. /*--------------------------------------------------------------------------*
  626. * By using bmi160 the following structure parameter can be accessed
  627. * Bus write function pointer: BMI160_WR_FUNC_PTR
  628. * Bus read function pointer: BMI160_RD_FUNC_PTR
  629. * bmi160_delay_ms function pointer: bmi160_delay_ms_msec
  630. *--------------------------------------------------------------------------*/
  631. s_bmi160.bus_write = bmi160_spi_bus_write;
  632. s_bmi160.bus_read = bmi160_spi_bus_read;
  633. s_bmi160.burst_read = bmi160_spi_burst_read;
  634. s_bmi160.delay_msec = bmi160_delay_ms;
  635. return C_BMI160_ZERO_U8X;
  636. }
  637. /**************************************************************/
  638. /**\name I2C/SPI read write function */
  639. /**************************************************************/
  640. /*-------------------------------------------------------------------*
  641. *
  642. * This is a sample code for read and write the data by using I2C/SPI
  643. * Use either I2C or SPI based on your need
  644. * Configure the below code to your SPI or I2C driver
  645. *
  646. *-----------------------------------------------------------------------*/
  647. /*!
  648. * @brief : The function is used as SPI bus read
  649. * @return : Status of the SPI read
  650. * @param dev_addr : The device address of the sensor
  651. * @param reg_addr : Address of the first register,
  652. * will data is going to be read
  653. * @param reg_data : This data read from the sensor,
  654. * which is hold in an array
  655. * @param cnt : The no of byte of data to be read
  656. */
  657. s8 bmi160_spi_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
  658. {
  659. s32 ierror = C_BMI160_ZERO_U8X;
  660. if(SPI_OnlyReadReg(BOARD_SPI0_CS0_IO, reg_addr, reg_data, cnt))
  661. {
  662. ierror = -1;
  663. }
  664. return (s8)ierror;
  665. }
  666. /*!
  667. * @brief : The function is used as SPI bus write
  668. * @return : Status of the SPI write
  669. * @param dev_addr : The device address of the sensor
  670. * @param reg_addr : Address of the first register,
  671. * will data is going to be written
  672. * @param reg_data : It is a value hold in the array,
  673. * will be used for write the value into the register
  674. * @param cnt : The no of byte of data to be write
  675. */
  676. s8 bmi160_spi_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
  677. {
  678. s32 ierror = C_BMI160_ZERO_U8X;
  679. if(SPI_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg_addr, reg_data, cnt))
  680. {
  681. ierror = -1;
  682. }
  683. return (s8)ierror;
  684. }
  685. //add by lwy
  686. s8 bmi160_spi_burst_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u32 cnt)
  687. {
  688. return -1;
  689. }
  690. #endif
  691. /*!
  692. * @brief This function is an example for delay
  693. * @param msek: delay in milli seconds
  694. * @return : communication result
  695. */
  696. void bmi160_delay_ms(u32 msek)
  697. {
  698. nrf_delay_ms(msek);
  699. }