drv_lsm6ds3tr_c.c 64 KB

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