drv_lsm6ds3tr_c.c 36 KB

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