drv_qma7981.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*Includes ----------------------------------------------*/
  2. #include "nrf_delay.h"
  3. #include "drv_iic_middle.h"
  4. #include "drv_qma7981.h"
  5. /*Private macro ------------------------------------------------*/
  6. #define ADO 0 // I2C Address pin, 0 for low and 1 for high
  7. #define QMA7981_ADDRESS_LEN 1
  8. #if ADO == 1
  9. #define I2C_ADDRESS (0x13 << 1) //0b0010011 Device address when ADO = 1
  10. #else
  11. #define I2C_ADDRESS (0x12 << 1) //0b0010010 Device address when ADO = 0
  12. #endif
  13. /*qma7981 full scale range*/
  14. #define RANGE_2G 0x01
  15. #define RANGE_4G 0x02
  16. #define RANGE_8G 0x04
  17. #define RANGE_16G 0x08
  18. #define RANGE_32G 0x0F
  19. /*qma7981 bandwidth*/
  20. #define MCLK_DIV_BY_7695 0x00
  21. #define MCLK_DIV_BY_3855 0x01
  22. #define MCLK_DIV_BY_1935 0x02
  23. #define MCLK_DIV_BY_975 0x03
  24. #define MCLK_DIV_BY_15375 0x05
  25. #define MCLK_DIV_BY_30735 0x06
  26. #define MCLK_DIV_BY_61455 0x07
  27. /*qma7981 clock freq*/
  28. #define CLK_500_KHZ 0x01
  29. #define CLK_333_KHZ 0x00
  30. #define CLK_200_KHZ 0x02
  31. #define CLK_100_KHZ 0x03
  32. #define CLK_50_KHZ 0x04
  33. #define CLK_25_KHZ 0x05
  34. #define CLK_12_KHZ_5 0x06
  35. #define CLK_5_KHZ 0x07
  36. /*qma7981 no motion duration*/
  37. #define NO_MOTION_1_SEC 0x00
  38. #define NO_MOTION_2_SEC 0x01
  39. #define NO_MOTION_3_SEC 0x02
  40. #define NO_MOTION_5_SEC 0x04
  41. #define NO_MOTION_10_SEC 0x09
  42. #define NO_MOTION_15_SEC 0x0E
  43. #define NO_MOTION_30_SEC 0x12
  44. #define NO_MOTION_1_MIN 0x18
  45. #define NO_MOTION_2_MIN 0x22
  46. #define NO_MOTION_3_MIN 0x28
  47. #define NO_MOTION_4_MIN 0x2E
  48. /*qma7981 any motion samples*/
  49. #define NUM_SAMPLES_1 0x00
  50. #define NUM_SAMPLES_2 0x01
  51. #define NUM_SAMPLES_3 0x02
  52. #define NUM_SAMPLES_4 0x03
  53. /*qma7981 mode*/
  54. #define MODE_STANDBY 0x00
  55. #define MODE_ACTIVE 0x01
  56. /*qma7981 motion_detect*/
  57. #define MOTION_DETECT_NOTHING 0x00
  58. #define MOTION_DETECT_ANY_MOTION 0x01
  59. #define MOTION_DETECT_NO_MOTION 0x02
  60. /*STRUCTION -----------------------------------------------------*/
  61. typedef enum qma7981_mode
  62. {
  63. Qma7981_Mode_Standby = 0,
  64. Qma7981_Mode_Active = 1,
  65. } Qma7981_Mode_t;
  66. typedef int32_t (*qma_write_ptr)(void *, uint8_t, const uint8_t *, uint16_t);
  67. typedef int32_t (*qma_read_ptr)(void *, uint8_t, uint8_t *, uint16_t);
  68. typedef struct
  69. {
  70. /** Component mandatory fields **/
  71. qma_write_ptr write_reg;
  72. qma_read_ptr read_reg;
  73. } qma_ctx_t;
  74. typedef struct drv_qma7981
  75. {
  76. qma_ctx_t ctx; //抽象层接口(如I2C/SPI,外部不可操作,属于私有成员)
  77. qma_data_t cur_data; //当前QMA数据
  78. drv_qma_config_param_t cur_param; //当前QMA配置
  79. } Drv_Qma7981_t;
  80. /*Local Variable ----------------------------------------------*/
  81. static Drv_Qma7981_t ob_qma7981;
  82. static qma_data_t data_raw_acceleration; //临时存储加速度原始数据
  83. /*Local Functions ----------------------------------------------*/
  84. static void platform_delay_ms(uint32_t ms)
  85. {
  86. nrf_delay_ms(ms);
  87. }
  88. static int32_t qma7981_platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
  89. {
  90. int32_t ierror = 0;
  91. if(IIC_MIDDLE_WriteBytes(I2C_ADDRESS,reg,(uint8_t *)bufp,len) != true)
  92. {
  93. ierror = -1;
  94. }
  95. return ierror;
  96. }
  97. static int32_t qma7981_platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  98. {
  99. int32_t ierror = 0;
  100. if(IIC_MIDDLE_ReadBytes(I2C_ADDRESS,reg,bufp,len) != true)
  101. {
  102. ierror = -1;
  103. }
  104. return ierror;
  105. }
  106. static int read_accel_axis(uint8_t address_msb, int16_t *result)
  107. {
  108. int ret;
  109. int16_t data;
  110. uint8_t accel_axis_buf[2];
  111. ret = ob_qma7981.ctx.read_reg(0,address_msb,accel_axis_buf,2);
  112. if(ret == -1)return -1;
  113. data = (accel_axis_buf[0]&0xFC) | (accel_axis_buf[1]<< 8);
  114. data = data / 4; // divide the result by 4 to maintain the sign, since the data is 14 bits
  115. *result = data;
  116. return 0;
  117. }
  118. static int get_accel_x(int16_t *result)
  119. {
  120. int ret;
  121. int16_t data;
  122. ret = read_accel_axis(0x01, &data);
  123. if(ret == -1)return -1;
  124. *result = data;
  125. return 0;
  126. }
  127. static int get_accel_y(int16_t *result)
  128. {
  129. int ret;
  130. int16_t data;
  131. ret = read_accel_axis(0x03, &data);
  132. if(ret == -1)return -1;
  133. *result = data;
  134. return 0;
  135. }
  136. static int get_accel_z(int16_t *result)
  137. {
  138. int ret;
  139. int16_t data;
  140. ret = read_accel_axis(0x05, &data);
  141. if(ret == -1)return -1;
  142. *result = data;
  143. return 0;
  144. }
  145. static void set_bit(uint8_t *byte, uint8_t n, bool value)
  146. {
  147. *byte = (*byte & ~(1UL << n)) | (value << n);
  148. }
  149. static void get_bit(uint8_t *byte, uint8_t n, bool *p_value)
  150. {
  151. if((*byte & (1UL << n)) > 0)
  152. {
  153. *p_value = true;
  154. }
  155. else
  156. {
  157. *p_value = false;
  158. }
  159. }
  160. static int soft_reset(void)
  161. {
  162. int ret;
  163. uint8_t data;
  164. data = 0xB6;
  165. ret = ob_qma7981.ctx.write_reg(0,0x36,&data,1); //0xB6, soft reset all of the registers.
  166. if(ret == -1)return 1;
  167. data = 0x00;
  168. ret = ob_qma7981.ctx.write_reg(0,0x36,&data,1); //After soft-reset, user should write 0x00 back
  169. if(ret == -1)return 1;
  170. return 0;
  171. }
  172. static int set_mode(Qma7981_Mode_t mode)
  173. {
  174. int ret;
  175. uint8_t data;
  176. ret = ob_qma7981.ctx.read_reg(0,0x11,&data,1);
  177. if(ret == -1)return -1;
  178. set_bit(&data,7,mode);
  179. ret = ob_qma7981.ctx.write_reg(0,0x11,&data,1);
  180. if(ret == -1)return -1;
  181. return 0;
  182. }
  183. static int get_mode(Qma7981_Mode_t *p_mode)
  184. {
  185. int ret;
  186. uint8_t data;
  187. bool is_set;
  188. ret = ob_qma7981.ctx.read_reg(0,0x11,&data,1);
  189. if(ret == -1)return -1;
  190. get_bit(&data, 7, &is_set);
  191. if(is_set)
  192. {
  193. *p_mode = Qma7981_Mode_Active;
  194. }
  195. else
  196. {
  197. *p_mode = Qma7981_Mode_Standby;
  198. }
  199. return 0;
  200. }
  201. static int set_clock_freq(uint8_t freq)
  202. {
  203. int ret;
  204. uint8_t data;
  205. ret = ob_qma7981.ctx.read_reg(0,0x11,&data,1);
  206. if(ret == -1)return -1;
  207. data &= 0xF0;
  208. data |= (freq & 0x0F);
  209. ret = ob_qma7981.ctx.write_reg(0,0x11,&data,1);
  210. if(ret == -1)return -1;
  211. return 0;
  212. }
  213. static int get_clock_freq(uint8_t *p_freq)
  214. {
  215. int ret;
  216. uint8_t data;
  217. ret = ob_qma7981.ctx.read_reg(0,0x11,&data,1);
  218. if(ret == -1)return -1;
  219. *p_freq = (data & 0x0F);
  220. return 0;
  221. }
  222. static int set_bandwidth(uint8_t bandwidth)
  223. {
  224. int ret;
  225. uint8_t data;
  226. data = 0xE0;
  227. data |= (bandwidth & 0x07);
  228. ret = ob_qma7981.ctx.write_reg(0,0x10,&data,1);
  229. if(ret == -1)return -1;
  230. return 0;
  231. }
  232. static int get_bandwidth(uint8_t *p_bandwidth)
  233. {
  234. int ret;
  235. uint8_t data;
  236. ret = ob_qma7981.ctx.read_reg(0,0x10,&data,1);
  237. if(ret == -1)return -1;
  238. data &= 0x07;
  239. *p_bandwidth = data;
  240. return 0;
  241. }
  242. static int set_interrupt_pin_1_type(bool open_drain, bool active_high)
  243. {
  244. int ret;
  245. uint8_t data;
  246. ret = ob_qma7981.ctx.read_reg(0,0x20,&data,1);
  247. if(ret == -1)return -1;
  248. set_bit(&data, 0, active_high);
  249. set_bit(&data, 1, open_drain);
  250. ret = ob_qma7981.ctx.write_reg(0,0x20,&data,1);
  251. if(ret == -1)return -1;
  252. return 0;
  253. }
  254. static int set_full_scale_range(uint8_t range)
  255. {
  256. int ret;
  257. uint8_t data;
  258. ret = ob_qma7981.ctx.read_reg(0,0x0F,&data,1);
  259. if(ret == -1)return -1;
  260. data &= 0xF0;
  261. data |= (range & 0x0F);
  262. ret = ob_qma7981.ctx.write_reg(0,0x0F,&data,1);
  263. if(ret == -1)return -1;
  264. return 0;
  265. }
  266. static int get_full_scale_range(uint8_t *p_range)
  267. {
  268. int ret;
  269. uint8_t data;
  270. ret = ob_qma7981.ctx.read_reg(0,0x0F,&data,1);
  271. if(ret == -1)return -1;
  272. data &= 0x0F;
  273. *p_range = data;
  274. return 0;
  275. }
  276. /*API ----------------------------------------------*/
  277. /**
  278. @brief 初始化QMA驱动
  279. @param 无
  280. @return 错误代码 - [out] -1失败,0成功
  281. */
  282. int drv_qma_Init(void)
  283. {
  284. int ret;
  285. //初始化结构体
  286. memset(&ob_qma7981.cur_data,0,sizeof(ob_qma7981.cur_data));
  287. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_2G;
  288. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_OFF;
  289. ob_qma7981.ctx.read_reg = qma7981_platform_read;
  290. ob_qma7981.ctx.write_reg = qma7981_platform_write;
  291. //供电
  292. nrf_gpio_cfg(
  293. PIN_QMA7981_POWER,
  294. NRF_GPIO_PIN_DIR_OUTPUT,
  295. NRF_GPIO_PIN_INPUT_DISCONNECT,
  296. NRF_GPIO_PIN_NOPULL,
  297. NRF_GPIO_PIN_H0H1,
  298. NRF_GPIO_PIN_NOSENSE);
  299. nrf_gpio_pin_write(PIN_QMA7981_POWER,0);
  300. platform_delay_ms(200);
  301. nrf_gpio_pin_write(PIN_QMA7981_POWER,1);
  302. nrf_gpio_cfg_output(PIN_QMA7981_AD0);
  303. nrf_gpio_pin_write(PIN_QMA7981_AD0,ADO);
  304. nrf_gpio_cfg_output(PIN_QMA7981_nCS);
  305. nrf_gpio_pin_write(PIN_QMA7981_nCS,1);
  306. platform_delay_ms(200);
  307. IIC_MIDDLE_Init();
  308. ret = soft_reset(); //soft reset all of the registers, need to at least delay 10ms after called the function.
  309. if(ret == -1)return -1;
  310. return 0;
  311. }
  312. /**
  313. @brief QMA断电
  314. @param 无
  315. @return 错误代码 - [out] -1失败,0成功
  316. */
  317. int drv_qma_power_off(void)
  318. {
  319. //断电,清空qma配置
  320. nrf_gpio_cfg(
  321. PIN_QMA7981_POWER,
  322. NRF_GPIO_PIN_DIR_OUTPUT,
  323. NRF_GPIO_PIN_INPUT_DISCONNECT,
  324. NRF_GPIO_PIN_NOPULL,
  325. NRF_GPIO_PIN_H0H1,
  326. NRF_GPIO_PIN_NOSENSE);
  327. nrf_gpio_cfg_output(PIN_QMA7981_AD0);
  328. nrf_gpio_cfg_output(PIN_QMA7981_SDA);
  329. nrf_gpio_cfg_output(PIN_QMA7981_SCLK);
  330. nrf_gpio_cfg_output(PIN_QMA7981_nCS);
  331. nrf_gpio_pin_write(PIN_QMA7981_AD0,0);
  332. nrf_gpio_pin_write(PIN_QMA7981_SDA,0);
  333. nrf_gpio_pin_write(PIN_QMA7981_SCLK,0);
  334. nrf_gpio_pin_write(PIN_QMA7981_nCS,0);
  335. nrf_gpio_pin_write(PIN_QMA7981_POWER,0);
  336. return 0;
  337. }
  338. /**
  339. @brief QMA上电(默认配置挂起)
  340. @param 无
  341. @return 错误代码 - [out] -1失败,0成功
  342. */
  343. int drv_qma_power_on(void)
  344. {
  345. int ret;
  346. nrf_gpio_cfg(
  347. PIN_QMA7981_POWER,
  348. NRF_GPIO_PIN_DIR_OUTPUT,
  349. NRF_GPIO_PIN_INPUT_DISCONNECT,
  350. NRF_GPIO_PIN_NOPULL,
  351. NRF_GPIO_PIN_H0H1,
  352. NRF_GPIO_PIN_NOSENSE);
  353. nrf_gpio_pin_write(PIN_QMA7981_POWER,1);
  354. nrf_gpio_cfg_output(PIN_QMA7981_AD0);
  355. nrf_gpio_pin_write(PIN_QMA7981_AD0,ADO);
  356. nrf_gpio_cfg_output(PIN_QMA7981_nCS);
  357. nrf_gpio_pin_write(PIN_QMA7981_nCS,1);
  358. IIC_MIDDLE_Init();
  359. //初始化结构体
  360. memset(&ob_qma7981.cur_data,0,sizeof(ob_qma7981.cur_data));
  361. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_2G;
  362. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_OFF;
  363. // platform_delay_ms(10);
  364. ret = soft_reset(); //soft reset all of the registers, need to at least delay 10ms after called the function.
  365. if(ret == -1)return -1;
  366. return 0;
  367. }
  368. /**
  369. @brief 设置ACC量程
  370. @param acc_fs - [in] ACC量程
  371. @return 错误代码 - [out] -1失败,0成功
  372. */
  373. int drv_qma_set_acc_fs(QMA_ACC_FS_e acc_fs)
  374. {
  375. int ret;
  376. uint8_t range;
  377. if(ob_qma7981.cur_param.acc_fs != acc_fs)
  378. {
  379. switch(acc_fs)
  380. {
  381. case QMA_ACC_FS_2G:
  382. ret = set_full_scale_range(RANGE_2G); //set full scale acceleration range
  383. if(ret == -1)return -1;
  384. ret = get_full_scale_range(&range);
  385. if(ret == -1)return -1;
  386. if(range != RANGE_2G)return -1;
  387. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_2G;
  388. break;
  389. case QMA_ACC_FS_16G:
  390. ret = set_full_scale_range(RANGE_16G); //set full scale acceleration range
  391. if(ret == -1)return -1;
  392. ret = get_full_scale_range(&range);
  393. if(ret == -1)return -1;
  394. if(range != RANGE_16G)return -1;
  395. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_16G;
  396. break;
  397. }
  398. }
  399. return 0;
  400. }
  401. /**
  402. @brief 设置ACC采样频率
  403. @param acc_odr - [in] ACC采样频率
  404. @return 错误代码 - [out] -1失败,0成功
  405. */
  406. int drv_qma_set_acc_odr(QMA_ACC_ODR_e acc_odr)
  407. {
  408. int ret;
  409. uint8_t freq, bandwidth;
  410. Qma7981_Mode_t mode;
  411. if(ob_qma7981.cur_param.acc_odr != acc_odr)
  412. {
  413. switch(acc_odr)
  414. {
  415. case QMA_ACC_ODR_OFF:
  416. ret = set_mode(Qma7981_Mode_Standby); //sleep mode
  417. if(ret == -1)return -1;
  418. ret = get_mode(&mode);
  419. if(ret == -1)return -1;
  420. if(mode != Qma7981_Mode_Standby)return -1;
  421. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_OFF;
  422. break;
  423. case QMA_ACC_ODR_12HZ5:
  424. ret = set_mode(Qma7981_Mode_Active); //bring out of sleep mode
  425. if(ret == -1)return -1;
  426. ret = get_mode(&mode);
  427. if(ret == -1)return -1;
  428. if(mode != Qma7981_Mode_Active)return -1;
  429. //odr = 2 * BW; odr = 2 * (12.5KHZ / 1935) = 12.91HZ;
  430. ret = set_clock_freq(CLK_12_KHZ_5); //set digital clock freq
  431. if(ret == -1)return -1;
  432. ret = get_clock_freq(&freq);
  433. if(ret == -1)return -1;
  434. if(freq != CLK_12_KHZ_5)return -1;
  435. ret = set_bandwidth(MCLK_DIV_BY_1935); //set bandwitch (samples per sec)
  436. if(ret == -1)return -1;
  437. ret = get_bandwidth(&bandwidth);
  438. if(ret == -1)return -1;
  439. if(bandwidth != MCLK_DIV_BY_1935)return -1;
  440. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_12HZ5;
  441. break;
  442. case QMA_ACC_ODR_104HZ:
  443. ret = set_mode(Qma7981_Mode_Active); //bring out of sleep mode
  444. if(ret == -1)return -1;
  445. ret = get_mode(&mode);
  446. if(ret == -1)return -1;
  447. if(mode != Qma7981_Mode_Active)return -1;
  448. //odr = 2 * BW; odr = 2 * (200KHZ / 3855) = 103.76HZ;
  449. ret = set_clock_freq(CLK_200_KHZ); //set digital clock freq
  450. if(ret == -1)return -1;
  451. ret = get_clock_freq(&freq);
  452. if(ret == -1)return -1;
  453. if(freq != CLK_200_KHZ)return -1;
  454. ret = set_bandwidth(MCLK_DIV_BY_3855); //set bandwitch (samples per sec)
  455. if(ret == -1)return -1;
  456. ret = get_bandwidth(&bandwidth);
  457. if(ret == -1)return -1;
  458. if(bandwidth != MCLK_DIV_BY_3855)return -1;
  459. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_104HZ;
  460. break;
  461. }
  462. }
  463. return 0;
  464. }
  465. /**
  466. @brief 获取QMA配置参数
  467. @param param - [in] QMA配置参数
  468. @return 错误代码 - [out] -1失败,0成功
  469. */
  470. int drv_qma_get_config_param(drv_qma_config_param_t *p_param)
  471. {
  472. *p_param = ob_qma7981.cur_param;
  473. return 0;
  474. }
  475. /**
  476. @brief 获取QMA的ACC数据
  477. @param p_data - [out] 返回的ACC三轴数据
  478. @return 错误代码 - [out] -1失败,0成功
  479. */
  480. int drv_qma_get_acc_data(qma_data_t *p_data)
  481. {
  482. int ret;
  483. if(ob_qma7981.cur_param.acc_odr != QMA_ACC_ODR_OFF)
  484. {
  485. memset( &data_raw_acceleration, 0, sizeof(data_raw_acceleration));
  486. ret = get_accel_x(&data_raw_acceleration.acc[0]);
  487. if(ret == -1)return -1;
  488. ret = get_accel_y(&data_raw_acceleration.acc[1]);
  489. if(ret == -1)return -1;
  490. ret = get_accel_z(&data_raw_acceleration.acc[2]);
  491. if(ret == -1)return -1;
  492. p_data->acc[0] = data_raw_acceleration.acc[0];
  493. p_data->acc[1] = data_raw_acceleration.acc[1];
  494. p_data->acc[2] = data_raw_acceleration.acc[2];
  495. }
  496. else
  497. {
  498. return -1;
  499. }
  500. return 0;
  501. }