drv_lsm6ds3tr_c.c 64 KB

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