drv_lsm6ds3tr_c.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*Includes ----------------------------------------------*/
  2. #include "bsp_spi.h"
  3. #include "nrf_delay.h"
  4. #include "drv_lsm6ds3tr_c.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 OP_TIMEOUT 200 //最大操作次数
  19. #define ACC_OUT_XYZ_WORD_NUM 3 //加速度三轴数据的字个数
  20. #define GRY_OUT_XYZ_WORD_NUM 3 //陀螺仪三轴数据的字个数
  21. #define MAG_OUT_XYZ_WORD_NUM 3 //地磁计三轴数据的字个数
  22. #define TIMESTAMP_OUT_WORD_NUM 3 //FIFO时间戳数据的字个数
  23. #define QMC6310_DATA_OUT_X_LSB_REG 0x01 //qmc6310地磁数据的寄存器
  24. #define HUB_MAG_DEV_ADDR 0x1C //挂载的地磁设备地址
  25. /*STRUCTION -----------------------------------------------------*/
  26. typedef union
  27. {
  28. int16_t i16bit[3];
  29. uint8_t u8bit[6];
  30. } axis3bit16_u;
  31. typedef struct drv_lsm6ds3tr_c
  32. {
  33. stmdev_ctx_t ctx; //抽象层接口(如I2C/SPI,外部不可操作,属于私有成员)
  34. lsm6ds3tr_c_emb_sh_read_t raw_data; //用于hub读取数据(外部不可操作,属于私有成员)
  35. lsm6ds3tr_c_all_sources_t endop; //用于hub判断是否操作完成
  36. uint32_t op_timeout; //最大操作次数
  37. lsm_data_t cur_data; //当前LSM数据
  38. drv_lsm_config_param_t cur_param; //当前LSM配置
  39. } Drv_Lsm6ds3tr_c_t;
  40. /*Local Variable ----------------------------------------------*/
  41. static lsm6ds3tr_c_sh_cfg_read_t sub_qmc6310 = {
  42. .slv_add = HUB_MAG_DEV_ADDR, //挂载的qmc6310设备地址
  43. .slv_subadd = QMC6310_DATA_OUT_X_LSB_REG, //预备读取qmc6310地磁数据的寄存器
  44. .slv_len = MAG_OUT_XYZ_WORD_NUM * 2, //转为字节长度
  45. };
  46. static Drv_Lsm6ds3tr_c_t ob_lsm6ds3tr_c;
  47. static axis3bit16_u data_raw_acceleration; //临时存储加速度原始数据
  48. static axis3bit16_u data_raw_angular_rate; //临时存储陀螺仪原始数据
  49. static axis3bit16_u data_raw_magnetic; //临时存储地磁计原始数据
  50. static axis3bit16_u data_raw_timestamp; //临时存储时间戳原始数据
  51. /*Local Functions ----------------------------------------------*/
  52. static void platform_delay_ms(uint32_t ms)
  53. {
  54. nrf_delay_ms(ms);
  55. }
  56. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  57. {
  58. int32_t ierror = 0;
  59. if(SPI0_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg, (uint8_t *)bufp, len))
  60. {
  61. ierror = -1;
  62. }
  63. return ierror;
  64. }
  65. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  66. {
  67. int32_t ierror = 0;
  68. if(SPI0_OnlyReadReg(BOARD_SPI0_CS0_IO, reg, bufp, len))
  69. {
  70. ierror = -1;
  71. }
  72. return ierror;
  73. }
  74. static int read_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint16_t len)
  75. {
  76. int32_t ret = -1;
  77. uint8_t drdy;
  78. static int flow_ctl = 0; //流程控制
  79. lsm6ds3tr_c_sh_cfg_read_t val = {
  80. .slv_add = device_addr,
  81. .slv_subadd = reg,
  82. .slv_len = len,
  83. };
  84. switch(flow_ctl)
  85. {
  86. case 0:
  87. /* Disable accelerometer */
  88. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  89. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  90. /* Configure Sensor Hub to read QMC6310 */
  91. /* Prepare sensor hub to read data from external Slave0 */
  92. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &val);
  93. /* Configure Sensor Hub to read two slaves */
  94. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  95. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  96. /* Enable I2C Master and I2C master Pull Up */
  97. /* Enable master and XL trigger */
  98. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  99. /* Wait Sensor Hub operation flag set */
  100. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.i16bit);
  101. /* Enable accelerometer to trigger Sensor Hub operation */
  102. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  103. /* Wait Sensor Hub operation flag set */
  104. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.i16bit);
  105. flow_ctl = 1;
  106. break;
  107. case 1:
  108. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  109. if(drdy == 0)return -1;
  110. flow_ctl = 2;
  111. break;
  112. case 2:
  113. lsm6ds3tr_c_all_sources_get(&ob_lsm6ds3tr_c.ctx,&ob_lsm6ds3tr_c.endop);
  114. if(ob_lsm6ds3tr_c.endop.func_src1.sensorhub_end_op == 0)return -1;
  115. flow_ctl = 3;
  116. break;
  117. case 3:
  118. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  119. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  120. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  121. lsm6ds3tr_c_sh_read_data_raw_get(&ob_lsm6ds3tr_c.ctx, &ob_lsm6ds3tr_c.raw_data);
  122. memcpy(data,&ob_lsm6ds3tr_c.raw_data,len);
  123. flow_ctl = 0;
  124. ret = 0;
  125. break;
  126. }
  127. return ret;
  128. }
  129. static int write_hub(uint8_t device_addr, uint8_t reg, uint8_t* data)
  130. {
  131. int32_t ret = -1;
  132. uint8_t drdy;
  133. static int flow_ctl = 0; //流程控制
  134. lsm6ds3tr_c_sh_cfg_write_t val = {
  135. .slv0_add = device_addr,
  136. .slv0_subadd = reg,
  137. .slv0_data = *data,
  138. };
  139. switch(flow_ctl)
  140. {
  141. case 0:
  142. /* Disable accelerometer */
  143. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  144. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  145. /* Configure Sensor Hub to write */
  146. lsm6ds3tr_c_sh_cfg_write(&ob_lsm6ds3tr_c.ctx, &val);
  147. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  148. /* Enable I2C Master and I2C master Pull Up */
  149. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  150. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.i16bit);
  151. /* Enable accelerometer to trigger Sensor Hub operation */
  152. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_208Hz);
  153. /* Wait Sensor Hub operation flag set */
  154. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.i16bit);
  155. flow_ctl = 1;
  156. break;
  157. case 1:
  158. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  159. if(drdy == 0)return -1;
  160. flow_ctl = 2;
  161. break;
  162. case 2:
  163. lsm6ds3tr_c_all_sources_get(&ob_lsm6ds3tr_c.ctx,&ob_lsm6ds3tr_c.endop);
  164. if(ob_lsm6ds3tr_c.endop.func_src1.sensorhub_end_op == 0)return -1;
  165. flow_ctl = 3;
  166. break;
  167. case 3:
  168. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  169. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  170. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  171. flow_ctl = 0;
  172. ret = 0;
  173. break;
  174. }
  175. return ret;
  176. }
  177. static int self_check(void)
  178. {
  179. uint8_t whoamI, rst;
  180. int16_t data_raw[3];
  181. float val_st_off[3];
  182. float val_st_on[3];
  183. float test_val[3];
  184. uint8_t st_result;
  185. uint8_t drdy;
  186. uint8_t i;
  187. uint8_t j;
  188. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  189. /* Wait sensor boot time */
  190. platform_delay_ms(BOOT_TIME);
  191. /* Check device ID */
  192. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  193. if(whoamI != LSM6DS3TR_C_ID)
  194. {
  195. SEGGER_RTT_printf(0,"LSM self_check error!!!\r\n");
  196. return -1;
  197. }
  198. /* Restore default configuration */
  199. lsm6ds3tr_c_reset_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  200. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  201. do {
  202. lsm6ds3tr_c_reset_get(&ob_lsm6ds3tr_c.ctx, &rst);
  203. ob_lsm6ds3tr_c.op_timeout--;
  204. } while(rst && ob_lsm6ds3tr_c.op_timeout!=0);
  205. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  206. /* Accelerometer Self Test */
  207. /* Set Output Data Rate */
  208. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_52Hz);
  209. /* Set full scale */
  210. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_4g);
  211. /* Wait stable output */
  212. platform_delay_ms(WAIT_TIME_A);
  213. /* Check if new value available */
  214. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  215. do {
  216. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  217. ob_lsm6ds3tr_c.op_timeout--;
  218. } while(!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  219. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  220. /* Read dummy data and discard it */
  221. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  222. /* Read 5 sample and get the average vale for each axis */
  223. memset(val_st_off, 0x00, 3 * sizeof(float));
  224. for (i = 0; i < 5; i++) {
  225. /* Check if new value available */
  226. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  227. do {
  228. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  229. ob_lsm6ds3tr_c.op_timeout--;
  230. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  231. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  232. /* Read data and accumulate the mg value */
  233. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  234. for (j = 0; j < 3; j++) {
  235. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  236. }
  237. }
  238. /* Calculate the mg average values */
  239. for (i = 0; i < 3; i++) {
  240. val_st_off[i] /= 5.0f;
  241. }
  242. /* Enable Self Test positive (or negative) */
  243. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  244. /* Wait stable output */
  245. platform_delay_ms(WAIT_TIME_A);
  246. /* Check if new value available */
  247. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  248. do {
  249. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  250. ob_lsm6ds3tr_c.op_timeout--;
  251. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  252. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  253. /* Read dummy data and discard it */
  254. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  255. /* Read 5 sample and get the average vale for each axis */
  256. memset(val_st_on, 0x00, 3 * sizeof(float));
  257. for (i = 0; i < 5; i++) {
  258. /* Check if new value available */
  259. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  260. do {
  261. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  262. ob_lsm6ds3tr_c.op_timeout--;
  263. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  264. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  265. /* Read data and accumulate the mg value */
  266. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  267. for (j = 0; j < 3; j++) {
  268. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  269. }
  270. }
  271. /* Calculate the mg average values */
  272. for (i = 0; i < 3; i++) {
  273. val_st_on[i] /= 5.0f;
  274. }
  275. /* Calculate the mg values for self test */
  276. for (i = 0; i < 3; i++) {
  277. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  278. }
  279. /* Check self test limit */
  280. st_result = ST_PASS;
  281. for (i = 0; i < 3; i++) {
  282. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  283. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  284. st_result = ST_FAIL;
  285. }
  286. }
  287. /* Disable Self Test */
  288. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  289. /* Disable sensor. */
  290. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  291. /* Gyroscope Self Test. */
  292. /* Set Output Data Rate */
  293. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_208Hz);
  294. /* Set full scale */
  295. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  296. /* Wait stable output */
  297. platform_delay_ms(WAIT_TIME_G_01);
  298. /* Check if new value available */
  299. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  300. do {
  301. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  302. ob_lsm6ds3tr_c.op_timeout--;
  303. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  304. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  305. /* Read dummy data and discard it */
  306. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  307. /* Read 5 sample and get the average vale for each axis */
  308. memset(val_st_off, 0x00, 3 * sizeof(float));
  309. for (i = 0; i < 5; i++) {
  310. /* Check if new value available */
  311. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  312. do {
  313. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  314. ob_lsm6ds3tr_c.op_timeout--;
  315. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  316. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  317. /* Read data and accumulate the mg value */
  318. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  319. for (j = 0; j < 3; j++) {
  320. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  321. 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_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  330. /* Wait stable output */
  331. platform_delay_ms(WAIT_TIME_G_02);
  332. /* Read 5 sample and get the average vale for each axis */
  333. memset(val_st_on, 0x00, 3 * sizeof(float));
  334. for (i = 0; i < 5; i++) {
  335. /* Check if new value available */
  336. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  337. do {
  338. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  339. ob_lsm6ds3tr_c.op_timeout--;
  340. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  341. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  342. /* Read data and accumulate the mg value */
  343. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  344. for (j = 0; j < 3; j++) {
  345. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  346. data_raw[j]);
  347. }
  348. }
  349. /* Calculate the mg average values */
  350. for (i = 0; i < 3; i++) {
  351. val_st_on[i] /= 5.0f;
  352. }
  353. /* Calculate the mg values for self test */
  354. for (i = 0; i < 3; i++) {
  355. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  356. }
  357. /* Check self test limit */
  358. for (i = 0; i < 3; i++) {
  359. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  360. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  361. st_result = ST_FAIL;
  362. }
  363. }
  364. /* Disable Self Test */
  365. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  366. /* Disable sensor. */
  367. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  368. if (st_result == ST_PASS) {
  369. SEGGER_RTT_printf(0,"LSM Self Test - PASS\r\n");
  370. }else{
  371. SEGGER_RTT_printf(0,"Self Test - FAIL\r\n");
  372. return -1;
  373. }
  374. return 0;
  375. }
  376. /*API ----------------------------------------------*/
  377. /**
  378. @brief 初始化LSM驱动
  379. @param 无
  380. @return 错误代码 - [out] -1失败,0成功
  381. */
  382. int drv_lsm_Init(void)
  383. {
  384. //初始化结构体
  385. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  386. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  387. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  388. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  389. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  390. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  391. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  392. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  393. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  394. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  395. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  396. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  397. ob_lsm6ds3tr_c.ctx.read_reg = platform_read;
  398. ob_lsm6ds3tr_c.ctx.write_reg = platform_write;
  399. //供电
  400. nrf_gpio_cfg(
  401. PIN_FRONT_SENSE_POWER,
  402. NRF_GPIO_PIN_DIR_OUTPUT,
  403. NRF_GPIO_PIN_INPUT_DISCONNECT,
  404. NRF_GPIO_PIN_NOPULL,
  405. NRF_GPIO_PIN_H0H1,
  406. NRF_GPIO_PIN_NOSENSE);
  407. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  408. nrf_gpio_cfg_output(BOARD_SPI0_MOSI_IO);
  409. nrf_gpio_cfg_output(BOARD_SPI0_CLK_IO);
  410. nrf_gpio_cfg_output(PIN_FRONT_SPI_nCS);
  411. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  412. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  413. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  414. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  415. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  416. platform_delay_ms(3);
  417. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  418. platform_delay_ms(3);
  419. //初始化SPI接口
  420. SPI0_Init();
  421. //开始自检
  422. return self_check();
  423. }
  424. /**
  425. @brief LSM挂起
  426. @param 无
  427. @return 错误代码 - [out] -1失败,0成功
  428. */
  429. int drv_lsm_suspend(void)
  430. {
  431. //关闭SPI
  432. SPI0_Disable();
  433. //断电,清空lsm配置
  434. nrf_gpio_cfg(
  435. PIN_FRONT_SENSE_POWER,
  436. NRF_GPIO_PIN_DIR_OUTPUT,
  437. NRF_GPIO_PIN_INPUT_DISCONNECT,
  438. NRF_GPIO_PIN_NOPULL,
  439. NRF_GPIO_PIN_H0H1,
  440. NRF_GPIO_PIN_NOSENSE);
  441. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  442. nrf_gpio_cfg_output(BOARD_SPI0_MOSI_IO);
  443. nrf_gpio_cfg_output(BOARD_SPI0_CLK_IO);
  444. nrf_gpio_cfg_output(PIN_FRONT_SPI_nCS);
  445. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  446. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  447. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  448. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  449. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  450. platform_delay_ms(5);
  451. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  452. platform_delay_ms(5);
  453. //初始化结构体
  454. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  455. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  456. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  457. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  458. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  459. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  460. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  461. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  462. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  463. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  464. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  465. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  466. //重新初始化SPI
  467. SPI0_Init();
  468. return 0;
  469. }
  470. /**
  471. @brief 设置加速度工作模式
  472. @param acc_power_mode - [in] 加速度工作模式
  473. @return 错误代码 - [out] -1失败,0成功
  474. */
  475. int drv_lsm_set_acc_power_mode(LSM_ACC_POWER_MODE_e acc_power_mode)
  476. {
  477. lsm6ds3tr_c_xl_hm_mode_t val;
  478. if(ob_lsm6ds3tr_c.cur_param.acc_power_mode != acc_power_mode)
  479. {
  480. switch(acc_power_mode)
  481. {
  482. case LSM_ACC_POWER_MODE_HIGH_PERFORMANCE:
  483. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_HIGH_PERFORMANCE);
  484. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  485. if(val != LSM6DS3TR_C_XL_HIGH_PERFORMANCE)return -1;
  486. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  487. break;
  488. case LSM_ACC_POWER_MODE_NORMAL:
  489. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_NORMAL);
  490. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  491. if(val != LSM6DS3TR_C_XL_NORMAL)return -1;
  492. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_NORMAL;
  493. break;
  494. }
  495. }
  496. return 0;
  497. }
  498. /**
  499. @brief 设置陀螺仪工作模式
  500. @param gry_power_mode - [in] 陀螺仪工作模式
  501. @return 错误代码 - [out] -1失败,0成功
  502. */
  503. int drv_lsm_set_gry_power_mode(LSM_GRY_POWER_MODE_e gry_power_mode)
  504. {
  505. lsm6ds3tr_c_g_hm_mode_t val;
  506. if(ob_lsm6ds3tr_c.cur_param.gry_power_mode != gry_power_mode)
  507. {
  508. switch(gry_power_mode)
  509. {
  510. case LSM_GRY_POWER_MODE_HIGH_PERFORMANCE:
  511. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
  512. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  513. if(val != LSM_GRY_POWER_MODE_HIGH_PERFORMANCE)return -1;
  514. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  515. break;
  516. case LSM_GRY_POWER_MODE_NORMAL:
  517. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_NORMAL);
  518. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  519. if(val != LSM_GRY_POWER_MODE_NORMAL)return -1;
  520. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_NORMAL;
  521. break;
  522. }
  523. }
  524. return 0;
  525. }
  526. /**
  527. @brief 设置时间戳精度
  528. @param timestamp_resolution - [in] 时间戳精度
  529. @return 错误代码 - [out] -1失败,0成功
  530. */
  531. int drv_lsm_set_timestamp_resolution(LSM_TIMESTAMP_RESOLUTION_e timestamp_resolution)
  532. {
  533. lsm6ds3tr_c_timer_hr_t val;
  534. if(ob_lsm6ds3tr_c.cur_param.timestamp_resolution != timestamp_resolution)
  535. {
  536. switch(timestamp_resolution)
  537. {
  538. case LSM_TIMESTAMP_6MS4:
  539. /* Set High Resolution Timestamp (6.4ms) */
  540. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_6ms4);
  541. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  542. if(val != LSM6DS3TR_C_LSB_6ms4)return -1;
  543. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  544. break;
  545. case LSM_TIMESTAMP_25US:
  546. /* Set High Resolution Timestamp (25 us tick) */
  547. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_25us);
  548. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  549. if(val != LSM6DS3TR_C_LSB_25us)return -1;
  550. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_25US;
  551. break;
  552. }
  553. }
  554. return 0;
  555. }
  556. /**
  557. @brief 设置时间戳开关
  558. @param timestamp_switch - [in] 时间戳开关
  559. @return 错误代码 - [out] -1失败,0成功
  560. */
  561. int drv_lsm_set_timestamp_switch(LSM_TIMESTAMP_SWITCH_e timestamp_switch)
  562. {
  563. uint8_t val;
  564. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch != timestamp_switch)
  565. {
  566. switch(timestamp_switch)
  567. {
  568. case LSM_TIMESTAMP_ON:
  569. /* Enable timestamp in HW */
  570. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  571. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  572. if(val != PROPERTY_ENABLE)return -1;
  573. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_ON;
  574. break;
  575. case LSM_TIMESTAMP_OFF:
  576. /*Disable timestamp in HW */
  577. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  578. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  579. if(val != PROPERTY_DISABLE)return -1;
  580. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  581. break;
  582. }
  583. }
  584. return 0;
  585. }
  586. /**
  587. @brief 设置FIFO采样频率
  588. @param fifo_odr - [in] FIFO采样频率
  589. @return 错误代码 - [out] -1失败,0成功
  590. */
  591. int drv_lsm_set_fifo_odr(LSM_FIFO_ODR_e fifo_odr)
  592. {
  593. uint8_t val;
  594. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  595. lsm6ds3tr_c_fifo_mode_t r_fifo_mode;
  596. lsm6ds3tr_c_dec_fifo_xl_t r_fifo_xl;
  597. lsm6ds3tr_c_dec_fifo_gyro_t r_fifo_gy;
  598. lsm6ds3tr_c_dec_ds3_fifo_t r_fifo_ds3;
  599. lsm6ds3tr_c_dec_ds4_fifo_t r_fifo_ds4;
  600. uint16_t fifo_watermark;
  601. uint16_t pattern_len = 0;
  602. if(ob_lsm6ds3tr_c.cur_param.fifo_odr != fifo_odr)
  603. {
  604. switch(fifo_odr)
  605. {
  606. case LSM_FIFO_ODR_OFF:
  607. //禁用步进计数器和时间戳数据作为第4步FIFO数据集
  608. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  609. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  610. if(val != PROPERTY_DISABLE)return -1;
  611. /* Set ODR FIFO */
  612. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  613. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  614. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  615. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  616. break;
  617. case LSM_FIFO_ODR_104HZ:
  618. //启用步进计数器和时间戳数据作为第4步FIFO数据集
  619. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  620. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  621. if(val != PROPERTY_ENABLE)return -1;
  622. /* Set FIFO mode to Stream mode */
  623. lsm6ds3tr_c_fifo_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_STREAM_MODE);
  624. lsm6ds3tr_c_fifo_mode_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_mode);
  625. if(r_fifo_mode != LSM6DS3TR_C_STREAM_MODE)return -1;
  626. /* Set FIFO sensor decimator */
  627. if(ob_lsm6ds3tr_c.cur_param.acc_odr == LSM_ACC_ODR_104HZ)
  628. {
  629. lsm6ds3tr_c_fifo_xl_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//acc和fifo的HZ一致
  630. lsm6ds3tr_c_fifo_xl_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_xl);
  631. if(r_fifo_xl != LSM6DS3TR_C_FIFO_XL_NO_DEC)return -1;
  632. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  633. }
  634. if(ob_lsm6ds3tr_c.cur_param.gry_odr == LSM_GRY_ODR_104HZ)
  635. {
  636. lsm6ds3tr_c_fifo_gy_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_GY_NO_DEC);//gry和fifo的HZ一致
  637. lsm6ds3tr_c_fifo_gy_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_gy);
  638. if(r_fifo_gy != LSM6DS3TR_C_FIFO_GY_NO_DEC)return -1;
  639. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  640. }
  641. if(ob_lsm6ds3tr_c.cur_param.mag_odr == LSM_MAG_ODR_100HZ)
  642. {
  643. lsm6ds3tr_c_fifo_dataset_3_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//mag和fifo的HZ一致
  644. lsm6ds3tr_c_fifo_dataset_3_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds3);
  645. if(r_fifo_ds3 != LSM6DS3TR_C_FIFO_DS3_NO_DEC)return -1;
  646. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  647. }
  648. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON)
  649. {
  650. lsm6ds3tr_c_fifo_dataset_4_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  651. lsm6ds3tr_c_fifo_dataset_4_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds4);
  652. if(r_fifo_ds4 != LSM6DS3TR_C_FIFO_DS4_NO_DEC)return -1;
  653. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  654. }
  655. /* Set FIFO watermark to a multiple of a pattern */
  656. lsm6ds3tr_c_fifo_watermark_set(&ob_lsm6ds3tr_c.ctx, pattern_len);
  657. lsm6ds3tr_c_fifo_watermark_get(&ob_lsm6ds3tr_c.ctx,&fifo_watermark);
  658. if(fifo_watermark != pattern_len)return -1;
  659. /* Set ODR FIFO */
  660. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_104Hz);//该参数关联到timestamp
  661. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  662. if(r_fifo_odr != LSM6DS3TR_C_FIFO_104Hz) return -1;
  663. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_104HZ;
  664. break;
  665. }
  666. }
  667. return 0;
  668. }
  669. /**
  670. @brief 设置ACC量程
  671. @param acc_fs - [in] ACC量程
  672. @return 错误代码 - [out] -1失败,0成功
  673. */
  674. int drv_lsm_set_acc_fs(LSM_ACC_FS_e acc_fs)
  675. {
  676. lsm6ds3tr_c_fs_xl_t val;
  677. if(ob_lsm6ds3tr_c.cur_param.acc_fs != acc_fs)
  678. {
  679. switch(acc_fs)
  680. {
  681. case LSM_ACC_FS_2G:
  682. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2g);
  683. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  684. if(val != LSM6DS3TR_C_2g)return -1;
  685. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  686. break;
  687. case LSM_ACC_FS_16G:
  688. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_16g);
  689. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  690. if(val != LSM6DS3TR_C_16g)return -1;
  691. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_16G;
  692. break;
  693. }
  694. }
  695. return 0;
  696. }
  697. /**
  698. @brief 设置ACC采样频率
  699. @param acc_odr - [in] ACC采样频率
  700. @return 错误代码 - [out] -1失败,0成功
  701. */
  702. int drv_lsm_set_acc_odr(LSM_ACC_ODR_e acc_odr)
  703. {
  704. lsm6ds3tr_c_odr_xl_t val;
  705. if(ob_lsm6ds3tr_c.cur_param.acc_odr != acc_odr)
  706. {
  707. switch(acc_odr)
  708. {
  709. case LSM_ACC_ODR_OFF:
  710. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  711. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  712. if(val != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  713. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  714. break;
  715. case LSM_ACC_ODR_104HZ:
  716. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  717. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  718. if(val != LSM6DS3TR_C_XL_ODR_104Hz)return -1;
  719. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_104HZ;
  720. break;
  721. case LSM_ACC_ODR_12HZ5:
  722. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  723. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  724. if(val != LSM6DS3TR_C_XL_ODR_12Hz5)return -1;
  725. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_12HZ5;
  726. break;
  727. }
  728. }
  729. return 0;
  730. }
  731. /**
  732. @brief 设置GRY量程
  733. @param gry_fs - [in] GRY量程
  734. @return 错误代码 - [out] -1失败,0成功
  735. */
  736. int drv_lsm_set_gry_fs(LSM_GRY_FS_e gry_fs)
  737. {
  738. lsm6ds3tr_c_fs_g_t val;
  739. if(ob_lsm6ds3tr_c.cur_param.gry_fs != gry_fs)
  740. {
  741. switch(gry_fs)
  742. {
  743. case LSM_GRY_FS_250DPS:
  744. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_250dps);
  745. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  746. if(val != LSM6DS3TR_C_250dps)return -1;
  747. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  748. break;
  749. case LSM_GRY_FS_2000DPS:
  750. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  751. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  752. if(val != LSM6DS3TR_C_2000dps)return -1;
  753. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_2000DPS;
  754. break;
  755. }
  756. }
  757. return 0;
  758. }
  759. /**
  760. @brief 设置GRY采样频率
  761. @param gry_odr - [in] GRY采样频率
  762. @return 错误代码 - [out] -1失败,0成功
  763. */
  764. int drv_lsm_set_gry_odr(LSM_GRY_ODR_e gry_odr)
  765. {
  766. lsm6ds3tr_c_odr_g_t val;
  767. if(ob_lsm6ds3tr_c.cur_param.gry_odr != gry_odr)
  768. {
  769. switch(gry_odr)
  770. {
  771. case LSM_GRY_ODR_OFF:
  772. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  773. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  774. if(val != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  775. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  776. break;
  777. case LSM_GRY_ODR_104HZ:
  778. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_104Hz);
  779. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  780. if(val != LSM6DS3TR_C_GY_ODR_104Hz)return -1;
  781. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_104HZ;
  782. break;
  783. case LSM_GRY_ODR_12HZ5:
  784. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  785. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  786. if(val != LSM6DS3TR_C_GY_ODR_12Hz5)return -1;
  787. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_12HZ5;
  788. break;
  789. }
  790. }
  791. return 0;
  792. }
  793. /**
  794. @brief 设置MAG量程
  795. @param mag_fs - [in] MAG量程
  796. @return 错误代码 - [out] -1失败,0成功
  797. */
  798. int drv_lsm_set_mag_fs(LSM_MAG_FS_e mag_fs)
  799. {
  800. int ret;
  801. uint8_t data;
  802. if(ob_lsm6ds3tr_c.cur_param.mag_fs != mag_fs)
  803. {
  804. switch(mag_fs)
  805. {
  806. case LSM_MAG_FS_30GS:
  807. data = LSM_MAG_FS_30GS;
  808. ret = write_hub(HUB_MAG_DEV_ADDR, 0x0b, &data);
  809. if(ret == -1)return -1;
  810. data = 0xFF;
  811. ret = read_hub(HUB_MAG_DEV_ADDR, 0x0b, &data,1);
  812. if(ret == -1 || data != LSM_MAG_FS_30GS)return -1;
  813. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  814. break;
  815. }
  816. }
  817. return 0;
  818. }
  819. /**
  820. @brief 设置MAG采样频率
  821. @param mag_odr - [in] MAG采样频率
  822. @return 错误代码 - [out] -1失败,0成功
  823. */
  824. int drv_lsm_set_mag_odr(LSM_MAG_ODR_e mag_odr, bool set_hub)
  825. {
  826. int ret = -1;
  827. int err;
  828. uint8_t data;
  829. static int flow_ctl = 0; //流程控制
  830. if(ob_lsm6ds3tr_c.cur_param.mag_odr != mag_odr)
  831. {
  832. switch(flow_ctl)
  833. {
  834. case 0:
  835. //define the sign for x y and z axis
  836. if(mag_odr != LSM_MAG_ODR_OFF)
  837. {
  838. data = 0x06;
  839. err = write_hub(HUB_MAG_DEV_ADDR, 0x29, &data);
  840. if(err == -1)return -1;
  841. flow_ctl = 1;
  842. }
  843. else flow_ctl = 1;
  844. break;
  845. case 1:
  846. //define the sign for x y and z axis
  847. if(mag_odr != LSM_MAG_ODR_OFF)
  848. {
  849. data = 0xFF;
  850. err = read_hub(HUB_MAG_DEV_ADDR, 0x29, &data,1);
  851. if(err == -1 || data != 0x06)return -1;
  852. flow_ctl = 2;
  853. }
  854. else flow_ctl = 2;
  855. break;
  856. case 2:
  857. //set odr
  858. data = mag_odr;
  859. err = write_hub(HUB_MAG_DEV_ADDR, 0x0a, &data);
  860. if(err == -1)return -1;
  861. flow_ctl = 3;
  862. break;
  863. case 3:
  864. // data = 0xFF;
  865. // err = read_hub(HUB_MAG_DEV_ADDR, 0x0a, &data,1);
  866. // if(err == -1 || data != mag_odr)
  867. // {
  868. // flow_ctl = 2;
  869. // return -1;
  870. // }
  871. flow_ctl = 4;
  872. break;
  873. case 4:
  874. //是否配置hub
  875. if(set_hub)
  876. {
  877. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &sub_qmc6310);
  878. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_BANK_A);
  879. data = 0xFF;
  880. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  881. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  882. if(data != QMC6310_DATA_OUT_X_LSB_REG)return -1;
  883. /* Configure Sensor Hub to read one slaves */
  884. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  885. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  886. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  887. ret = 0;
  888. flow_ctl = 0;
  889. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  890. }
  891. else{
  892. ret = 0;
  893. flow_ctl = 0;
  894. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  895. }
  896. break;
  897. }
  898. }
  899. else
  900. {
  901. return 0;
  902. }
  903. return ret;
  904. }
  905. /**
  906. @brief 获取LSM配置参数
  907. @param param - [in] LSM配置参数
  908. @return 错误代码 - [out] -1失败,0成功
  909. */
  910. int drv_lsm_get_config_param(drv_lsm_config_param_t *p_param)
  911. {
  912. *p_param = ob_lsm6ds3tr_c.cur_param;
  913. return 0;
  914. }
  915. /**
  916. @brief 当前LSM的FIFO有几组数据
  917. @return 错误代码 - [out] FIFO当前有几组数据
  918. */
  919. int drv_lsm_get_fifo_group_num(void)
  920. {
  921. uint8_t wt;
  922. uint16_t num = 0;
  923. uint16_t num_pattern = 0;
  924. uint16_t pattern_len = 0;
  925. /* Read FIFO watermark flag in polling mode */
  926. lsm6ds3tr_c_fifo_wtm_flag_get(&ob_lsm6ds3tr_c.ctx, &wt);
  927. if(wt)
  928. {
  929. /* Read number of word in FIFO */
  930. lsm6ds3tr_c_fifo_data_level_get(&ob_lsm6ds3tr_c.ctx, &num);
  931. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  932. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  933. }
  934. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  935. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  936. }
  937. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  938. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  939. }
  940. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  941. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  942. }
  943. num_pattern = num / pattern_len;
  944. }
  945. return num_pattern;
  946. }
  947. /**
  948. @brief 获取LSM的FIFO数据
  949. @param p_data - [out] 返回的FIFO数据
  950. @return 错误代码 - [out] -1失败,0成功
  951. */
  952. int drv_lsm_get_fifo_data(lsm_data_t *p_data)
  953. {
  954. //获取原始数据
  955. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  956. /* Read XL samples */
  957. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.u8bit,ACC_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  958. }
  959. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  960. /* Read gyro samples */
  961. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_angular_rate.u8bit,GRY_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  962. }
  963. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  964. /* Read mag samples */
  965. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_magnetic.u8bit,MAG_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  966. }
  967. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  968. /* Read timestamp samples */
  969. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_timestamp.u8bit,TIMESTAMP_OUT_WORD_NUM * sizeof(int16_t));
  970. }
  971. //返回数据
  972. if(p_data != NULL)
  973. {
  974. p_data->gry[0] = data_raw_angular_rate.i16bit[0];
  975. p_data->gry[1] = data_raw_angular_rate.i16bit[1];
  976. p_data->gry[2] = data_raw_angular_rate.i16bit[2];
  977. p_data->acc[0] = data_raw_acceleration.i16bit[0];
  978. p_data->acc[1] = data_raw_acceleration.i16bit[1];
  979. p_data->acc[2] = data_raw_acceleration.i16bit[2];
  980. p_data->mag[0] = data_raw_magnetic.i16bit[0];
  981. p_data->mag[1] = data_raw_magnetic.i16bit[1];
  982. p_data->mag[2] = data_raw_magnetic.i16bit[2];
  983. /* 时间戳和计步数数据
  984. 字节1 字节2 字节3 字节4 字节5 字节6
  985. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  986. [15:8] [23:16] [7:0] [7:0] [15:8]
  987. */
  988. p_data->fifo_timestamp = ((data_raw_timestamp.u8bit[1] << 16) | (data_raw_timestamp.u8bit[0] << 8) | (data_raw_timestamp.u8bit[3]))*25;//counter == 25us
  989. }
  990. return 0;
  991. }
  992. /**
  993. @brief 获取LSM的ACC数据
  994. @param p_data - [out] 返回的ACC三轴数据
  995. @return 错误代码 - [out] -1失败,0成功
  996. */
  997. int drv_lsm_get_acc_data(lsm_data_t *p_data)
  998. {
  999. lsm6ds3tr_c_reg_t reg;
  1000. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF)
  1001. {
  1002. /* Read samples in polling mode (no int) */
  1003. /* Read output only if new value is available */
  1004. lsm6ds3tr_c_status_reg_get(&ob_lsm6ds3tr_c.ctx, &reg.status_reg);
  1005. if(reg.status_reg.xlda)
  1006. {
  1007. /* Read magnetic field data */
  1008. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  1009. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx,data_raw_acceleration.i16bit);
  1010. p_data->acc[0] = data_raw_acceleration.i16bit[0];
  1011. p_data->acc[1] = data_raw_acceleration.i16bit[1];
  1012. p_data->acc[2] = data_raw_acceleration.i16bit[2];
  1013. }
  1014. }
  1015. else
  1016. {
  1017. return -1;
  1018. }
  1019. return 0;
  1020. }