read_chip_id.c 6.6 KB

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