lsm6ds3tr_c.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "lsm6ds3tr_c.h"
  3. #include "drv_qmc6310.h"
  4. #include "bsp_spi.h"
  5. /* Private macro -------------------------------------------------------------*/
  6. #define BOOT_TIME 15 //ms
  7. #define WAIT_TIME_A 100 //ms
  8. #define WAIT_TIME_G_01 150 //ms
  9. #define WAIT_TIME_G_02 50 //ms
  10. /* Self test limits. */
  11. #define MIN_ST_LIMIT_mg 90.0f
  12. #define MAX_ST_LIMIT_mg 1700.0f
  13. #define MIN_ST_LIMIT_mdps 150000.0f
  14. #define MAX_ST_LIMIT_mdps 700000.0f
  15. /* Self test results. */
  16. #define ST_PASS 1U
  17. #define ST_FAIL 0U
  18. #define MIN_ODR(x, y) (x < y ? x : y)
  19. #define MAX_ODR(x, y) (x > y ? x : y)
  20. #define MAX_PATTERN_NUM FIFO_THRESHOLD / 6
  21. #define LSM6DS3_ODR_LSB_TO_HZ(_odr) (_odr ? (13 << (_odr - 1)) : 0)
  22. #define ACC_OUT_XYZ_WORD_SIZE 3
  23. #define GRY_OUT_XYZ_WORD_SIZE 3
  24. #define MAG_OUT_XYZ_WORD_SIZE 3
  25. #define TIMESTAMP_OUT_WORD_SIZE 3
  26. #define USED_ACC_FIFO_104_HZ LSM6DS3TR_C_XL_ODR_104Hz
  27. #define USED_GRY_FIFO_104_HZ LSM6DS3TR_C_GY_ODR_104Hz
  28. #define USED_FIFO_104_HZ LSM6DS3TR_C_FIFO_104Hz //同时也是timestamp的频率
  29. #define TIMEOUT 200
  30. #define SENSOR_HUB_HZ LSM6DS3TR_C_XL_ODR_1k66Hz
  31. /* Private type -------------------------------------------------------------*/
  32. typedef union{
  33. int16_t i16bit[3];
  34. uint8_t u8bit[6];
  35. } axis3bit16_t;
  36. typedef struct {
  37. uint8_t enable;
  38. lsm6ds3tr_c_odr_xl_t odr;
  39. uint16_t odr_hz_val;
  40. lsm6ds3tr_c_fs_xl_t fs;
  41. uint8_t decimation;
  42. uint8_t samples_num_in_pattern;
  43. } sensor_lsm6ds3_xl;
  44. typedef struct {
  45. uint8_t enable;
  46. lsm6ds3tr_c_odr_g_t odr;
  47. uint16_t odr_hz_val;
  48. lsm6ds3tr_c_fs_g_t fs;
  49. uint8_t decimation;
  50. uint8_t samples_num_in_pattern;
  51. } sensor_lsm6ds3_gy;
  52. /* static variable ------------------------------------------------------------------*/
  53. static uint8_t whoamI, rst;
  54. static stmdev_ctx_t dev_ctx;
  55. static axis3bit16_t data_raw_acceleration;
  56. static axis3bit16_t data_raw_angular_rate;
  57. static axis3bit16_t data_raw_magnetic;
  58. static axis3bit16_t data_raw_timestamp;
  59. static lsm6ds3tr_c_emb_sh_read_t raw_data;
  60. static uint16_t acc_mag_timestamp_pattern_len = ACC_OUT_XYZ_WORD_SIZE + MAG_OUT_XYZ_WORD_SIZE + TIMESTAMP_OUT_WORD_SIZE;
  61. static uint16_t gry_acc_mag_timestamp_pattern_len = GRY_OUT_XYZ_WORD_SIZE + ACC_OUT_XYZ_WORD_SIZE + MAG_OUT_XYZ_WORD_SIZE + TIMESTAMP_OUT_WORD_SIZE;
  62. /* 6ds3 Accelerometer test parameters */
  63. static sensor_lsm6ds3_xl config_6ds3_xl;
  64. /* 6ds3 Gyroscope test parameters */
  65. static sensor_lsm6ds3_gy config_6ds3_gyro;
  66. lsm6ds3tr_c_sh_cfg_read_t qmc6310 = {
  67. .slv_add = 0x1C,
  68. .slv_subadd = QMC6310_DATA_OUT_X_LSB_REG, //预备读取qmc6310地磁数据的寄存器
  69. .slv_len = MAG_OUT_XYZ_WORD_SIZE * 2, //转为字节长度
  70. };
  71. /* static fuction ------------------------------------------------------------------*/
  72. /** Please note that is MANDATORY: return 0 -> no Error.**/
  73. static int32_t platform_front_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  74. {
  75. int32_t ierror = 0;
  76. if(SPI0_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg, (uint8_t *)bufp, len))
  77. {
  78. ierror = -1;
  79. }
  80. return ierror;
  81. }
  82. static int32_t platform_front_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  83. {
  84. int32_t ierror = 0;
  85. if(SPI0_OnlyReadReg(BOARD_SPI0_CS0_IO, reg, bufp, len))
  86. {
  87. ierror = -1;
  88. }
  89. return ierror;
  90. }
  91. static int32_t platform_center_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  92. {
  93. int32_t ierror = 0;
  94. // if(SPI1_OnlyWriteReg(BOARD_SPI1_CS0_IO, reg, (uint8_t *)bufp, len))
  95. // {
  96. // ierror = -1;
  97. // }
  98. return ierror;
  99. }
  100. static int32_t platform_center_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  101. {
  102. int32_t ierror = 0;
  103. // if(SPI1_OnlyReadReg(BOARD_SPI1_CS0_IO, reg, bufp, len))
  104. // {
  105. // ierror = -1;
  106. // }
  107. return ierror;
  108. }
  109. static void platform_delay(uint32_t ms)
  110. {
  111. nrf_delay_ms(ms);
  112. }
  113. static void switch_dev(lsm_device_t dex_num)
  114. {
  115. /* Initialize mems driver interface */
  116. switch(dex_num)
  117. {
  118. case LSM_DEV_FRONT:
  119. dev_ctx.write_reg = platform_front_write;
  120. dev_ctx.read_reg = platform_front_read;
  121. dev_ctx.handle = NULL;
  122. break;
  123. case LSM_DEV_CENTER:
  124. dev_ctx.write_reg = platform_center_write;
  125. dev_ctx.read_reg = platform_center_read;
  126. dev_ctx.handle = NULL;
  127. break;
  128. }
  129. }
  130. /* Read data byte from internal register of a slave device connected
  131. * to master I2C interface
  132. * 注意:因为该函数会配置加速度的采样率
  133. */
  134. static int32_t lsm6ds3tr_c_read_sensor_hub_cx(stmdev_ctx_t *dev_ctx, uint8_t device_addr, uint8_t reg, uint8_t* data,uint16_t len)
  135. {
  136. axis3bit16_t data_raw_acceleration;
  137. int32_t mm_error;
  138. uint8_t drdy;
  139. lsm6ds3tr_c_all_sources_t endop;
  140. lsm6ds3tr_c_sh_cfg_read_t val = {
  141. .slv_add = device_addr,
  142. .slv_subadd = reg,
  143. .slv_len = len,
  144. };
  145. /* Disable accelerometer */
  146. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  147. /* Configure Sensor Hub to read QMC6310 */
  148. /* Prepare sensor hub to read data from external Slave0 */
  149. mm_error = lsm6ds3tr_c_sh_slv0_cfg_read(dev_ctx, &val);
  150. /* Configure Sensor Hub to read two slaves */
  151. lsm6ds3tr_c_sh_num_of_dev_connected_set(dev_ctx, LSM6DS3TR_C_SLV_0_1_2_3); //这里注意,IIC要比你外设多一个,原因未知,不这么做容易读不到数据。
  152. lsm6ds3tr_c_func_en_set(dev_ctx,PROPERTY_ENABLE);
  153. /* Enable I2C Master and I2C master Pull Up */
  154. /* Enable master and XL trigger */
  155. lsm6ds3tr_c_sh_master_set(dev_ctx, PROPERTY_ENABLE);
  156. lsm6ds3tr_c_sh_pin_mode_set(dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  157. /* Enable accelerometer to trigger Sensor Hub operation */
  158. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, SENSOR_HUB_HZ);
  159. /* Wait Sensor Hub operation flag set */
  160. lsm6ds3tr_c_acceleration_raw_get(dev_ctx, data_raw_acceleration.i16bit);
  161. uint16_t timeout = TIMEOUT;
  162. do {
  163. lsm6ds3tr_c_xl_flag_data_ready_get(dev_ctx, &drdy);
  164. timeout--;
  165. } while (!drdy && timeout!=0);
  166. // SEGGER_RTT_printf(0,"read sensor hub timeout_1:%d\n",timeout);
  167. if(timeout == 0)return -1;
  168. timeout = TIMEOUT;
  169. do
  170. {
  171. lsm6ds3tr_c_all_sources_get(dev_ctx,&endop);
  172. timeout--;
  173. } while (!endop.func_src1.sensorhub_end_op && timeout!=0);
  174. // SEGGER_RTT_printf(0,"read sensor hub timeout_2:%d\n",timeout);
  175. if(timeout == 0)return -1;
  176. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  177. lsm6ds3tr_c_sh_read_data_raw_get(dev_ctx, &raw_data);
  178. memcpy(data,&raw_data,len);
  179. lsm6ds3tr_c_sh_master_set(dev_ctx, PROPERTY_DISABLE);
  180. lsm6ds3tr_c_func_en_set(dev_ctx,PROPERTY_DISABLE);
  181. return mm_error;
  182. }
  183. /* Write data byte to internal register of a slave device connected
  184. * to master I2C interface
  185. * 必须先配置外挂的地磁先,之后才能配置加速度或陀螺仪。因为该函数会配置加速度的采样率
  186. */
  187. static int32_t lsm6ds3_write_sensor_hub_cx(stmdev_ctx_t *dev_ctx, uint8_t device_addr, uint8_t reg, uint8_t* data)
  188. {
  189. axis3bit16_t data_raw_acceleration;
  190. int32_t mm_error;
  191. uint8_t drdy;
  192. lsm6ds3tr_c_all_sources_t endop;
  193. lsm6ds3tr_c_sh_cfg_write_t val = {
  194. .slv0_add = device_addr,
  195. .slv0_subadd = reg,
  196. .slv0_data = *data,
  197. };
  198. /* Disable accelerometer */
  199. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  200. /* Configure Sensor Hub to write */
  201. mm_error = lsm6ds3tr_c_sh_cfg_write(dev_ctx, &val);
  202. lsm6ds3tr_c_func_en_set(dev_ctx,PROPERTY_ENABLE);
  203. /* Enable I2C Master and I2C master Pull Up */
  204. lsm6ds3tr_c_sh_master_set(dev_ctx, PROPERTY_ENABLE);
  205. lsm6ds3tr_c_sh_pin_mode_set(dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  206. /* Enable accelerometer to trigger Sensor Hub operation */
  207. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, SENSOR_HUB_HZ);
  208. /* Wait Sensor Hub operation flag set */
  209. lsm6ds3tr_c_acceleration_raw_get(dev_ctx, data_raw_acceleration.i16bit);
  210. uint16_t timeout = TIMEOUT;
  211. do {
  212. lsm6ds3tr_c_xl_flag_data_ready_get(dev_ctx, &drdy);
  213. timeout--;
  214. } while (!drdy && timeout !=0);
  215. // SEGGER_RTT_printf(0,"write sensor hub timeout_1:%d\n",timeout);
  216. if(timeout == 0)return -1;
  217. timeout = TIMEOUT;
  218. do
  219. {
  220. lsm6ds3tr_c_all_sources_get(dev_ctx,&endop);
  221. timeout--;
  222. } while (!endop.func_src1.sensorhub_end_op && timeout!=0);
  223. // SEGGER_RTT_printf(0,"write sensor hub timeout_2:%d\n",timeout);
  224. if(timeout == 0)return -1;
  225. lsm6ds3tr_c_xl_data_rate_set(dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  226. lsm6ds3tr_c_sh_master_set(dev_ctx, PROPERTY_DISABLE);
  227. lsm6ds3tr_c_func_en_set(dev_ctx,PROPERTY_DISABLE);
  228. return mm_error;
  229. }
  230. /* API ------------------------------------------------------------------*/
  231. int32_t lsm6ds3tr_c_switch_read_sensor_hub_cx(lsm_device_t dex_num, uint8_t device_addr, uint8_t reg, uint8_t* data,uint16_t len)
  232. {
  233. switch_dev(dex_num);
  234. return lsm6ds3tr_c_read_sensor_hub_cx(&dev_ctx, device_addr, reg, data,len);
  235. }
  236. int32_t lsm6ds3_c_switch_write_sensor_hub_cx(lsm_device_t dex_num, uint8_t device_addr, uint8_t reg, uint8_t* data)
  237. {
  238. switch_dev(dex_num);
  239. return lsm6ds3_write_sensor_hub_cx(&dev_ctx, device_addr, reg, data);
  240. }
  241. int lsm6ds3tr_c_init(lsm_device_t dex_num)
  242. {
  243. int16_t data_raw[3];
  244. float val_st_off[3];
  245. float val_st_on[3];
  246. float test_val[3];
  247. uint8_t st_result;
  248. uint8_t drdy;
  249. uint8_t i;
  250. uint8_t j;
  251. switch(dex_num)
  252. {
  253. case LSM_DEV_FRONT:
  254. nrf_gpio_cfg(
  255. PIN_FRONT_SENSE_POWER,
  256. NRF_GPIO_PIN_DIR_OUTPUT,
  257. NRF_GPIO_PIN_INPUT_DISCONNECT,
  258. NRF_GPIO_PIN_NOPULL,
  259. NRF_GPIO_PIN_H0H1,
  260. NRF_GPIO_PIN_NOSENSE);
  261. //供电
  262. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  263. nrf_gpio_cfg_output(BOARD_SPI0_MOSI_IO);
  264. nrf_gpio_cfg_output(BOARD_SPI0_CLK_IO);
  265. nrf_gpio_cfg_output(PIN_FRONT_SPI_nCS);
  266. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  267. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  268. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  269. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  270. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  271. platform_delay(100);
  272. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  273. platform_delay(100);
  274. SPI0_Init();
  275. break;
  276. case LSM_DEV_CENTER:
  277. // SPI1_Init();
  278. break;
  279. }
  280. switch_dev(dex_num);
  281. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  282. /* Wait sensor boot time */
  283. platform_delay(BOOT_TIME);
  284. /* Check device ID */
  285. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  286. if(whoamI != LSM6DS3TR_C_ID)
  287. {
  288. SEGGER_RTT_printf(0,"lsm6ds3tr_c_init error:%d!!!\r\n",dex_num);
  289. return -1;
  290. }
  291. /* Restore default configuration */
  292. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  293. do {
  294. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  295. } while(rst);
  296. // /* Enable Block Data Update */
  297. // lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  298. /*
  299. * Accelerometer Self Test
  300. */
  301. /* Set Output Data Rate */
  302. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_52Hz);
  303. /* Set full scale */
  304. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, LSM6DS3TR_C_4g);
  305. /* Wait stable output */
  306. platform_delay(WAIT_TIME_A);
  307. /* Check if new value available */
  308. do {
  309. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  310. } while (!drdy);
  311. /* Read dummy data and discard it */
  312. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  313. /* Read 5 sample and get the average vale for each axis */
  314. memset(val_st_off, 0x00, 3 * sizeof(float));
  315. for (i = 0; i < 5; i++) {
  316. /* Check if new value available */
  317. do {
  318. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  319. } while (!drdy);
  320. /* Read data and accumulate the mg value */
  321. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  322. for (j = 0; j < 3; j++) {
  323. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  324. }
  325. }
  326. /* Calculate the mg average values */
  327. for (i = 0; i < 3; i++) {
  328. val_st_off[i] /= 5.0f;
  329. }
  330. /* Enable Self Test positive (or negative) */
  331. lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  332. //lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_POSITIVE);
  333. /* Wait stable output */
  334. platform_delay(WAIT_TIME_A);
  335. /* Check if new value available */
  336. do {
  337. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  338. } while (!drdy);
  339. /* Read dummy data and discard it */
  340. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  341. /* Read 5 sample and get the average vale for each axis */
  342. memset(val_st_on, 0x00, 3 * sizeof(float));
  343. for (i = 0; i < 5; i++) {
  344. /* Check if new value available */
  345. do {
  346. lsm6ds3tr_c_xl_flag_data_ready_get(&dev_ctx, &drdy);
  347. } while (!drdy);
  348. /* Read data and accumulate the mg value */
  349. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx, data_raw);
  350. for (j = 0; j < 3; j++) {
  351. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  352. }
  353. }
  354. /* Calculate the mg average values */
  355. for (i = 0; i < 3; i++) {
  356. val_st_on[i] /= 5.0f;
  357. }
  358. /* Calculate the mg values for self test */
  359. for (i = 0; i < 3; i++) {
  360. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  361. }
  362. /* Check self test limit */
  363. st_result = ST_PASS;
  364. for (i = 0; i < 3; i++) {
  365. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  366. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  367. st_result = ST_FAIL;
  368. }
  369. }
  370. /* Disable Self Test */
  371. lsm6ds3tr_c_xl_self_test_set(&dev_ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  372. /* Disable sensor. */
  373. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  374. /*
  375. * Gyroscope Self Test
  376. */
  377. /* Set Output Data Rate */
  378. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_208Hz);
  379. /* Set full scale */
  380. lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, LSM6DS3TR_C_2000dps);
  381. /* Wait stable output */
  382. platform_delay(WAIT_TIME_G_01);
  383. /* Check if new value available */
  384. do {
  385. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  386. } while (!drdy);
  387. /* Read dummy data and discard it */
  388. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  389. /* Read 5 sample and get the average vale for each axis */
  390. memset(val_st_off, 0x00, 3 * sizeof(float));
  391. for (i = 0; i < 5; i++) {
  392. /* Check if new value available */
  393. do {
  394. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  395. } while (!drdy);
  396. /* Read data and accumulate the mg value */
  397. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  398. for (j = 0; j < 3; j++) {
  399. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  400. data_raw[j]);
  401. }
  402. }
  403. /* Calculate the mg average values */
  404. for (i = 0; i < 3; i++) {
  405. val_st_off[i] /= 5.0f;
  406. }
  407. /* Enable Self Test positive (or negative) */
  408. lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  409. //lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LIS2DH12_GY_ST_NEGATIVE);
  410. /* Wait stable output */
  411. platform_delay(WAIT_TIME_G_02);
  412. /* Read 5 sample and get the average vale for each axis */
  413. memset(val_st_on, 0x00, 3 * sizeof(float));
  414. for (i = 0; i < 5; i++) {
  415. /* Check if new value available */
  416. do {
  417. lsm6ds3tr_c_gy_flag_data_ready_get(&dev_ctx, &drdy);
  418. } while (!drdy);
  419. /* Read data and accumulate the mg value */
  420. lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx, data_raw);
  421. for (j = 0; j < 3; j++) {
  422. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  423. data_raw[j]);
  424. }
  425. }
  426. /* Calculate the mg average values */
  427. for (i = 0; i < 3; i++) {
  428. val_st_on[i] /= 5.0f;
  429. }
  430. /* Calculate the mg values for self test */
  431. for (i = 0; i < 3; i++) {
  432. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  433. }
  434. /* Check self test limit */
  435. for (i = 0; i < 3; i++) {
  436. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  437. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  438. st_result = ST_FAIL;
  439. }
  440. }
  441. /* Disable Self Test */
  442. lsm6ds3tr_c_gy_self_test_set(&dev_ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  443. /* Disable sensor. */
  444. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_OFF);
  445. if (st_result == ST_PASS) {
  446. SEGGER_RTT_printf(0,"Self Test:%d - PASS\r\n",dex_num);
  447. }
  448. else {
  449. SEGGER_RTT_printf(0,"Self Test:%d - FAIL\r\n",dex_num);
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. void lsm6ds3tr_c_read_data_polling_mode(lsm_device_t dex_num)
  455. {
  456. // /* 6ds3 Accelerometer test parameters */
  457. // config_6ds3_xl.enable = PROPERTY_ENABLE;
  458. // config_6ds3_xl.odr = LSM6DS3TR_C_XL_ODR_104Hz;
  459. // config_6ds3_xl.odr_hz_val = 0;
  460. // config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  461. // config_6ds3_xl.decimation = 0;
  462. // config_6ds3_xl.samples_num_in_pattern = 0;
  463. // /* 6ds3 Gyroscope test parameters */
  464. // config_6ds3_gyro.enable = PROPERTY_ENABLE;
  465. // config_6ds3_gyro.odr = LSM6DS3TR_C_GY_ODR_104Hz;
  466. // config_6ds3_gyro.odr_hz_val = 0;
  467. // config_6ds3_gyro.fs = LSM6DS3TR_C_2000dps;
  468. // config_6ds3_gyro.decimation = 0;
  469. // config_6ds3_gyro.samples_num_in_pattern = 0;
  470. //
  471. // switch_dev(dex_num);
  472. // /* Check device ID */
  473. // whoamI = 0;
  474. // lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  475. // if(whoamI != LSM6DS3TR_C_ID)
  476. // {
  477. // SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_data_polling_config error:%d!!!\r\n",dex_num);
  478. // return;
  479. // }
  480. //
  481. // /* Restore default configuration */
  482. // lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  483. // do {
  484. // lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  485. // } while (rst);
  486. // /* Enable Block Data Update */
  487. // lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  488. //
  489. // lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  490. //
  491. // lsm6ds3tr_c_gy_power_mode_set(&dev_ctx,LSM6DS3TR_C_GY_NORMAL);
  492. //
  493. // /* Set Output Data Rate */
  494. // lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  495. // lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, config_6ds3_gyro.odr);
  496. // /* Set full scale */
  497. // lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  498. // lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, config_6ds3_gyro.fs);
  499. //
  500. // platform_delay(20);
  501. //
  502. // /* Configure filtering chain(No aux interface) */
  503. // /* Accelerometer - analog filter */
  504. //// lsm6ds3tr_c_xl_filter_analog_set(&dev_ctx,
  505. //// LSM6DS3TR_C_XL_ANA_BW_400Hz);
  506. //// /* Accelerometer - LPF1 path ( LPF2 not used )*/
  507. //// //lsm6ds3tr_c_xl_lp1_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_LP1_ODR_DIV_4);
  508. //// /* Accelerometer - LPF1 + LPF2 path */
  509. //// lsm6ds3tr_c_xl_lp2_bandwidth_set(&dev_ctx,
  510. //// LSM6DS3TR_C_XL_LOW_NOISE_LP_ODR_DIV_100);
  511. //// /* Accelerometer - High Pass / Slope path */
  512. //// //lsm6ds3tr_c_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);
  513. //// //lsm6ds3tr_c_xl_hp_bandwidth_set(&dev_ctx, LSM6DS3TR_C_XL_HP_ODR_DIV_100);
  514. //// /* Gyroscope - filtering chain */
  515. //// lsm6ds3tr_c_gy_band_pass_set(&dev_ctx,
  516. //// LSM6DS3TR_C_HP_260mHz_LP1_STRONG);
  517. }
  518. void lsm6ds3tr_c_read_data_polling(lsm_device_t dex_num, int16_t *acc, int16_t *gry, int16_t *temp)
  519. {
  520. // switch_dev(dex_num);
  521. //
  522. // /* Read samples in polling mode (no int) */
  523. // /* Read output only if new value is available */
  524. // lsm6ds3tr_c_reg_t reg;
  525. // lsm6ds3tr_c_status_reg_get(&dev_ctx, &reg.status_reg);
  526. // if (reg.status_reg.xlda) {
  527. // /* Read acc field data */
  528. // memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  529. // lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  530. // data_raw_acceleration.i16bit);
  531. // acc[0] = data_raw_acceleration.i16bit[0];
  532. // acc[1] = data_raw_acceleration.i16bit[1];
  533. // acc[2] = data_raw_acceleration.i16bit[2];
  534. // }
  535. // if (reg.status_reg.gda) {
  536. // /* Read gry field data */
  537. // memset(data_raw_angular_rate.i16bit, 0x00, 3 * sizeof(int16_t));
  538. // lsm6ds3tr_c_angular_rate_raw_get(&dev_ctx,
  539. // data_raw_angular_rate.i16bit);
  540. // gry[0] = data_raw_angular_rate.i16bit[0];
  541. // gry[1] = data_raw_angular_rate.i16bit[1];
  542. // gry[2] = data_raw_angular_rate.i16bit[2];
  543. // }
  544. // if (reg.status_reg.tda) {
  545. // /* Read temperature data */
  546. // memset(&data_raw_temperature, 0x00, sizeof(int16_t));
  547. // lsm6ds3tr_c_temperature_raw_get(&dev_ctx, &data_raw_temperature);
  548. // *temp = data_raw_temperature;
  549. // }
  550. //
  551. }
  552. void lsm6ds3tr_c_standby_power_acc_mode_config(lsm_device_t dex_num)
  553. {
  554. switch_dev(dex_num);
  555. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  556. platform_delay(3);
  557. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  558. platform_delay(3);
  559. /* 6ds3 Accelerometer test parameters */
  560. config_6ds3_xl.enable = PROPERTY_ENABLE;
  561. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  562. config_6ds3_xl.odr_hz_val = 0;
  563. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  564. config_6ds3_xl.decimation = 0;
  565. config_6ds3_xl.samples_num_in_pattern = 0;
  566. /* Check device ID */
  567. whoamI = 0;
  568. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  569. if(whoamI != LSM6DS3TR_C_ID)
  570. {
  571. SEGGER_RTT_printf(0,"lsm6ds3tr_c_low_power_acc_mode_config error:%d!!!\r\n",dex_num);
  572. return;
  573. }
  574. /* Restore default configuration */
  575. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  576. do {
  577. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  578. } while (rst);
  579. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  580. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  581. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  582. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  583. }
  584. void lsm6ds3tr_c_standby_power_acc_mode_start(lsm_device_t dex_num)
  585. {
  586. switch_dev(dex_num);
  587. /* Set XL Output Data Rate */
  588. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  589. }
  590. void lsm6ds3tr_c_standby_power_acc_read(lsm_device_t dex_num, int16_t *acc)
  591. {
  592. switch_dev(dex_num);
  593. /* Read samples in polling mode (no int) */
  594. /* Read output only if new value is available */
  595. lsm6ds3tr_c_reg_t reg;
  596. lsm6ds3tr_c_status_reg_get(&dev_ctx, &reg.status_reg);
  597. if (reg.status_reg.xlda) {
  598. /* Read magnetic field data */
  599. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  600. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  601. data_raw_acceleration.i16bit);
  602. acc[0] = data_raw_acceleration.i16bit[0];
  603. acc[1] = data_raw_acceleration.i16bit[1];
  604. acc[2] = data_raw_acceleration.i16bit[2];
  605. #if DEBUG_IMU
  606. SEGGER_RTT_printf(0,"dev:%d h_ax=%d\r,h_ay=%d\r,h_az=%d\r\n",dex_num,acc[0],acc[1],acc[2]);
  607. #endif
  608. }
  609. }
  610. int16_t lsm6ds3tr_c_fifo_mode_read_group_num(lsm_device_t dex_num, lsm_mode_t fifo_mode)
  611. {
  612. uint8_t wt;
  613. uint16_t num = 0;
  614. uint16_t num_pattern = 0;
  615. switch_dev(dex_num);
  616. /* Read FIFO watermark flag in polling mode */
  617. lsm6ds3tr_c_fifo_wtm_flag_get(&dev_ctx, &wt);
  618. if(wt)
  619. {
  620. /* Read number of word in FIFO */
  621. lsm6ds3tr_c_fifo_data_level_get(&dev_ctx, &num);
  622. // SEGGER_RTT_printf(0,"---------->dex_num:%d num:%d!!!\r\n",dex_num,num);
  623. switch(fifo_mode)
  624. {
  625. case LSM_STATE_ACC_MODE:
  626. break;
  627. case LSM_STATE_ACC_QMC_FIFO_MODE:
  628. num_pattern = num / acc_mag_timestamp_pattern_len;
  629. break;
  630. case LSM_STATE_GRY_ACC_QMC_FIFO_MODE:
  631. num_pattern = num / gry_acc_mag_timestamp_pattern_len;
  632. break;
  633. case LSM_STATE_ILLEGAL_MODE:
  634. num_pattern = 0;
  635. break;
  636. }
  637. }
  638. return num_pattern;
  639. }
  640. void lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_config(lsm_device_t dex_num)
  641. {
  642. switch_dev(dex_num);
  643. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  644. platform_delay(3);
  645. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  646. platform_delay(3);
  647. /* 6ds3 Accelerometer test parameters */
  648. config_6ds3_xl.enable = PROPERTY_ENABLE;
  649. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  650. config_6ds3_xl.odr_hz_val = 0;
  651. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  652. config_6ds3_xl.decimation = 0;
  653. config_6ds3_xl.samples_num_in_pattern = 0;
  654. /* Check device ID */
  655. whoamI = 0;
  656. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  657. if(whoamI != LSM6DS3TR_C_ID)
  658. {
  659. SEGGER_RTT_printf(0,"lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode error:%d!!!\r\n",dex_num);
  660. return;
  661. }
  662. /* Restore default configuration */
  663. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  664. do {
  665. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  666. } while (rst);
  667. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  668. /*qmc6310-------------------------------------------------------------*/
  669. /* Some hardware require to enable pull up on master I2C interface */
  670. lsm6ds3tr_c_sh_pin_mode_set(&dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  671. //qmc6310初始化
  672. switch(dex_num)
  673. {
  674. case LSM_DEV_FRONT:
  675. if(qmc6310_front_Init(QMC6310_100HZ))SEGGER_RTT_printf(0,"qmc6310_front_Init sucess!!\n");
  676. else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_front_Init fail!!\n");return;}
  677. break;
  678. case LSM_DEV_CENTER:
  679. // if(qmc6310_center_Init(QMC6310_100HZ))SEGGER_RTT_printf(0,"qmc6310_center_Init sucess!!\n");
  680. // else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_center_Init fail!!\n");return;}
  681. break;
  682. }
  683. uint8_t data = 0xFF,timeout = 3;
  684. do{
  685. lsm6ds3tr_c_sh_slv0_cfg_read(&dev_ctx, &qmc6310); //这里只有一个地磁,但不能用slv1,只能用slv0,不知道是不是要先配置slv0后,才能配置slv1。
  686. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_BANK_A);
  687. lsm6ds3tr_c_read_reg(&dev_ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  688. // SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_reg LSM6DS3TR_C_SLV0_SUBADD:%x!!!\r\n",data);
  689. // platform_delay(100);
  690. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  691. timeout--;
  692. }while(data != QMC6310_DATA_OUT_X_LSB_REG && timeout != 0);
  693. /* Configure Sensor Hub to read two slaves */
  694. lsm6ds3tr_c_sh_num_of_dev_connected_set(&dev_ctx, LSM6DS3TR_C_SLV_0_1_2_3);
  695. lsm6ds3tr_c_func_en_set(&dev_ctx,PROPERTY_ENABLE);
  696. lsm6ds3tr_c_sh_master_set(&dev_ctx, PROPERTY_ENABLE);
  697. /*acc-------------------------------------------------------------*/
  698. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  699. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  700. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  701. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&dev_ctx,PROPERTY_ENABLE); //使能开关
  702. /* Set High Resolution Timestamp (25 us tick) */
  703. lsm6ds3tr_c_timestamp_res_set(&dev_ctx, LSM6DS3TR_C_LSB_25us);
  704. /* Enable timestamp in HW */
  705. lsm6ds3tr_c_timestamp_set(&dev_ctx, PROPERTY_ENABLE);
  706. /* Set FIFO watermark to a multiple of a pattern */
  707. lsm6ds3tr_c_fifo_watermark_set(&dev_ctx, acc_mag_timestamp_pattern_len);//pattern_len
  708. /* Set FIFO mode to Stream mode */
  709. lsm6ds3tr_c_fifo_mode_set(&dev_ctx, LSM6DS3TR_C_STREAM_MODE);
  710. /* Set ODR FIFO */
  711. lsm6ds3tr_c_fifo_data_rate_set(&dev_ctx, USED_FIFO_104_HZ);//该参数关联到timestamp
  712. /* Set FIFO sensor decimator */
  713. lsm6ds3tr_c_fifo_xl_batch_set(&dev_ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//因为acc和fifo都是104HZ,所以不用抽取因子。
  714. lsm6ds3tr_c_fifo_dataset_3_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//因为mag和fifo都是104HZ,所以不用抽取因子。
  715. lsm6ds3tr_c_fifo_dataset_4_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  716. }
  717. void lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_start(lsm_device_t dex_num)
  718. {
  719. switch_dev(dex_num);
  720. /* Set XL Output Data Rate */
  721. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  722. }
  723. int lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_read(lsm_device_t dex_num, int16_t *acc, int16_t *mag, int32_t *timestamp, int16_t group_num)
  724. {
  725. uint16_t num_pattern = 0;
  726. int16_t cur_count = 0;
  727. switch_dev(dex_num);
  728. if(group_num <= 0)return 0;
  729. num_pattern = group_num;
  730. if (num_pattern)
  731. {
  732. // SEGGER_RTT_printf(0,"acc_mag_timestamp_pattern_len:%d num_pattern:%d\r\n",acc_mag_timestamp_pattern_len,num_pattern);
  733. while (num_pattern-- > 0){
  734. // SEGGER_RTT_printf(0,"num:%d pattern_len:%d num_pattern:%d mag_num:%d xl_num:%d\r\n",num,pattern_len,num_pattern,mag_num,xl_num);
  735. /* FIFO pattern is composed by gy_num gyroscope triplets and
  736. * xl_num accelerometer triplets. The sequence has always following order:
  737. * gyro first, accelerometer second , mag third
  738. */
  739. /* Read XL samples */
  740. if (config_6ds3_xl.enable)
  741. {
  742. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_acceleration.u8bit,
  743. ACC_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  744. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_magnetic.u8bit,
  745. MAG_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  746. /* Read timestamp samples */
  747. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_timestamp.u8bit,TIMESTAMP_OUT_WORD_SIZE * sizeof(int16_t));
  748. if(cur_count != group_num )
  749. {
  750. acc[cur_count*3 + 0] = data_raw_acceleration.i16bit[0];
  751. acc[cur_count*3 + 1] = data_raw_acceleration.i16bit[1];
  752. acc[cur_count*3 + 2] = data_raw_acceleration.i16bit[2];
  753. QMC6310_map c_map;
  754. switch(dex_num)
  755. {
  756. case LSM_DEV_FRONT:
  757. c_map = qmc6310_front_GetMap();
  758. break;
  759. case LSM_DEV_CENTER:
  760. // c_map = qmc6310_center_GetMap();
  761. break;
  762. }
  763. mag[cur_count*3 + 0] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[0]]);
  764. mag[cur_count*3 + 1] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[1]]);
  765. mag[cur_count*3 + 2] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[2]]);
  766. /* 时间戳和计步数数据
  767. 字节1 字节2 字节3 字节4 字节5 字节6
  768. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  769. [15:8] [23:16] [7:0] [7:0] [15:8]
  770. */
  771. timestamp[cur_count] = ((data_raw_timestamp.u8bit[1] << 16) | (data_raw_timestamp.u8bit[0] << 8) | (data_raw_timestamp.u8bit[3]))*25;//counter == 25us
  772. cur_count++;
  773. }
  774. }
  775. }
  776. }
  777. return cur_count;
  778. }
  779. void lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_config(lsm_device_t dex_num)
  780. {
  781. switch_dev(dex_num);
  782. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  783. platform_delay(3);
  784. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  785. platform_delay(3);
  786. /* 6ds3 Accelerometer test parameters */
  787. config_6ds3_xl.enable = PROPERTY_ENABLE;
  788. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  789. config_6ds3_xl.odr_hz_val = 0;
  790. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  791. config_6ds3_xl.decimation = 0;
  792. config_6ds3_xl.samples_num_in_pattern = 0;
  793. /* 6ds3 Gyroscope test parameters */
  794. config_6ds3_gyro.enable = PROPERTY_ENABLE;
  795. config_6ds3_gyro.odr = USED_GRY_FIFO_104_HZ;
  796. config_6ds3_gyro.odr_hz_val = 0;
  797. config_6ds3_gyro.fs = LSM6DS3TR_C_2000dps;
  798. config_6ds3_gyro.decimation = 0;
  799. config_6ds3_gyro.samples_num_in_pattern = 0;
  800. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  801. /* Check device ID */
  802. whoamI = 0;
  803. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  804. if(whoamI != LSM6DS3TR_C_ID)
  805. {
  806. SEGGER_RTT_printf(0,"lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode error:%d!!!\r\n",dex_num);
  807. return;
  808. }
  809. /* Restore default configuration */
  810. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  811. do {
  812. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  813. } while (rst);
  814. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  815. /*qmc6310-------------------------------------------------------------*/
  816. /* Some hardware require to enable pull up on master I2C interface */
  817. lsm6ds3tr_c_sh_pin_mode_set(&dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  818. //qmc6310初始化
  819. switch(dex_num)
  820. {
  821. case LSM_DEV_FRONT:
  822. if(qmc6310_front_Init(QMC6310_200HZ))SEGGER_RTT_printf(0,"qmc6310_front_Init sucess!!\n");
  823. else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_front_Init fail!!\n");return;}
  824. break;
  825. case LSM_DEV_CENTER:
  826. // if(qmc6310_center_Init(QMC6310_100HZ))SEGGER_RTT_printf(0,"qmc6310_center_Init sucess!!\n");
  827. // else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_center_Init fail!!\n");return;}
  828. break;
  829. }
  830. uint8_t data = 0xFF,timeout = 3;
  831. do{
  832. lsm6ds3tr_c_sh_slv0_cfg_read(&dev_ctx, &qmc6310); //这里只有一个地磁,但不能用slv1,只能用slv0,不知道是不是要先配置slv0后,才能配置slv1。
  833. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_BANK_A);
  834. lsm6ds3tr_c_read_reg(&dev_ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  835. // SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_reg LSM6DS3TR_C_SLV0_SUBADD:%x!!!\r\n",data);
  836. // platform_delay(100);
  837. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  838. timeout--;
  839. }while(data != QMC6310_DATA_OUT_X_LSB_REG && timeout != 0);
  840. /* Configure Sensor Hub to read two slaves */
  841. lsm6ds3tr_c_sh_num_of_dev_connected_set(&dev_ctx, LSM6DS3TR_C_SLV_0_1_2_3);
  842. lsm6ds3tr_c_func_en_set(&dev_ctx,PROPERTY_ENABLE);
  843. lsm6ds3tr_c_sh_master_set(&dev_ctx, PROPERTY_ENABLE);
  844. /*acc-------------------------------------------------------------*/
  845. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  846. lsm6ds3tr_c_gy_power_mode_set(&dev_ctx,LSM6DS3TR_C_GY_NORMAL);
  847. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  848. lsm6ds3tr_c_gy_full_scale_set(&dev_ctx, config_6ds3_gyro.fs);
  849. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&dev_ctx,PROPERTY_ENABLE); //使能开关
  850. /* Set High Resolution Timestamp (25 us tick) */
  851. lsm6ds3tr_c_timestamp_res_set(&dev_ctx, LSM6DS3TR_C_LSB_25us);
  852. /* Enable timestamp in HW */
  853. lsm6ds3tr_c_timestamp_set(&dev_ctx, PROPERTY_ENABLE);
  854. // lsm6ds3tr_c_fifo_write_trigger_set(&dev_ctx,LSM6DS3TR_C_TRG_XL_GY_DRDY);
  855. /* Set FIFO watermark to a multiple of a pattern */
  856. lsm6ds3tr_c_fifo_watermark_set(&dev_ctx, gry_acc_mag_timestamp_pattern_len);//pattern_len
  857. /* Set FIFO mode to Stream mode */
  858. lsm6ds3tr_c_fifo_mode_set(&dev_ctx, LSM6DS3TR_C_STREAM_MODE);
  859. /* Set ODR FIFO */
  860. lsm6ds3tr_c_fifo_data_rate_set(&dev_ctx, USED_FIFO_104_HZ);//该参数关联到timestamp
  861. /* Set FIFO sensor decimator */
  862. lsm6ds3tr_c_fifo_xl_batch_set(&dev_ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//acc和fifo的HZ一致
  863. lsm6ds3tr_c_fifo_gy_batch_set(&dev_ctx, LSM6DS3TR_C_FIFO_GY_NO_DEC);//gry和fifo的HZ一致
  864. lsm6ds3tr_c_fifo_dataset_3_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//mag和fifo的HZ一致
  865. lsm6ds3tr_c_fifo_dataset_4_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  866. }
  867. void lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_start(lsm_device_t dex_num)
  868. {
  869. switch_dev(dex_num);
  870. /* Set XL Output Data Rate */
  871. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  872. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, config_6ds3_gyro.odr);
  873. }
  874. int lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_read(lsm_device_t dex_num, int16_t *gry, int16_t *acc, int16_t *mag, int32_t *timestamp, int16_t group_num)
  875. {
  876. uint16_t num_pattern = 0;
  877. int16_t cur_count = 0;
  878. switch_dev(dex_num);
  879. if(group_num <= 0)return 0;
  880. num_pattern = group_num;
  881. if (num_pattern)
  882. {
  883. // SEGGER_RTT_printf(0,"gry_acc_mag_timestamp_pattern_len:%d num_pattern:%d\r\n",gry_acc_mag_timestamp_pattern_len,num_pattern);
  884. while (num_pattern-- > 0){
  885. // SEGGER_RTT_printf(0,"num:%d pattern_len:%d num_pattern:%d mag_num:%d xl_num:%d\r\n",num,pattern_len,num_pattern,mag_num,xl_num);
  886. /* FIFO pattern is composed by gy_num gyroscope triplets and
  887. * xl_num accelerometer triplets. The sequence has always following order:
  888. * gyro first, accelerometer second , mag third , timestamp fourth
  889. */
  890. if (config_6ds3_gyro.enable && config_6ds3_xl.enable)
  891. {
  892. /* Read gyro samples */
  893. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_angular_rate.u8bit,
  894. GRY_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  895. /* Read XL samples */
  896. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_acceleration.u8bit,
  897. ACC_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  898. /* Read mag samples */
  899. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_magnetic.u8bit,
  900. MAG_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  901. /* Read timestamp samples */
  902. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_timestamp.u8bit,TIMESTAMP_OUT_WORD_SIZE * sizeof(int16_t));
  903. if(cur_count != group_num)
  904. {
  905. gry[cur_count*3 + 0] = data_raw_angular_rate.i16bit[0];
  906. gry[cur_count*3 + 1] = data_raw_angular_rate.i16bit[1];
  907. gry[cur_count*3 + 2] = data_raw_angular_rate.i16bit[2];
  908. acc[cur_count*3 + 0] = data_raw_acceleration.i16bit[0];
  909. acc[cur_count*3 + 1] = data_raw_acceleration.i16bit[1];
  910. acc[cur_count*3 + 2] = data_raw_acceleration.i16bit[2];
  911. QMC6310_map c_map;
  912. switch(dex_num)
  913. {
  914. case LSM_DEV_FRONT:
  915. c_map = qmc6310_front_GetMap();
  916. break;
  917. case LSM_DEV_CENTER:
  918. // c_map = qmc6310_center_GetMap();
  919. break;
  920. }
  921. mag[cur_count*3 + 0] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[0]]);
  922. mag[cur_count*3 + 1] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[1]]);
  923. mag[cur_count*3 + 2] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[2]]);
  924. /* 时间戳和计步数数据
  925. 字节1 字节2 字节3 字节4 字节5 字节6
  926. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  927. [15:8] [23:16] [7:0] [7:0] [15:8]
  928. */
  929. timestamp[cur_count] = ((data_raw_timestamp.u8bit[1] << 16) | (data_raw_timestamp.u8bit[0] << 8) | (data_raw_timestamp.u8bit[3]))*25;//counter == 25us
  930. cur_count++;
  931. }
  932. }
  933. }
  934. }
  935. return cur_count;
  936. }
  937. void lsm6ds3tr_c_get_mode_stat(lsm_device_t dex_num, uint8_t *stat)
  938. {
  939. switch_dev(dex_num);
  940. int16_t lsm_acc[3]={0,0,0}, lsm_gry[3] = {0,0,0}, mag[3] = {0,0,0};
  941. lsm6ds3tr_c_odr_xl_t acc_odr;
  942. lsm6ds3tr_c_odr_g_t gry_odr;
  943. uint8_t reg_val = 0;
  944. int32_t ret,timestamp;
  945. uint16_t timeout = TIMEOUT;
  946. int16_t fifo_num = 0;
  947. *stat = LSM_STATE_ILLEGAL_MODE;
  948. ret = lsm6ds3tr_c_read_reg(&dev_ctx, LSM6DS3TR_C_FIFO_CTRL5,
  949. &reg_val, 1);
  950. if(ret == 0 && (reg_val & 0x78)!=0) //fifo 开启
  951. {
  952. lsm6ds3tr_c_gy_sleep_mode_get(&dev_ctx, &reg_val);
  953. switch(reg_val)
  954. {
  955. case 1: // gry关闭
  956. lsm6ds3tr_c_xl_data_rate_get(&dev_ctx, &acc_odr);
  957. if(acc_odr != USED_ACC_FIFO_104_HZ){
  958. *stat = LSM_STATE_ILLEGAL_MODE;
  959. }else{
  960. do{
  961. fifo_num = lsm6ds3tr_c_fifo_mode_read_group_num(dex_num,LSM_STATE_ACC_QMC_FIFO_MODE);
  962. timeout--;
  963. }while(fifo_num == 0 && timeout != 0);
  964. while(fifo_num--)lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_read(dex_num, lsm_acc, mag, &timestamp, 1);
  965. if((lsm_acc[0] == 0 && lsm_acc[1] == 0 && lsm_acc[2] == 0) || (mag[0] == 0 && mag[1] == 0 && mag[2] == 0) || timestamp == 0){
  966. *stat = LSM_STATE_ILLEGAL_MODE;
  967. }else{
  968. *stat = LSM_STATE_ACC_QMC_FIFO_MODE;
  969. // SEGGER_RTT_printf(0,"LSM_STATE_ACC_QMC_FIFO_MODE dex_num:%d fifo_num:%d timeout:%d\n",dex_num,fifo_num,timeout);
  970. }
  971. }
  972. break;
  973. case 0: // gry 开启
  974. lsm6ds3tr_c_xl_data_rate_get(&dev_ctx, &acc_odr);
  975. lsm6ds3tr_c_gy_data_rate_get(&dev_ctx,&gry_odr);
  976. if(acc_odr != USED_ACC_FIFO_104_HZ || gry_odr != USED_GRY_FIFO_104_HZ){
  977. *stat = LSM_STATE_ILLEGAL_MODE;
  978. }else{
  979. do{
  980. fifo_num = lsm6ds3tr_c_fifo_mode_read_group_num(dex_num,LSM_STATE_GRY_ACC_QMC_FIFO_MODE);
  981. timeout--;
  982. }while(fifo_num == 0 && timeout != 0);
  983. while(fifo_num--)lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_read(dex_num, lsm_gry, lsm_acc, mag, &timestamp, 1);
  984. if((lsm_acc[0] == 0 && lsm_acc[1] == 0 && lsm_acc[2] == 0) || (mag[0] == 0 && mag[1] == 0 && mag[2] == 0) || (lsm_gry[0] == 0 && lsm_gry[1] == 0 && lsm_gry[2] == 0) || timestamp == 0){
  985. *stat = LSM_STATE_ILLEGAL_MODE;
  986. }else{
  987. *stat = LSM_STATE_GRY_ACC_QMC_FIFO_MODE;
  988. // SEGGER_RTT_printf(0,"LSM_STATE_GRY_ACC_QMC_FIFO_MODE dex_num:%d fifo_num:%d timeout:%d\n",dex_num,fifo_num,timeout);
  989. }
  990. }
  991. break;
  992. }
  993. }
  994. else //fifo 关闭
  995. {
  996. lsm6ds3tr_c_gy_sleep_mode_get(&dev_ctx, &reg_val);
  997. if(reg_val){ // gry关闭
  998. lsm6ds3tr_c_xl_data_rate_get(&dev_ctx, &acc_odr);
  999. if(acc_odr != USED_ACC_FIFO_104_HZ){
  1000. *stat = LSM_STATE_ILLEGAL_MODE;
  1001. }else{
  1002. lsm6ds3tr_c_standby_power_acc_read(dex_num, lsm_acc);
  1003. if(lsm_acc[0] == 0 && lsm_acc[1] == 0 && lsm_acc[2] == 0){
  1004. *stat = LSM_STATE_ILLEGAL_MODE;
  1005. }else{
  1006. *stat = LSM_STATE_ACC_MODE;
  1007. }
  1008. }
  1009. }else{
  1010. *stat = LSM_STATE_ILLEGAL_MODE;
  1011. }
  1012. }
  1013. }
  1014. void lsm6ds3tr_c_powerdown_mode(lsm_device_t dex_num)
  1015. {
  1016. switch_dev(dex_num);
  1017. //关闭qmc6310
  1018. switch(dex_num)
  1019. {
  1020. case LSM_DEV_FRONT:
  1021. if(qmc6310_front_Suspend())SEGGER_RTT_printf(0,"qmc6310_front_Suspend sucess!!\n");
  1022. break;
  1023. case LSM_DEV_CENTER:
  1024. // if(qmc6310_center_Suspend())SEGGER_RTT_printf(0,"qmc6310_center_Suspend sucess!!\n");
  1025. break;
  1026. }
  1027. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  1028. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1029. switch(dex_num)
  1030. {
  1031. case LSM_DEV_FRONT:
  1032. SPI0_Disable();
  1033. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  1034. platform_delay(100);
  1035. break;
  1036. case LSM_DEV_CENTER:
  1037. // SPI1_Disable();
  1038. break;
  1039. }
  1040. }
  1041. void lsm6ds3tr_c_suspend_mode(lsm_device_t dex_num)
  1042. {
  1043. uint16_t timeout = TIMEOUT;
  1044. lsm6ds3tr_c_fifo_mode_t fifo_mode;
  1045. lsm6ds3tr_c_odr_xl_t acc_odr;
  1046. lsm6ds3tr_c_odr_g_t gry_odr;
  1047. switch_dev(dex_num);
  1048. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  1049. //
  1050. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_BANK_A);
  1051. //
  1052. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_BANK_B);
  1053. // /* Restore default configuration */
  1054. // lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  1055. // do {
  1056. // lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  1057. // } while (rst);
  1058. // /* Enable Block Data Update */
  1059. // lsm6ds3tr_c_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  1060. do{
  1061. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, LSM6DS3TR_C_XL_ODR_OFF);
  1062. lsm6ds3tr_c_xl_data_rate_get(&dev_ctx, &acc_odr);
  1063. timeout--;
  1064. }while(acc_odr != LSM6DS3TR_C_XL_ODR_OFF && timeout != 0);
  1065. // if(acc_odr == LSM6DS3TR_C_XL_ODR_OFF && timeout != 0)SEGGER_RTT_printf(0,"acc_odr == LSM6DS3TR_C_XL_ODR_OFF %d sucess!!\n",dex_num);
  1066. timeout = TIMEOUT;
  1067. do{
  1068. lsm6ds3tr_c_gy_data_rate_set(&dev_ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1069. lsm6ds3tr_c_gy_data_rate_get(&dev_ctx,&gry_odr);
  1070. timeout--;
  1071. }while(gry_odr != LSM6DS3TR_C_GY_ODR_OFF && timeout != 0);
  1072. // if(gry_odr == LSM6DS3TR_C_GY_ODR_OFF && timeout != 0)SEGGER_RTT_printf(0,"gry_odr == LSM6DS3TR_C_GY_ODR_OFF %d sucess!!\n",dex_num);
  1073. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  1074. //
  1075. // lsm6ds3tr_c_sh_master_set(&dev_ctx, PROPERTY_DISABLE);
  1076. //
  1077. // lsm6ds3tr_c_func_en_set(&dev_ctx,PROPERTY_DISABLE);
  1078. timeout = TIMEOUT;
  1079. //设置旁路模式用于清空FIFO
  1080. do{
  1081. lsm6ds3tr_c_fifo_mode_set(&dev_ctx, LSM6DS3TR_C_BYPASS_MODE);
  1082. lsm6ds3tr_c_fifo_mode_get(&dev_ctx,&fifo_mode);
  1083. timeout--;
  1084. }while(fifo_mode != LSM6DS3TR_C_BYPASS_MODE && timeout != 0);
  1085. // if(fifo_mode == LSM6DS3TR_C_BYPASS_MODE && timeout != 0)SEGGER_RTT_printf(0,"lsm6ds3tr_c_suspend_mode %d sucess!!\n",dex_num);
  1086. //设置为旁路模式后,在设置为不同的FIFO工作模式之前,必须要等待至少30us
  1087. platform_delay(10);
  1088. }