drv_lsm6ds3tr_c - 副本.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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. //
  302. // lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  303. //
  304. // lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  305. //
  306. // if(up_en != LSM6DS3TR_C_INTERNAL_PULL_UP)return -1;
  307. //SPI切换为IIC
  308. SPI0_Disable();
  309. LSM_IIC_Init();
  310. if(LSM_IIC_ReadBytes(device_addr,reg,data,len))
  311. {
  312. ret = 0;
  313. }
  314. else
  315. {
  316. ret = -1;
  317. SEGGER_RTT_printf(0,"-------->LSM_IIC_ReadBytes err\n");
  318. }
  319. //IIC切换为SPI
  320. LSM_IIC_UnInit();
  321. SPI0_Init();
  322. //关闭直通模式
  323. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  324. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  325. if(val != 0)return -1;
  326. // //关闭内部上拉
  327. //
  328. // lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_EXT_PULL_UP);
  329. //
  330. // up_en = LSM6DS3TR_C_SH_PIN_MODE;
  331. // lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  332. //
  333. // if(up_en != LSM6DS3TR_C_EXT_PULL_UP)return -1;
  334. return ret;
  335. }
  336. static int write_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint8_t len)
  337. {
  338. int32_t ret;
  339. uint8_t val;
  340. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  341. lsm6ds3tr_c_odr_xl_t acc_odr;
  342. lsm6ds3tr_c_odr_g_t gry_odr;
  343. lsm6ds3tr_c_ctrl10_c_t ctrl10_c;
  344. // lsm6ds3tr_c_pull_up_en_t up_en;
  345. //关闭ACC
  346. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  347. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&acc_odr);
  348. if(acc_odr != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  349. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  350. //关闭GRY
  351. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  352. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&gry_odr);
  353. if(gry_odr != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  354. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  355. //关闭FIFO
  356. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  357. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  358. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  359. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  360. //设置hub不使用iic
  361. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  362. val = 0xFF;
  363. lsm6ds3tr_c_sh_master_get(&ob_lsm6ds3tr_c.ctx, &val);
  364. if(val != PROPERTY_DISABLE)return -1;
  365. //设置hub总开关为关闭
  366. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  367. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_CTRL10_C,(uint8_t *)&ctrl10_c, 1);
  368. if(ctrl10_c.func_en != PROPERTY_DISABLE)return -1;
  369. //开启直通模式
  370. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  371. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  372. if(val != 1)return -1;
  373. // //开启内部上拉
  374. //
  375. // lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  376. //
  377. // lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  378. //
  379. // if(up_en != LSM6DS3TR_C_INTERNAL_PULL_UP)return -1;
  380. //SPI切换为IIC
  381. SPI0_Disable();
  382. LSM_IIC_Init();
  383. if(LSM_IIC_WriteBytes(device_addr,reg,data,len))
  384. {
  385. ret = 0;
  386. }
  387. else
  388. {
  389. ret = -1;
  390. SEGGER_RTT_printf(0,"-------->LSM_IIC_WriteBytes err\n");
  391. }
  392. //IIC切换为SPI
  393. LSM_IIC_UnInit();
  394. SPI0_Init();
  395. //关闭直通模式
  396. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  397. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  398. if(val != 0)return -1;
  399. // //关闭内部上拉
  400. //
  401. // lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_EXT_PULL_UP);
  402. //
  403. // up_en = LSM6DS3TR_C_SH_PIN_MODE;
  404. // lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  405. //
  406. // if(up_en != LSM6DS3TR_C_EXT_PULL_UP)return -1;
  407. return ret;
  408. }
  409. static int self_check(void)
  410. {
  411. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  412. /* Wait sensor boot time */
  413. platform_delay_ms(BOOT_TIME);
  414. /* Check device ID */
  415. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  416. if(whoamI != LSM6DS3TR_C_ID)
  417. {
  418. DEBUG_LOG("LSM self_check error!!!\r\n");
  419. return -1;
  420. }
  421. /* Restore default configuration */
  422. lsm6ds3tr_c_reset_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  423. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  424. do {
  425. lsm6ds3tr_c_reset_get(&ob_lsm6ds3tr_c.ctx, &rst);
  426. ob_lsm6ds3tr_c.op_timeout--;
  427. } while(rst && ob_lsm6ds3tr_c.op_timeout!=0);
  428. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  429. /* Accelerometer Self Test */
  430. /* Set Output Data Rate */
  431. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_52Hz);
  432. /* Set full scale */
  433. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_4g);
  434. /* Wait stable output */
  435. platform_delay_ms(WAIT_TIME_A);
  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 dummy data and discard it */
  444. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  445. /* Read 5 sample and get the average vale for each axis */
  446. memset(val_st_off, 0x00, 3 * sizeof(float));
  447. for (i = 0; i < 5; i++) {
  448. /* Check if new value available */
  449. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  450. do {
  451. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  452. ob_lsm6ds3tr_c.op_timeout--;
  453. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  454. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  455. /* Read data and accumulate the mg value */
  456. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  457. for (j = 0; j < 3; j++) {
  458. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  459. }
  460. }
  461. /* Calculate the mg average values */
  462. for (i = 0; i < 3; i++) {
  463. val_st_off[i] /= 5.0f;
  464. }
  465. /* Enable Self Test positive (or negative) */
  466. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  467. /* Wait stable output */
  468. platform_delay_ms(WAIT_TIME_A);
  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 dummy data and discard it */
  477. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  478. /* Read 5 sample and get the average vale for each axis */
  479. memset(val_st_on, 0x00, 3 * sizeof(float));
  480. for (i = 0; i < 5; i++) {
  481. /* Check if new value available */
  482. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  483. do {
  484. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  485. ob_lsm6ds3tr_c.op_timeout--;
  486. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  487. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  488. /* Read data and accumulate the mg value */
  489. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  490. for (j = 0; j < 3; j++) {
  491. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  492. }
  493. }
  494. /* Calculate the mg average values */
  495. for (i = 0; i < 3; i++) {
  496. val_st_on[i] /= 5.0f;
  497. }
  498. /* Calculate the mg values for self test */
  499. for (i = 0; i < 3; i++) {
  500. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  501. }
  502. /* Check self test limit */
  503. st_result = ST_PASS;
  504. for (i = 0; i < 3; i++) {
  505. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  506. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  507. st_result = ST_FAIL;
  508. }
  509. }
  510. /* Disable Self Test */
  511. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  512. /* Disable sensor. */
  513. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  514. /* Gyroscope Self Test. */
  515. /* Set Output Data Rate */
  516. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_208Hz);
  517. /* Set full scale */
  518. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  519. /* Wait stable output */
  520. platform_delay_ms(WAIT_TIME_G_01);
  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 dummy data and discard it */
  529. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  530. /* Read 5 sample and get the average vale for each axis */
  531. memset(val_st_off, 0x00, 3 * sizeof(float));
  532. for (i = 0; i < 5; i++) {
  533. /* Check if new value available */
  534. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  535. do {
  536. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  537. ob_lsm6ds3tr_c.op_timeout--;
  538. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  539. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  540. /* Read data and accumulate the mg value */
  541. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  542. for (j = 0; j < 3; j++) {
  543. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  544. self_check_data_raw[j]);
  545. }
  546. }
  547. /* Calculate the mg average values */
  548. for (i = 0; i < 3; i++) {
  549. val_st_off[i] /= 5.0f;
  550. }
  551. /* Enable Self Test positive (or negative) */
  552. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  553. /* Wait stable output */
  554. platform_delay_ms(WAIT_TIME_G_02);
  555. /* Read 5 sample and get the average vale for each axis */
  556. memset(val_st_on, 0x00, 3 * sizeof(float));
  557. for (i = 0; i < 5; i++) {
  558. /* Check if new value available */
  559. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  560. do {
  561. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  562. ob_lsm6ds3tr_c.op_timeout--;
  563. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  564. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  565. /* Read data and accumulate the mg value */
  566. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  567. for (j = 0; j < 3; j++) {
  568. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  569. self_check_data_raw[j]);
  570. }
  571. }
  572. /* Calculate the mg average values */
  573. for (i = 0; i < 3; i++) {
  574. val_st_on[i] /= 5.0f;
  575. }
  576. /* Calculate the mg values for self test */
  577. for (i = 0; i < 3; i++) {
  578. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  579. }
  580. /* Check self test limit */
  581. for (i = 0; i < 3; i++) {
  582. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  583. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  584. st_result = ST_FAIL;
  585. }
  586. }
  587. /* Disable Self Test */
  588. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  589. /* Disable sensor. */
  590. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  591. if (st_result == ST_PASS) {
  592. DEBUG_LOG("LSM Self Test - PASS\r\n");
  593. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  594. rst = 0Xff;
  595. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  596. if(rst != 1)return -1;
  597. }else{
  598. DEBUG_LOG("Self Test - FAIL\r\n");
  599. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  600. rst = 0Xff;
  601. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  602. if(rst != 1)return -1;
  603. return -1;
  604. }
  605. return 0;
  606. }
  607. /*API ----------------------------------------------*/
  608. /**
  609. @brief 初始化LSM驱动
  610. @param 无
  611. @return 错误代码 - [out] -1失败,0成功
  612. */
  613. int drv_lsm_Init(void)
  614. {
  615. //初始化结构体
  616. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  617. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  618. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  619. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  620. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  621. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  622. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  623. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  624. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  625. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  626. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  627. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  628. ob_lsm6ds3tr_c.ctx.read_reg = platform_read;
  629. ob_lsm6ds3tr_c.ctx.write_reg = platform_write;
  630. ob_lsm6ds3tr_c.op_timeout = 0;
  631. //供电
  632. nrf_gpio_cfg(
  633. PIN_FRONT_SENSE_POWER,
  634. NRF_GPIO_PIN_DIR_OUTPUT,
  635. NRF_GPIO_PIN_INPUT_DISCONNECT,
  636. NRF_GPIO_PIN_NOPULL,
  637. NRF_GPIO_PIN_H0H1,
  638. NRF_GPIO_PIN_NOSENSE);
  639. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  640. nrf_gpio_cfg_output(BOARD_SPI0_MOSI_IO);
  641. nrf_gpio_cfg_output(BOARD_SPI0_CLK_IO);
  642. nrf_gpio_cfg_output(PIN_FRONT_SPI_nCS);
  643. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  644. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  645. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  646. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  647. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  648. platform_delay_ms(3);
  649. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  650. platform_delay_ms(3);
  651. //初始化SPI接口
  652. SPI0_Init();
  653. //初始化SPI错误上报
  654. Process_Start(1000,"drv_lsm_spi_error_report_process",drv_lsm_spi_error_report_process);
  655. //开始自检
  656. return self_check();
  657. }
  658. /**
  659. @brief LSM断电
  660. @param 无
  661. @return 错误代码 - [out] -1失败,0成功
  662. */
  663. int drv_lsm_power_off(void)
  664. {
  665. //关闭SPI
  666. SPI0_Disable();
  667. //断电,清空lsm配置
  668. nrf_gpio_cfg(
  669. PIN_FRONT_SENSE_POWER,
  670. NRF_GPIO_PIN_DIR_OUTPUT,
  671. NRF_GPIO_PIN_INPUT_DISCONNECT,
  672. NRF_GPIO_PIN_NOPULL,
  673. NRF_GPIO_PIN_H0H1,
  674. NRF_GPIO_PIN_NOSENSE);
  675. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  676. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  677. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  678. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  679. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  680. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  681. return 0;
  682. }
  683. /**
  684. @brief LSM上电(默认配置挂起)
  685. @param 无
  686. @return 错误代码 - [out] -1失败,0成功
  687. */
  688. int drv_lsm_power_on(void)
  689. {
  690. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  691. nrf_gpio_cfg_input(BOARD_SPI0_MISO_IO, (nrf_gpio_pin_pull_t)NRFX_SPIM_MISO_PULL_CFG);
  692. //初始化结构体
  693. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  694. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  695. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  696. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  697. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  698. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  699. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  700. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  701. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  702. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  703. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  704. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  705. ob_lsm6ds3tr_c.op_timeout = 0;
  706. //关闭SPI
  707. SPI0_Disable(); //注意:若SPI线短路,该函数内部实现有个while循环导致卡死。已修改源文件规避。
  708. //重新初始化SPI
  709. SPI0_Init();
  710. // lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  711. return 0;
  712. }
  713. /**
  714. @brief 设置加速度工作模式
  715. @param acc_power_mode - [in] 加速度工作模式
  716. @return 错误代码 - [out] -1失败,0成功
  717. */
  718. int drv_lsm_set_acc_power_mode(LSM_ACC_POWER_MODE_e acc_power_mode)
  719. {
  720. lsm6ds3tr_c_xl_hm_mode_t val;
  721. if(ob_lsm6ds3tr_c.cur_param.acc_power_mode != acc_power_mode)
  722. {
  723. switch(acc_power_mode)
  724. {
  725. case LSM_ACC_POWER_MODE_HIGH_PERFORMANCE:
  726. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_HIGH_PERFORMANCE);
  727. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  728. if(val != LSM6DS3TR_C_XL_HIGH_PERFORMANCE)return -1;
  729. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  730. break;
  731. case LSM_ACC_POWER_MODE_NORMAL:
  732. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_NORMAL);
  733. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  734. if(val != LSM6DS3TR_C_XL_NORMAL)return -1;
  735. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_NORMAL;
  736. break;
  737. }
  738. }
  739. return 0;
  740. }
  741. /**
  742. @brief 设置陀螺仪工作模式
  743. @param gry_power_mode - [in] 陀螺仪工作模式
  744. @return 错误代码 - [out] -1失败,0成功
  745. */
  746. int drv_lsm_set_gry_power_mode(LSM_GRY_POWER_MODE_e gry_power_mode)
  747. {
  748. lsm6ds3tr_c_g_hm_mode_t val;
  749. if(ob_lsm6ds3tr_c.cur_param.gry_power_mode != gry_power_mode)
  750. {
  751. switch(gry_power_mode)
  752. {
  753. case LSM_GRY_POWER_MODE_HIGH_PERFORMANCE:
  754. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
  755. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  756. if(val != LSM_GRY_POWER_MODE_HIGH_PERFORMANCE)return -1;
  757. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  758. break;
  759. case LSM_GRY_POWER_MODE_NORMAL:
  760. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_NORMAL);
  761. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  762. if(val != LSM_GRY_POWER_MODE_NORMAL)return -1;
  763. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_NORMAL;
  764. break;
  765. }
  766. }
  767. return 0;
  768. }
  769. /**
  770. @brief 设置时间戳精度
  771. @param timestamp_resolution - [in] 时间戳精度
  772. @return 错误代码 - [out] -1失败,0成功
  773. */
  774. int drv_lsm_set_timestamp_resolution(LSM_TIMESTAMP_RESOLUTION_e timestamp_resolution)
  775. {
  776. lsm6ds3tr_c_timer_hr_t val;
  777. if(ob_lsm6ds3tr_c.cur_param.timestamp_resolution != timestamp_resolution)
  778. {
  779. switch(timestamp_resolution)
  780. {
  781. case LSM_TIMESTAMP_6MS4:
  782. /* Set High Resolution Timestamp (6.4ms) */
  783. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_6ms4);
  784. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  785. if(val != LSM6DS3TR_C_LSB_6ms4)return -1;
  786. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  787. break;
  788. case LSM_TIMESTAMP_25US:
  789. /* Set High Resolution Timestamp (25 us tick) */
  790. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_25us);
  791. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  792. if(val != LSM6DS3TR_C_LSB_25us)return -1;
  793. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_25US;
  794. break;
  795. }
  796. }
  797. return 0;
  798. }
  799. /**
  800. @brief 设置时间戳开关
  801. @param timestamp_switch - [in] 时间戳开关
  802. @return 错误代码 - [out] -1失败,0成功
  803. */
  804. int drv_lsm_set_timestamp_switch(LSM_TIMESTAMP_SWITCH_e timestamp_switch)
  805. {
  806. uint8_t val;
  807. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch != timestamp_switch)
  808. {
  809. switch(timestamp_switch)
  810. {
  811. case LSM_TIMESTAMP_ON:
  812. /* Enable timestamp in HW */
  813. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  814. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  815. if(val != PROPERTY_ENABLE)return -1;
  816. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_ON;
  817. break;
  818. case LSM_TIMESTAMP_OFF:
  819. /*Disable timestamp in HW */
  820. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  821. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  822. if(val != PROPERTY_DISABLE)return -1;
  823. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  824. break;
  825. }
  826. }
  827. return 0;
  828. }
  829. /**
  830. @brief 设置FIFO采样频率
  831. @param fifo_odr - [in] FIFO采样频率
  832. @return 错误代码 - [out] -1失败,0成功
  833. */
  834. 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)
  835. {
  836. uint8_t val;
  837. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  838. lsm6ds3tr_c_fifo_mode_t r_fifo_mode;
  839. lsm6ds3tr_c_dec_fifo_xl_t r_fifo_xl;
  840. lsm6ds3tr_c_dec_fifo_gyro_t r_fifo_gy;
  841. lsm6ds3tr_c_dec_ds3_fifo_t r_fifo_ds3;
  842. lsm6ds3tr_c_dec_ds4_fifo_t r_fifo_ds4;
  843. uint16_t fifo_watermark;
  844. uint16_t pattern_len = 0;
  845. if(ob_lsm6ds3tr_c.cur_param.fifo_odr != fifo_odr)
  846. {
  847. switch(fifo_odr)
  848. {
  849. case LSM_FIFO_ODR_OFF:
  850. //禁用步进计数器和时间戳数据作为第4步FIFO数据集
  851. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  852. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  853. if(val != PROPERTY_DISABLE)return -1;
  854. /* Set ODR FIFO */
  855. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  856. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  857. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  858. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  859. break;
  860. case LSM_FIFO_ODR_104HZ:
  861. //启用步进计数器和时间戳数据作为第4步FIFO数据集
  862. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  863. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  864. if(val != PROPERTY_ENABLE)return -1;
  865. /* Set FIFO mode to Stream mode */
  866. lsm6ds3tr_c_fifo_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_STREAM_MODE);
  867. lsm6ds3tr_c_fifo_mode_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_mode);
  868. if(r_fifo_mode != LSM6DS3TR_C_STREAM_MODE)return -1;
  869. /* Set FIFO sensor decimator */
  870. if(acc_odr == LSM_ACC_ODR_104HZ)
  871. {
  872. lsm6ds3tr_c_fifo_xl_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//acc和fifo的HZ一致
  873. lsm6ds3tr_c_fifo_xl_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_xl);
  874. if(r_fifo_xl != LSM6DS3TR_C_FIFO_XL_NO_DEC)return -1;
  875. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  876. }
  877. if(gry_odr == LSM_GRY_ODR_104HZ)
  878. {
  879. lsm6ds3tr_c_fifo_gy_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_GY_NO_DEC);//gry和fifo的HZ一致
  880. lsm6ds3tr_c_fifo_gy_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_gy);
  881. if(r_fifo_gy != LSM6DS3TR_C_FIFO_GY_NO_DEC)return -1;
  882. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  883. }
  884. if(mag_odr != LSM_MAG_ODR_OFF)
  885. {
  886. lsm6ds3tr_c_fifo_dataset_3_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//mag和fifo的HZ一致
  887. lsm6ds3tr_c_fifo_dataset_3_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds3);
  888. if(r_fifo_ds3 != LSM6DS3TR_C_FIFO_DS3_NO_DEC)return -1;
  889. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  890. }
  891. if(timestamp_switch == LSM_TIMESTAMP_ON)
  892. {
  893. lsm6ds3tr_c_fifo_dataset_4_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  894. lsm6ds3tr_c_fifo_dataset_4_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds4);
  895. if(r_fifo_ds4 != LSM6DS3TR_C_FIFO_DS4_NO_DEC)return -1;
  896. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  897. }
  898. /* Set FIFO watermark to a multiple of a pattern */
  899. lsm6ds3tr_c_fifo_watermark_set(&ob_lsm6ds3tr_c.ctx, pattern_len);
  900. lsm6ds3tr_c_fifo_watermark_get(&ob_lsm6ds3tr_c.ctx,&fifo_watermark);
  901. if(fifo_watermark != pattern_len)return -1;
  902. /* Set ODR FIFO */
  903. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_104Hz);//该参数关联到timestamp
  904. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  905. if(r_fifo_odr != LSM6DS3TR_C_FIFO_104Hz) return -1;
  906. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_104HZ;
  907. break;
  908. }
  909. }
  910. return 0;
  911. }
  912. /**
  913. @brief 设置ACC量程
  914. @param acc_fs - [in] ACC量程
  915. @return 错误代码 - [out] -1失败,0成功
  916. */
  917. int drv_lsm_set_acc_fs(LSM_ACC_FS_e acc_fs)
  918. {
  919. lsm6ds3tr_c_fs_xl_t val;
  920. if(ob_lsm6ds3tr_c.cur_param.acc_fs != acc_fs)
  921. {
  922. switch(acc_fs)
  923. {
  924. case LSM_ACC_FS_2G:
  925. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2g);
  926. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  927. if(val != LSM6DS3TR_C_2g)return -1;
  928. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  929. break;
  930. case LSM_ACC_FS_16G:
  931. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_16g);
  932. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  933. if(val != LSM6DS3TR_C_16g)return -1;
  934. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_16G;
  935. break;
  936. }
  937. }
  938. return 0;
  939. }
  940. /**
  941. @brief 设置ACC采样频率
  942. @param acc_odr - [in] ACC采样频率
  943. @return 错误代码 - [out] -1失败,0成功
  944. */
  945. int drv_lsm_set_acc_odr(LSM_ACC_ODR_e acc_odr)
  946. {
  947. lsm6ds3tr_c_odr_xl_t val;
  948. if(ob_lsm6ds3tr_c.cur_param.acc_odr != acc_odr)
  949. {
  950. switch(acc_odr)
  951. {
  952. case LSM_ACC_ODR_OFF:
  953. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  954. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  955. if(val != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  956. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  957. break;
  958. case LSM_ACC_ODR_104HZ:
  959. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  960. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  961. if(val != LSM6DS3TR_C_XL_ODR_104Hz)return -1;
  962. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_104HZ;
  963. break;
  964. case LSM_ACC_ODR_12HZ5:
  965. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  966. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  967. if(val != LSM6DS3TR_C_XL_ODR_12Hz5)return -1;
  968. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_12HZ5;
  969. break;
  970. }
  971. }
  972. return 0;
  973. }
  974. /**
  975. @brief 设置GRY量程
  976. @param gry_fs - [in] GRY量程
  977. @return 错误代码 - [out] -1失败,0成功
  978. */
  979. int drv_lsm_set_gry_fs(LSM_GRY_FS_e gry_fs)
  980. {
  981. lsm6ds3tr_c_fs_g_t val;
  982. if(ob_lsm6ds3tr_c.cur_param.gry_fs != gry_fs)
  983. {
  984. switch(gry_fs)
  985. {
  986. case LSM_GRY_FS_250DPS:
  987. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_250dps);
  988. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  989. if(val != LSM6DS3TR_C_250dps)return -1;
  990. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  991. break;
  992. case LSM_GRY_FS_2000DPS:
  993. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  994. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  995. if(val != LSM6DS3TR_C_2000dps)return -1;
  996. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_2000DPS;
  997. break;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. /**
  1003. @brief 设置GRY采样频率
  1004. @param gry_odr - [in] GRY采样频率
  1005. @return 错误代码 - [out] -1失败,0成功
  1006. */
  1007. int drv_lsm_set_gry_odr(LSM_GRY_ODR_e gry_odr)
  1008. {
  1009. lsm6ds3tr_c_odr_g_t val;
  1010. if(ob_lsm6ds3tr_c.cur_param.gry_odr != gry_odr)
  1011. {
  1012. switch(gry_odr)
  1013. {
  1014. case LSM_GRY_ODR_OFF:
  1015. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1016. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1017. if(val != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  1018. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  1019. break;
  1020. case LSM_GRY_ODR_104HZ:
  1021. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_104Hz);
  1022. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1023. if(val != LSM6DS3TR_C_GY_ODR_104Hz)return -1;
  1024. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_104HZ;
  1025. break;
  1026. case LSM_GRY_ODR_12HZ5:
  1027. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  1028. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1029. if(val != LSM6DS3TR_C_GY_ODR_12Hz5)return -1;
  1030. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_12HZ5;
  1031. break;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. /**
  1037. @brief 获取配置MAG量程需要的步骤数量
  1038. @return 错误代码 - [out] 配置MAG量程需要的步骤数量
  1039. */
  1040. int drv_lsm_get_mag_fs_flow(void)
  1041. {
  1042. return 1;
  1043. }
  1044. /**
  1045. @brief 设置MAG量程
  1046. @param mag_fs - [in] MAG量程
  1047. @param flow - [in] 当前需要处理的步骤
  1048. @return 错误代码 - [out] -1失败,0成功
  1049. */
  1050. int drv_lsm_set_mag_fs(LSM_MAG_FS_e mag_fs, int flow)
  1051. {
  1052. int ret;
  1053. uint8_t data;
  1054. if(flow <= 0 || flow >= 2)return -1;
  1055. if(ob_lsm6ds3tr_c.cur_param.mag_fs != mag_fs)
  1056. {
  1057. switch(mag_fs)
  1058. {
  1059. case LSM_MAG_FS_30GS:
  1060. data = LSM_MAG_FS_30GS;
  1061. ret = write_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1062. if(ret == -1)return -1;
  1063. data = 0xFF;
  1064. ret = read_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1065. if(ret == -1 || data != LSM_MAG_FS_30GS)return -1;
  1066. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  1067. break;
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. /**
  1073. @brief 获取配置MAG采样频率需要的步骤数量
  1074. @return 错误代码 - [out] 配置MAG采样频率需要的步骤数量
  1075. */
  1076. int drv_lsm_get_mag_odr_flow(void)
  1077. {
  1078. return 5;
  1079. }
  1080. /**
  1081. @brief 设置MAG采样频率
  1082. @param mag_odr - [in] MAG采样频率
  1083. @param set_hub - [in] 是否配置hub
  1084. @param flow - [in] 当前需要处理的步骤
  1085. @return 错误代码 - [out] -1失败,0成功
  1086. */
  1087. int drv_lsm_set_mag_odr(LSM_MAG_ODR_e mag_odr, bool set_hub, int flow)
  1088. {
  1089. int ret;
  1090. uint8_t data;
  1091. if(flow <= 0 || flow >= 6)return -1;
  1092. if(ob_lsm6ds3tr_c.cur_param.mag_odr != mag_odr)
  1093. {
  1094. switch(flow)
  1095. {
  1096. case 1:
  1097. //define the sign for x y and z axis
  1098. if(mag_odr != LSM_MAG_ODR_OFF)
  1099. {
  1100. data = 0x06;
  1101. ret = write_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1102. }
  1103. else
  1104. {
  1105. ret = 0;
  1106. }
  1107. break;
  1108. case 2:
  1109. //define the sign for x y and z axis
  1110. if(mag_odr != LSM_MAG_ODR_OFF)
  1111. {
  1112. data = 0xFF;
  1113. ret = read_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1114. if(ret == -1 || data != 0x06)ret = -1;
  1115. else ret = 0;
  1116. }
  1117. else
  1118. {
  1119. ret = 0;
  1120. }
  1121. break;
  1122. case 3:
  1123. //set odr
  1124. data = mag_odr;
  1125. ret = write_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1126. break;
  1127. case 4:
  1128. data = 0xFF;
  1129. ret = read_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1130. if(ret == -1 || data != mag_odr)ret = -1;
  1131. else{
  1132. ret = 0;
  1133. }
  1134. break;
  1135. case 5:
  1136. //是否配置hub
  1137. if(set_hub)
  1138. {
  1139. // lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  1140. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &sub_qmc6310);
  1141. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_BANK_A);
  1142. data = 0xFF;
  1143. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  1144. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  1145. if(data != QMC6310_DATA_OUT_X_LSB_REG)return -1;
  1146. /* Configure Sensor Hub to read one slaves */
  1147. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  1148. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  1149. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1150. ret = 0;
  1151. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1152. }
  1153. else{
  1154. ret = 0;
  1155. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1156. }
  1157. break;
  1158. }
  1159. }
  1160. else
  1161. {
  1162. return 0;
  1163. }
  1164. return ret;
  1165. }
  1166. /**
  1167. @brief 获取LSM配置参数
  1168. @param param - [in] LSM配置参数
  1169. @return 错误代码 - [out] -1失败,0成功
  1170. */
  1171. int drv_lsm_get_config_param(drv_lsm_config_param_t *p_param)
  1172. {
  1173. *p_param = ob_lsm6ds3tr_c.cur_param;
  1174. return 0;
  1175. }
  1176. /**
  1177. @brief 当前LSM的FIFO有几组数据
  1178. @return 错误代码 - [out] FIFO当前有几组数据
  1179. */
  1180. int drv_lsm_get_fifo_group_num(void)
  1181. {
  1182. uint8_t wt;
  1183. uint16_t num = 0;
  1184. uint16_t num_pattern = 0;
  1185. uint16_t pattern_len = 0;
  1186. /* Read FIFO watermark flag in polling mode */
  1187. lsm6ds3tr_c_fifo_wtm_flag_get(&ob_lsm6ds3tr_c.ctx, &wt);
  1188. if(wt)
  1189. {
  1190. /* Read number of word in FIFO */
  1191. lsm6ds3tr_c_fifo_data_level_get(&ob_lsm6ds3tr_c.ctx, &num);
  1192. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1193. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  1194. }
  1195. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1196. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  1197. }
  1198. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1199. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  1200. }
  1201. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1202. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  1203. }
  1204. num_pattern = num / pattern_len;
  1205. }
  1206. return num_pattern;
  1207. }
  1208. /**
  1209. @brief 获取LSM的FIFO数据
  1210. @param p_data - [out] 返回的FIFO数据
  1211. @return 错误代码 - [out] -1失败,0成功
  1212. */
  1213. int drv_lsm_get_fifo_data(lsm_data_t *p_data)
  1214. {
  1215. int ret;
  1216. int read_len = 0;
  1217. int read_num = 0;
  1218. //获取原始数据的长度
  1219. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1220. /* Read gyro samples len */
  1221. read_len += GRY_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1222. }
  1223. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1224. /* Read XL samples len*/
  1225. read_len += ACC_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1226. }
  1227. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1228. /* Read mag samples len*/
  1229. read_len += MAG_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1230. }
  1231. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1232. /* Read timestamp samples len*/
  1233. read_len += TIMESTAMP_OUT_WORD_NUM * sizeof(int16_t);
  1234. }
  1235. //清空缓存
  1236. memset(temp_data_raw,0,sizeof(temp_data_raw));
  1237. //获取原始数据
  1238. ret = lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, temp_data_raw[0].u8bit,read_len);
  1239. if(ret == -1)return -1;
  1240. //返回数据
  1241. if(p_data != NULL)
  1242. {
  1243. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1244. /* Read gyro samples */
  1245. p_data->gry[0] = temp_data_raw[read_num].i16bit[0];
  1246. p_data->gry[1] = temp_data_raw[read_num].i16bit[1];
  1247. p_data->gry[2] = temp_data_raw[read_num].i16bit[2];
  1248. read_num++;
  1249. }
  1250. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1251. /* Read XL samples */
  1252. p_data->acc[0] = temp_data_raw[read_num].i16bit[0];
  1253. p_data->acc[1] = temp_data_raw[read_num].i16bit[1];
  1254. p_data->acc[2] = temp_data_raw[read_num].i16bit[2];
  1255. read_num++;
  1256. }
  1257. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1258. /* Read mag samples */
  1259. p_data->mag[0] = temp_data_raw[read_num].i16bit[0];
  1260. p_data->mag[1] = temp_data_raw[read_num].i16bit[1];
  1261. p_data->mag[2] = temp_data_raw[read_num].i16bit[2];
  1262. read_num++;
  1263. }
  1264. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1265. /* 时间戳和计步数数据
  1266. 字节1 字节2 字节3 字节4 字节5 字节6
  1267. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  1268. [15:8] [23:16] [7:0] [7:0] [15:8]
  1269. */
  1270. 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
  1271. }
  1272. }
  1273. return 0;
  1274. }
  1275. /**
  1276. @brief 获取LSM的ACC数据
  1277. @param p_data - [out] 返回的ACC三轴数据
  1278. @return 错误代码 - [out] -1失败,0成功
  1279. */
  1280. int drv_lsm_get_acc_data(lsm_data_t *p_data)
  1281. {
  1282. lsm6ds3tr_c_reg_t reg;
  1283. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF)
  1284. {
  1285. /* Read samples in polling mode (no int) */
  1286. /* Read output only if new value is available */
  1287. lsm6ds3tr_c_status_reg_get(&ob_lsm6ds3tr_c.ctx, &reg.status_reg);
  1288. if(reg.status_reg.xlda)
  1289. {
  1290. /* Read magnetic field data */
  1291. memset(temp_data_raw[0].i16bit, 0x00, 3 * sizeof(int16_t));
  1292. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx,temp_data_raw[0].i16bit);
  1293. p_data->acc[0] = temp_data_raw[0].i16bit[0];
  1294. p_data->acc[1] = temp_data_raw[0].i16bit[1];
  1295. p_data->acc[2] = temp_data_raw[0].i16bit[2];
  1296. }
  1297. else return -1;
  1298. }
  1299. else
  1300. {
  1301. return -1;
  1302. }
  1303. return 0;
  1304. }
  1305. /**
  1306. @brief LSM自检步骤拆分——1,执行玩需等待100ms稳定输出才能执行步骤2
  1307. @param 无
  1308. @return 错误代码 - [out] -1失败,0成功
  1309. */
  1310. int drv_lsm_self_check_1(void)
  1311. {
  1312. /* Check device ID */
  1313. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  1314. if(whoamI != LSM6DS3TR_C_ID)
  1315. {
  1316. DEBUG_LOG("LSM self_check error!!!\r\n");
  1317. return -1;
  1318. }
  1319. /* Restore default configuration */
  1320. lsm6ds3tr_c_reset_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1321. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1322. do {
  1323. lsm6ds3tr_c_reset_get(&ob_lsm6ds3tr_c.ctx, &rst);
  1324. ob_lsm6ds3tr_c.op_timeout--;
  1325. } while(rst && ob_lsm6ds3tr_c.op_timeout!=0);
  1326. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1327. /* Accelerometer Self Test */
  1328. /* Set Output Data Rate */
  1329. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_833Hz);
  1330. /* Set full scale */
  1331. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_4g);
  1332. return 0;
  1333. }
  1334. /**
  1335. @brief LSM自检步骤拆分——2,执行玩需等待100ms稳定输出才能执行步骤3
  1336. @param 无
  1337. @return 错误代码 - [out] -1失败,0成功
  1338. */
  1339. int drv_lsm_self_check_2(void)
  1340. {
  1341. /* Check if new value available */
  1342. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1343. do {
  1344. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1345. ob_lsm6ds3tr_c.op_timeout--;
  1346. } while(!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1347. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1348. /* Read dummy data and discard it */
  1349. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1350. /* Read 5 sample and get the average vale for each axis */
  1351. memset(val_st_off, 0x00, 3 * sizeof(float));
  1352. for (i = 0; i < 5; i++) {
  1353. /* Check if new value available */
  1354. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1355. do {
  1356. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1357. ob_lsm6ds3tr_c.op_timeout--;
  1358. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1359. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1360. /* Read data and accumulate the mg value */
  1361. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1362. for (j = 0; j < 3; j++) {
  1363. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  1364. }
  1365. }
  1366. /* Calculate the mg average values */
  1367. for (i = 0; i < 3; i++) {
  1368. val_st_off[i] /= 5.0f;
  1369. }
  1370. /* Enable Self Test positive (or negative) */
  1371. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  1372. return 0;
  1373. }
  1374. /**
  1375. @brief LSM自检步骤拆分——3,执行玩需等待150ms稳定输出才能执行步骤4
  1376. @param 无
  1377. @return 错误代码 - [out] -1失败,0成功
  1378. */
  1379. int drv_lsm_self_check_3(void)
  1380. {
  1381. /* Check if new value available */
  1382. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1383. do {
  1384. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1385. ob_lsm6ds3tr_c.op_timeout--;
  1386. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1387. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1388. /* Read dummy data and discard it */
  1389. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1390. /* Read 5 sample and get the average vale for each axis */
  1391. memset(val_st_on, 0x00, 3 * sizeof(float));
  1392. for (i = 0; i < 5; i++) {
  1393. /* Check if new value available */
  1394. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1395. do {
  1396. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1397. ob_lsm6ds3tr_c.op_timeout--;
  1398. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1399. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1400. /* Read data and accumulate the mg value */
  1401. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1402. for (j = 0; j < 3; j++) {
  1403. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  1404. }
  1405. }
  1406. /* Calculate the mg average values */
  1407. for (i = 0; i < 3; i++) {
  1408. val_st_on[i] /= 5.0f;
  1409. }
  1410. /* Calculate the mg values for self test */
  1411. for (i = 0; i < 3; i++) {
  1412. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  1413. }
  1414. /* Check self test limit */
  1415. st_result = ST_PASS;
  1416. for (i = 0; i < 3; i++) {
  1417. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  1418. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  1419. st_result = ST_FAIL;
  1420. }
  1421. }
  1422. /* Disable Self Test */
  1423. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  1424. /* Disable sensor. */
  1425. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  1426. /* Gyroscope Self Test. */
  1427. /* Set Output Data Rate */
  1428. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_833Hz);
  1429. /* Set full scale */
  1430. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  1431. return 0;
  1432. }
  1433. /**
  1434. @brief LSM自检步骤拆分——4,执行玩需等待50ms稳定输出才能执行步骤5
  1435. @param 无
  1436. @return 错误代码 - [out] -1失败,0成功
  1437. */
  1438. int drv_lsm_self_check_4(void)
  1439. {
  1440. /* Check if new value available */
  1441. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1442. do {
  1443. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1444. ob_lsm6ds3tr_c.op_timeout--;
  1445. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1446. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1447. /* Read dummy data and discard it */
  1448. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1449. /* Read 5 sample and get the average vale for each axis */
  1450. memset(val_st_off, 0x00, 3 * sizeof(float));
  1451. for (i = 0; i < 5; i++) {
  1452. /* Check if new value available */
  1453. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1454. do {
  1455. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1456. ob_lsm6ds3tr_c.op_timeout--;
  1457. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1458. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1459. /* Read data and accumulate the mg value */
  1460. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1461. for (j = 0; j < 3; j++) {
  1462. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  1463. self_check_data_raw[j]);
  1464. }
  1465. }
  1466. /* Calculate the mg average values */
  1467. for (i = 0; i < 3; i++) {
  1468. val_st_off[i] /= 5.0f;
  1469. }
  1470. /* Enable Self Test positive (or negative) */
  1471. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  1472. return 0;
  1473. }
  1474. /**
  1475. @brief LSM自检步骤拆分——5,获取自检结果。
  1476. @param 无
  1477. @return 错误代码 - [out] -1失败,0成功
  1478. */
  1479. int drv_lsm_self_check_5(void)
  1480. {
  1481. /* Read 5 sample and get the average vale for each axis */
  1482. memset(val_st_on, 0x00, 3 * sizeof(float));
  1483. for (i = 0; i < 5; i++) {
  1484. /* Check if new value available */
  1485. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1486. do {
  1487. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1488. ob_lsm6ds3tr_c.op_timeout--;
  1489. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1490. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1491. /* Read data and accumulate the mg value */
  1492. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1493. for (j = 0; j < 3; j++) {
  1494. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  1495. self_check_data_raw[j]);
  1496. }
  1497. }
  1498. /* Calculate the mg average values */
  1499. for (i = 0; i < 3; i++) {
  1500. val_st_on[i] /= 5.0f;
  1501. }
  1502. /* Calculate the mg values for self test */
  1503. for (i = 0; i < 3; i++) {
  1504. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  1505. }
  1506. /* Check self test limit */
  1507. for (i = 0; i < 3; i++) {
  1508. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  1509. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  1510. st_result = ST_FAIL;
  1511. }
  1512. }
  1513. /* Disable Self Test */
  1514. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  1515. /* Disable sensor. */
  1516. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1517. if (st_result == ST_PASS) {
  1518. DEBUG_LOG("LSM Self Test - PASS\r\n");
  1519. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  1520. rst = 0Xff;
  1521. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  1522. if(rst != 1)return -1;
  1523. }else{
  1524. DEBUG_LOG("Self Test - FAIL\r\n");
  1525. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  1526. rst = 0Xff;
  1527. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  1528. if(rst != 1)return -1;
  1529. return -1;
  1530. }
  1531. return 0;
  1532. }