read_chip_id.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Copyright (C) 2018 Bosch Sensortec GmbH
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * @file bmi160_read_chip_id.c
  7. * @brief Sample file how to read bmi160 sensor chip ID using LIB COINES
  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. }
  126. /*!
  127. * @brief This internal API is used to set the sensor driver interface to
  128. * read/write the data.
  129. *
  130. * @param[in] void
  131. *
  132. * @return void
  133. *
  134. */
  135. static void init_bmi160_sensor_driver_interface(void)
  136. {
  137. #if BMI160_INTERFACE_I2C == 1
  138. /* I2C setup */
  139. /* link read/write/delay function of host system to appropriate
  140. * bmi160 function call prototypes */
  141. bmi160dev.write = coines_write_i2c;
  142. bmi160dev.read = coines_read_i2c;
  143. bmi160dev.delay_ms = coines_delay_msec;
  144. /* set correct i2c address */
  145. bmi160dev.id = BMI160_DEV_ADDR;
  146. bmi160dev.intf = BMI160_I2C_INTF;
  147. #endif
  148. #if BMI160_INTERFACE_SPI == 1
  149. /* SPI setup */
  150. /* link read/write/delay function of host system to appropriate
  151. * bmi160 function call prototypes */
  152. bmi160dev.write = coines_write_spi;
  153. bmi160dev.read = coines_read_spi;
  154. bmi160dev.delay_ms = coines_delay_msec;
  155. bmi160dev.id = COINES_SHUTTLE_PIN_7;
  156. bmi160dev.intf = BMI160_SPI_INTF;
  157. #endif
  158. }
  159. /*!
  160. * @brief Main Function where the execution getting started to test the code.
  161. *
  162. * @param[in] argc
  163. * @param[in] argv
  164. *
  165. * @return status
  166. *
  167. */
  168. int main(int argc, char *argv[])
  169. {
  170. struct coines_board_info board_info;
  171. int16_t rslt;
  172. init_bmi160_sensor_driver_interface();
  173. rslt = coines_open_comm_intf(COINES_COMM_INTF_USB);
  174. if (rslt < 0)
  175. {
  176. printf(
  177. "\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"
  178. " 3. Check if board is in use by another application. (Insufficient permissions to access USB) \n");
  179. exit(rslt);
  180. }
  181. rslt = coines_get_board_info(&board_info);
  182. if (rslt == COINES_SUCCESS)
  183. {
  184. if (board_info.shuttle_id != BMI160_SHUTTLE_ID)
  185. {
  186. printf("! Warning invalid sensor shuttle \n ," "This application will not support this sensor \n");
  187. exit(COINES_E_FAILURE);
  188. }
  189. }
  190. init_sensor_interface();
  191. /* After sensor init introduce 200 msec sleep */
  192. coines_delay_msec(200);
  193. init_bmi160();
  194. coines_close_comm_intf(COINES_COMM_INTF_USB);
  195. return EXIT_SUCCESS;
  196. }