drv_lsm6ds3tr_c.c 39 KB

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