read_sensor_data.c 7.8 KB

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