lsm6ds3tr_c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "lsm6ds3tr_c.h"
  3. /* Private macro -------------------------------------------------------------*/
  4. #define BOOT_TIME 15 //ms
  5. #define WAIT_TIME_A 100 //ms
  6. #define WAIT_TIME_G_01 150 //ms
  7. #define WAIT_TIME_G_02 50 //ms
  8. /* Self test limits. */
  9. #define MIN_ST_LIMIT_mg 90.0f
  10. #define MAX_ST_LIMIT_mg 1700.0f
  11. #define MIN_ST_LIMIT_mdps 150000.0f
  12. #define MAX_ST_LIMIT_mdps 700000.0f
  13. /* Self test results. */
  14. #define ST_PASS 1U
  15. #define ST_FAIL 0U
  16. #define MIN_ODR(x, y) (x < y ? x : y)
  17. #define MAX_ODR(x, y) (x > y ? x : y)
  18. #define MAX_PATTERN_NUM FIFO_THRESHOLD / 6
  19. #define LSM6DS3_ODR_LSB_TO_HZ(_odr) (_odr ? (13 << (_odr - 1)) : 0)
  20. /* static variable ------------------------------------------------------------------*/
  21. static uint8_t whoamI, rst;
  22. static uint16_t pattern_len;
  23. static stmdev_ctx_t dev_ctx;
  24. static axis3bit16_t data_raw_acceleration;
  25. static axis3bit16_t data_raw_angular_rate;
  26. static int16_t data_raw_temperature;
  27. /* 6ds3 Accelerometer test parameters */
  28. static sensor_lsm6ds3_xl test_6ds3_xl;
  29. /* 6ds3 Gyroscope test parameters */
  30. static sensor_lsm6ds3_gy test_6ds3_gyro;
  31. //static float acceleration_mg[3];
  32. //static float angular_rate_mdps[3];
  33. //static float temperature_degC;
  34. /* static fuction ------------------------------------------------------------------*/
  35. /** Please note that is MANDATORY: return 0 -> no Error.**/
  36. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  37. {
  38. int32_t ierror = 0;
  39. if(SPI_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg, (uint8_t *)bufp, len))
  40. {
  41. ierror = -1;
  42. }
  43. return ierror;
  44. }
  45. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  46. {
  47. int32_t ierror = 0;
  48. if(SPI_OnlyReadReg(BOARD_SPI0_CS0_IO, reg, bufp, len))
  49. {
  50. ierror = -1;
  51. }
  52. return ierror;
  53. }
  54. static void platform_delay(uint32_t ms)
  55. {
  56. nrf_delay_ms(ms);
  57. }
  58. /* Following routine calculate the FIFO pattern composition based
  59. * on gyro and acc enable state and ODR freq
  60. */
  61. static uint16_t LSM6DS3TR_C_Calculate_FIFO_Pattern(uint16_t *min_odr, uint16_t *max_odr)
  62. {
  63. uint16_t fifo_samples_tot_num = 0;
  64. /* Calculate min_odr and max_odr for current configuration */
  65. if (test_6ds3_gyro.enable)
  66. {
  67. test_6ds3_gyro.odr_hz_val = LSM6DS3_ODR_LSB_TO_HZ(test_6ds3_gyro.odr);
  68. *max_odr = MAX_ODR(*max_odr, test_6ds3_gyro.odr_hz_val);
  69. *min_odr = MIN_ODR(*min_odr, test_6ds3_gyro.odr_hz_val);
  70. }
  71. if (test_6ds3_xl.enable)
  72. {
  73. test_6ds3_xl.odr_hz_val = LSM6DS3_ODR_LSB_TO_HZ(test_6ds3_xl.odr);
  74. *max_odr = MAX_ODR(*max_odr, test_6ds3_xl.odr_hz_val);
  75. *min_odr = MIN_ODR(*min_odr, test_6ds3_xl.odr_hz_val);
  76. }
  77. /* Calculate how many samples for each sensor are in current FIFO pattern */
  78. if (test_6ds3_gyro.enable)
  79. {
  80. test_6ds3_gyro.samples_num_in_pattern = test_6ds3_gyro.odr_hz_val / *min_odr;
  81. test_6ds3_gyro.decimation = *max_odr / test_6ds3_gyro.odr_hz_val;
  82. fifo_samples_tot_num += test_6ds3_gyro.samples_num_in_pattern;
  83. }
  84. if (test_6ds3_xl.enable)
  85. {
  86. test_6ds3_xl.samples_num_in_pattern = test_6ds3_xl.odr_hz_val / *min_odr;
  87. test_6ds3_xl.decimation = *max_odr / test_6ds3_xl.odr_hz_val;
  88. fifo_samples_tot_num += test_6ds3_xl.samples_num_in_pattern;
  89. }
  90. /* Return the total number of 16-bit samples in the pattern */
  91. return(6 * fifo_samples_tot_num);
  92. }
  93. /* Following routine read a pattern from FIFO */
  94. static void LSM6DS3TR_C_Read_FIFO_Pattern(axis3bit16_t *acc, uint16_t *acc_num, axis3bit16_t *gry, uint16_t *gry_num)
  95. {
  96. uint8_t gy_num = test_6ds3_gyro.samples_num_in_pattern;
  97. uint8_t xl_num = test_6ds3_xl.samples_num_in_pattern;
  98. /* FIFO pattern is composed by gy_num gyroscope triplets and
  99. * xl_num accelerometer triplets. The sequence has always following order:
  100. * gyro first, accelerometer second
  101. */
  102. while(gy_num > 0 || xl_num > 0)
  103. {
  104. /* Read gyro samples */
  105. if (test_6ds3_gyro.enable && gy_num > 0)
  106. {
  107. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_angular_rate.u8bit,
  108. 3 * sizeof(int16_t));
  109. gry[*gry_num].i16bit[0] = data_raw_angular_rate.i16bit[0];
  110. gry[*gry_num].i16bit[1] = data_raw_angular_rate.i16bit[1];
  111. gry[*gry_num].i16bit[2] = data_raw_angular_rate.i16bit[2];
  112. (*gry_num)++;
  113. // angular_rate_mdps[0] =
  114. // lsm6ds3_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[0]);
  115. // angular_rate_mdps[1] =
  116. // lsm6ds3_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[1]);
  117. // angular_rate_mdps[2] =
  118. // lsm6ds3_from_fs2000dps_to_mdps(data_raw_angular_rate.i16bit[2]);
  119. // sprintf((char*)tx_buffer, "Angular rate [mdps]:%4.2f\t%4.2f\t%4.2f\r\n",
  120. // angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
  121. // tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
  122. gy_num--;
  123. }
  124. /* Read XL samples */
  125. if (test_6ds3_xl.enable && xl_num > 0)
  126. {
  127. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_acceleration.u8bit,
  128. 3 * sizeof(int16_t));
  129. acc[*acc_num].i16bit[0] = data_raw_acceleration.i16bit[0];
  130. acc[*acc_num].i16bit[1] = data_raw_acceleration.i16bit[1];
  131. acc[*acc_num].i16bit[2] = data_raw_acceleration.i16bit[2];
  132. (*acc_num)++;
  133. // acceleration_mg[0] =
  134. // lsm6ds3_from_fs2g_to_mg(data_raw_acceleration.i16bit[0]);
  135. // acceleration_mg[1] =
  136. // lsm6ds3_from_fs2g_to_mg(data_raw_acceleration.i16bit[1]);
  137. // acceleration_mg[2] =
  138. // lsm6ds3_from_fs2g_to_mg(data_raw_acceleration.i16bit[2]);
  139. // sprintf((char*)tx_buffer, "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
  140. // acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
  141. // tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
  142. xl_num--;
  143. }
  144. }
  145. }
  146. /* API ------------------------------------------------------------------*/
  147. void lsm6ds3tr_c_init(void)
  148. {
  149. int16_t data_raw[3];
  150. float val_st_off[3];
  151. float val_st_on[3];
  152. float test_val[3];
  153. uint8_t st_result;
  154. uint8_t drdy;
  155. uint8_t i;
  156. uint8_t j;
  157. SPI_Init();
  158. /* Initialize mems driver interface */
  159. dev_ctx.write_reg = platform_write;
  160. dev_ctx.read_reg = platform_read;
  161. dev_ctx.handle = NULL;
  162. /* Wait sensor boot time */
  163. platform_delay(BOOT_TIME);
  164. /* Check device ID */
  165. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  166. if(whoamI != LSM6DS3TR_C_ID)
  167. {
  168. SEGGER_RTT_printf(0,"lsm6ds3tr_c_init error!!!\r\n");
  169. return;
  170. }
  171. /* Restore default configuration */
  172. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  173. do {
  174. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  175. } while(rst);
  176. /* Enable Block Data Update */
  177. lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  178. /*
  179. * Accelerometer Self Test
  180. */
  181. /* Set Output Data Rate */
  182. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_52Hz);
  183. /* Set full scale */
  184. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, LSM6DS3TR_C_4g);
  185. /* Wait stable output */
  186. platform_delay(WAIT_TIME_A);
  187. /* Check if new value available */
  188. do {
  189. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  190. } while (!drdy);
  191. /* Read dummy data and discard it */
  192. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  193. /* Read 5 sample and get the average vale for each axis */
  194. memset(val_st_off, 0x00, 3 * sizeof(float));
  195. for (i = 0; i < 5; i++) {
  196. /* Check if new value available */
  197. do {
  198. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  199. } while (!drdy);
  200. /* Read data and accumulate the mg value */
  201. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  202. for (j = 0; j < 3; j++) {
  203. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  204. }
  205. }
  206. /* Calculate the mg average values */
  207. for (i = 0; i < 3; i++) {
  208. val_st_off[i] /= 5.0f;
  209. }
  210. /* Enable Self Test positive (or negative) */
  211. lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  212. //lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_POSITIVE);
  213. /* Wait stable output */
  214. platform_delay(WAIT_TIME_A);
  215. /* Check if new value available */
  216. do {
  217. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  218. } while (!drdy);
  219. /* Read dummy data and discard it */
  220. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  221. /* Read 5 sample and get the average vale for each axis */
  222. memset(val_st_on, 0x00, 3 * sizeof(float));
  223. for (i = 0; i < 5; i++) {
  224. /* Check if new value available */
  225. do {
  226. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  227. } while (!drdy);
  228. /* Read data and accumulate the mg value */
  229. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  230. for (j = 0; j < 3; j++) {
  231. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  232. }
  233. }
  234. /* Calculate the mg average values */
  235. for (i = 0; i < 3; i++) {
  236. val_st_on[i] /= 5.0f;
  237. }
  238. /* Calculate the mg values for self test */
  239. for (i = 0; i < 3; i++) {
  240. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  241. }
  242. /* Check self test limit */
  243. st_result = ST_PASS;
  244. for (i = 0; i < 3; i++) {
  245. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  246. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  247. st_result = ST_FAIL;
  248. }
  249. }
  250. /* Disable Self Test */
  251. lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  252. /* Disable sensor. */
  253. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  254. /*
  255. * Gyroscope Self Test
  256. */
  257. /* Set Output Data Rate */
  258. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_208Hz);
  259. /* Set full scale */
  260. lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, LSM6DS3TR_C_2000dps);
  261. /* Wait stable output */
  262. platform_delay(WAIT_TIME_G_01);
  263. /* Check if new value available */
  264. do {
  265. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  266. } while (!drdy);
  267. /* Read dummy data and discard it */
  268. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  269. /* Read 5 sample and get the average vale for each axis */
  270. memset(val_st_off, 0x00, 3 * sizeof(float));
  271. for (i = 0; i < 5; i++) {
  272. /* Check if new value available */
  273. do {
  274. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  275. } while (!drdy);
  276. /* Read data and accumulate the mg value */
  277. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  278. for (j = 0; j < 3; j++) {
  279. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  280. data_raw[j]);
  281. }
  282. }
  283. /* Calculate the mg average values */
  284. for (i = 0; i < 3; i++) {
  285. val_st_off[i] /= 5.0f;
  286. }
  287. /* Enable Self Test positive (or negative) */
  288. lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  289. //lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LIS2DH12_GY_ST_NEGATIVE);
  290. /* Wait stable output */
  291. platform_delay(WAIT_TIME_G_02);
  292. /* Read 5 sample and get the average vale for each axis */
  293. memset(val_st_on, 0x00, 3 * sizeof(float));
  294. for (i = 0; i < 5; i++) {
  295. /* Check if new value available */
  296. do {
  297. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  298. } while (!drdy);
  299. /* Read data and accumulate the mg value */
  300. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  301. for (j = 0; j < 3; j++) {
  302. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  303. data_raw[j]);
  304. }
  305. }
  306. /* Calculate the mg average values */
  307. for (i = 0; i < 3; i++) {
  308. val_st_on[i] /= 5.0f;
  309. }
  310. /* Calculate the mg values for self test */
  311. for (i = 0; i < 3; i++) {
  312. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  313. }
  314. /* Check self test limit */
  315. for (i = 0; i < 3; i++) {
  316. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  317. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  318. st_result = ST_FAIL;
  319. }
  320. }
  321. /* Disable Self Test */
  322. lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  323. /* Disable sensor. */
  324. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_OFF);
  325. if (st_result == ST_PASS) {
  326. SEGGER_RTT_printf(0,"Self Test - PASS\r\n");
  327. }
  328. else {
  329. SEGGER_RTT_printf(0,"Self Test - FAIL\r\n");
  330. }
  331. }
  332. void lsm6ds3tr_c_fifo_mode(void)
  333. {
  334. uint16_t max_odr = 0, min_odr = 0xffff;
  335. /* 6ds3 Accelerometer test parameters */
  336. test_6ds3_xl.enable = PROPERTY_ENABLE;
  337. test_6ds3_xl.odr = LSM6DS3TR_C_XL_ODR_104Hz;
  338. test_6ds3_xl.odr_hz_val = 0;
  339. test_6ds3_xl.fs = LSM6DS3TR_C_16g;
  340. test_6ds3_xl.decimation = 0;
  341. test_6ds3_xl.samples_num_in_pattern = 0;
  342. // /* 6ds3 Gyroscope test parameters */
  343. // test_6ds3_gyro.enable = PROPERTY_ENABLE;
  344. // test_6ds3_gyro.odr = LSM6DS3TR_C_GY_ODR_104Hz;
  345. // test_6ds3_gyro.odr_hz_val = 0;
  346. // test_6ds3_gyro.fs = LSM6DS3TR_C_2000dps;
  347. // test_6ds3_gyro.decimation = 0;
  348. // test_6ds3_gyro.samples_num_in_pattern = 0;
  349. /* Interrupt generation on FIFO watermark INT1/INT2 pin */
  350. //lsm6ds3_int2_route_t int_2_reg;
  351. //lsm6ds3_int1_route_t int_1_reg;
  352. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  353. /* Set XL and Gyro Output Data Rate */
  354. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, test_6ds3_xl.odr);
  355. // lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, test_6ds3_gyro.odr);
  356. /* Set XL full scale and Gyro full scale */
  357. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, test_6ds3_xl.fs);
  358. // lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, test_6ds3_gyro.fs);
  359. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  360. /* Calculate number of sensors samples in each FIFO pattern */
  361. pattern_len = LSM6DS3TR_C_Calculate_FIFO_Pattern(&min_odr, &max_odr);
  362. /* Set FIFO watermark to a multiple of a pattern */
  363. lsm6ds3tr_c_fifo_watermark_set(&dev_ctx, pattern_len);
  364. /* Set FIFO mode to Stream mode */
  365. lsm6ds3tr_c_fifo_mode_set(&dev_ctx, LSM6DS3TR_C_STREAM_MODE);
  366. /* Enable FIFO watermark interrupt generation on INT1 pin */
  367. //lsm6ds3_pin_int1_route_get(&dev_ctx, &int_1_reg);
  368. //int_1_reg.int1_fth = PROPERTY_ENABLE;
  369. //lsm6ds3_pin_int1_route_set(&dev_ctx, &int_1_reg);
  370. /* FIFO watermark interrupt on INT2 pin */
  371. //lsm6ds3_pin_int2_route_get(&dev_ctx, &int_2_reg);
  372. //int_2_reg.int2_fth = PROPERTY_ENABLE;
  373. //lsm6ds3_pin_int2_route_set(&dev_ctx, &int_2_reg);
  374. /* Set ODR FIFO */
  375. lsm6ds3tr_c_fifo_data_rate_set(&dev_ctx, LSM6DS3TR_C_FIFO_416Hz);
  376. /* Set FIFO sensor decimator */
  377. lsm6ds3tr_c_fifo_xl_batch_set(&dev_ctx, (lsm6ds3tr_c_dec_fifo_xl_t)test_6ds3_xl.decimation);
  378. // lsm6ds3tr_c_fifo_gy_batch_set(&dev_ctx, (lsm6ds3tr_c_dec_fifo_gyro_t)test_6ds3_gyro.decimation);
  379. }
  380. void lsm6ds3tr_c_fifo_read(axis3bit16_t **acc, uint16_t *acc_num, axis3bit16_t **gry, uint16_t *gry_num)
  381. {
  382. uint8_t wt;
  383. uint16_t num = 0;
  384. uint16_t num_pattern = 0;
  385. uint16_t acc_buf_num = 0, gry_buf_num = 0;
  386. static axis3bit16_t acc_buf[100], gry_buf[100];
  387. /* Read FIFO watermark flag in polling mode */
  388. lsm6ds3tr_c_fifo_wtm_flag_get(&dev_ctx, &wt);
  389. if (wt)
  390. {
  391. /* Read number of word in FIFO */
  392. lsm6ds3tr_c_fifo_data_level_get(&dev_ctx, &num);
  393. num_pattern = num / pattern_len;
  394. while (num_pattern-- > 0)
  395. LSM6DS3TR_C_Read_FIFO_Pattern(acc_buf, &acc_buf_num, gry_buf, &gry_buf_num);
  396. }
  397. if(acc != NULL)
  398. *acc = acc_buf;
  399. if(acc_num != NULL)
  400. *acc_num = acc_buf_num;
  401. if(gry != NULL)
  402. *gry = gry_buf;
  403. if(gry_num != NULL)
  404. *gry_num = gry_buf_num;
  405. }
  406. void lsm6ds3tr_c_read_data_polling_mode(void)
  407. {
  408. /* 6ds3 Accelerometer test parameters */
  409. test_6ds3_xl.enable = PROPERTY_ENABLE;
  410. test_6ds3_xl.odr = LSM6DS3TR_C_XL_ODR_104Hz;
  411. test_6ds3_xl.odr_hz_val = 0;
  412. test_6ds3_xl.fs = LSM6DS3TR_C_16g;
  413. test_6ds3_xl.decimation = 0;
  414. test_6ds3_xl.samples_num_in_pattern = 0;
  415. /* 6ds3 Gyroscope test parameters */
  416. test_6ds3_gyro.enable = PROPERTY_ENABLE;
  417. test_6ds3_gyro.odr = LSM6DS3TR_C_GY_ODR_104Hz;
  418. test_6ds3_gyro.odr_hz_val = 0;
  419. test_6ds3_gyro.fs = LSM6DS3TR_C_2000dps;
  420. test_6ds3_gyro.decimation = 0;
  421. test_6ds3_gyro.samples_num_in_pattern = 0;
  422. /* Check device ID */
  423. whoamI = 0;
  424. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  425. if(whoamI != LSM6DS3TR_C_ID)
  426. {
  427. SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_data_polling_config error!!!\r\n");
  428. return;
  429. }
  430. /* Restore default configuration */
  431. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  432. do {
  433. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  434. } while (rst);
  435. /* Enable Block Data Update */
  436. lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  437. /* Set Output Data Rate */
  438. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, test_6ds3_xl.odr);
  439. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, test_6ds3_gyro.odr);
  440. /* Set full scale */
  441. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, test_6ds3_xl.fs);
  442. lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, test_6ds3_gyro.fs);
  443. /* Configure filtering chain(No aux interface) */
  444. /* Accelerometer - analog filter */
  445. // lsm6ds3tr_c_xl_filter_analog_set(&dev_ctx,
  446. // LSM6DS3TR_C_XL_ANA_BW_400Hz);
  447. // /* Accelerometer - LPF1 path ( LPF2 not used )*/
  448. // //lsm6ds3tr_c_xl_lp1_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_LP1_ODR_DIV_4);
  449. // /* Accelerometer - LPF1 + LPF2 path */
  450. // lsm6ds3tr_c_xl_lp2_bandwidth_set(&dev_ctx,
  451. // LSM6DS3TR_C_XL_LOW_NOISE_LP_ODR_DIV_100);
  452. /* Accelerometer - High Pass / Slope path */
  453. //lsm6ds3tr_c_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);
  454. //lsm6ds3tr_c_xl_hp_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_HP_ODR_DIV_100);
  455. /* Gyroscope - filtering chain */
  456. // lsm6ds3tr_c_gy_band_pass_set(&dev_ctx,
  457. // LSM6DS3TR_C_HP_260mHz_LP1_STRONG);
  458. }
  459. void lsm6ds3tr_c_read_data_polling(int16_t *acc, int16_t *gry, int16_t *temp)
  460. {
  461. /* Read samples in polling mode (no int) */
  462. /* Read output only if new value is available */
  463. lsm6ds3tr_c_reg_t reg;
  464. lsm6ds3tr_c_status_reg_get(&dev_ctx, &reg.status_reg);
  465. if (reg.status_reg.xlda) {
  466. /* Read magnetic field data */
  467. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  468. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  469. data_raw_acceleration.i16bit);
  470. // acceleration_mg[0] = lsm6ds3tr_c_from_fs2g_to_mg(
  471. // data_raw_acceleration[0]);
  472. // acceleration_mg[1] = lsm6ds3tr_c_from_fs2g_to_mg(
  473. // data_raw_acceleration[1]);
  474. // acceleration_mg[2] = lsm6ds3tr_c_from_fs2g_to_mg(
  475. // data_raw_acceleration[2]);
  476. // sprintf((char *)tx_buffer,
  477. // "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
  478. // acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
  479. acc[0] = data_raw_acceleration.i16bit[0];
  480. acc[1] = data_raw_acceleration.i16bit[1];
  481. acc[2] = data_raw_acceleration.i16bit[2];
  482. }
  483. if (reg.status_reg.gda) {
  484. /* Read magnetic field data */
  485. memset(data_raw_angular_rate.i16bit, 0x00, 3 * sizeof(int16_t));
  486. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx,
  487. data_raw_angular_rate.i16bit);
  488. // angular_rate_mdps[0] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  489. // data_raw_angular_rate[0]);
  490. // angular_rate_mdps[1] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  491. // data_raw_angular_rate[1]);
  492. // angular_rate_mdps[2] = lsm6ds3tr_c_from_fs2000dps_to_mdps(
  493. // data_raw_angular_rate[2]);
  494. // sprintf((char *)tx_buffer,
  495. // "Angular rate [mdps]:%4.2f\t%4.2f\t%4.2f\r\n",
  496. // angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
  497. gry[0] = data_raw_angular_rate.i16bit[0];
  498. gry[1] = data_raw_angular_rate.i16bit[1];
  499. gry[2] = data_raw_angular_rate.i16bit[2];
  500. }
  501. if (reg.status_reg.tda) {
  502. /* Read temperature data */
  503. memset(&data_raw_temperature, 0x00, sizeof(int16_t));
  504. lsm6ds3tr_c_temperature_raw_get(&dev_ctx, &data_raw_temperature);
  505. // temperature_degC = lsm6ds3tr_c_from_lsb_to_celsius(
  506. // data_raw_temperature );
  507. // sprintf((char *)tx_buffer, "Temperature [degC]:%6.2f\r\n",
  508. // temperature_degC );
  509. *temp = data_raw_temperature;
  510. }
  511. }
  512. void lsm6ds3tr_c_low_power_acc_mode(void)
  513. {
  514. /* 6ds3 Accelerometer test parameters */
  515. test_6ds3_xl.enable = PROPERTY_ENABLE;
  516. test_6ds3_xl.odr = LSM6DS3TR_C_XL_ODR_12Hz5;
  517. test_6ds3_xl.odr_hz_val = 0;
  518. test_6ds3_xl.fs = LSM6DS3TR_C_16g;
  519. test_6ds3_xl.decimation = 0;
  520. test_6ds3_xl.samples_num_in_pattern = 0;
  521. /* Restore default configuration */
  522. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  523. do {
  524. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  525. } while (rst);
  526. /* Enable Block Data Update */
  527. lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  528. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  529. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, test_6ds3_xl.odr);
  530. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, test_6ds3_xl.fs);
  531. lsm6ds3tr_c_xl_filter_analog_set(&dev_ctx,
  532. LSM6DS3TR_C_XL_ANA_BW_400Hz);
  533. lsm6ds3tr_c_xl_lp2_bandwidth_set(&dev_ctx,
  534. LSM6DS3TR_C_XL_LOW_NOISE_LP_ODR_DIV_100);
  535. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  536. }
  537. void lsm6ds3tr_c_low_power_acc(int16_t *acc)
  538. {
  539. /* Read samples in polling mode (no int) */
  540. /* Read output only if new value is available */
  541. lsm6ds3tr_c_reg_t reg;
  542. lsm6ds3tr_c_status_reg_get(&dev_ctx, &reg.status_reg);
  543. if (reg.status_reg.xlda) {
  544. /* Read magnetic field data */
  545. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  546. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  547. data_raw_acceleration.i16bit);
  548. // acceleration_mg[0] = lsm6ds3tr_c_from_fs2g_to_mg(
  549. // data_raw_acceleration[0]);
  550. // acceleration_mg[1] = lsm6ds3tr_c_from_fs2g_to_mg(
  551. // data_raw_acceleration[1]);
  552. // acceleration_mg[2] = lsm6ds3tr_c_from_fs2g_to_mg(
  553. // data_raw_acceleration[2]);
  554. // sprintf((char *)tx_buffer,
  555. // "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
  556. // acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
  557. acc[0] = data_raw_acceleration.i16bit[0];
  558. acc[1] = data_raw_acceleration.i16bit[1];
  559. acc[2] = data_raw_acceleration.i16bit[2];
  560. }
  561. }
  562. void lsm6ds3tr_c_get_accel_power_mode_stat(uint8_t *stat)
  563. {
  564. uint8_t reg_val;
  565. int32_t ret;
  566. ret = lsm6ds3tr_c_read_reg(&dev_ctx, LSM6DS3TR_C_FIFO_CTRL5,
  567. &reg_val, 1);
  568. if(ret == 0 && (reg_val & 0x78)!=0)
  569. {
  570. *stat = 1; //fifo mode
  571. return;
  572. }
  573. ret = lsm6ds3tr_c_gy_sleep_mode_get(&dev_ctx, &reg_val);
  574. if(ret == 0 && (1 == reg_val))
  575. {
  576. *stat = 2; //low power acc mode
  577. return;
  578. }
  579. *stat = 3; //acc + gy
  580. }
  581. // //test
  582. // uint16_t i;
  583. // uint8_t stat = 0;
  584. // int16_t acc[3], gry[3], temp;
  585. // axis3bit16_t *acc_fifo, *gry_fifo;
  586. // uint16_t acc_num, gry_num;
  587. // lsm6ds3tr_c_init();
  588. //// lsm6ds3tr_c_read_data_polling_mode();
  589. //// lsm6ds3tr_c_low_power_acc_mode();
  590. //// lsm6ds3tr_c_fifo_mode();
  591. // while(1)
  592. // {
  593. //// lsm6ds3tr_c_read_data_polling(acc, gry, &temp);
  594. //// SEGGER_RTT_printf(0,"h_ax=%d\r,h_ay=%d\r,h_az=%d\r\n",acc[0],acc[1],acc[2]);
  595. //// SEGGER_RTT_printf(0,"h_gx=%d\r,h_gy=%d\r,h_gz=%d\r\n",gry[0],gry[1],gry[2]);
  596. //// SEGGER_RTT_printf(0,"temp:%d\r\n",temp);
  597. //// nrf_delay_ms(10);
  598. //
  599. //
  600. //// lsm6ds3tr_c_read_data_polling_mode();
  601. //// lsm6ds3tr_c_get_accel_power_mode_stat(&stat);
  602. //// SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_data_polling_mode stat:%d\r\n",stat);
  603. //// nrf_delay_ms(1000);
  604. ////
  605. //// lsm6ds3tr_c_low_power_acc_mode();
  606. //// lsm6ds3tr_c_get_accel_power_mode_stat(&stat);
  607. //// SEGGER_RTT_printf(0,"lsm6ds3tr_c_low_power_acc_mode stat:%d\r\n",stat);
  608. //// nrf_delay_ms(1000);
  609. ////
  610. //// lsm6ds3tr_c_fifo_mode();
  611. //// lsm6ds3tr_c_get_accel_power_mode_stat(&stat);
  612. //// SEGGER_RTT_printf(0,"lsm6ds3tr_c_fifo_mode stat:%d\r\n",stat);
  613. //// nrf_delay_ms(1000);
  614. //
  615. //// lsm6ds3tr_c_low_power_acc(acc);
  616. //// SEGGER_RTT_printf(0,"h_ax=%d\r,h_ay=%d\r,h_az=%d\r\n",acc[0],acc[1],acc[2]);
  617. //// nrf_delay_ms(10);
  618. //
  619. //// lsm6ds3tr_c_fifo_read(&acc_fifo, &acc_num, &gry_fifo, &gry_num);
  620. //// for(i=0;i<acc_num;i++)
  621. //// {
  622. //// SEGGER_RTT_printf(0,"acc_num:%d h_ax=%d,h_ay=%d,h_az=%d\r\n",acc_num,acc_fifo[i].i16bit[0],acc_fifo[i].i16bit[1],acc_fifo[i].i16bit[2]);
  623. //// }
  624. //// for(i=0;i<gry_num;i++)
  625. //// {
  626. //// SEGGER_RTT_printf(0,"gry_num:%d h_gx=%d,h_gy=%d,h_gz=%d\r\n",gry_num,gry_fifo[i].i16bit[0],gry_fifo[i].i16bit[1],gry_fifo[i].i16bit[2]);
  627. //// }
  628. //// nrf_delay_ms(100);
  629. // }