lsm6ds3tr_c.c 41 KB

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