read_sensor_data.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * Copyright (C) 2018 Bosch Sensortec GmbH
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * @file bmi160_read_sensor_data.c
  7. * @brief Sample file to read BMI160 sensor data using COINES library
  8. *
  9. */
  10. /*********************************************************************/
  11. /* system header files */
  12. /*********************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. /*********************************************************************/
  17. /* own header files */
  18. /*********************************************************************/
  19. #include "coines.h"
  20. #include "bmi160.h"
  21. /*********************************************************************/
  22. /* local macro definitions */
  23. /*! I2C interface communication, 1 - Enable; 0- Disable */
  24. #define BMI160_INTERFACE_I2C 1
  25. /*! SPI interface communication, 1 - Enable; 0- Disable */
  26. #define BMI160_INTERFACE_SPI 0
  27. #if (!((BMI160_INTERFACE_I2C == 1) && (BMI160_INTERFACE_SPI == 0)) && \
  28. (!((BMI160_INTERFACE_I2C == 0) && (BMI160_INTERFACE_SPI == 1))))
  29. #error "Invalid value given for the macros BMI160_INTERFACE_I2C / BMI160_INTERFACE_SPI"
  30. #endif
  31. /*! bmi160 shuttle id */
  32. #define BMI160_SHUTTLE_ID 0x38
  33. /*! bmi160 Device address */
  34. #define BMI160_DEV_ADDR BMI160_I2C_ADDR
  35. /*********************************************************************/
  36. /* global variables */
  37. /*********************************************************************/
  38. /*! @brief This structure containing relevant bmi160 info */
  39. struct bmi160_dev bmi160dev;
  40. /*! @brief variable to hold the bmi160 accel data */
  41. struct bmi160_sensor_data bmi160_accel;
  42. /*! @brief variable to hold the bmi160 gyro data */
  43. struct bmi160_sensor_data bmi160_gyro;
  44. /*********************************************************************/
  45. /* static function declarations */
  46. /*********************************************************************/
  47. /*!
  48. * @brief internal API is used to initialize the sensor interface
  49. */
  50. static void init_sensor_interface(void);
  51. /*!
  52. * @brief This internal API is used to initialize the bmi160 sensor with default
  53. */
  54. static void init_bmi160(void);
  55. /*!
  56. * @brief This internal API is used to initialize the sensor driver interface
  57. */
  58. static void init_bmi160_sensor_driver_interface(void);
  59. /*********************************************************************/
  60. /* functions */
  61. /*********************************************************************/
  62. /*!
  63. * @brief This internal API is used to initialize the sensor interface depending
  64. * on selection either SPI or I2C.
  65. *
  66. * @param[in] void
  67. *
  68. * @return void
  69. *
  70. */
  71. static void init_sensor_interface(void)
  72. {
  73. /* Switch VDD for sensor off */
  74. coines_set_shuttleboard_vdd_vddio_config(0, 0);
  75. /* wait until the sensor goes off */
  76. coines_delay_msec(10);
  77. #if BMI160_INTERFACE_I2C == 1
  78. /* SDO pin is made low for selecting I2C address 0x68 */
  79. coines_set_pin_config(COINES_SHUTTLE_PIN_15, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_LOW);
  80. /* set the sensor interface as I2C */
  81. coines_config_i2c_bus(COINES_I2C_BUS_0, COINES_I2C_FAST_MODE);
  82. coines_delay_msec(10);
  83. /* CSB pin is made high for selecting I2C protocol*/
  84. coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_HIGH);
  85. #endif
  86. #if BMI160_INTERFACE_SPI == 1
  87. /* CSB pin is made low for selecting SPI protocol*/
  88. coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_LOW);
  89. coines_delay_msec(10);
  90. coines_config_spi_bus(COINES_SPI_BUS_0, COINES_SPI_SPEED_5_MHZ, COINES_SPI_MODE3);
  91. #endif
  92. coines_delay_msec(10);
  93. /* Switch VDD for sensor on */
  94. coines_set_shuttleboard_vdd_vddio_config(3300, 3300);
  95. #if BMI160_INTERFACE_SPI == 1
  96. coines_delay_msec(10);
  97. /* CSB pin is made high for selecting SPI protocol
  98. * Note: CSB has to see rising after power up, to switch to SPI protocol */
  99. coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_HIGH);
  100. #endif
  101. }
  102. /*!
  103. * @brief This internal API is used to initializes the bmi160 sensor
  104. * settings like power mode and OSRS settings.
  105. *
  106. * @param[in] void
  107. *
  108. * @return void
  109. *
  110. */
  111. static void init_bmi160(void)
  112. {
  113. int8_t rslt;
  114. rslt = bmi160_init(&bmi160dev);
  115. if (rslt == BMI160_OK)
  116. {
  117. printf("BMI160 initialization success !\n");
  118. printf("Chip ID 0x%X\n", bmi160dev.chip_id);
  119. }
  120. else
  121. {
  122. printf("BMI160 initialization failure !\n");
  123. exit(COINES_E_FAILURE);
  124. }
  125. /* Select the Output data rate, range of accelerometer sensor */
  126. bmi160dev.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
  127. bmi160dev.accel_cfg.range = BMI160_ACCEL_RANGE_16G;
  128. bmi160dev.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
  129. /* Select the power mode of accelerometer sensor */
  130. bmi160dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
  131. /* Select the Output data rate, range of Gyroscope sensor */
  132. bmi160dev.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
  133. bmi160dev.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
  134. bmi160dev.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
  135. /* Select the power mode of Gyroscope sensor */
  136. bmi160dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
  137. /* Set the sensor configuration */
  138. rslt = bmi160_set_sens_conf(&bmi160dev);
  139. }
  140. /*!
  141. * @brief This internal API is used to set the sensor driver interface to
  142. * read/write the data.
  143. *
  144. * @param[in] void
  145. *
  146. * @return void
  147. *
  148. */
  149. static void init_bmi160_sensor_driver_interface(void)
  150. {
  151. #if BMI160_INTERFACE_I2C == 1
  152. /* I2C setup */
  153. /* link read/write/delay function of host system to appropriate
  154. * bmi160 function call prototypes */
  155. bmi160dev.write = coines_write_i2c;
  156. bmi160dev.read = coines_read_i2c;
  157. bmi160dev.delay_ms = coines_delay_msec;
  158. /* set correct i2c address */
  159. bmi160dev.id = BMI160_DEV_ADDR;
  160. bmi160dev.intf = BMI160_I2C_INTF;
  161. #endif
  162. #if BMI160_INTERFACE_SPI == 1
  163. /* SPI setup */
  164. /* link read/write/delay function of host system to appropriate
  165. * bmi160 function call prototypes */
  166. bmi160dev.write = coines_write_spi;
  167. bmi160dev.read = coines_read_spi;
  168. bmi160dev.delay_ms = coines_delay_msec;
  169. bmi160dev.id = COINES_SHUTTLE_PIN_7;
  170. bmi160dev.intf = BMI160_SPI_INTF;
  171. #endif
  172. }
  173. /*!
  174. * @brief Main Function where the execution getting started to test the code.
  175. *
  176. * @param[in] argc
  177. * @param[in] argv
  178. *
  179. * @return status
  180. *
  181. */
  182. int main(int argc, char *argv[])
  183. {
  184. struct coines_board_info board_info;
  185. int16_t rslt;
  186. int times_to_read = 0;
  187. init_bmi160_sensor_driver_interface();
  188. rslt = coines_open_comm_intf(COINES_COMM_INTF_USB);
  189. if (rslt < 0)
  190. {
  191. printf(
  192. "\n Unable to connect with Application Board ! \n" " 1. Check if the board is connected and powered on. \n" " 2. Check if Application Board USB driver is installed. \n"
  193. " 3. Check if board is in use by another application. (Insufficient permissions to access USB) \n");
  194. exit(rslt);
  195. }
  196. rslt = coines_get_board_info(&board_info);
  197. if (rslt == COINES_SUCCESS)
  198. {
  199. if (board_info.shuttle_id != BMI160_SHUTTLE_ID)
  200. {
  201. printf("! Warning invalid sensor shuttle \n ," "This application will not support this sensor \n");
  202. exit(COINES_E_FAILURE);
  203. }
  204. }
  205. init_sensor_interface();
  206. /* After sensor init introduce 200 msec sleep */
  207. coines_delay_msec(200);
  208. init_bmi160();
  209. while (times_to_read < 100)
  210. {
  211. /* To read both Accel and Gyro data */
  212. bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &bmi160_accel, &bmi160_gyro, &bmi160dev);
  213. printf("ax:%d\tay:%d\taz:%d\n", bmi160_accel.x, bmi160_accel.y, bmi160_accel.z);
  214. printf("gx:%d\tgy:%d\tgz:%d\n", bmi160_gyro.x, bmi160_gyro.y, bmi160_gyro.z);
  215. fflush(stdout);
  216. coines_delay_msec(10);
  217. times_to_read = times_to_read + 1;
  218. }
  219. coines_close_comm_intf(COINES_COMM_INTF_USB);
  220. return EXIT_SUCCESS;
  221. }