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 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_standby_power_acc_mode_config(lsm_device_t dex_num)
  551. {
  552. switch_dev(dex_num);
  553. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  554. platform_delay(10);
  555. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  556. platform_delay(10);
  557. /* 6ds3 Accelerometer test parameters */
  558. config_6ds3_xl.enable = PROPERTY_ENABLE;
  559. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  560. config_6ds3_xl.odr_hz_val = 0;
  561. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  562. config_6ds3_xl.decimation = 0;
  563. config_6ds3_xl.samples_num_in_pattern = 0;
  564. /* Check device ID */
  565. whoamI = 0;
  566. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  567. if(whoamI != LSM6DS3TR_C_ID)
  568. {
  569. SEGGER_RTT_printf(0,"lsm6ds3tr_c_low_power_acc_mode_config error:%d!!!\r\n",dex_num);
  570. return;
  571. }
  572. /* Restore default configuration */
  573. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  574. do {
  575. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  576. } while (rst);
  577. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  578. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  579. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  580. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  581. }
  582. void lsm6ds3tr_c_standby_power_acc_mode_start(lsm_device_t dex_num)
  583. {
  584. switch_dev(dex_num);
  585. /* Set XL Output Data Rate */
  586. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  587. }
  588. void lsm6ds3tr_c_standby_power_acc_read(lsm_device_t dex_num, int16_t *acc)
  589. {
  590. switch_dev(dex_num);
  591. /* Read samples in polling mode (no int) */
  592. /* Read output only if new value is available */
  593. lsm6ds3tr_c_reg_t reg;
  594. lsm6ds3tr_c_status_reg_get(&dev_ctx, &reg.status_reg);
  595. if (reg.status_reg.xlda) {
  596. /* Read magnetic field data */
  597. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  598. lsm6ds3tr_c_acceleration_raw_get(&dev_ctx,
  599. data_raw_acceleration.i16bit);
  600. acc[0] = data_raw_acceleration.i16bit[0];
  601. acc[1] = data_raw_acceleration.i16bit[1];
  602. acc[2] = data_raw_acceleration.i16bit[2];
  603. #if DEBUG_IMU
  604. 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]);
  605. #endif
  606. }
  607. }
  608. int16_t lsm6ds3tr_c_fifo_mode_read_group_num(lsm_device_t dex_num, lsm_mode_t fifo_mode)
  609. {
  610. uint8_t wt;
  611. uint16_t num = 0;
  612. uint16_t num_pattern = 0;
  613. switch_dev(dex_num);
  614. /* Read FIFO watermark flag in polling mode */
  615. lsm6ds3tr_c_fifo_wtm_flag_get(&dev_ctx, &wt);
  616. if(wt)
  617. {
  618. /* Read number of word in FIFO */
  619. lsm6ds3tr_c_fifo_data_level_get(&dev_ctx, &num);
  620. // SEGGER_RTT_printf(0,"---------->dex_num:%d num:%d!!!\r\n",dex_num,num);
  621. switch(fifo_mode)
  622. {
  623. case LSM_STATE_ACC_MODE:
  624. break;
  625. case LSM_STATE_ACC_QMC_FIFO_MODE:
  626. num_pattern = num / acc_mag_timestamp_pattern_len;
  627. break;
  628. case LSM_STATE_GRY_ACC_QMC_FIFO_MODE:
  629. num_pattern = num / gry_acc_mag_timestamp_pattern_len;
  630. break;
  631. case LSM_STATE_ILLEGAL_MODE:
  632. num_pattern = 0;
  633. break;
  634. }
  635. }
  636. return num_pattern;
  637. }
  638. void lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_config(lsm_device_t dex_num)
  639. {
  640. switch_dev(dex_num);
  641. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  642. platform_delay(10);
  643. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  644. platform_delay(10);
  645. /* 6ds3 Accelerometer test parameters */
  646. config_6ds3_xl.enable = PROPERTY_ENABLE;
  647. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  648. config_6ds3_xl.odr_hz_val = 0;
  649. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  650. config_6ds3_xl.decimation = 0;
  651. config_6ds3_xl.samples_num_in_pattern = 0;
  652. /* Check device ID */
  653. whoamI = 0;
  654. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  655. if(whoamI != LSM6DS3TR_C_ID)
  656. {
  657. SEGGER_RTT_printf(0,"lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode error:%d!!!\r\n",dex_num);
  658. return;
  659. }
  660. /* Restore default configuration */
  661. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  662. do {
  663. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  664. } while (rst);
  665. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  666. /*qmc6310-------------------------------------------------------------*/
  667. /* Some hardware require to enable pull up on master I2C interface */
  668. lsm6ds3tr_c_sh_pin_mode_set(&dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  669. platform_delay(10);
  670. //qmc6310初始化
  671. switch(dex_num)
  672. {
  673. case LSM_DEV_FRONT:
  674. if(qmc6310_front_Init(QMC6310_100HZ))SEGGER_RTT_printf(0,"qmc6310_front_Init sucess!!\n");
  675. else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_front_Init fail!!\n");return;}
  676. break;
  677. case LSM_DEV_CENTER:
  678. // if(qmc6310_center_Init(QMC6310_100HZ))SEGGER_RTT_printf(0,"qmc6310_center_Init sucess!!\n");
  679. // else {lsm6ds3tr_c_suspend_mode(dex_num);SEGGER_RTT_printf(0,"qmc6310_center_Init fail!!\n");return;}
  680. break;
  681. }
  682. uint8_t data = 0xFF,timeout = 3;
  683. do{
  684. lsm6ds3tr_c_sh_slv0_cfg_read(&dev_ctx, &qmc6310); //这里只有一个地磁,但不能用slv1,只能用slv0,不知道是不是要先配置slv0后,才能配置slv1。
  685. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_BANK_A);
  686. lsm6ds3tr_c_read_reg(&dev_ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  687. // SEGGER_RTT_printf(0,"lsm6ds3tr_c_read_reg LSM6DS3TR_C_SLV0_SUBADD:%x!!!\r\n",data);
  688. platform_delay(100);
  689. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  690. timeout--;
  691. }while(data != QMC6310_DATA_OUT_X_LSB_REG && timeout != 0);
  692. /* Configure Sensor Hub to read two slaves */
  693. lsm6ds3tr_c_sh_num_of_dev_connected_set(&dev_ctx, LSM6DS3TR_C_SLV_0_1_2_3);
  694. lsm6ds3tr_c_func_en_set(&dev_ctx,PROPERTY_ENABLE);
  695. lsm6ds3tr_c_sh_master_set(&dev_ctx, PROPERTY_ENABLE);
  696. /*acc-------------------------------------------------------------*/
  697. lsm6ds3tr_c_xl_power_mode_set(&dev_ctx,LSM6DS3TR_C_XL_NORMAL);
  698. lsm6ds3tr_c_xl_full_scale_set(&dev_ctx, config_6ds3_xl.fs);
  699. lsm6ds3tr_c_gy_sleep_mode_set(&dev_ctx, 1);
  700. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&dev_ctx,PROPERTY_ENABLE); //使能开关
  701. /* Set High Resolution Timestamp (25 us tick) */
  702. lsm6ds3tr_c_timestamp_res_set(&dev_ctx, LSM6DS3TR_C_LSB_25us);
  703. /* Enable timestamp in HW */
  704. lsm6ds3tr_c_timestamp_set(&dev_ctx, PROPERTY_ENABLE);
  705. /* Set FIFO watermark to a multiple of a pattern */
  706. lsm6ds3tr_c_fifo_watermark_set(&dev_ctx, acc_mag_timestamp_pattern_len);//pattern_len
  707. /* Set FIFO mode to Stream mode */
  708. lsm6ds3tr_c_fifo_mode_set(&dev_ctx, LSM6DS3TR_C_STREAM_MODE);
  709. /* Set ODR FIFO */
  710. lsm6ds3tr_c_fifo_data_rate_set(&dev_ctx, USED_FIFO_104_HZ);//该参数关联到timestamp
  711. /* Set FIFO sensor decimator */
  712. lsm6ds3tr_c_fifo_xl_batch_set(&dev_ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//因为acc和fifo都是104HZ,所以不用抽取因子。
  713. lsm6ds3tr_c_fifo_dataset_3_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//因为mag和fifo都是104HZ,所以不用抽取因子。
  714. lsm6ds3tr_c_fifo_dataset_4_batch_set(&dev_ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  715. }
  716. void lsm6ds3tr_c_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_start(lsm_device_t dex_num)
  717. {
  718. switch_dev(dex_num);
  719. /* Set XL Output Data Rate */
  720. lsm6ds3tr_c_xl_data_rate_set(&dev_ctx, config_6ds3_xl.odr);
  721. }
  722. 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)
  723. {
  724. uint16_t num_pattern = 0;
  725. int16_t cur_count = 0;
  726. switch_dev(dex_num);
  727. if(group_num <= 0)return 0;
  728. num_pattern = group_num;
  729. if (num_pattern)
  730. {
  731. // SEGGER_RTT_printf(0,"acc_mag_timestamp_pattern_len:%d num_pattern:%d\r\n",acc_mag_timestamp_pattern_len,num_pattern);
  732. while (num_pattern-- > 0){
  733. // 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);
  734. /* FIFO pattern is composed by gy_num gyroscope triplets and
  735. * xl_num accelerometer triplets. The sequence has always following order:
  736. * gyro first, accelerometer second , mag third
  737. */
  738. /* Read XL samples */
  739. if (config_6ds3_xl.enable)
  740. {
  741. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_acceleration.u8bit,
  742. ACC_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  743. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_magnetic.u8bit,
  744. MAG_OUT_XYZ_WORD_SIZE * sizeof(int16_t));
  745. /* Read timestamp samples */
  746. lsm6ds3tr_c_fifo_raw_data_get(&dev_ctx, data_raw_timestamp.u8bit,TIMESTAMP_OUT_WORD_SIZE * sizeof(int16_t));
  747. if(cur_count != group_num )
  748. {
  749. acc[cur_count*3 + 0] = data_raw_acceleration.i16bit[0];
  750. acc[cur_count*3 + 1] = data_raw_acceleration.i16bit[1];
  751. acc[cur_count*3 + 2] = data_raw_acceleration.i16bit[2];
  752. QMC6310_map c_map;
  753. switch(dex_num)
  754. {
  755. case LSM_DEV_FRONT:
  756. c_map = qmc6310_front_GetMap();
  757. break;
  758. case LSM_DEV_CENTER:
  759. // c_map = qmc6310_center_GetMap();
  760. break;
  761. }
  762. mag[cur_count*3 + 0] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[0]]);
  763. mag[cur_count*3 + 1] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[1]]);
  764. mag[cur_count*3 + 2] = (int16_t)(c_map.sign[0]*data_raw_magnetic.i16bit[c_map.map[2]]);
  765. /* 时间戳和计步数数据
  766. 字节1 字节2 字节3 字节4 字节5 字节6
  767. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  768. [15:8] [23:16] [7:0] [7:0] [15:8]
  769. */
  770. timestamp[cur_count] = ((data_raw_timestamp.u8bit[1] << 16) | (data_raw_timestamp.u8bit[0] << 8) | (data_raw_timestamp.u8bit[3]))*25;//counter == 25us
  771. cur_count++;
  772. }
  773. }
  774. }
  775. }
  776. return cur_count;
  777. }
  778. void lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode_config(lsm_device_t dex_num)
  779. {
  780. switch_dev(dex_num);
  781. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  782. platform_delay(10);
  783. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  784. platform_delay(10);
  785. /* 6ds3 Accelerometer test parameters */
  786. config_6ds3_xl.enable = PROPERTY_ENABLE;
  787. config_6ds3_xl.odr = USED_ACC_FIFO_104_HZ;
  788. config_6ds3_xl.odr_hz_val = 0;
  789. config_6ds3_xl.fs = LSM6DS3TR_C_16g;
  790. config_6ds3_xl.decimation = 0;
  791. config_6ds3_xl.samples_num_in_pattern = 0;
  792. /* 6ds3 Gyroscope test parameters */
  793. config_6ds3_gyro.enable = PROPERTY_ENABLE;
  794. config_6ds3_gyro.odr = USED_GRY_FIFO_104_HZ;
  795. config_6ds3_gyro.odr_hz_val = 0;
  796. config_6ds3_gyro.fs = LSM6DS3TR_C_2000dps;
  797. config_6ds3_gyro.decimation = 0;
  798. config_6ds3_gyro.samples_num_in_pattern = 0;
  799. // lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  800. /* Check device ID */
  801. whoamI = 0;
  802. lsm6ds3tr_c_device_id_get(&dev_ctx, &whoamI);
  803. if(whoamI != LSM6DS3TR_C_ID)
  804. {
  805. SEGGER_RTT_printf(0,"lsm6ds3tr_c_gry_acc_sensor_hub_qmc6310_fifo_with_timestamp_mode error:%d!!!\r\n",dex_num);
  806. return;
  807. }
  808. /* Restore default configuration */
  809. lsm6ds3tr_c_reset_set(&dev_ctx, PROPERTY_ENABLE);
  810. do {
  811. lsm6ds3tr_c_reset_get(&dev_ctx, &rst);
  812. } while (rst);
  813. lsm6ds3tr_c_mem_bank_set(&dev_ctx, LSM6DS3TR_C_USER_BANK);
  814. /*qmc6310-------------------------------------------------------------*/
  815. /* Some hardware require to enable pull up on master I2C interface */
  816. lsm6ds3tr_c_sh_pin_mode_set(&dev_ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  817. platform_delay(10);
  818. //qmc6310初始化
  819. switch(dex_num)
  820. {
  821. case LSM_DEV_FRONT:
  822. if(qmc6310_front_Init(QMC6310_100HZ))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. }