drv_qma7981.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. platform_delay_ms(200);
  305. IIC_MIDDLE_Init();
  306. ret = soft_reset(); //soft reset all of the registers, need to at least delay 10ms after called the function.
  307. if(ret == -1)return -1;
  308. return 0;
  309. }
  310. /**
  311. @brief QMA断电
  312. @param 无
  313. @return 错误代码 - [out] -1失败,0成功
  314. */
  315. int drv_qma_power_off(void)
  316. {
  317. //断电,清空qma配置
  318. nrf_gpio_cfg(
  319. PIN_QMA7981_POWER,
  320. NRF_GPIO_PIN_DIR_OUTPUT,
  321. NRF_GPIO_PIN_INPUT_DISCONNECT,
  322. NRF_GPIO_PIN_NOPULL,
  323. NRF_GPIO_PIN_H0H1,
  324. NRF_GPIO_PIN_NOSENSE);
  325. nrf_gpio_cfg_output(PIN_QMA7981_AD0);
  326. nrf_gpio_cfg_output(PIN_QMA7981_SDA);
  327. nrf_gpio_cfg_output(PIN_QMA7981_SCLK);
  328. nrf_gpio_cfg_output(PIN_QMA7981_nCS);
  329. nrf_gpio_pin_write(PIN_QMA7981_AD0,0);
  330. nrf_gpio_pin_write(PIN_QMA7981_SDA,0);
  331. nrf_gpio_pin_write(PIN_QMA7981_SCLK,0);
  332. nrf_gpio_pin_write(PIN_QMA7981_nCS,0);
  333. nrf_gpio_pin_write(PIN_QMA7981_POWER,0);
  334. return 0;
  335. }
  336. /**
  337. @brief QMA上电(默认配置挂起)
  338. @param 无
  339. @return 错误代码 - [out] -1失败,0成功
  340. */
  341. int drv_qma_power_on(void)
  342. {
  343. int ret;
  344. nrf_gpio_cfg(
  345. PIN_QMA7981_POWER,
  346. NRF_GPIO_PIN_DIR_OUTPUT,
  347. NRF_GPIO_PIN_INPUT_DISCONNECT,
  348. NRF_GPIO_PIN_NOPULL,
  349. NRF_GPIO_PIN_H0H1,
  350. NRF_GPIO_PIN_NOSENSE);
  351. nrf_gpio_pin_write(PIN_QMA7981_POWER,1);
  352. nrf_gpio_cfg_output(PIN_QMA7981_AD0);
  353. nrf_gpio_pin_write(PIN_QMA7981_AD0,ADO);
  354. IIC_MIDDLE_Init();
  355. //初始化结构体
  356. memset(&ob_qma7981.cur_data,0,sizeof(ob_qma7981.cur_data));
  357. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_2G;
  358. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_OFF;
  359. // platform_delay_ms(10);
  360. ret = soft_reset(); //soft reset all of the registers, need to at least delay 10ms after called the function.
  361. if(ret == -1)return -1;
  362. return 0;
  363. }
  364. /**
  365. @brief 设置ACC量程
  366. @param acc_fs - [in] ACC量程
  367. @return 错误代码 - [out] -1失败,0成功
  368. */
  369. int drv_qma_set_acc_fs(QMA_ACC_FS_e acc_fs)
  370. {
  371. int ret;
  372. uint8_t range;
  373. if(ob_qma7981.cur_param.acc_fs != acc_fs)
  374. {
  375. switch(acc_fs)
  376. {
  377. case QMA_ACC_FS_2G:
  378. ret = set_full_scale_range(RANGE_2G); //set full scale acceleration range
  379. if(ret == -1)return -1;
  380. ret = get_full_scale_range(&range);
  381. if(ret == -1)return -1;
  382. if(range != RANGE_2G)return -1;
  383. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_2G;
  384. break;
  385. case QMA_ACC_FS_16G:
  386. ret = set_full_scale_range(RANGE_16G); //set full scale acceleration range
  387. if(ret == -1)return -1;
  388. ret = get_full_scale_range(&range);
  389. if(ret == -1)return -1;
  390. if(range != RANGE_16G)return -1;
  391. ob_qma7981.cur_param.acc_fs = QMA_ACC_FS_16G;
  392. break;
  393. }
  394. }
  395. return 0;
  396. }
  397. /**
  398. @brief 设置ACC采样频率
  399. @param acc_odr - [in] ACC采样频率
  400. @return 错误代码 - [out] -1失败,0成功
  401. */
  402. int drv_qma_set_acc_odr(QMA_ACC_ODR_e acc_odr)
  403. {
  404. int ret;
  405. uint8_t freq, bandwidth;
  406. Qma7981_Mode_t mode;
  407. if(ob_qma7981.cur_param.acc_odr != acc_odr)
  408. {
  409. switch(acc_odr)
  410. {
  411. case QMA_ACC_ODR_OFF:
  412. ret = set_mode(Qma7981_Mode_Standby); //sleep mode
  413. if(ret == -1)return -1;
  414. ret = get_mode(&mode);
  415. if(ret == -1)return -1;
  416. if(mode != Qma7981_Mode_Standby)return -1;
  417. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_OFF;
  418. break;
  419. case QMA_ACC_ODR_12HZ5:
  420. ret = set_mode(Qma7981_Mode_Active); //bring out of sleep mode
  421. if(ret == -1)return -1;
  422. ret = get_mode(&mode);
  423. if(ret == -1)return -1;
  424. if(mode != Qma7981_Mode_Active)return -1;
  425. //odr = 2 * BW; odr = 2 * (12.5KHZ / 1935) = 12.91HZ;
  426. ret = set_clock_freq(CLK_12_KHZ_5); //set digital clock freq
  427. if(ret == -1)return -1;
  428. ret = get_clock_freq(&freq);
  429. if(ret == -1)return -1;
  430. if(freq != CLK_12_KHZ_5)return -1;
  431. ret = set_bandwidth(MCLK_DIV_BY_1935); //set bandwitch (samples per sec)
  432. if(ret == -1)return -1;
  433. ret = get_bandwidth(&bandwidth);
  434. if(ret == -1)return -1;
  435. if(bandwidth != MCLK_DIV_BY_1935)return -1;
  436. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_12HZ5;
  437. break;
  438. case QMA_ACC_ODR_104HZ:
  439. ret = set_mode(Qma7981_Mode_Active); //bring out of sleep mode
  440. if(ret == -1)return -1;
  441. ret = get_mode(&mode);
  442. if(ret == -1)return -1;
  443. if(mode != Qma7981_Mode_Active)return -1;
  444. //odr = 2 * BW; odr = 2 * (200KHZ / 3855) = 103.76HZ;
  445. ret = set_clock_freq(CLK_200_KHZ); //set digital clock freq
  446. if(ret == -1)return -1;
  447. ret = get_clock_freq(&freq);
  448. if(ret == -1)return -1;
  449. if(freq != CLK_200_KHZ)return -1;
  450. ret = set_bandwidth(MCLK_DIV_BY_3855); //set bandwitch (samples per sec)
  451. if(ret == -1)return -1;
  452. ret = get_bandwidth(&bandwidth);
  453. if(ret == -1)return -1;
  454. if(bandwidth != MCLK_DIV_BY_3855)return -1;
  455. ob_qma7981.cur_param.acc_odr = QMA_ACC_ODR_104HZ;
  456. break;
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. @brief 获取QMA配置参数
  463. @param param - [in] QMA配置参数
  464. @return 错误代码 - [out] -1失败,0成功
  465. */
  466. int drv_qma_get_config_param(drv_qma_config_param_t *p_param)
  467. {
  468. *p_param = ob_qma7981.cur_param;
  469. return 0;
  470. }
  471. /**
  472. @brief 获取QMA的ACC数据
  473. @param p_data - [out] 返回的ACC三轴数据
  474. @return 错误代码 - [out] -1失败,0成功
  475. */
  476. int drv_qma_get_acc_data(qma_data_t *p_data)
  477. {
  478. int ret;
  479. if(ob_qma7981.cur_param.acc_odr != QMA_ACC_ODR_OFF)
  480. {
  481. memset( &data_raw_acceleration, 0, sizeof(data_raw_acceleration));
  482. ret = get_accel_x(&data_raw_acceleration.acc[0]);
  483. if(ret == -1)return -1;
  484. ret = get_accel_y(&data_raw_acceleration.acc[1]);
  485. if(ret == -1)return -1;
  486. ret = get_accel_z(&data_raw_acceleration.acc[2]);
  487. if(ret == -1)return -1;
  488. p_data->acc[0] = data_raw_acceleration.acc[0];
  489. p_data->acc[1] = data_raw_acceleration.acc[1];
  490. p_data->acc[2] = data_raw_acceleration.acc[2];
  491. }
  492. else
  493. {
  494. return -1;
  495. }
  496. return 0;
  497. }