drv_lsm6ds3tr_c.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /*Includes ----------------------------------------------*/
  2. #include "bsp_spi.h"
  3. #include "nrf_delay.h"
  4. #include "drv_lsm6ds3tr_c.h"
  5. /*Private macro ------------------------------------------------*/
  6. #define BOOT_TIME 15 //启动等待时间 - ms
  7. #define WAIT_TIME_A 100 //自检等待加速度操作时间 - ms
  8. #define WAIT_TIME_G_01 150 //自检等待陀螺仪操作时间 - ms
  9. #define WAIT_TIME_G_02 50 //自检等待陀螺仪操作时间 - ms
  10. /* Self test limits. */
  11. #define MIN_ST_LIMIT_mg 90.0f //自检参数
  12. #define MAX_ST_LIMIT_mg 1700.0f //自检参数
  13. #define MIN_ST_LIMIT_mdps 150000.0f //自检参数
  14. #define MAX_ST_LIMIT_mdps 700000.0f //自检参数
  15. /* Self test results. */
  16. #define ST_PASS 1U //自检成功
  17. #define ST_FAIL 0U //自检失败
  18. #define OP_TIMEOUT 200 //最大操作次数
  19. #define ACC_OUT_XYZ_WORD_NUM 3 //加速度三轴数据的字个数
  20. #define GRY_OUT_XYZ_WORD_NUM 3 //陀螺仪三轴数据的字个数
  21. #define MAG_OUT_XYZ_WORD_NUM 3 //地磁计三轴数据的字个数
  22. #define TIMESTAMP_OUT_WORD_NUM 3 //FIFO时间戳数据的字个数
  23. #define QMC6310_DATA_OUT_X_LSB_REG 0x01 //qmc6310地磁数据的寄存器
  24. #define HUB_MAG_DEV_ADDR 0x1C //挂载的地磁设备地址
  25. #define LSM_IIC_QMC_ADDR (0x1c<<1)
  26. #define LSM_IIC_SA0 PIN_FRONT_SPI_MISO
  27. #define LSM_IIC_NCS PIN_FRONT_SPI_nCS
  28. #define LSM_IIC_SCL PIN_FRONT_SPI_SCLK
  29. #define LSM_IIC_SDA PIN_FRONT_SPI_MOSI
  30. #define LSM_IIC_SCL_SET nrf_gpio_pin_write(LSM_IIC_SCL,1)
  31. #define LSM_IIC_SCL_CLR nrf_gpio_pin_write(LSM_IIC_SCL,0)
  32. #define LSM_IIC_SDA_SET nrf_gpio_pin_write(LSM_IIC_SDA,1)
  33. #define LSM_IIC_SDA_CLR nrf_gpio_pin_write(LSM_IIC_SDA,0)
  34. #define LSM_IIC_SDA_READ nrf_gpio_pin_read(LSM_IIC_SDA)
  35. /*STRUCTION -----------------------------------------------------*/
  36. typedef union
  37. {
  38. int16_t i16bit[3];
  39. uint8_t u8bit[6];
  40. } axis3bit16_u;
  41. typedef struct drv_lsm6ds3tr_c
  42. {
  43. stmdev_ctx_t ctx; //抽象层接口(如I2C/SPI,外部不可操作,属于私有成员)
  44. lsm6ds3tr_c_emb_sh_read_t raw_data; //用于hub读取数据(外部不可操作,属于私有成员)
  45. lsm6ds3tr_c_all_sources_t endop; //用于hub判断是否操作完成
  46. uint32_t op_timeout; //最大操作次数
  47. lsm_data_t cur_data; //当前LSM数据
  48. drv_lsm_config_param_t cur_param; //当前LSM配置
  49. } Drv_Lsm6ds3tr_c_t;
  50. /*Local Variable ----------------------------------------------*/
  51. static lsm6ds3tr_c_sh_cfg_read_t sub_qmc6310 = {
  52. .slv_add = HUB_MAG_DEV_ADDR, //挂载的qmc6310设备地址
  53. .slv_subadd = QMC6310_DATA_OUT_X_LSB_REG, //预备读取qmc6310地磁数据的寄存器
  54. .slv_len = MAG_OUT_XYZ_WORD_NUM * 2, //转为字节长度
  55. };
  56. static Drv_Lsm6ds3tr_c_t ob_lsm6ds3tr_c;
  57. static axis3bit16_u data_raw_acceleration; //临时存储加速度原始数据
  58. static axis3bit16_u data_raw_angular_rate; //临时存储陀螺仪原始数据
  59. static axis3bit16_u data_raw_magnetic; //临时存储地磁计原始数据
  60. static axis3bit16_u data_raw_timestamp; //临时存储时间戳原始数据
  61. /*Local Functions ----------------------------------------------*/
  62. static void platform_delay_ms(uint32_t ms)
  63. {
  64. nrf_delay_ms(ms);
  65. }
  66. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  67. {
  68. int32_t ierror = 0;
  69. if(SPI0_OnlyWriteReg(BOARD_SPI0_CS0_IO, reg, (uint8_t *)bufp, len))
  70. {
  71. ierror = -1;
  72. }
  73. return ierror;
  74. }
  75. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  76. {
  77. int32_t ierror = 0;
  78. if(SPI0_OnlyReadReg(BOARD_SPI0_CS0_IO, reg, bufp, len))
  79. {
  80. ierror = -1;
  81. }
  82. return ierror;
  83. }
  84. static void LSM_IIC_Start(void)
  85. {
  86. LSM_IIC_SDA_SET;
  87. LSM_IIC_SCL_SET;
  88. nrf_delay_us(5);
  89. LSM_IIC_SDA_CLR;
  90. nrf_delay_us(5);
  91. LSM_IIC_SCL_CLR;
  92. }
  93. static void LSM_IIC_Stop(void)
  94. {
  95. LSM_IIC_SCL_CLR;
  96. LSM_IIC_SDA_CLR;
  97. nrf_delay_us(5);
  98. LSM_IIC_SCL_SET;
  99. LSM_IIC_SDA_SET;
  100. nrf_delay_us(5);
  101. }
  102. static void LSM_IIC_ACK(void)
  103. {
  104. LSM_IIC_SCL_CLR;
  105. nrf_delay_us(1);
  106. LSM_IIC_SDA_CLR;
  107. nrf_delay_us(1);
  108. LSM_IIC_SCL_SET;
  109. nrf_delay_us(1);
  110. LSM_IIC_SCL_CLR;
  111. }
  112. static void LSM_IIC_NoACK(void)
  113. {
  114. LSM_IIC_SCL_CLR;
  115. LSM_IIC_SDA_SET;
  116. nrf_delay_us(1);
  117. LSM_IIC_SCL_SET;
  118. nrf_delay_us(1);
  119. LSM_IIC_SCL_CLR;
  120. }
  121. static bool LSM_IIC_WaitACK(void)
  122. {
  123. uint8_t tim = 0;
  124. // IIC_BACK_SCL_CLR;
  125. LSM_IIC_SDA_SET;
  126. nrf_delay_us(1);
  127. LSM_IIC_SCL_SET;
  128. // nrf_delay_us(1);
  129. while(LSM_IIC_SDA_READ){
  130. if(++tim>=50){
  131. LSM_IIC_Stop();
  132. return false;
  133. }
  134. nrf_delay_us(1);
  135. }
  136. LSM_IIC_SCL_CLR;
  137. return true;
  138. }
  139. static void LSM_IIC_SendByte(uint8_t _byte)
  140. {
  141. uint8_t i = 0;
  142. for (i=0; i<8; i++){
  143. LSM_IIC_SCL_CLR;
  144. nrf_delay_us(5);
  145. if (_byte&0x80) LSM_IIC_SDA_SET;
  146. else LSM_IIC_SDA_CLR;
  147. _byte <<= 1;
  148. LSM_IIC_SCL_SET;
  149. nrf_delay_us(5);
  150. }
  151. LSM_IIC_SCL_CLR;
  152. }
  153. static uint8_t LSM_IIC_RecByte(void)
  154. {
  155. uint8_t i = 0;
  156. uint8_t rec_byte;
  157. LSM_IIC_SDA_SET;
  158. for (i=0; i<8; i++){
  159. rec_byte <<= 1;
  160. LSM_IIC_SCL_CLR;
  161. nrf_delay_us(5);
  162. LSM_IIC_SCL_SET;
  163. nrf_delay_us(5);
  164. if (LSM_IIC_SDA_READ) rec_byte |= 0x01;
  165. }
  166. LSM_IIC_SCL_CLR;
  167. return rec_byte;
  168. }
  169. /********************************************/
  170. static bool LSM_IIC_WriteBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
  171. {
  172. uint8_t i = 0;
  173. LSM_IIC_Start();
  174. LSM_IIC_SendByte(add);
  175. if(!LSM_IIC_WaitACK()) return false;
  176. LSM_IIC_SendByte(reg);
  177. if(!LSM_IIC_WaitACK()) return false;
  178. for(i=0;i<len;i++){
  179. LSM_IIC_SendByte(p[i]);
  180. if(!LSM_IIC_WaitACK()) return false;
  181. }
  182. LSM_IIC_Stop();
  183. return true;
  184. }
  185. static bool LSM_IIC_ReadBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
  186. {
  187. uint8_t i = 0;
  188. LSM_IIC_Start();
  189. LSM_IIC_SendByte(add);
  190. if(!LSM_IIC_WaitACK()) return false;
  191. LSM_IIC_SendByte(reg);
  192. if(!LSM_IIC_WaitACK()) return false;
  193. LSM_IIC_Start();
  194. LSM_IIC_SendByte(add+1);
  195. if(!LSM_IIC_WaitACK()) return false;
  196. for(i=0;i<len-1;i++){
  197. p[i] = LSM_IIC_RecByte(); LSM_IIC_ACK();
  198. }
  199. p[i] = LSM_IIC_RecByte(); LSM_IIC_NoACK();
  200. LSM_IIC_Stop();
  201. return true;
  202. }
  203. static void LSM_IIC_Init(void)
  204. {
  205. nrf_gpio_cfg(
  206. LSM_IIC_SCL,
  207. NRF_GPIO_PIN_DIR_OUTPUT,
  208. NRF_GPIO_PIN_INPUT_DISCONNECT,
  209. NRF_GPIO_PIN_NOPULL,
  210. NRF_GPIO_PIN_S0S1,
  211. NRF_GPIO_PIN_NOSENSE);
  212. nrf_gpio_cfg_watcher(LSM_IIC_SCL);
  213. nrf_gpio_pin_set(LSM_IIC_SCL);
  214. nrf_gpio_cfg(
  215. LSM_IIC_SDA,
  216. NRF_GPIO_PIN_DIR_OUTPUT,
  217. NRF_GPIO_PIN_INPUT_DISCONNECT,
  218. NRF_GPIO_PIN_NOPULL,
  219. NRF_GPIO_PIN_S0S1,
  220. NRF_GPIO_PIN_NOSENSE);
  221. nrf_gpio_cfg_watcher(LSM_IIC_SDA);
  222. nrf_gpio_pin_set(LSM_IIC_SDA);
  223. nrf_gpio_cfg_output(LSM_IIC_NCS);nrf_gpio_pin_write(LSM_IIC_NCS,1);
  224. nrf_gpio_cfg_output(LSM_IIC_SA0);
  225. }
  226. static void LSM_IIC_UnInit(void)
  227. {
  228. nrf_gpio_cfg_default(LSM_IIC_SCL);
  229. nrf_gpio_cfg_default(LSM_IIC_SDA);
  230. nrf_gpio_cfg_default(LSM_IIC_NCS);
  231. nrf_gpio_cfg_default(LSM_IIC_SA0);
  232. }
  233. static int read_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint8_t len)
  234. {
  235. int32_t ret;
  236. uint8_t val;
  237. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  238. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  239. if(val != 1)return -1;
  240. SPI0_Disable();
  241. LSM_IIC_Init();
  242. if(LSM_IIC_ReadBytes(device_addr,reg,data,len))
  243. {
  244. ret = 0;
  245. }
  246. else
  247. {
  248. ret = -1;
  249. }
  250. LSM_IIC_UnInit();
  251. SPI0_Init();
  252. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  253. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  254. if(val != 0)return -1;
  255. return ret;
  256. }
  257. static int write_hub(uint8_t device_addr, uint8_t reg, uint8_t* data,uint8_t len)
  258. {
  259. int32_t ret;
  260. uint8_t val;
  261. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,1);
  262. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  263. if(val != 1)return -1;
  264. SPI0_Disable();
  265. LSM_IIC_Init();
  266. if(LSM_IIC_WriteBytes(device_addr,reg,data,len))
  267. {
  268. ret = 0;
  269. }
  270. else
  271. {
  272. ret = -1;
  273. }
  274. LSM_IIC_UnInit();
  275. SPI0_Init();
  276. lsm6ds3tr_c_sh_pass_through_set(&ob_lsm6ds3tr_c.ctx,0);
  277. lsm6ds3tr_c_sh_pass_through_get(&ob_lsm6ds3tr_c.ctx,&val);
  278. if(val != 0)return -1;
  279. return ret;
  280. }
  281. static int self_check(void)
  282. {
  283. uint8_t whoamI, rst;
  284. int16_t data_raw[3];
  285. float val_st_off[3];
  286. float val_st_on[3];
  287. float test_val[3];
  288. uint8_t st_result;
  289. uint8_t drdy;
  290. uint8_t i;
  291. uint8_t j;
  292. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  293. /* Wait sensor boot time */
  294. platform_delay_ms(BOOT_TIME);
  295. /* Check device ID */
  296. lsm6ds3tr_c_device_id_get(&ob_lsm6ds3tr_c.ctx, &whoamI);
  297. if(whoamI != LSM6DS3TR_C_ID)
  298. {
  299. SEGGER_RTT_printf(0,"LSM self_check error!!!\r\n");
  300. return -1;
  301. }
  302. /* Restore default configuration */
  303. lsm6ds3tr_c_reset_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  304. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  305. do {
  306. lsm6ds3tr_c_reset_get(&ob_lsm6ds3tr_c.ctx, &rst);
  307. ob_lsm6ds3tr_c.op_timeout--;
  308. } while(rst && ob_lsm6ds3tr_c.op_timeout!=0);
  309. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  310. /* Accelerometer Self Test */
  311. /* Set Output Data Rate */
  312. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_52Hz);
  313. /* Set full scale */
  314. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_4g);
  315. /* Wait stable output */
  316. platform_delay_ms(WAIT_TIME_A);
  317. /* Check if new value available */
  318. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  319. do {
  320. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  321. ob_lsm6ds3tr_c.op_timeout--;
  322. } while(!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  323. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  324. /* Read dummy data and discard it */
  325. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  326. /* Read 5 sample and get the average vale for each axis */
  327. memset(val_st_off, 0x00, 3 * sizeof(float));
  328. for (i = 0; i < 5; i++) {
  329. /* Check if new value available */
  330. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  331. do {
  332. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  333. ob_lsm6ds3tr_c.op_timeout--;
  334. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  335. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  336. /* Read data and accumulate the mg value */
  337. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  338. for (j = 0; j < 3; j++) {
  339. val_st_off[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  340. }
  341. }
  342. /* Calculate the mg average values */
  343. for (i = 0; i < 3; i++) {
  344. val_st_off[i] /= 5.0f;
  345. }
  346. /* Enable Self Test positive (or negative) */
  347. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_NEGATIVE);
  348. /* Wait stable output */
  349. platform_delay_ms(WAIT_TIME_A);
  350. /* Check if new value available */
  351. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  352. do {
  353. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  354. ob_lsm6ds3tr_c.op_timeout--;
  355. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  356. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  357. /* Read dummy data and discard it */
  358. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  359. /* Read 5 sample and get the average vale for each axis */
  360. memset(val_st_on, 0x00, 3 * sizeof(float));
  361. for (i = 0; i < 5; i++) {
  362. /* Check if new value available */
  363. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  364. do {
  365. lsm6ds3tr_c_xl_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  366. ob_lsm6ds3tr_c.op_timeout--;
  367. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  368. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  369. /* Read data and accumulate the mg value */
  370. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  371. for (j = 0; j < 3; j++) {
  372. val_st_on[j] += lsm6ds3tr_c_from_fs4g_to_mg(data_raw[j]);
  373. }
  374. }
  375. /* Calculate the mg average values */
  376. for (i = 0; i < 3; i++) {
  377. val_st_on[i] /= 5.0f;
  378. }
  379. /* Calculate the mg values for self test */
  380. for (i = 0; i < 3; i++) {
  381. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  382. }
  383. /* Check self test limit */
  384. st_result = ST_PASS;
  385. for (i = 0; i < 3; i++) {
  386. if (( MIN_ST_LIMIT_mg > test_val[i] ) ||
  387. ( test_val[i] > MAX_ST_LIMIT_mg)) {
  388. st_result = ST_FAIL;
  389. }
  390. }
  391. /* Disable Self Test */
  392. lsm6ds3tr_c_xl_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ST_DISABLE);
  393. /* Disable sensor. */
  394. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  395. /* Gyroscope Self Test. */
  396. /* Set Output Data Rate */
  397. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_208Hz);
  398. /* Set full scale */
  399. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  400. /* Wait stable output */
  401. platform_delay_ms(WAIT_TIME_G_01);
  402. /* Check if new value available */
  403. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  404. do {
  405. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  406. ob_lsm6ds3tr_c.op_timeout--;
  407. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  408. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  409. /* Read dummy data and discard it */
  410. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  411. /* Read 5 sample and get the average vale for each axis */
  412. memset(val_st_off, 0x00, 3 * sizeof(float));
  413. for (i = 0; i < 5; i++) {
  414. /* Check if new value available */
  415. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  416. do {
  417. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  418. ob_lsm6ds3tr_c.op_timeout--;
  419. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  420. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  421. /* Read data and accumulate the mg value */
  422. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  423. for (j = 0; j < 3; j++) {
  424. val_st_off[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  425. data_raw[j]);
  426. }
  427. }
  428. /* Calculate the mg average values */
  429. for (i = 0; i < 3; i++) {
  430. val_st_off[i] /= 5.0f;
  431. }
  432. /* Enable Self Test positive (or negative) */
  433. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_POSITIVE);
  434. /* Wait stable output */
  435. platform_delay_ms(WAIT_TIME_G_02);
  436. /* Read 5 sample and get the average vale for each axis */
  437. memset(val_st_on, 0x00, 3 * sizeof(float));
  438. for (i = 0; i < 5; i++) {
  439. /* Check if new value available */
  440. ob_lsm6ds3tr_c.op_timeout = OP_TIMEOUT * 10;
  441. do {
  442. lsm6ds3tr_c_gy_flag_data_ready_get(&ob_lsm6ds3tr_c.ctx, &drdy);
  443. ob_lsm6ds3tr_c.op_timeout--;
  444. } while (!drdy && ob_lsm6ds3tr_c.op_timeout!=0);
  445. if(ob_lsm6ds3tr_c.op_timeout == 0)return -1;
  446. /* Read data and accumulate the mg value */
  447. lsm6ds3tr_c_angular_rate_raw_get(&ob_lsm6ds3tr_c.ctx, data_raw);
  448. for (j = 0; j < 3; j++) {
  449. val_st_on[j] += lsm6ds3tr_c_from_fs2000dps_to_mdps(
  450. data_raw[j]);
  451. }
  452. }
  453. /* Calculate the mg average values */
  454. for (i = 0; i < 3; i++) {
  455. val_st_on[i] /= 5.0f;
  456. }
  457. /* Calculate the mg values for self test */
  458. for (i = 0; i < 3; i++) {
  459. test_val[i] = fabs((val_st_on[i] - val_st_off[i]));
  460. }
  461. /* Check self test limit */
  462. for (i = 0; i < 3; i++) {
  463. if (( MIN_ST_LIMIT_mdps > test_val[i] ) ||
  464. ( test_val[i] > MAX_ST_LIMIT_mdps)) {
  465. st_result = ST_FAIL;
  466. }
  467. }
  468. /* Disable Self Test */
  469. lsm6ds3tr_c_gy_self_test_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ST_DISABLE);
  470. /* Disable sensor. */
  471. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  472. if (st_result == ST_PASS) {
  473. SEGGER_RTT_printf(0,"LSM Self Test - PASS\r\n");
  474. }else{
  475. SEGGER_RTT_printf(0,"Self Test - FAIL\r\n");
  476. return -1;
  477. }
  478. return 0;
  479. }
  480. /*API ----------------------------------------------*/
  481. /**
  482. @brief 初始化LSM驱动
  483. @param 无
  484. @return 错误代码 - [out] -1失败,0成功
  485. */
  486. int drv_lsm_Init(void)
  487. {
  488. //初始化结构体
  489. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  490. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  491. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  492. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  493. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  494. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  495. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  496. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  497. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  498. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  499. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  500. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  501. ob_lsm6ds3tr_c.ctx.read_reg = platform_read;
  502. ob_lsm6ds3tr_c.ctx.write_reg = platform_write;
  503. ob_lsm6ds3tr_c.op_timeout = 0;
  504. //供电
  505. nrf_gpio_cfg(
  506. PIN_FRONT_SENSE_POWER,
  507. NRF_GPIO_PIN_DIR_OUTPUT,
  508. NRF_GPIO_PIN_INPUT_DISCONNECT,
  509. NRF_GPIO_PIN_NOPULL,
  510. NRF_GPIO_PIN_H0H1,
  511. NRF_GPIO_PIN_NOSENSE);
  512. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  513. nrf_gpio_cfg_output(BOARD_SPI0_MOSI_IO);
  514. nrf_gpio_cfg_output(BOARD_SPI0_CLK_IO);
  515. nrf_gpio_cfg_output(PIN_FRONT_SPI_nCS);
  516. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  517. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  518. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  519. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  520. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  521. platform_delay_ms(3);
  522. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  523. platform_delay_ms(3);
  524. //初始化SPI接口
  525. SPI0_Init();
  526. //开始自检
  527. return self_check();
  528. }
  529. /**
  530. @brief LSM断电
  531. @param 无
  532. @return 错误代码 - [out] -1失败,0成功
  533. */
  534. int drv_lsm_power_off(void)
  535. {
  536. //关闭SPI
  537. SPI0_Disable();
  538. //断电,清空lsm配置
  539. nrf_gpio_cfg(
  540. PIN_FRONT_SENSE_POWER,
  541. NRF_GPIO_PIN_DIR_OUTPUT,
  542. NRF_GPIO_PIN_INPUT_DISCONNECT,
  543. NRF_GPIO_PIN_NOPULL,
  544. NRF_GPIO_PIN_H0H1,
  545. NRF_GPIO_PIN_NOSENSE);
  546. nrf_gpio_cfg_output(BOARD_SPI0_MISO_IO);
  547. nrf_gpio_pin_write(BOARD_SPI0_MISO_IO,0);
  548. nrf_gpio_pin_write(BOARD_SPI0_MOSI_IO,0);
  549. nrf_gpio_pin_write(BOARD_SPI0_CLK_IO,0);
  550. nrf_gpio_pin_write(PIN_FRONT_SPI_nCS,0);
  551. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,0);
  552. return 0;
  553. }
  554. /**
  555. @brief LSM上电(默认配置挂起)
  556. @param 无
  557. @return 错误代码 - [out] -1失败,0成功
  558. */
  559. int drv_lsm_power_on(void)
  560. {
  561. nrf_gpio_pin_write(PIN_FRONT_SENSE_POWER,1);
  562. nrf_gpio_cfg_input(BOARD_SPI0_MISO_IO, (nrf_gpio_pin_pull_t)NRFX_SPIM_MISO_PULL_CFG);
  563. //初始化结构体
  564. memset(&ob_lsm6ds3tr_c.cur_data,0,sizeof(ob_lsm6ds3tr_c.cur_data));
  565. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  566. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  567. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  568. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  569. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  570. ob_lsm6ds3tr_c.cur_param.mag_odr = LSM_MAG_ODR_OFF;
  571. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  572. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  573. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  574. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  575. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  576. ob_lsm6ds3tr_c.op_timeout = 0;
  577. //关闭SPI
  578. SPI0_Disable(); //注意:若SPI线短路,该函数内部实现有个while循环导致卡死。
  579. //重新初始化SPI
  580. SPI0_Init();
  581. return 0;
  582. }
  583. /**
  584. @brief 设置加速度工作模式
  585. @param acc_power_mode - [in] 加速度工作模式
  586. @return 错误代码 - [out] -1失败,0成功
  587. */
  588. int drv_lsm_set_acc_power_mode(LSM_ACC_POWER_MODE_e acc_power_mode)
  589. {
  590. lsm6ds3tr_c_xl_hm_mode_t val;
  591. if(ob_lsm6ds3tr_c.cur_param.acc_power_mode != acc_power_mode)
  592. {
  593. switch(acc_power_mode)
  594. {
  595. case LSM_ACC_POWER_MODE_HIGH_PERFORMANCE:
  596. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_HIGH_PERFORMANCE);
  597. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  598. if(val != LSM6DS3TR_C_XL_HIGH_PERFORMANCE)return -1;
  599. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_HIGH_PERFORMANCE;
  600. break;
  601. case LSM_ACC_POWER_MODE_NORMAL:
  602. lsm6ds3tr_c_xl_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_XL_NORMAL);
  603. lsm6ds3tr_c_xl_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  604. if(val != LSM6DS3TR_C_XL_NORMAL)return -1;
  605. ob_lsm6ds3tr_c.cur_param.acc_power_mode = LSM_ACC_POWER_MODE_NORMAL;
  606. break;
  607. }
  608. }
  609. return 0;
  610. }
  611. /**
  612. @brief 设置陀螺仪工作模式
  613. @param gry_power_mode - [in] 陀螺仪工作模式
  614. @return 错误代码 - [out] -1失败,0成功
  615. */
  616. int drv_lsm_set_gry_power_mode(LSM_GRY_POWER_MODE_e gry_power_mode)
  617. {
  618. lsm6ds3tr_c_g_hm_mode_t val;
  619. if(ob_lsm6ds3tr_c.cur_param.gry_power_mode != gry_power_mode)
  620. {
  621. switch(gry_power_mode)
  622. {
  623. case LSM_GRY_POWER_MODE_HIGH_PERFORMANCE:
  624. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
  625. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  626. if(val != LSM_GRY_POWER_MODE_HIGH_PERFORMANCE)return -1;
  627. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_HIGH_PERFORMANCE;
  628. break;
  629. case LSM_GRY_POWER_MODE_NORMAL:
  630. lsm6ds3tr_c_gy_power_mode_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_GY_NORMAL);
  631. lsm6ds3tr_c_gy_power_mode_get(&ob_lsm6ds3tr_c.ctx,&val);
  632. if(val != LSM_GRY_POWER_MODE_NORMAL)return -1;
  633. ob_lsm6ds3tr_c.cur_param.gry_power_mode = LSM_GRY_POWER_MODE_NORMAL;
  634. break;
  635. }
  636. }
  637. return 0;
  638. }
  639. /**
  640. @brief 设置时间戳精度
  641. @param timestamp_resolution - [in] 时间戳精度
  642. @return 错误代码 - [out] -1失败,0成功
  643. */
  644. int drv_lsm_set_timestamp_resolution(LSM_TIMESTAMP_RESOLUTION_e timestamp_resolution)
  645. {
  646. lsm6ds3tr_c_timer_hr_t val;
  647. if(ob_lsm6ds3tr_c.cur_param.timestamp_resolution != timestamp_resolution)
  648. {
  649. switch(timestamp_resolution)
  650. {
  651. case LSM_TIMESTAMP_6MS4:
  652. /* Set High Resolution Timestamp (6.4ms) */
  653. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_6ms4);
  654. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  655. if(val != LSM6DS3TR_C_LSB_6ms4)return -1;
  656. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_6MS4;
  657. break;
  658. case LSM_TIMESTAMP_25US:
  659. /* Set High Resolution Timestamp (25 us tick) */
  660. lsm6ds3tr_c_timestamp_res_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_LSB_25us);
  661. lsm6ds3tr_c_timestamp_res_get(&ob_lsm6ds3tr_c.ctx,&val);
  662. if(val != LSM6DS3TR_C_LSB_25us)return -1;
  663. ob_lsm6ds3tr_c.cur_param.timestamp_resolution = LSM_TIMESTAMP_25US;
  664. break;
  665. }
  666. }
  667. return 0;
  668. }
  669. /**
  670. @brief 设置时间戳开关
  671. @param timestamp_switch - [in] 时间戳开关
  672. @return 错误代码 - [out] -1失败,0成功
  673. */
  674. int drv_lsm_set_timestamp_switch(LSM_TIMESTAMP_SWITCH_e timestamp_switch)
  675. {
  676. uint8_t val;
  677. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch != timestamp_switch)
  678. {
  679. switch(timestamp_switch)
  680. {
  681. case LSM_TIMESTAMP_ON:
  682. /* Enable timestamp in HW */
  683. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  684. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  685. if(val != PROPERTY_ENABLE)return -1;
  686. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_ON;
  687. break;
  688. case LSM_TIMESTAMP_OFF:
  689. /*Disable timestamp in HW */
  690. lsm6ds3tr_c_timestamp_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_DISABLE);
  691. lsm6ds3tr_c_timestamp_get(&ob_lsm6ds3tr_c.ctx, &val);
  692. if(val != PROPERTY_DISABLE)return -1;
  693. ob_lsm6ds3tr_c.cur_param.timestamp_switch = LSM_TIMESTAMP_OFF;
  694. break;
  695. }
  696. }
  697. return 0;
  698. }
  699. /**
  700. @brief 设置FIFO采样频率
  701. @param fifo_odr - [in] FIFO采样频率
  702. @return 错误代码 - [out] -1失败,0成功
  703. */
  704. 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)
  705. {
  706. uint8_t val;
  707. lsm6ds3tr_c_odr_fifo_t r_fifo_odr;
  708. lsm6ds3tr_c_fifo_mode_t r_fifo_mode;
  709. lsm6ds3tr_c_dec_fifo_xl_t r_fifo_xl;
  710. lsm6ds3tr_c_dec_fifo_gyro_t r_fifo_gy;
  711. lsm6ds3tr_c_dec_ds3_fifo_t r_fifo_ds3;
  712. lsm6ds3tr_c_dec_ds4_fifo_t r_fifo_ds4;
  713. uint16_t fifo_watermark;
  714. uint16_t pattern_len = 0;
  715. if(ob_lsm6ds3tr_c.cur_param.fifo_odr != fifo_odr)
  716. {
  717. switch(fifo_odr)
  718. {
  719. case LSM_FIFO_ODR_OFF:
  720. //禁用步进计数器和时间戳数据作为第4步FIFO数据集
  721. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_DISABLE);
  722. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  723. if(val != PROPERTY_DISABLE)return -1;
  724. /* Set ODR FIFO */
  725. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_DISABLE);
  726. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  727. if(r_fifo_odr != LSM6DS3TR_C_FIFO_DISABLE) return -1;
  728. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_OFF;
  729. break;
  730. case LSM_FIFO_ODR_104HZ:
  731. //启用步进计数器和时间戳数据作为第4步FIFO数据集
  732. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  733. lsm6ds3tr_c_fifo_pedo_and_timestamp_batch_get(&ob_lsm6ds3tr_c.ctx,&val);
  734. if(val != PROPERTY_ENABLE)return -1;
  735. /* Set FIFO mode to Stream mode */
  736. lsm6ds3tr_c_fifo_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_STREAM_MODE);
  737. lsm6ds3tr_c_fifo_mode_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_mode);
  738. if(r_fifo_mode != LSM6DS3TR_C_STREAM_MODE)return -1;
  739. /* Set FIFO sensor decimator */
  740. if(acc_odr == LSM_ACC_ODR_104HZ)
  741. {
  742. lsm6ds3tr_c_fifo_xl_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_XL_NO_DEC);//acc和fifo的HZ一致
  743. lsm6ds3tr_c_fifo_xl_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_xl);
  744. if(r_fifo_xl != LSM6DS3TR_C_FIFO_XL_NO_DEC)return -1;
  745. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  746. }
  747. if(gry_odr == LSM_GRY_ODR_104HZ)
  748. {
  749. lsm6ds3tr_c_fifo_gy_batch_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_GY_NO_DEC);//gry和fifo的HZ一致
  750. lsm6ds3tr_c_fifo_gy_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_gy);
  751. if(r_fifo_gy != LSM6DS3TR_C_FIFO_GY_NO_DEC)return -1;
  752. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  753. }
  754. if(mag_odr != LSM_MAG_ODR_OFF)
  755. {
  756. lsm6ds3tr_c_fifo_dataset_3_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS3_NO_DEC);//mag和fifo的HZ一致
  757. lsm6ds3tr_c_fifo_dataset_3_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds3);
  758. if(r_fifo_ds3 != LSM6DS3TR_C_FIFO_DS3_NO_DEC)return -1;
  759. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  760. }
  761. if(timestamp_switch == LSM_TIMESTAMP_ON)
  762. {
  763. lsm6ds3tr_c_fifo_dataset_4_batch_set(&ob_lsm6ds3tr_c.ctx,LSM6DS3TR_C_FIFO_DS4_NO_DEC);//该参数关联到fifo_odr,timestamp和fifo的HZ一致
  764. lsm6ds3tr_c_fifo_dataset_4_batch_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_ds4);
  765. if(r_fifo_ds4 != LSM6DS3TR_C_FIFO_DS4_NO_DEC)return -1;
  766. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  767. }
  768. /* Set FIFO watermark to a multiple of a pattern */
  769. lsm6ds3tr_c_fifo_watermark_set(&ob_lsm6ds3tr_c.ctx, pattern_len);
  770. lsm6ds3tr_c_fifo_watermark_get(&ob_lsm6ds3tr_c.ctx,&fifo_watermark);
  771. if(fifo_watermark != pattern_len)return -1;
  772. /* Set ODR FIFO */
  773. lsm6ds3tr_c_fifo_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_FIFO_104Hz);//该参数关联到timestamp
  774. lsm6ds3tr_c_fifo_data_rate_get(&ob_lsm6ds3tr_c.ctx,&r_fifo_odr);
  775. if(r_fifo_odr != LSM6DS3TR_C_FIFO_104Hz) return -1;
  776. ob_lsm6ds3tr_c.cur_param.fifo_odr = LSM_FIFO_ODR_104HZ;
  777. break;
  778. }
  779. }
  780. return 0;
  781. }
  782. /**
  783. @brief 设置ACC量程
  784. @param acc_fs - [in] ACC量程
  785. @return 错误代码 - [out] -1失败,0成功
  786. */
  787. int drv_lsm_set_acc_fs(LSM_ACC_FS_e acc_fs)
  788. {
  789. lsm6ds3tr_c_fs_xl_t val;
  790. if(ob_lsm6ds3tr_c.cur_param.acc_fs != acc_fs)
  791. {
  792. switch(acc_fs)
  793. {
  794. case LSM_ACC_FS_2G:
  795. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2g);
  796. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  797. if(val != LSM6DS3TR_C_2g)return -1;
  798. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_2G;
  799. break;
  800. case LSM_ACC_FS_16G:
  801. lsm6ds3tr_c_xl_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_16g);
  802. lsm6ds3tr_c_xl_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  803. if(val != LSM6DS3TR_C_16g)return -1;
  804. ob_lsm6ds3tr_c.cur_param.acc_fs = LSM_ACC_FS_16G;
  805. break;
  806. }
  807. }
  808. return 0;
  809. }
  810. /**
  811. @brief 设置ACC采样频率
  812. @param acc_odr - [in] ACC采样频率
  813. @return 错误代码 - [out] -1失败,0成功
  814. */
  815. int drv_lsm_set_acc_odr(LSM_ACC_ODR_e acc_odr)
  816. {
  817. lsm6ds3tr_c_odr_xl_t val;
  818. if(ob_lsm6ds3tr_c.cur_param.acc_odr != acc_odr)
  819. {
  820. switch(acc_odr)
  821. {
  822. case LSM_ACC_ODR_OFF:
  823. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_OFF);
  824. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  825. if(val != LSM6DS3TR_C_XL_ODR_OFF)return -1;
  826. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_OFF;
  827. break;
  828. case LSM_ACC_ODR_104HZ:
  829. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_104Hz);
  830. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  831. if(val != LSM6DS3TR_C_XL_ODR_104Hz)return -1;
  832. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_104HZ;
  833. break;
  834. case LSM_ACC_ODR_12HZ5:
  835. lsm6ds3tr_c_xl_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_XL_ODR_12Hz5);
  836. lsm6ds3tr_c_xl_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  837. if(val != LSM6DS3TR_C_XL_ODR_12Hz5)return -1;
  838. ob_lsm6ds3tr_c.cur_param.acc_odr = LSM_ACC_ODR_12HZ5;
  839. break;
  840. }
  841. }
  842. return 0;
  843. }
  844. /**
  845. @brief 设置GRY量程
  846. @param gry_fs - [in] GRY量程
  847. @return 错误代码 - [out] -1失败,0成功
  848. */
  849. int drv_lsm_set_gry_fs(LSM_GRY_FS_e gry_fs)
  850. {
  851. lsm6ds3tr_c_fs_g_t val;
  852. if(ob_lsm6ds3tr_c.cur_param.gry_fs != gry_fs)
  853. {
  854. switch(gry_fs)
  855. {
  856. case LSM_GRY_FS_250DPS:
  857. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_250dps);
  858. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  859. if(val != LSM6DS3TR_C_250dps)return -1;
  860. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_250DPS;
  861. break;
  862. case LSM_GRY_FS_2000DPS:
  863. lsm6ds3tr_c_gy_full_scale_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_2000dps);
  864. lsm6ds3tr_c_gy_full_scale_get(&ob_lsm6ds3tr_c.ctx,&val);
  865. if(val != LSM6DS3TR_C_2000dps)return -1;
  866. ob_lsm6ds3tr_c.cur_param.gry_fs = LSM_GRY_FS_2000DPS;
  867. break;
  868. }
  869. }
  870. return 0;
  871. }
  872. /**
  873. @brief 设置GRY采样频率
  874. @param gry_odr - [in] GRY采样频率
  875. @return 错误代码 - [out] -1失败,0成功
  876. */
  877. int drv_lsm_set_gry_odr(LSM_GRY_ODR_e gry_odr)
  878. {
  879. lsm6ds3tr_c_odr_g_t val;
  880. if(ob_lsm6ds3tr_c.cur_param.gry_odr != gry_odr)
  881. {
  882. switch(gry_odr)
  883. {
  884. case LSM_GRY_ODR_OFF:
  885. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_OFF);
  886. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  887. if(val != LSM6DS3TR_C_GY_ODR_OFF)return -1;
  888. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_OFF;
  889. break;
  890. case LSM_GRY_ODR_104HZ:
  891. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_104Hz);
  892. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  893. if(val != LSM6DS3TR_C_GY_ODR_104Hz)return -1;
  894. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_104HZ;
  895. break;
  896. case LSM_GRY_ODR_12HZ5:
  897. lsm6ds3tr_c_gy_data_rate_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_GY_ODR_12Hz5);
  898. lsm6ds3tr_c_gy_data_rate_get(&ob_lsm6ds3tr_c.ctx,&val);
  899. if(val != LSM6DS3TR_C_GY_ODR_12Hz5)return -1;
  900. ob_lsm6ds3tr_c.cur_param.gry_odr = LSM_GRY_ODR_12HZ5;
  901. break;
  902. }
  903. }
  904. return 0;
  905. }
  906. /**
  907. @brief 获取配置MAG量程需要的步骤数量
  908. @return 错误代码 - [out] 配置MAG量程需要的步骤数量
  909. */
  910. int drv_lsm_get_mag_fs_flow(void)
  911. {
  912. return 1;
  913. }
  914. /**
  915. @brief 设置MAG量程
  916. @param mag_fs - [in] MAG量程
  917. @param flow - [in] 当前需要处理的步骤
  918. @return 错误代码 - [out] -1失败,0成功
  919. */
  920. int drv_lsm_set_mag_fs(LSM_MAG_FS_e mag_fs, int flow)
  921. {
  922. int ret;
  923. uint8_t data;
  924. if(flow <= 0 || flow >= 2)return -1;
  925. if(ob_lsm6ds3tr_c.cur_param.mag_fs != mag_fs)
  926. {
  927. switch(mag_fs)
  928. {
  929. case LSM_MAG_FS_30GS:
  930. data = LSM_MAG_FS_30GS;
  931. ret = write_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  932. if(ret == -1)return -1;
  933. data = 0xFF;
  934. ret = read_hub(LSM_IIC_QMC_ADDR,0x0b,&data,1);
  935. if(ret == -1 || data != LSM_MAG_FS_30GS)return -1;
  936. ob_lsm6ds3tr_c.cur_param.mag_fs = LSM_MAG_FS_30GS;
  937. break;
  938. }
  939. }
  940. return 0;
  941. }
  942. /**
  943. @brief 获取配置MAG采样频率需要的步骤数量
  944. @return 错误代码 - [out] 配置MAG采样频率需要的步骤数量
  945. */
  946. int drv_lsm_get_mag_odr_flow(void)
  947. {
  948. return 5;
  949. }
  950. /**
  951. @brief 设置MAG采样频率
  952. @param mag_odr - [in] MAG采样频率
  953. @param set_hub - [in] 是否配置hub
  954. @param flow - [in] 当前需要处理的步骤
  955. @return 错误代码 - [out] -1失败,0成功
  956. */
  957. int drv_lsm_set_mag_odr(LSM_MAG_ODR_e mag_odr, bool set_hub, int flow)
  958. {
  959. int ret;
  960. uint8_t data;
  961. if(flow <= 0 || flow >= 6)return -1;
  962. if(ob_lsm6ds3tr_c.cur_param.mag_odr != mag_odr)
  963. {
  964. switch(flow)
  965. {
  966. case 1:
  967. //define the sign for x y and z axis
  968. if(mag_odr != LSM_MAG_ODR_OFF)
  969. {
  970. data = 0x06;
  971. ret = write_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  972. }
  973. else
  974. {
  975. ret = 0;
  976. }
  977. break;
  978. case 2:
  979. //define the sign for x y and z axis
  980. if(mag_odr != LSM_MAG_ODR_OFF)
  981. {
  982. data = 0xFF;
  983. ret = read_hub(LSM_IIC_QMC_ADDR,0x29,&data,1);
  984. if(ret == -1 || data != 0x06)ret = -1;
  985. else ret = 0;
  986. }
  987. else
  988. {
  989. ret = 0;
  990. }
  991. break;
  992. case 3:
  993. //set odr
  994. data = mag_odr;
  995. ret = write_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  996. break;
  997. case 4:
  998. data = 0xFF;
  999. ret = read_hub(LSM_IIC_QMC_ADDR,0x0a,&data,1);
  1000. if(ret == -1 || data != mag_odr)ret = -1;
  1001. else{
  1002. ret = 0;
  1003. }
  1004. break;
  1005. case 5:
  1006. //是否配置hub
  1007. if(set_hub)
  1008. {
  1009. lsm6ds3tr_c_sh_pin_mode_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_INTERNAL_PULL_UP);
  1010. lsm6ds3tr_c_sh_slv0_cfg_read(&ob_lsm6ds3tr_c.ctx, &sub_qmc6310);
  1011. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_BANK_A);
  1012. data = 0xFF;
  1013. lsm6ds3tr_c_read_reg(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV0_SUBADD,&data, 1);
  1014. lsm6ds3tr_c_mem_bank_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_USER_BANK);
  1015. if(data != QMC6310_DATA_OUT_X_LSB_REG)return -1;
  1016. /* Configure Sensor Hub to read one slaves */
  1017. lsm6ds3tr_c_sh_num_of_dev_connected_set(&ob_lsm6ds3tr_c.ctx, LSM6DS3TR_C_SLV_0);
  1018. lsm6ds3tr_c_func_en_set(&ob_lsm6ds3tr_c.ctx,PROPERTY_ENABLE);
  1019. lsm6ds3tr_c_sh_master_set(&ob_lsm6ds3tr_c.ctx, PROPERTY_ENABLE);
  1020. ret = 0;
  1021. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1022. }
  1023. else{
  1024. ret = 0;
  1025. ob_lsm6ds3tr_c.cur_param.mag_odr = mag_odr;
  1026. }
  1027. break;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. return 0;
  1033. }
  1034. return ret;
  1035. }
  1036. /**
  1037. @brief 获取LSM配置参数
  1038. @param param - [in] LSM配置参数
  1039. @return 错误代码 - [out] -1失败,0成功
  1040. */
  1041. int drv_lsm_get_config_param(drv_lsm_config_param_t *p_param)
  1042. {
  1043. *p_param = ob_lsm6ds3tr_c.cur_param;
  1044. return 0;
  1045. }
  1046. /**
  1047. @brief 当前LSM的FIFO有几组数据
  1048. @return 错误代码 - [out] FIFO当前有几组数据
  1049. */
  1050. int drv_lsm_get_fifo_group_num(void)
  1051. {
  1052. uint8_t wt;
  1053. uint16_t num = 0;
  1054. uint16_t num_pattern = 0;
  1055. uint16_t pattern_len = 0;
  1056. /* Read FIFO watermark flag in polling mode */
  1057. lsm6ds3tr_c_fifo_wtm_flag_get(&ob_lsm6ds3tr_c.ctx, &wt);
  1058. if(wt)
  1059. {
  1060. /* Read number of word in FIFO */
  1061. lsm6ds3tr_c_fifo_data_level_get(&ob_lsm6ds3tr_c.ctx, &num);
  1062. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1063. pattern_len += ACC_OUT_XYZ_WORD_NUM;
  1064. }
  1065. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1066. pattern_len += GRY_OUT_XYZ_WORD_NUM;
  1067. }
  1068. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1069. pattern_len += MAG_OUT_XYZ_WORD_NUM;
  1070. }
  1071. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1072. pattern_len += TIMESTAMP_OUT_WORD_NUM;
  1073. }
  1074. num_pattern = num / pattern_len;
  1075. }
  1076. return num_pattern;
  1077. }
  1078. /**
  1079. @brief 获取LSM的FIFO数据
  1080. @param p_data - [out] 返回的FIFO数据
  1081. @return 错误代码 - [out] -1失败,0成功
  1082. */
  1083. int drv_lsm_get_fifo_data(lsm_data_t *p_data)
  1084. {
  1085. //清空缓存
  1086. memset(&data_raw_angular_rate,0,sizeof(data_raw_angular_rate));
  1087. memset(&data_raw_acceleration,0,sizeof(data_raw_acceleration));
  1088. memset(&data_raw_magnetic,0,sizeof(data_raw_magnetic));
  1089. memset(&data_raw_timestamp,0,sizeof(data_raw_timestamp));
  1090. //获取原始数据
  1091. if(ob_lsm6ds3tr_c.cur_param.gry_odr != LSM_GRY_ODR_OFF){
  1092. /* Read gyro samples */
  1093. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_angular_rate.u8bit,GRY_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  1094. }
  1095. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF){
  1096. /* Read XL samples */
  1097. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_acceleration.u8bit,ACC_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  1098. }
  1099. if(ob_lsm6ds3tr_c.cur_param.mag_odr != LSM_MAG_ODR_OFF){
  1100. /* Read mag samples */
  1101. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_magnetic.u8bit,MAG_OUT_XYZ_WORD_NUM * sizeof(int16_t));
  1102. }
  1103. if(ob_lsm6ds3tr_c.cur_param.timestamp_switch == LSM_TIMESTAMP_ON){
  1104. /* Read timestamp samples */
  1105. lsm6ds3tr_c_fifo_raw_data_get(&ob_lsm6ds3tr_c.ctx, data_raw_timestamp.u8bit,TIMESTAMP_OUT_WORD_NUM * sizeof(int16_t));
  1106. }
  1107. //返回数据
  1108. if(p_data != NULL)
  1109. {
  1110. p_data->gry[0] = data_raw_angular_rate.i16bit[0];
  1111. p_data->gry[1] = data_raw_angular_rate.i16bit[1];
  1112. p_data->gry[2] = data_raw_angular_rate.i16bit[2];
  1113. p_data->acc[0] = data_raw_acceleration.i16bit[0];
  1114. p_data->acc[1] = data_raw_acceleration.i16bit[1];
  1115. p_data->acc[2] = data_raw_acceleration.i16bit[2];
  1116. p_data->mag[0] = data_raw_magnetic.i16bit[0];
  1117. p_data->mag[1] = data_raw_magnetic.i16bit[1];
  1118. p_data->mag[2] = data_raw_magnetic.i16bit[2];
  1119. /* 时间戳和计步数数据
  1120. 字节1 字节2 字节3 字节4 字节5 字节6
  1121. TIMESTAMP TIMESTAMP ----- TIMESTAMP STEPS STEPS
  1122. [15:8] [23:16] [7:0] [7:0] [15:8]
  1123. */
  1124. p_data->fifo_timestamp = ((data_raw_timestamp.u8bit[1] << 16) | (data_raw_timestamp.u8bit[0] << 8) | (data_raw_timestamp.u8bit[3]))*25;//counter == 25us
  1125. }
  1126. return 0;
  1127. }
  1128. /**
  1129. @brief 获取LSM的ACC数据
  1130. @param p_data - [out] 返回的ACC三轴数据
  1131. @return 错误代码 - [out] -1失败,0成功
  1132. */
  1133. int drv_lsm_get_acc_data(lsm_data_t *p_data)
  1134. {
  1135. lsm6ds3tr_c_reg_t reg;
  1136. if(ob_lsm6ds3tr_c.cur_param.acc_odr != LSM_ACC_ODR_OFF)
  1137. {
  1138. /* Read samples in polling mode (no int) */
  1139. /* Read output only if new value is available */
  1140. lsm6ds3tr_c_status_reg_get(&ob_lsm6ds3tr_c.ctx, &reg.status_reg);
  1141. if(reg.status_reg.xlda)
  1142. {
  1143. /* Read magnetic field data */
  1144. memset( data_raw_acceleration.i16bit, 0x00, 3 * sizeof(int16_t));
  1145. lsm6ds3tr_c_acceleration_raw_get(&ob_lsm6ds3tr_c.ctx,data_raw_acceleration.i16bit);
  1146. p_data->acc[0] = data_raw_acceleration.i16bit[0];
  1147. p_data->acc[1] = data_raw_acceleration.i16bit[1];
  1148. p_data->acc[2] = data_raw_acceleration.i16bit[2];
  1149. }
  1150. else return -1;
  1151. }
  1152. else
  1153. {
  1154. return -1;
  1155. }
  1156. return 0;
  1157. }