drv_lsm6ds3tr_c.c 46 KB

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