drv_lsm6ds3tr_c.c 63 KB

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