tap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 0
  21. /*! spi interface communication, 1 - Enable; 0- Disable */
  22. #define BMI160_INTERFACE_SPI 1
  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. * @brief This internal API is used to set tap configurations
  57. */
  58. static int8_t set_tap_config(uint8_t feature_enable);
  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_100HZ;
  127. bmi160dev.accel_cfg.range = BMI160_ACCEL_RANGE_8G;
  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. /* Set the sensor configuration */
  132. rslt = bmi160_set_sens_conf(&bmi160dev);
  133. }
  134. /*!
  135. * @brief This internal API is used to set the sensor driver interface to
  136. * read/write the data.
  137. *
  138. * @param[in] void
  139. *
  140. * @return void
  141. *
  142. */
  143. static void init_bmi160_sensor_driver_interface(void)
  144. {
  145. #if BMI160_INTERFACE_I2C == 1
  146. /* I2C setup */
  147. /* link read/write/delay function of host system to appropriate
  148. * bmi160 function call prototypes */
  149. bmi160dev.write = coines_write_i2c;
  150. bmi160dev.read = coines_read_i2c;
  151. bmi160dev.delay_ms = coines_delay_msec;
  152. /* set correct i2c address */
  153. bmi160dev.id = BMI160_DEV_ADDR;
  154. bmi160dev.intf = BMI160_I2C_INTF;
  155. #endif
  156. #if BMI160_INTERFACE_SPI == 1
  157. /* SPI setup */
  158. /* link read/write/delay function of host system to appropriate
  159. * bmi160 function call prototypes */
  160. bmi160dev.write = coines_write_spi;
  161. bmi160dev.read = coines_read_spi;
  162. bmi160dev.delay_ms = coines_delay_msec;
  163. bmi160dev.id = COINES_SHUTTLE_PIN_7;
  164. bmi160dev.intf = BMI160_SPI_INTF;
  165. #endif
  166. }
  167. /*!
  168. * @brief Main Function where the execution getting started to test the code.
  169. *
  170. * @param[in] argc
  171. * @param[in] argv
  172. *
  173. * @return status
  174. *
  175. */
  176. int main(int argc, char *argv[])
  177. {
  178. struct coines_board_info board_info;
  179. int16_t rslt;
  180. init_bmi160_sensor_driver_interface();
  181. rslt = coines_open_comm_intf(COINES_COMM_INTF_USB);
  182. if (rslt < 0)
  183. {
  184. printf(
  185. "\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"
  186. " 3. Check if board is in use by another application. (Insufficient permissions to access USB) \n");
  187. exit(rslt);
  188. }
  189. rslt = coines_get_board_info(&board_info);
  190. if (rslt == COINES_SUCCESS)
  191. {
  192. if (board_info.shuttle_id != BMI160_SHUTTLE_ID)
  193. {
  194. printf("! Warning invalid sensor shuttle \n ," "This application will not support this sensor \n");
  195. exit(COINES_E_FAILURE);
  196. }
  197. }
  198. init_sensor_interface();
  199. /* after sensor init introduce 200 msec sleep */
  200. coines_delay_msec(200);
  201. init_bmi160();
  202. rslt = set_tap_config(BMI160_ENABLE);
  203. if (rslt == BMI160_OK)
  204. {
  205. union bmi160_int_status int_status;
  206. uint8_t loop = 0;
  207. uint32_t last_time = 0;
  208. uint32_t current_time = 0;
  209. printf("Do Single or Double Tap the board\n");
  210. fflush(stdout);
  211. memset(int_status.data, 0x00, sizeof(int_status.data));
  212. while (loop < 10)
  213. {
  214. /* Read interrupt status */
  215. rslt = bmi160_get_int_status(BMI160_INT_STATUS_ALL, &int_status, &bmi160dev);
  216. current_time = coines_get_millis();
  217. /* Enters only if the obtained interrupt is single-tap */
  218. if (rslt == BMI160_OK)
  219. {
  220. /* Enters only if the obtained interrupt is single-tap */
  221. if (int_status.bit.s_tap)
  222. {
  223. printf("Single tap, iter:%d, time:%d ms, delta:%d ms, int_status:0x%x\n",
  224. loop++,
  225. current_time,
  226. current_time - last_time,
  227. int_status.data[0]);
  228. }
  229. /* Enters only if the obtained interrupt is double-tap */
  230. else if (int_status.bit.d_tap)
  231. {
  232. printf("Double tap, iter:%d, time:%d ms, delta:%d ms, int_status:0x%x\n",
  233. loop++,
  234. current_time,
  235. current_time - last_time,
  236. int_status.data[0]);
  237. }
  238. fflush(stdout);
  239. }
  240. else
  241. {
  242. break;
  243. }
  244. memset(int_status.data, 0x00, sizeof(int_status.data));
  245. last_time = current_time;
  246. }
  247. /* Disable tap feature */
  248. printf("\nDisable tap test...\n");
  249. rslt = set_tap_config(BMI160_DISABLE);
  250. printf("bmi160_set_int_config(tap enable) status:%d\n", rslt);
  251. fflush(stdout);
  252. }
  253. coines_close_comm_intf(COINES_COMM_INTF_USB);
  254. return EXIT_SUCCESS;
  255. }
  256. static int8_t set_tap_config(uint8_t feature_enable)
  257. {
  258. int8_t rslt = BMI160_OK;
  259. struct bmi160_int_settg int_config;
  260. if (feature_enable > 0)
  261. {
  262. /* Select the Interrupt channel/pin */
  263. int_config.int_channel = BMI160_INT_CHANNEL_1; /* Interrupt channel/pin 1 */
  264. /* Select the interrupt channel/pin settings */
  265. int_config.int_pin_settg.output_en = BMI160_ENABLE; /* Enabling interrupt pins to act as output pin */
  266. int_config.int_pin_settg.output_mode = BMI160_DISABLE; /* Choosing push-pull mode for interrupt pin */
  267. int_config.int_pin_settg.output_type = BMI160_ENABLE; /* Choosing active low output */
  268. int_config.int_pin_settg.edge_ctrl = BMI160_DISABLE; /* Choosing edge triggered output */
  269. int_config.int_pin_settg.input_en = BMI160_DISABLE; /* Disabling interrupt pin to act as input */
  270. int_config.int_pin_settg.latch_dur = BMI160_LATCH_DUR_NONE; /* non-latched output */
  271. /* Select the Interrupt type */
  272. int_config.int_type = BMI160_ACC_SINGLE_TAP_INT; /* Choosing tap interrupt */
  273. /* Select the Any-motion interrupt parameters */
  274. int_config.int_type_cfg.acc_tap_int.tap_en = BMI160_ENABLE; /* 1- Enable tap, 0- disable tap */
  275. int_config.int_type_cfg.acc_tap_int.tap_thr = 2; /* Set tap threshold */
  276. int_config.int_type_cfg.acc_tap_int.tap_dur = 2; /* Set tap duration */
  277. int_config.int_type_cfg.acc_tap_int.tap_shock = 0; /* Set tap shock value */
  278. int_config.int_type_cfg.acc_tap_int.tap_quiet = 0; /* Set tap quiet duration */
  279. int_config.int_type_cfg.acc_tap_int.tap_data_src = 1; /* data source 0 : filter or 1 : pre-filter */
  280. /* Set the Any-motion interrupt */
  281. rslt = bmi160_set_int_config(&int_config, &bmi160dev); /* sensor is an instance of the structure bmi160_dev */
  282. printf("bmi160_set_int_config(tap enable) status:%d\n", rslt);
  283. }
  284. else
  285. {
  286. /* Select the Interrupt channel/pin */
  287. int_config.int_channel = BMI160_INT_CHANNEL_1;
  288. int_config.int_pin_settg.output_en = BMI160_DISABLE; /* Disabling interrupt pins to act as output pin */
  289. int_config.int_pin_settg.edge_ctrl = BMI160_DISABLE; /* Choosing edge triggered output */
  290. /* Select the Interrupt type */
  291. int_config.int_type = BMI160_ACC_SINGLE_TAP_INT; /* Choosing Tap interrupt */
  292. int_config.int_type_cfg.acc_tap_int.tap_en = BMI160_DISABLE; /* 1- Enable tap, 0- disable tap */
  293. /* Set the Data ready interrupt */
  294. rslt = bmi160_set_int_config(&int_config, &bmi160dev); /* sensor is an instance of the structure bmi160_dev */
  295. printf("bmi160_set_int_config(tap disable) status:%d\n", rslt);
  296. }
  297. return rslt;
  298. }