drv_lsm6ds3tr_c.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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 data_raw[4]; //临时存储原始数据
  61. static uint32_t spi_write_error_counter = 0; //spi写错误上报
  62. static uint32_t spi_read_error_counter = 0; //spi读错误上报
  63. /*Local Functions ----------------------------------------------*/
  64. static void platform_delay_ms(uint32_t ms)
  65. {
  66. nrf_delay_ms(ms);
  67. }
  68. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  69. {
  70. int32_t ierror = 0;
  71. if(SPI0_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg, (uint8_t *)bufp, len))
  72. {
  73. ierror = -1;
  74. spi_write_error_counter++;
  75. }
  76. return ierror;
  77. }
  78. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  79. {
  80. int32_t ierror = 0;
  81. uint32_t errcode = SPI0_OnlyReadReg(BOARD_SPI0_CS0_IO, reg, bufp, len);
  82. if(errcode)
  83. {
  84. ierror = -1;
  85. spi_read_error_counter++;
  86. }
  87. return ierror;
  88. }
  89. static void drv_lsm_spi_error_report_process(void)
  90. {
  91. char buff[30]={0};
  92. if(spi_write_error_counter > 0)
  93. {
  94. sprintf(buff,"spi_write_err,%d\r\n",spi_write_error_counter);
  95. Except_TxError(EXCEPT_SPI_RW,(const char *)buff);
  96. }
  97. if(spi_read_error_counter > 0)
  98. {
  99. sprintf(buff,"spi_read_err,%d\r\n",spi_read_error_counter);
  100. Except_TxError(EXCEPT_SPI_RW,(const char *)buff);
  101. }
  102. spi_write_error_counter = 0;
  103. spi_read_error_counter = 0;
  104. }
  105. static void LSM_IIC_Start(void)
  106. {
  107. LSM_IIC_SDA_SET;
  108. LSM_IIC_SCL_SET;
  109. nrf_delay_us(5);
  110. LSM_IIC_SDA_CLR;
  111. nrf_delay_us(5);
  112. LSM_IIC_SCL_CLR;
  113. }
  114. static void LSM_IIC_Stop(void)
  115. {
  116. LSM_IIC_SCL_CLR;
  117. LSM_IIC_SDA_CLR;
  118. nrf_delay_us(5);
  119. LSM_IIC_SCL_SET;
  120. LSM_IIC_SDA_SET;
  121. nrf_delay_us(5);
  122. }
  123. static void LSM_IIC_ACK(void)
  124. {
  125. LSM_IIC_SCL_CLR;
  126. nrf_delay_us(1);
  127. LSM_IIC_SDA_CLR;
  128. nrf_delay_us(1);
  129. LSM_IIC_SCL_SET;
  130. nrf_delay_us(1);
  131. LSM_IIC_SCL_CLR;
  132. }
  133. static void LSM_IIC_NoACK(void)
  134. {
  135. LSM_IIC_SCL_CLR;
  136. LSM_IIC_SDA_SET;
  137. nrf_delay_us(1);
  138. LSM_IIC_SCL_SET;
  139. nrf_delay_us(1);
  140. LSM_IIC_SCL_CLR;
  141. }
  142. static bool LSM_IIC_WaitACK(void)
  143. {
  144. uint8_t tim = 0;
  145. // IIC_BACK_SCL_CLR;
  146. LSM_IIC_SDA_SET;
  147. nrf_delay_us(1);
  148. LSM_IIC_SCL_SET;
  149. // nrf_delay_us(1);
  150. while(LSM_IIC_SDA_READ){
  151. if(++tim>=50){
  152. LSM_IIC_Stop();
  153. return false;
  154. }
  155. nrf_delay_us(1);
  156. }
  157. LSM_IIC_SCL_CLR;
  158. return true;
  159. }
  160. static void LSM_IIC_SendByte(uint8_t _byte)
  161. {
  162. uint8_t i = 0;
  163. for (i=0; i<8; i++){
  164. LSM_IIC_SCL_CLR;
  165. nrf_delay_us(5);
  166. if (_byte&0x80) LSM_IIC_SDA_SET;
  167. else LSM_IIC_SDA_CLR;
  168. _byte <<= 1;
  169. LSM_IIC_SCL_SET;
  170. nrf_delay_us(5);
  171. }
  172. LSM_IIC_SCL_CLR;
  173. }
  174. static uint8_t LSM_IIC_RecByte(void)
  175. {
  176. uint8_t i = 0;
  177. uint8_t rec_byte;
  178. LSM_IIC_SDA_SET;
  179. for (i=0; i<8; i++){
  180. rec_byte <<= 1;
  181. LSM_IIC_SCL_CLR;
  182. nrf_delay_us(5);
  183. LSM_IIC_SCL_SET;
  184. nrf_delay_us(5);
  185. if (LSM_IIC_SDA_READ) rec_byte |= 0x01;
  186. }
  187. LSM_IIC_SCL_CLR;
  188. return rec_byte;
  189. }
  190. /********************************************/
  191. static bool LSM_IIC_WriteBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
  192. {
  193. uint8_t i = 0;
  194. LSM_IIC_Start();
  195. LSM_IIC_SendByte(add);
  196. if(!LSM_IIC_WaitACK()) return false;
  197. LSM_IIC_SendByte(reg);
  198. if(!LSM_IIC_WaitACK()) return false;
  199. for(i=0;i<len;i++){
  200. LSM_IIC_SendByte(p[i]);
  201. if(!LSM_IIC_WaitACK()) return false;
  202. }
  203. LSM_IIC_Stop();
  204. return true;
  205. }
  206. static bool LSM_IIC_ReadBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
  207. {
  208. uint8_t i = 0;
  209. LSM_IIC_Start();
  210. LSM_IIC_SendByte(add);
  211. if(!LSM_IIC_WaitACK()) return false;
  212. LSM_IIC_SendByte(reg);
  213. if(!LSM_IIC_WaitACK()) return false;
  214. LSM_IIC_Start();
  215. LSM_IIC_SendByte(add+1);
  216. if(!LSM_IIC_WaitACK()) return false;
  217. for(i=0;i<len-1;i++){
  218. p[i] = LSM_IIC_RecByte(); LSM_IIC_ACK();
  219. }
  220. p[i] = LSM_IIC_RecByte(); LSM_IIC_NoACK();
  221. LSM_IIC_Stop();
  222. return true;
  223. }
  224. static void LSM_IIC_Init(void)
  225. {
  226. nrf_gpio_cfg(
  227. LSM_IIC_SCL,
  228. NRF_GPIO_PIN_DIR_OUTPUT,
  229. NRF_GPIO_PIN_INPUT_DISCONNECT,
  230. NRF_GPIO_PIN_NOPULL,
  231. NRF_GPIO_PIN_S0S1,
  232. NRF_GPIO_PIN_NOSENSE);
  233. nrf_gpio_cfg_watcher(LSM_IIC_SCL);
  234. nrf_gpio_pin_set(LSM_IIC_SCL);
  235. nrf_gpio_cfg(
  236. LSM_IIC_SDA,
  237. NRF_GPIO_PIN_DIR_OUTPUT,
  238. NRF_GPIO_PIN_INPUT_DISCONNECT,
  239. NRF_GPIO_PIN_NOPULL,
  240. NRF_GPIO_PIN_S0D1,
  241. NRF_GPIO_PIN_NOSENSE);
  242. nrf_gpio_cfg_watcher(LSM_IIC_SDA);
  243. nrf_gpio_pin_set(LSM_IIC_SDA);
  244. nrf_gpio_cfg_output(LSM_IIC_NCS);nrf_gpio_pin_write(LSM_IIC_NCS,1);
  245. nrf_gpio_cfg_output(LSM_IIC_SA0);
  246. }
  247. static void LSM_IIC_UnInit(void)
  248. {
  249. nrf_gpio_cfg_default(LSM_IIC_SCL);
  250. nrf_gpio_cfg_default(LSM_IIC_SDA);
  251. nrf_gpio_cfg_default(LSM_IIC_NCS);
  252. nrf_gpio_cfg_default(LSM_IIC_SA0);
  253. }
  254. static int read_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint8_t len)
  255. {
  256. int32_t ret;
  257. uint8_t val;
  258. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  259. lsm6ds3tr_c_odr_xl_t acc_odr;
  260. lsm6ds3tr_c_odr_g_t gry_odr;
  261. lsm6ds3tr_c_ctrl10_c_t ctrl10_c;
  262. lsm6ds3tr_c_pull_up_en_t up_en;
  263. //关闭ACC
  264. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  265. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&acc_odr);
  266. if(acc_odr != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  267. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  268. //关闭GRY
  269. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  270. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&gry_odr);
  271. if(gry_odr != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  272. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  273. //关闭FIFO
  274. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  275. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  276. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  277. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  278. //设置hub不使用iic
  279. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  280. val = 0xFF;
  281. lsm6ds3tr_c_sh_master_get(&ob_lsm6ds3tr_c.ctx, &val);
  282. if(val != PROPERTY_DISABLE)return -1;
  283. //设置hub总开关为关闭
  284. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  285. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_CTRL10_C,(uint8_t *)&ctrl10_c, 1);
  286. if(ctrl10_c.func_en != PROPERTY_DISABLE)return -1;
  287. //开启直通模式
  288. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  289. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  290. if(val != 1)return -1;
  291. //开启内部上拉
  292. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  293. lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  294. if(up_en != LSM6DS3TR_C_INTERNAL_PULL_UP)return -1;
  295. //SPI切换为IIC
  296. SPI0_Disable();
  297. LSM_IIC_Init();
  298. if(LSM_IIC_ReadBytes(device_addr,reg,data,len))
  299. {
  300. ret = 0;
  301. }
  302. else
  303. {
  304. ret = -1;
  305. SEGGER_RTT_printf(0,"-------->LSM_IIC_ReadBytes err\n");
  306. }
  307. //IIC切换为SPI
  308. LSM_IIC_UnInit();
  309. SPI0_Init();
  310. //关闭直通模式
  311. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  312. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  313. if(val != 0)return -1;
  314. //关闭内部上拉
  315. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_EXT_PULL_UP);
  316. up_en = 0xFF;
  317. lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  318. if(up_en != LSM6DS3TR_C_EXT_PULL_UP)return -1;
  319. return ret;
  320. }
  321. static int write_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint8_t len)
  322. {
  323. int32_t ret;
  324. uint8_t val;
  325. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  326. lsm6ds3tr_c_odr_xl_t acc_odr;
  327. lsm6ds3tr_c_odr_g_t gry_odr;
  328. lsm6ds3tr_c_ctrl10_c_t ctrl10_c;
  329. lsm6ds3tr_c_pull_up_en_t up_en;
  330. //关闭ACC
  331. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  332. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&acc_odr);
  333. if(acc_odr != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  334. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  335. //关闭GRY
  336. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  337. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&gry_odr);
  338. if(gry_odr != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  339. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  340. //关闭FIFO
  341. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  342. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  343. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  344. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  345. //设置hub不使用iic
  346. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  347. val = 0xFF;
  348. lsm6ds3tr_c_sh_master_get(&ob_lsm6ds3tr_c.ctx, &val);
  349. if(val != PROPERTY_DISABLE)return -1;
  350. //设置hub总开关为关闭
  351. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  352. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_CTRL10_C,(uint8_t *)&ctrl10_c, 1);
  353. if(ctrl10_c.func_en != PROPERTY_DISABLE)return -1;
  354. //开启直通模式
  355. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  356. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  357. if(val != 1)return -1;
  358. //开启内部上拉
  359. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  360. lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  361. if(up_en != LSM6DS3TR_C_INTERNAL_PULL_UP)return -1;
  362. //SPI切换为IIC
  363. SPI0_Disable();
  364. LSM_IIC_Init();
  365. if(LSM_IIC_WriteBytes(device_addr,reg,data,len))
  366. {
  367. ret = 0;
  368. }
  369. else
  370. {
  371. ret = -1;
  372. SEGGER_RTT_printf(0,"-------->LSM_IIC_WriteBytes err\n");
  373. }
  374. //IIC切换为SPI
  375. LSM_IIC_UnInit();
  376. SPI0_Init();
  377. //关闭直通模式
  378. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  379. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  380. if(val != 0)return -1;
  381. //关闭内部上拉
  382. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_EXT_PULL_UP);
  383. up_en = 0xFF;
  384. lsm6ds3tr_c_sh_pin_mode_get(&ob_lsm6ds3tr_c.ctx,&up_en);
  385. if(up_en != LSM6DS3TR_C_EXT_PULL_UP)return -1;
  386. return ret;
  387. }
  388. static int self_check(void)
  389. {
  390. uint8_t whoamI, rst;
  391. int16_t data_raw[3];
  392. float val_st_off[3];
  393. float val_st_on[3];
  394. float test_val[3];
  395. uint8_t st_result;
  396. uint8_t drdy;
  397. uint8_t i;
  398. uint8_t j;
  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, 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, data_raw);
  445. for (j = 0; j < 3; j++) {
  446. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(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, 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, data_raw);
  478. for (j = 0; j < 3; j++) {
  479. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(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, 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, data_raw);
  530. for (j = 0; j < 3; j++) {
  531. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  532. 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, data_raw);
  555. for (j = 0; j < 3; j++) {
  556. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  557. 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. int ret;
  679. uint8_t val;
  680. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  681. nrf_gpio_cfg_input(BOARD_SPI0_MISO_IO, (nrf_gpio_pin_pull_t)NRFX_SPIM_MISO_PULL_CFG);
  682. //初始化结构体
  683. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  684. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  685. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  686. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  687. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  688. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  689. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  690. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  691. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  692. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  693. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  694. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  695. ob_lsm6ds3tr_c.op_timeout = 0;
  696. //关闭SPI
  697. SPI0_Disable(); //注意:若SPI线短路,该函数内部实现有个while循环导致卡死。已修改源文件规避。
  698. //重新初始化SPI
  699. SPI0_Init();
  700. ret = self_check();
  701. if(ret != 0)return -1;
  702. val = 0xff;
  703. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  704. if(val != 1)return -1;
  705. return 0;
  706. }
  707. /**
  708. @brief 设置加速度工作模式
  709. @param acc_power_mode - [in] 加速度工作模式
  710. @return 错误代码 - [out] -1失败,0成功
  711. */
  712. int drv_lsm_set_acc_power_mode(LSM_ACC_POWER_MODE_e acc_power_mode)
  713. {
  714. lsm6ds3tr_c_xl_hm_mode_t val;
  715. if(ob_lsm6ds3tr_c.cur_param.acc_power_mode != acc_power_mode)
  716. {
  717. switch(acc_power_mode)
  718. {
  719. case LSM_ACC_POWER_MODE_HIGH_PERFORMANCE:
  720. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_HIGH_PERFORMANCE);
  721. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  722. if(val != LSM6DS3TR_C_XL_HIGH_PERFORMANCE)return -1;
  723. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  724. break;
  725. case LSM_ACC_POWER_MODE_NORMAL:
  726. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_NORMAL);
  727. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  728. if(val != LSM6DS3TR_C_XL_NORMAL)return -1;
  729. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_NORMAL;
  730. break;
  731. }
  732. }
  733. return 0;
  734. }
  735. /**
  736. @brief 设置陀螺仪工作模式
  737. @param gry_power_mode - [in] 陀螺仪工作模式
  738. @return 错误代码 - [out] -1失败,0成功
  739. */
  740. int drv_lsm_set_gry_power_mode(LSM_GRY_POWER_MODE_e gry_power_mode)
  741. {
  742. lsm6ds3tr_c_g_hm_mode_t val;
  743. if(ob_lsm6ds3tr_c.cur_param.gry_power_mode != gry_power_mode)
  744. {
  745. switch(gry_power_mode)
  746. {
  747. case LSM_GRY_POWER_MODE_HIGH_PERFORMANCE:
  748. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
  749. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  750. if(val != LSM_GRY_POWER_MODE_HIGH_PERFORMANCE)return -1;
  751. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  752. break;
  753. case LSM_GRY_POWER_MODE_NORMAL:
  754. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_NORMAL);
  755. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  756. if(val != LSM_GRY_POWER_MODE_NORMAL)return -1;
  757. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_NORMAL;
  758. break;
  759. }
  760. }
  761. return 0;
  762. }
  763. /**
  764. @brief 设置时间戳精度
  765. @param timestamp_resolution - [in] 时间戳精度
  766. @return 错误代码 - [out] -1失败,0成功
  767. */
  768. int drv_lsm_set_timestamp_resolution(LSM_TIMESTAMP_RESOLUTION_e timestamp_resolution)
  769. {
  770. lsm6ds3tr_c_timer_hr_t val;
  771. if(ob_lsm6ds3tr_c.cur_param.timestamp_resolution != timestamp_resolution)
  772. {
  773. switch(timestamp_resolution)
  774. {
  775. case LSM_TIMESTAMP_6MS4:
  776. /* Set High Resolution Timestamp (6.4ms) */
  777. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_6ms4);
  778. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  779. if(val != LSM6DS3TR_C_LSB_6ms4)return -1;
  780. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  781. break;
  782. case LSM_TIMESTAMP_25US:
  783. /* Set High Resolution Timestamp (25 us tick) */
  784. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_25us);
  785. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  786. if(val != LSM6DS3TR_C_LSB_25us)return -1;
  787. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_25US;
  788. break;
  789. }
  790. }
  791. return 0;
  792. }
  793. /**
  794. @brief 设置时间戳开关
  795. @param timestamp_switch - [in] 时间戳开关
  796. @return 错误代码 - [out] -1失败,0成功
  797. */
  798. int drv_lsm_set_timestamp_switch(LSM_TIMESTAMP_SWITCH_e timestamp_switch)
  799. {
  800. uint8_t val;
  801. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch != timestamp_switch)
  802. {
  803. switch(timestamp_switch)
  804. {
  805. case LSM_TIMESTAMP_ON:
  806. /* Enable timestamp in HW */
  807. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  808. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  809. if(val != PROPERTY_ENABLE)return -1;
  810. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_ON;
  811. break;
  812. case LSM_TIMESTAMP_OFF:
  813. /*Disable timestamp in HW */
  814. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  815. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  816. if(val != PROPERTY_DISABLE)return -1;
  817. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  818. break;
  819. }
  820. }
  821. return 0;
  822. }
  823. /**
  824. @brief 设置FIFO采样频率
  825. @param fifo_odr - [in] FIFO采样频率
  826. @return 错误代码 - [out] -1失败,0成功
  827. */
  828. 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)
  829. {
  830. uint8_t val;
  831. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  832. lsm6ds3tr_c_fifo_mode_t r_fifo_mode;
  833. lsm6ds3tr_c_dec_fifo_xl_t r_fifo_xl;
  834. lsm6ds3tr_c_dec_fifo_gyro_t r_fifo_gy;
  835. lsm6ds3tr_c_dec_ds3_fifo_t r_fifo_ds3;
  836. lsm6ds3tr_c_dec_ds4_fifo_t r_fifo_ds4;
  837. uint16_t fifo_watermark;
  838. uint16_t pattern_len = 0;
  839. if(ob_lsm6ds3tr_c.cur_param.fifo_odr != fifo_odr)
  840. {
  841. switch(fifo_odr)
  842. {
  843. case LSM_FIFO_ODR_OFF:
  844. //禁用步进计数器和时间戳数据作为第4步FIFO数据集
  845. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  846. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  847. if(val != PROPERTY_DISABLE)return -1;
  848. /* Set ODR FIFO */
  849. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  850. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  851. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  852. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  853. break;
  854. case LSM_FIFO_ODR_104HZ:
  855. //启用步进计数器和时间戳数据作为第4步FIFO数据集
  856. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  857. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  858. if(val != PROPERTY_ENABLE)return -1;
  859. /* Set FIFO mode to Stream mode */
  860. lsm6ds3tr_c_fifo_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_STREAM_MODE);
  861. lsm6ds3tr_c_fifo_mode_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_mode);
  862. if(r_fifo_mode != LSM6DS3TR_C_STREAM_MODE)return -1;
  863. /* Set FIFO sensor decimator */
  864. if(acc_odr == LSM_ACC_ODR_104HZ)
  865. {
  866. lsm6ds3tr_c_fifo_xl_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//acc和fifo的HZ一致
  867. lsm6ds3tr_c_fifo_xl_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_xl);
  868. if(r_fifo_xl != LSM6DS3TR_C_FIFO_XL_NO_DEC)return -1;
  869. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  870. }
  871. if(gry_odr == LSM_GRY_ODR_104HZ)
  872. {
  873. lsm6ds3tr_c_fifo_gy_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_GY_NO_DEC);//gry和fifo的HZ一致
  874. lsm6ds3tr_c_fifo_gy_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_gy);
  875. if(r_fifo_gy != LSM6DS3TR_C_FIFO_GY_NO_DEC)return -1;
  876. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  877. }
  878. if(mag_odr != LSM_MAG_ODR_OFF)
  879. {
  880. lsm6ds3tr_c_fifo_dataset_3_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//mag和fifo的HZ一致
  881. lsm6ds3tr_c_fifo_dataset_3_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds3);
  882. if(r_fifo_ds3 != LSM6DS3TR_C_FIFO_DS3_NO_DEC)return -1;
  883. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  884. }
  885. if(timestamp_switch == LSM_TIMESTAMP_ON)
  886. {
  887. lsm6ds3tr_c_fifo_dataset_4_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  888. lsm6ds3tr_c_fifo_dataset_4_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds4);
  889. if(r_fifo_ds4 != LSM6DS3TR_C_FIFO_DS4_NO_DEC)return -1;
  890. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  891. }
  892. /* Set FIFO watermark to a multiple of a pattern */
  893. lsm6ds3tr_c_fifo_watermark_set(&ob_lsm6ds3tr_c.ctx, pattern_len);
  894. lsm6ds3tr_c_fifo_watermark_get(&ob_lsm6ds3tr_c.ctx,&fifo_watermark);
  895. if(fifo_watermark != pattern_len)return -1;
  896. /* Set ODR FIFO */
  897. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_104Hz);//该参数关联到timestamp
  898. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  899. if(r_fifo_odr != LSM6DS3TR_C_FIFO_104Hz) return -1;
  900. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_104HZ;
  901. break;
  902. }
  903. }
  904. return 0;
  905. }
  906. /**
  907. @brief 设置ACC量程
  908. @param acc_fs - [in] ACC量程
  909. @return 错误代码 - [out] -1失败,0成功
  910. */
  911. int drv_lsm_set_acc_fs(LSM_ACC_FS_e acc_fs)
  912. {
  913. lsm6ds3tr_c_fs_xl_t val;
  914. if(ob_lsm6ds3tr_c.cur_param.acc_fs != acc_fs)
  915. {
  916. switch(acc_fs)
  917. {
  918. case LSM_ACC_FS_2G:
  919. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2g);
  920. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  921. if(val != LSM6DS3TR_C_2g)return -1;
  922. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  923. break;
  924. case LSM_ACC_FS_16G:
  925. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_16g);
  926. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  927. if(val != LSM6DS3TR_C_16g)return -1;
  928. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_16G;
  929. break;
  930. }
  931. }
  932. return 0;
  933. }
  934. /**
  935. @brief 设置ACC采样频率
  936. @param acc_odr - [in] ACC采样频率
  937. @return 错误代码 - [out] -1失败,0成功
  938. */
  939. int drv_lsm_set_acc_odr(LSM_ACC_ODR_e acc_odr)
  940. {
  941. lsm6ds3tr_c_odr_xl_t val;
  942. if(ob_lsm6ds3tr_c.cur_param.acc_odr != acc_odr)
  943. {
  944. switch(acc_odr)
  945. {
  946. case LSM_ACC_ODR_OFF:
  947. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  948. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  949. if(val != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  950. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  951. break;
  952. case LSM_ACC_ODR_104HZ:
  953. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  954. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  955. if(val != LSM6DS3TR_C_XL_ODR_104Hz)return -1;
  956. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_104HZ;
  957. break;
  958. case LSM_ACC_ODR_12HZ5:
  959. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  960. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  961. if(val != LSM6DS3TR_C_XL_ODR_12Hz5)return -1;
  962. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_12HZ5;
  963. break;
  964. }
  965. }
  966. return 0;
  967. }
  968. /**
  969. @brief 设置GRY量程
  970. @param gry_fs - [in] GRY量程
  971. @return 错误代码 - [out] -1失败,0成功
  972. */
  973. int drv_lsm_set_gry_fs(LSM_GRY_FS_e gry_fs)
  974. {
  975. lsm6ds3tr_c_fs_g_t val;
  976. if(ob_lsm6ds3tr_c.cur_param.gry_fs != gry_fs)
  977. {
  978. switch(gry_fs)
  979. {
  980. case LSM_GRY_FS_250DPS:
  981. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_250dps);
  982. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  983. if(val != LSM6DS3TR_C_250dps)return -1;
  984. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  985. break;
  986. case LSM_GRY_FS_2000DPS:
  987. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  988. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  989. if(val != LSM6DS3TR_C_2000dps)return -1;
  990. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_2000DPS;
  991. break;
  992. }
  993. }
  994. return 0;
  995. }
  996. /**
  997. @brief 设置GRY采样频率
  998. @param gry_odr - [in] GRY采样频率
  999. @return 错误代码 - [out] -1失败,0成功
  1000. */
  1001. int drv_lsm_set_gry_odr(LSM_GRY_ODR_e gry_odr)
  1002. {
  1003. lsm6ds3tr_c_odr_g_t val;
  1004. if(ob_lsm6ds3tr_c.cur_param.gry_odr != gry_odr)
  1005. {
  1006. switch(gry_odr)
  1007. {
  1008. case LSM_GRY_ODR_OFF:
  1009. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  1010. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1011. if(val != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  1012. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  1013. break;
  1014. case LSM_GRY_ODR_104HZ:
  1015. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_104Hz);
  1016. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1017. if(val != LSM6DS3TR_C_GY_ODR_104Hz)return -1;
  1018. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_104HZ;
  1019. break;
  1020. case LSM_GRY_ODR_12HZ5:
  1021. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  1022. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  1023. if(val != LSM6DS3TR_C_GY_ODR_12Hz5)return -1;
  1024. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_12HZ5;
  1025. break;
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. /**
  1031. @brief 获取配置MAG量程需要的步骤数量
  1032. @return 错误代码 - [out] 配置MAG量程需要的步骤数量
  1033. */
  1034. int drv_lsm_get_mag_fs_flow(void)
  1035. {
  1036. return 1;
  1037. }
  1038. /**
  1039. @brief 设置MAG量程
  1040. @param mag_fs - [in] MAG量程
  1041. @param flow - [in] 当前需要处理的步骤
  1042. @return 错误代码 - [out] -1失败,0成功
  1043. */
  1044. int drv_lsm_set_mag_fs(LSM_MAG_FS_e mag_fs, int flow)
  1045. {
  1046. int ret;
  1047. uint8_t data;
  1048. if(flow <= 0 || flow >= 2)return -1;
  1049. if(ob_lsm6ds3tr_c.cur_param.mag_fs != mag_fs)
  1050. {
  1051. switch(mag_fs)
  1052. {
  1053. case LSM_MAG_FS_30GS:
  1054. data = LSM_MAG_FS_30GS;
  1055. ret = write_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1056. if(ret == -1)return -1;
  1057. data = 0xFF;
  1058. ret = read_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  1059. if(ret == -1 || data != LSM_MAG_FS_30GS)return -1;
  1060. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  1061. break;
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. /**
  1067. @brief 获取配置MAG采样频率需要的步骤数量
  1068. @return 错误代码 - [out] 配置MAG采样频率需要的步骤数量
  1069. */
  1070. int drv_lsm_get_mag_odr_flow(void)
  1071. {
  1072. return 5;
  1073. }
  1074. /**
  1075. @brief 设置MAG采样频率
  1076. @param mag_odr - [in] MAG采样频率
  1077. @param set_hub - [in] 是否配置hub
  1078. @param flow - [in] 当前需要处理的步骤
  1079. @return 错误代码 - [out] -1失败,0成功
  1080. */
  1081. int drv_lsm_set_mag_odr(LSM_MAG_ODR_e mag_odr, bool set_hub, int flow)
  1082. {
  1083. int ret;
  1084. uint8_t data;
  1085. if(flow <= 0 || flow >= 6)return -1;
  1086. if(ob_lsm6ds3tr_c.cur_param.mag_odr != mag_odr)
  1087. {
  1088. switch(flow)
  1089. {
  1090. case 1:
  1091. //define the sign for x y and z axis
  1092. if(mag_odr != LSM_MAG_ODR_OFF)
  1093. {
  1094. data = 0x06;
  1095. ret = write_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1096. }
  1097. else
  1098. {
  1099. ret = 0;
  1100. }
  1101. break;
  1102. case 2:
  1103. //define the sign for x y and z axis
  1104. if(mag_odr != LSM_MAG_ODR_OFF)
  1105. {
  1106. data = 0xFF;
  1107. ret = read_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  1108. if(ret == -1 || data != 0x06)ret = -1;
  1109. else ret = 0;
  1110. }
  1111. else
  1112. {
  1113. ret = 0;
  1114. }
  1115. break;
  1116. case 3:
  1117. //set odr
  1118. data = mag_odr;
  1119. ret = write_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1120. break;
  1121. case 4:
  1122. data = 0xFF;
  1123. ret = read_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1124. if(ret == -1 || data != mag_odr)ret = -1;
  1125. else{
  1126. ret = 0;
  1127. }
  1128. break;
  1129. case 5:
  1130. //是否配置hub
  1131. if(set_hub)
  1132. {
  1133. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  1134. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &sub_qmc6310);
  1135. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_BANK_A);
  1136. data = 0xFF;
  1137. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  1138. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  1139. if(data != QMC6310_DATA_OUT_X_LSB_REG)return -1;
  1140. /* Configure Sensor Hub to read one slaves */
  1141. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  1142. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  1143. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1144. ret = 0;
  1145. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1146. }
  1147. else{
  1148. ret = 0;
  1149. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1150. }
  1151. break;
  1152. }
  1153. }
  1154. else
  1155. {
  1156. return 0;
  1157. }
  1158. return ret;
  1159. }
  1160. /**
  1161. @brief 获取LSM配置参数
  1162. @param param - [in] LSM配置参数
  1163. @return 错误代码 - [out] -1失败,0成功
  1164. */
  1165. int drv_lsm_get_config_param(drv_lsm_config_param_t *p_param)
  1166. {
  1167. *p_param = ob_lsm6ds3tr_c.cur_param;
  1168. return 0;
  1169. }
  1170. /**
  1171. @brief 当前LSM的FIFO有几组数据
  1172. @return 错误代码 - [out] FIFO当前有几组数据
  1173. */
  1174. int drv_lsm_get_fifo_group_num(void)
  1175. {
  1176. uint8_t wt;
  1177. uint16_t num = 0;
  1178. uint16_t num_pattern = 0;
  1179. uint16_t pattern_len = 0;
  1180. /* Read FIFO watermark flag in polling mode */
  1181. lsm6ds3tr_c_fifo_wtm_flag_get(&ob_lsm6ds3tr_c.ctx, &wt);
  1182. if(wt)
  1183. {
  1184. /* Read number of word in FIFO */
  1185. lsm6ds3tr_c_fifo_data_level_get(&ob_lsm6ds3tr_c.ctx, &num);
  1186. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1187. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  1188. }
  1189. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1190. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  1191. }
  1192. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1193. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  1194. }
  1195. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1196. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  1197. }
  1198. num_pattern = num / pattern_len;
  1199. }
  1200. return num_pattern;
  1201. }
  1202. /**
  1203. @brief 获取LSM的FIFO数据
  1204. @param p_data - [out] 返回的FIFO数据
  1205. @return 错误代码 - [out] -1失败,0成功
  1206. */
  1207. int drv_lsm_get_fifo_data(lsm_data_t *p_data)
  1208. {
  1209. int ret;
  1210. int read_len = 0;
  1211. int read_num = 0;
  1212. //获取原始数据的长度
  1213. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1214. /* Read gyro samples len */
  1215. read_len += GRY_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1216. }
  1217. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1218. /* Read XL samples len*/
  1219. read_len += ACC_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1220. }
  1221. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1222. /* Read mag samples len*/
  1223. read_len += MAG_OUT_XYZ_WORD_NUM * sizeof(int16_t);
  1224. }
  1225. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1226. /* Read timestamp samples len*/
  1227. read_len += TIMESTAMP_OUT_WORD_NUM * sizeof(int16_t);
  1228. }
  1229. //清空缓存
  1230. memset(data_raw,0,sizeof(data_raw));
  1231. //获取原始数据
  1232. ret = lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw[0].u8bit,read_len);
  1233. if(ret == -1)return -1;
  1234. //返回数据
  1235. if(p_data != NULL)
  1236. {
  1237. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1238. /* Read gyro samples */
  1239. p_data->gry[0] = data_raw[read_num].i16bit[0];
  1240. p_data->gry[1] = data_raw[read_num].i16bit[1];
  1241. p_data->gry[2] = data_raw[read_num].i16bit[2];
  1242. read_num++;
  1243. }
  1244. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1245. /* Read XL samples */
  1246. p_data->acc[0] = data_raw[read_num].i16bit[0];
  1247. p_data->acc[1] = data_raw[read_num].i16bit[1];
  1248. p_data->acc[2] = data_raw[read_num].i16bit[2];
  1249. read_num++;
  1250. }
  1251. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1252. /* Read mag samples */
  1253. p_data->mag[0] = data_raw[read_num].i16bit[0];
  1254. p_data->mag[1] = data_raw[read_num].i16bit[1];
  1255. p_data->mag[2] = data_raw[read_num].i16bit[2];
  1256. read_num++;
  1257. }
  1258. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1259. /* 时间戳和计步数数据
  1260. 字节1 字节2 字节3 字节4 字节5 字节6
  1261. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  1262. [15:8] [23:16] [7:0] [7:0] [15:8]
  1263. */
  1264. p_data->fifo_timestamp = ((data_raw[read_num].u8bit[1] << 16) | (data_raw[read_num].u8bit[0] << 8) | (data_raw[read_num].u8bit[3]))*25;//counter == 25us
  1265. }
  1266. }
  1267. return 0;
  1268. }
  1269. /**
  1270. @brief 获取LSM的ACC数据
  1271. @param p_data - [out] 返回的ACC三轴数据
  1272. @return 错误代码 - [out] -1失败,0成功
  1273. */
  1274. int drv_lsm_get_acc_data(lsm_data_t *p_data)
  1275. {
  1276. lsm6ds3tr_c_reg_t reg;
  1277. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF)
  1278. {
  1279. /* Read samples in polling mode (no int) */
  1280. /* Read output only if new value is available */
  1281. lsm6ds3tr_c_status_reg_get(&ob_lsm6ds3tr_c.ctx, &reg.status_reg);
  1282. if(reg.status_reg.xlda)
  1283. {
  1284. /* Read magnetic field data */
  1285. memset(data_raw[0].i16bit, 0x00, 3 * sizeof(int16_t));
  1286. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx,data_raw[0].i16bit);
  1287. p_data->acc[0] = data_raw[0].i16bit[0];
  1288. p_data->acc[1] = data_raw[0].i16bit[1];
  1289. p_data->acc[2] = data_raw[0].i16bit[2];
  1290. }
  1291. else return -1;
  1292. }
  1293. else
  1294. {
  1295. return -1;
  1296. }
  1297. return 0;
  1298. }
  1299. uint8_t drv_lsm_get_id(void)
  1300. {
  1301. uint8_t whoamI = 0xFF;
  1302. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  1303. return whoamI;
  1304. }
  1305. void drv_lsm_config_printf(char *string, bool is_self_check)
  1306. {
  1307. uint8_t whoamI = 0xFF;
  1308. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  1309. lsm6ds3tr_c_odr_xl_t acc_odr = 0xFF;
  1310. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&acc_odr);
  1311. lsm6ds3tr_c_odr_g_t gry_odr = 0xFF;
  1312. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&gry_odr);
  1313. uint8_t clt = 0xFF;
  1314. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_CTRL10_C,&clt, 1);
  1315. uint8_t master = 0xFF;
  1316. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_MASTER_CONFIG,&master, 1);
  1317. lsm6ds3tr_c_odr_fifo_t fifo_odr = 0xFF;
  1318. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&fifo_odr);
  1319. lsm6ds3tr_c_fifo_mode_t fifo_mode = 0xFF;
  1320. lsm6ds3tr_c_fifo_mode_get(&ob_lsm6ds3tr_c.ctx,&fifo_mode);
  1321. lsm6ds3tr_c_dec_fifo_xl_t fifo_xl = 0xFF;
  1322. lsm6ds3tr_c_fifo_xl_batch_get(&ob_lsm6ds3tr_c.ctx,&fifo_xl);
  1323. lsm6ds3tr_c_dec_fifo_gyro_t fifo_gy = 0xFF;
  1324. lsm6ds3tr_c_fifo_gy_batch_get(&ob_lsm6ds3tr_c.ctx,&fifo_gy);
  1325. lsm6ds3tr_c_dec_ds3_fifo_t fifo_ds3 = 0xFF;
  1326. lsm6ds3tr_c_fifo_dataset_3_batch_get(&ob_lsm6ds3tr_c.ctx,&fifo_ds3);
  1327. lsm6ds3tr_c_dec_ds4_fifo_t fifo_ds4 = 0xFF;
  1328. lsm6ds3tr_c_fifo_dataset_4_batch_get(&ob_lsm6ds3tr_c.ctx,&fifo_ds4);
  1329. if(is_self_check)
  1330. {
  1331. sprintf(string,"id:0x%x; acc_odr:0x%x; gry_odr:0x%x; p_acc_odr:0x%x; p_gry_odr:0x%x; \
  1332. p_mag_odr:0x%x; clt:0x%x; master:0x%x; fifo_odr:0x%x; fifo_mode:0x%x; fifo_xl:0x%x; fifo_gy:0x%x; fifo_ds3:0x%x; fifo_ds4:0x%x; self_check:0x%x\r\n", \
  1333. whoamI, \
  1334. acc_odr, \
  1335. gry_odr, \
  1336. ob_lsm6ds3tr_c.cur_param.acc_odr, \
  1337. ob_lsm6ds3tr_c.cur_param.gry_odr, \
  1338. ob_lsm6ds3tr_c.cur_param.mag_odr, \
  1339. clt, \
  1340. master, \
  1341. fifo_odr, \
  1342. fifo_mode, \
  1343. fifo_xl, \
  1344. fifo_gy, \
  1345. fifo_ds3, \
  1346. fifo_ds4, \
  1347. self_check());
  1348. }
  1349. else
  1350. {
  1351. sprintf(string,"id:0x%x; acc_odr:0x%x; gry_odr:0x%x; p_acc_odr:0x%x; p_gry_odr:0x%x; \
  1352. p_mag_odr:0x%x; clt:0x%x; master:0x%x; fifo_odr:0x%x; fifo_mode:0x%x; fifo_xl:0x%x; fifo_gy:0x%x; fifo_ds3:0x%x; fifo_ds4:0x%x;\r\n", \
  1353. whoamI, \
  1354. acc_odr, \
  1355. gry_odr, \
  1356. ob_lsm6ds3tr_c.cur_param.acc_odr, \
  1357. ob_lsm6ds3tr_c.cur_param.gry_odr, \
  1358. ob_lsm6ds3tr_c.cur_param.mag_odr, \
  1359. clt, \
  1360. master, \
  1361. fifo_odr, \
  1362. fifo_mode, \
  1363. fifo_xl, \
  1364. fifo_gy, \
  1365. fifo_ds3, \
  1366. fifo_ds4);
  1367. }
  1368. }