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