drv_lsm6ds3tr_c.c 45 KB

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