lsm6ds3tr_c.c 43 KB

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