drv_lsm6ds3tr_c.c 51 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. 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. SEGGER_RTT_printf(0,"-------->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. SEGGER_RTT_printf(0,"-------->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_104HZ:
  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_104HZ)
  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_104HZ)
  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_104Hz);//该参数关联到timestamp
  892. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  893. if(r_fifo_odr != LSM6DS3TR_C_FIFO_104Hz) return -1;
  894. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_104HZ;
  895. break;
  896. }
  897. }
  898. return 0;
  899. }
  900. /**
  901. @brief 设置ACC量程
  902. @param acc_fs - [in] ACC量程
  903. @return 错误代码 - [out] -1失败,0成功
  904. */
  905. int drv_lsm_set_acc_fs(LSM_ACC_FS_e acc_fs)
  906. {
  907. lsm6ds3tr_c_fs_xl_t val;
  908. if(ob_lsm6ds3tr_c.cur_param.acc_fs != acc_fs)
  909. {
  910. switch(acc_fs)
  911. {
  912. case LSM_ACC_FS_2G:
  913. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2g);
  914. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  915. if(val != LSM6DS3TR_C_2g)return -1;
  916. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  917. break;
  918. case LSM_ACC_FS_16G:
  919. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_16g);
  920. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  921. if(val != LSM6DS3TR_C_16g)return -1;
  922. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_16G;
  923. break;
  924. }
  925. }
  926. return 0;
  927. }
  928. /**
  929. @brief 设置ACC采样频率
  930. @param acc_odr - [in] ACC采样频率
  931. @return 错误代码 - [out] -1失败,0成功
  932. */
  933. int drv_lsm_set_acc_odr(LSM_ACC_ODR_e acc_odr)
  934. {
  935. lsm6ds3tr_c_odr_xl_t val;
  936. if(ob_lsm6ds3tr_c.cur_param.acc_odr != acc_odr)
  937. {
  938. switch(acc_odr)
  939. {
  940. case LSM_ACC_ODR_OFF:
  941. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  942. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  943. if(val != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  944. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  945. break;
  946. case LSM_ACC_ODR_104HZ:
  947. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  948. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  949. if(val != LSM6DS3TR_C_XL_ODR_104Hz)return -1;
  950. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_104HZ;
  951. break;
  952. case LSM_ACC_ODR_12HZ5:
  953. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  954. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  955. if(val != LSM6DS3TR_C_XL_ODR_12Hz5)return -1;
  956. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_12HZ5;
  957. break;
  958. }
  959. }
  960. return 0;
  961. }
  962. /**
  963. @brief 设置GRY量程
  964. @param gry_fs - [in] GRY量程
  965. @return 错误代码 - [out] -1失败,0成功
  966. */
  967. int drv_lsm_set_gry_fs(LSM_GRY_FS_e gry_fs)
  968. {
  969. lsm6ds3tr_c_fs_g_t val;
  970. if(ob_lsm6ds3tr_c.cur_param.gry_fs != gry_fs)
  971. {
  972. switch(gry_fs)
  973. {
  974. case LSM_GRY_FS_250DPS:
  975. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_250dps);
  976. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  977. if(val != LSM6DS3TR_C_250dps)return -1;
  978. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  979. break;
  980. case LSM_GRY_FS_2000DPS:
  981. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  982. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  983. if(val != LSM6DS3TR_C_2000dps)return -1;
  984. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_2000DPS;
  985. break;
  986. }
  987. }
  988. return 0;
  989. }
  990. /**
  991. @brief 设置GRY采样频率
  992. @param gry_odr - [in] GRY采样频率
  993. @return 错误代码 - [out] -1失败,0成功
  994. */
  995. int drv_lsm_set_gry_odr(LSM_GRY_ODR_e gry_odr)
  996. {
  997. lsm6ds3tr_c_odr_g_t val;
  998. if(ob_lsm6ds3tr_c.cur_param.gry_odr != gry_odr)
  999. {
  1000. switch(gry_odr)
  1001. {
  1002. case LSM_GRY_ODR_OFF:
  1003. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1004. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1005. if(val != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  1006. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  1007. break;
  1008. case LSM_GRY_ODR_104HZ:
  1009. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_104Hz);
  1010. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1011. if(val != LSM6DS3TR_C_GY_ODR_104Hz)return -1;
  1012. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_104HZ;
  1013. break;
  1014. case LSM_GRY_ODR_12HZ5:
  1015. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  1016. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1017. if(val != LSM6DS3TR_C_GY_ODR_12Hz5)return -1;
  1018. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_12HZ5;
  1019. break;
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. /**
  1025. @brief 获取配置MAG量程需要的步骤数量
  1026. @return 错误代码 - [out] 配置MAG量程需要的步骤数量
  1027. */
  1028. int drv_lsm_get_mag_fs_flow(void)
  1029. {
  1030. return 1;
  1031. }
  1032. /**
  1033. @brief 设置MAG量程
  1034. @param mag_fs - [in] MAG量程
  1035. @param flow - [in] 当前需要处理的步骤
  1036. @return 错误代码 - [out] -1失败,0成功
  1037. */
  1038. int drv_lsm_set_mag_fs(LSM_MAG_FS_e mag_fs, int flow)
  1039. {
  1040. int ret;
  1041. uint8_t data;
  1042. if(flow <= 0 || flow >= 2)return -1;
  1043. if(ob_lsm6ds3tr_c.cur_param.mag_fs != mag_fs)
  1044. {
  1045. switch(mag_fs)
  1046. {
  1047. case LSM_MAG_FS_30GS:
  1048. data = LSM_MAG_FS_30GS;
  1049. ret = write_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1050. if(ret == -1)return -1;
  1051. data = 0xFF;
  1052. ret = read_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1053. if(ret == -1 || data != LSM_MAG_FS_30GS)return -1;
  1054. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  1055. break;
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. /**
  1061. @brief 获取配置MAG采样频率需要的步骤数量
  1062. @return 错误代码 - [out] 配置MAG采样频率需要的步骤数量
  1063. */
  1064. int drv_lsm_get_mag_odr_flow(void)
  1065. {
  1066. return 5;
  1067. }
  1068. /**
  1069. @brief 设置MAG采样频率
  1070. @param mag_odr - [in] MAG采样频率
  1071. @param set_hub - [in] 是否配置hub
  1072. @param flow - [in] 当前需要处理的步骤
  1073. @return 错误代码 - [out] -1失败,0成功
  1074. */
  1075. int drv_lsm_set_mag_odr(LSM_MAG_ODR_e mag_odr, bool set_hub, int flow)
  1076. {
  1077. int ret;
  1078. uint8_t data;
  1079. if(flow <= 0 || flow >= 6)return -1;
  1080. if(ob_lsm6ds3tr_c.cur_param.mag_odr != mag_odr)
  1081. {
  1082. switch(flow)
  1083. {
  1084. case 1:
  1085. //define the sign for x y and z axis
  1086. if(mag_odr != LSM_MAG_ODR_OFF)
  1087. {
  1088. data = 0x06;
  1089. ret = write_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1090. }
  1091. else
  1092. {
  1093. ret = 0;
  1094. }
  1095. break;
  1096. case 2:
  1097. //define the sign for x y and z axis
  1098. if(mag_odr != LSM_MAG_ODR_OFF)
  1099. {
  1100. data = 0xFF;
  1101. ret = read_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1102. if(ret == -1 || data != 0x06)ret = -1;
  1103. else ret = 0;
  1104. }
  1105. else
  1106. {
  1107. ret = 0;
  1108. }
  1109. break;
  1110. case 3:
  1111. //set odr
  1112. data = mag_odr;
  1113. ret = write_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1114. break;
  1115. case 4:
  1116. data = 0xFF;
  1117. ret = read_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1118. if(ret == -1 || data != mag_odr)ret = -1;
  1119. else{
  1120. ret = 0;
  1121. }
  1122. break;
  1123. case 5:
  1124. //是否配置hub
  1125. if(set_hub)
  1126. {
  1127. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  1128. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &sub_qmc6310);
  1129. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_BANK_A);
  1130. data = 0xFF;
  1131. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  1132. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  1133. if(data != QMC6310_DATA_OUT_X_LSB_REG)return -1;
  1134. /* Configure Sensor Hub to read one slaves */
  1135. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  1136. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  1137. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1138. ret = 0;
  1139. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1140. }
  1141. else{
  1142. ret = 0;
  1143. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1144. }
  1145. break;
  1146. }
  1147. }
  1148. else
  1149. {
  1150. return 0;
  1151. }
  1152. return ret;
  1153. }
  1154. /**
  1155. @brief 获取LSM配置参数
  1156. @param param - [in] LSM配置参数
  1157. @return 错误代码 - [out] -1失败,0成功
  1158. */
  1159. int drv_lsm_get_config_param(drv_lsm_config_param_t *p_param)
  1160. {
  1161. *p_param = ob_lsm6ds3tr_c.cur_param;
  1162. return 0;
  1163. }
  1164. /**
  1165. @brief 当前LSM的FIFO有几组数据
  1166. @return 错误代码 - [out] FIFO当前有几组数据
  1167. */
  1168. int drv_lsm_get_fifo_group_num(void)
  1169. {
  1170. uint8_t wt;
  1171. uint16_t num = 0;
  1172. uint16_t num_pattern = 0;
  1173. uint16_t pattern_len = 0;
  1174. /* Read FIFO watermark flag in polling mode */
  1175. lsm6ds3tr_c_fifo_wtm_flag_get(&ob_lsm6ds3tr_c.ctx, &wt);
  1176. if(wt)
  1177. {
  1178. /* Read number of word in FIFO */
  1179. lsm6ds3tr_c_fifo_data_level_get(&ob_lsm6ds3tr_c.ctx, &num);
  1180. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1181. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  1182. }
  1183. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1184. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  1185. }
  1186. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1187. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  1188. }
  1189. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1190. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  1191. }
  1192. num_pattern = num / pattern_len;
  1193. }
  1194. return num_pattern;
  1195. }
  1196. /**
  1197. @brief 获取LSM的FIFO数据
  1198. @param p_data - [out] 返回的FIFO数据
  1199. @return 错误代码 - [out] -1失败,0成功
  1200. */
  1201. int drv_lsm_get_fifo_data(lsm_data_t *p_data)
  1202. {
  1203. int ret;
  1204. int read_len = 0;
  1205. int read_num = 0;
  1206. //获取原始数据的长度
  1207. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1208. /* Read gyro samples len */
  1209. read_len += GRY_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1210. }
  1211. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1212. /* Read XL samples len*/
  1213. read_len += ACC_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1214. }
  1215. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1216. /* Read mag samples len*/
  1217. read_len += MAG_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1218. }
  1219. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1220. /* Read timestamp samples len*/
  1221. read_len += TIMESTAMP_OUT_WORD_NUM * sizeof(int16_t);
  1222. }
  1223. //清空缓存
  1224. memset(temp_data_raw,0,sizeof(temp_data_raw));
  1225. //获取原始数据
  1226. ret = lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, temp_data_raw[0].u8bit,read_len);
  1227. if(ret == -1)return -1;
  1228. //返回数据
  1229. if(p_data != NULL)
  1230. {
  1231. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1232. /* Read gyro samples */
  1233. p_data->gry[0] = temp_data_raw[read_num].i16bit[0];
  1234. p_data->gry[1] = temp_data_raw[read_num].i16bit[1];
  1235. p_data->gry[2] = temp_data_raw[read_num].i16bit[2];
  1236. read_num++;
  1237. }
  1238. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1239. /* Read XL samples */
  1240. p_data->acc[0] = temp_data_raw[read_num].i16bit[0];
  1241. p_data->acc[1] = temp_data_raw[read_num].i16bit[1];
  1242. p_data->acc[2] = temp_data_raw[read_num].i16bit[2];
  1243. read_num++;
  1244. }
  1245. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1246. /* Read mag samples */
  1247. p_data->mag[0] = temp_data_raw[read_num].i16bit[0];
  1248. p_data->mag[1] = temp_data_raw[read_num].i16bit[1];
  1249. p_data->mag[2] = temp_data_raw[read_num].i16bit[2];
  1250. read_num++;
  1251. }
  1252. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1253. /* 时间戳和计步数数据
  1254. 字节1 字节2 字节3 字节4 字节5 字节6
  1255. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  1256. [15:8] [23:16] [7:0] [7:0] [15:8]
  1257. */
  1258. 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
  1259. }
  1260. }
  1261. return 0;
  1262. }
  1263. /**
  1264. @brief 获取LSM的ACC数据
  1265. @param p_data - [out] 返回的ACC三轴数据
  1266. @return 错误代码 - [out] -1失败,0成功
  1267. */
  1268. int drv_lsm_get_acc_data(lsm_data_t *p_data)
  1269. {
  1270. lsm6ds3tr_c_reg_t reg;
  1271. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF)
  1272. {
  1273. /* Read samples in polling mode (no int) */
  1274. /* Read output only if new value is available */
  1275. lsm6ds3tr_c_status_reg_get(&ob_lsm6ds3tr_c.ctx, &reg.status_reg);
  1276. if(reg.status_reg.xlda)
  1277. {
  1278. /* Read magnetic field data */
  1279. memset(temp_data_raw[0].i16bit, 0x00, 3 * sizeof(int16_t));
  1280. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx,temp_data_raw[0].i16bit);
  1281. p_data->acc[0] = temp_data_raw[0].i16bit[0];
  1282. p_data->acc[1] = temp_data_raw[0].i16bit[1];
  1283. p_data->acc[2] = temp_data_raw[0].i16bit[2];
  1284. }
  1285. else return -1;
  1286. }
  1287. else
  1288. {
  1289. return -1;
  1290. }
  1291. return 0;
  1292. }
  1293. /**
  1294. @brief LSM自检步骤拆分——1,执行玩需等待100ms稳定输出才能执行步骤2
  1295. @param 无
  1296. @return 错误代码 - [out] -1失败,0成功
  1297. */
  1298. int drv_lsm_self_check_1(void)
  1299. {
  1300. /* Check device ID */
  1301. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  1302. if(whoamI != LSM6DS3TR_C_ID)
  1303. {
  1304. DEBUG_LOG("LSM self_check error!!!\r\n");
  1305. return -1;
  1306. }
  1307. /* Restore default configuration */
  1308. lsm6ds3tr_c_reset_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1309. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1310. do {
  1311. lsm6ds3tr_c_reset_get(&ob_lsm6ds3tr_c.ctx, &rst);
  1312. ob_lsm6ds3tr_c.op_timeout--;
  1313. } while(rst && ob_lsm6ds3tr_c.op_timeout!=0);
  1314. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1315. /* Accelerometer Self Test */
  1316. /* Set Output Data Rate */
  1317. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_833Hz);
  1318. /* Set full scale */
  1319. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_4g);
  1320. return 0;
  1321. }
  1322. /**
  1323. @brief LSM自检步骤拆分——2,执行玩需等待100ms稳定输出才能执行步骤3
  1324. @param 无
  1325. @return 错误代码 - [out] -1失败,0成功
  1326. */
  1327. int drv_lsm_self_check_2(void)
  1328. {
  1329. /* Check if new value available */
  1330. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1331. do {
  1332. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1333. ob_lsm6ds3tr_c.op_timeout--;
  1334. } while(!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1335. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1336. /* Read dummy data and discard it */
  1337. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1338. /* Read 5 sample and get the average vale for each axis */
  1339. memset(val_st_off, 0x00, 3 * sizeof(float));
  1340. for (i = 0; i < 5; i++) {
  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 data and accumulate the mg value */
  1349. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1350. for (j = 0; j < 3; j++) {
  1351. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  1352. }
  1353. }
  1354. /* Calculate the mg average values */
  1355. for (i = 0; i < 3; i++) {
  1356. val_st_off[i] /= 5.0f;
  1357. }
  1358. /* Enable Self Test positive (or negative) */
  1359. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  1360. return 0;
  1361. }
  1362. /**
  1363. @brief LSM自检步骤拆分——3,执行玩需等待150ms稳定输出才能执行步骤4
  1364. @param 无
  1365. @return 错误代码 - [out] -1失败,0成功
  1366. */
  1367. int drv_lsm_self_check_3(void)
  1368. {
  1369. /* Check if new value available */
  1370. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1371. do {
  1372. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1373. ob_lsm6ds3tr_c.op_timeout--;
  1374. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1375. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1376. /* Read dummy data and discard it */
  1377. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1378. /* Read 5 sample and get the average vale for each axis */
  1379. memset(val_st_on, 0x00, 3 * sizeof(float));
  1380. for (i = 0; i < 5; i++) {
  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 data and accumulate the mg value */
  1389. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1390. for (j = 0; j < 3; j++) {
  1391. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(self_check_data_raw[j]);
  1392. }
  1393. }
  1394. /* Calculate the mg average values */
  1395. for (i = 0; i < 3; i++) {
  1396. val_st_on[i] /= 5.0f;
  1397. }
  1398. /* Calculate the mg values for self test */
  1399. for (i = 0; i < 3; i++) {
  1400. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  1401. }
  1402. /* Check self test limit */
  1403. st_result = ST_PASS;
  1404. for (i = 0; i < 3; i++) {
  1405. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  1406. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  1407. st_result = ST_FAIL;
  1408. }
  1409. }
  1410. /* Disable Self Test */
  1411. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  1412. /* Disable sensor. */
  1413. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  1414. /* Gyroscope Self Test. */
  1415. /* Set Output Data Rate */
  1416. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_833Hz);
  1417. /* Set full scale */
  1418. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  1419. return 0;
  1420. }
  1421. /**
  1422. @brief LSM自检步骤拆分——4,执行玩需等待50ms稳定输出才能执行步骤5
  1423. @param 无
  1424. @return 错误代码 - [out] -1失败,0成功
  1425. */
  1426. int drv_lsm_self_check_4(void)
  1427. {
  1428. /* Check if new value available */
  1429. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1430. do {
  1431. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1432. ob_lsm6ds3tr_c.op_timeout--;
  1433. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1434. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1435. /* Read dummy data and discard it */
  1436. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1437. /* Read 5 sample and get the average vale for each axis */
  1438. memset(val_st_off, 0x00, 3 * sizeof(float));
  1439. for (i = 0; i < 5; i++) {
  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 data and accumulate the mg value */
  1448. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1449. for (j = 0; j < 3; j++) {
  1450. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  1451. self_check_data_raw[j]);
  1452. }
  1453. }
  1454. /* Calculate the mg average values */
  1455. for (i = 0; i < 3; i++) {
  1456. val_st_off[i] /= 5.0f;
  1457. }
  1458. /* Enable Self Test positive (or negative) */
  1459. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  1460. return 0;
  1461. }
  1462. /**
  1463. @brief LSM自检步骤拆分——5,获取自检结果。
  1464. @param 无
  1465. @return 错误代码 - [out] -1失败,0成功
  1466. */
  1467. int drv_lsm_self_check_5(void)
  1468. {
  1469. /* Read 5 sample and get the average vale for each axis */
  1470. memset(val_st_on, 0x00, 3 * sizeof(float));
  1471. for (i = 0; i < 5; i++) {
  1472. /* Check if new value available */
  1473. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  1474. do {
  1475. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  1476. ob_lsm6ds3tr_c.op_timeout--;
  1477. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  1478. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  1479. /* Read data and accumulate the mg value */
  1480. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, self_check_data_raw);
  1481. for (j = 0; j < 3; j++) {
  1482. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  1483. self_check_data_raw[j]);
  1484. }
  1485. }
  1486. /* Calculate the mg average values */
  1487. for (i = 0; i < 3; i++) {
  1488. val_st_on[i] /= 5.0f;
  1489. }
  1490. /* Calculate the mg values for self test */
  1491. for (i = 0; i < 3; i++) {
  1492. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  1493. }
  1494. /* Check self test limit */
  1495. for (i = 0; i < 3; i++) {
  1496. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  1497. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  1498. st_result = ST_FAIL;
  1499. }
  1500. }
  1501. /* Disable Self Test */
  1502. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  1503. /* Disable sensor. */
  1504. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1505. if (st_result == ST_PASS) {
  1506. DEBUG_LOG("LSM Self Test - PASS\r\n");
  1507. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  1508. rst = 0Xff;
  1509. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  1510. if(rst != 1)return -1;
  1511. }else{
  1512. DEBUG_LOG("Self Test - FAIL\r\n");
  1513. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  1514. rst = 0Xff;
  1515. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&rst);
  1516. if(rst != 1)return -1;
  1517. return -1;
  1518. }
  1519. return 0;
  1520. }