drv_qmc6310_v2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*Includes ----------------------------------------------*/
  2. #include "drv_iic_back.h"
  3. #include "nrf_delay.h"
  4. #include "drv_qmc6310_v2.h"
  5. /*Private macro ------------------------------------------------*/
  6. /* vendor chip id*/
  7. #define QMC6310U_IIC_ADDR (0x1c<<1)
  8. #define QMC6310N_IIC_ADDR (0x3c<<1)
  9. #define QMC6310_CHIP_ID_REG 0x00
  10. /*data output register*/
  11. #define QMC6310_DATA_OUT_X_LSB_REG 0x01
  12. #define QMC6310_DATA_OUT_X_MSB_REG 0x02
  13. #define QMC6310_DATA_OUT_Y_LSB_REG 0x03
  14. #define QMC6310_DATA_OUT_Y_MSB_REG 0x04
  15. #define QMC6310_DATA_OUT_Z_LSB_REG 0x05
  16. #define QMC6310_DATA_OUT_Z_MSB_REG 0x06
  17. /*Status registers */
  18. #define QMC6310_STATUS_REG 0x09
  19. /* configuration registers */
  20. #define QMC6310_CTL_REG_ONE 0x0A /* Contrl register one */
  21. #define QMC6310_CTL_REG_TWO 0x0B /* Contrl register two */
  22. /* Magnetic Sensor Operating Mode MODE[1:0]*/
  23. #define QMC6310_SUSPEND_MODE 0x00
  24. #define QMC6310_NORMAL_MODE 0x01
  25. #define QMC6310_SINGLE_MODE 0x02
  26. #define QMC6310_H_PFM_MODE 0x03
  27. /*data output rate OSR2[2:0]*/
  28. #define OUTPUT_DATA_RATE_800HZ 0x00
  29. #define OUTPUT_DATA_RATE_400HZ 0x01
  30. #define OUTPUT_DATA_RATE_200HZ 0x02
  31. #define OUTPUT_DATA_RATE_100HZ 0x03
  32. /*oversample Ratio OSR[1]*/
  33. #define OVERSAMPLE_RATE_256 0x01
  34. #define OVERSAMPLE_RATE_128 0x00
  35. #define SET_RESET_ON 0x00
  36. #define SET_ONLY_ON 0x01
  37. #define SET_RESET_OFF 0x02
  38. #define QMC6310_DEFAULT_DELAY 200
  39. #define QMC6310_MAXHZ 0xC3
  40. #define QMC6310_200HZ 0x3D
  41. #define QMC6310_100HZ 0x39
  42. #define QMC6310_10HZ 0x31
  43. /*STRUCTION -----------------------------------------------------*/
  44. typedef struct drv_qmc6310
  45. {
  46. bool (*write)(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len); //IIC - 写
  47. bool (*read)(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len); //IIC - 读
  48. qmc_data_t cur_data; //当前QMC的数据
  49. drv_qmc_config_param_t cur_param; //当前QMC的配置
  50. } Drv_Qmc6310_t;
  51. /*Local Variable ----------------------------------------------*/
  52. static Drv_Qmc6310_t ob_qmc6310;
  53. /*Local Functions ----------------------------------------------*/
  54. static bool qmc6310_get_chipid(void)
  55. {
  56. bool ret;
  57. uint8_t chipid = 0xFF;
  58. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_CHIP_ID_REG,&chipid,1);
  59. if(ret == false)return false;
  60. return true;
  61. }
  62. /*API ----------------------------------------------*/
  63. /**
  64. @brief 初始化QMC6310驱动
  65. @param 无
  66. @return 错误代码 - [out] -1失败,0成功
  67. */
  68. int drv_qmc6310_Init(void)
  69. {
  70. nrf_gpio_cfg(
  71. PIN_BACK_SENSE_POWER,
  72. NRF_GPIO_PIN_DIR_OUTPUT,
  73. NRF_GPIO_PIN_INPUT_DISCONNECT,
  74. NRF_GPIO_PIN_NOPULL,
  75. NRF_GPIO_PIN_H0H1,
  76. NRF_GPIO_PIN_NOSENSE);
  77. nrf_gpio_cfg_output(PIN_BACK_SCL);
  78. nrf_gpio_cfg_output(PIN_BACK_SDA);
  79. nrf_gpio_pin_write(PIN_BACK_SCL,0);
  80. nrf_gpio_pin_write(PIN_BACK_SDA,0);
  81. //供电
  82. nrf_gpio_pin_write(PIN_BACK_SENSE_POWER,0);
  83. nrf_delay_ms(5);
  84. nrf_gpio_pin_write(PIN_BACK_SENSE_POWER,1);
  85. nrf_delay_ms(5);
  86. //初始化IIC
  87. IIC_BACK_Init();
  88. //初始化结构体
  89. memset(&ob_qmc6310, 0, sizeof(ob_qmc6310));
  90. ob_qmc6310.read = IIC_BACK_ReadBytes;
  91. ob_qmc6310.write = IIC_BACK_WriteBytes;
  92. ob_qmc6310.cur_param.mag_fs = QMC_MAG_FS_30GS;
  93. ob_qmc6310.cur_param.mag_odr = QMC_MAG_ODR_OFF;
  94. if(qmc6310_get_chipid())
  95. {
  96. return 0;
  97. }
  98. return -1;
  99. }
  100. /**
  101. @brief LSM挂起
  102. @param 无
  103. @return 错误代码 - [out] -1失败,0成功
  104. */
  105. int drv_qmc6310_suspend(void)
  106. {
  107. nrf_gpio_cfg(
  108. PIN_BACK_SENSE_POWER,
  109. NRF_GPIO_PIN_DIR_OUTPUT,
  110. NRF_GPIO_PIN_INPUT_DISCONNECT,
  111. NRF_GPIO_PIN_NOPULL,
  112. NRF_GPIO_PIN_H0H1,
  113. NRF_GPIO_PIN_NOSENSE);
  114. nrf_gpio_cfg_output(PIN_BACK_SCL);
  115. nrf_gpio_cfg_output(PIN_BACK_SDA);
  116. nrf_gpio_pin_write(PIN_BACK_SCL,0);
  117. nrf_gpio_pin_write(PIN_BACK_SDA,0);
  118. //供电
  119. nrf_gpio_pin_write(PIN_BACK_SENSE_POWER,0);
  120. nrf_delay_ms(5);
  121. nrf_gpio_pin_write(PIN_BACK_SENSE_POWER,1);
  122. nrf_delay_ms(5);
  123. //初始化IIC
  124. IIC_BACK_Init();
  125. memset(&ob_qmc6310.cur_data,0,sizeof(ob_qmc6310.cur_data));
  126. ob_qmc6310.cur_param.mag_fs = QMC_MAG_FS_30GS;
  127. ob_qmc6310.cur_param.mag_odr = QMC_MAG_ODR_OFF;
  128. return 0;
  129. }
  130. /**
  131. @brief 设置MAG量程
  132. @param mag_fs - [in] MAG量程
  133. @return 错误代码 - [out] -1失败,0成功
  134. */
  135. int drv_qmc6310_set_mag_fs(QMC_MAG_FS_e mag_fs)
  136. {
  137. bool ret;
  138. uint8_t data;
  139. if(ob_qmc6310.cur_param.mag_fs != mag_fs)
  140. {
  141. switch(mag_fs)
  142. {
  143. case QMC_MAG_FS_30GS:
  144. data = QMC_MAG_FS_30GS;
  145. ret = ob_qmc6310.write(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_TWO,&data,1);
  146. if(ret == false)return -1;
  147. data = 0xFF;
  148. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_TWO,&data,1);
  149. if(ret == false || data != QMC_MAG_FS_30GS)return -1;
  150. ob_qmc6310.cur_param.mag_fs = QMC_MAG_FS_30GS;
  151. break;
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. @brief 设置MAG采样频率
  158. @param mag_odr - [in] MAG采样频率
  159. @return 错误代码 - [out] -1失败,0成功
  160. */
  161. int drv_qmc6310_set_mag_odr(QMC_MAG_ODR_e mag_odr)
  162. {
  163. bool ret;
  164. uint8_t data;
  165. if(ob_qmc6310.cur_param.mag_odr != mag_odr)
  166. {
  167. if(mag_odr != QMC_MAG_ODR_OFF)
  168. {
  169. data = 0x06;
  170. ret = ob_qmc6310.write(QMC6310U_IIC_ADDR,0x29,&data,1);
  171. if(ret == false)return -1;
  172. data = 0xFF;
  173. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,0x29,&data,1);
  174. if(ret == false || data != 0x06)return -1;
  175. }
  176. //set odr
  177. data = mag_odr;
  178. ret = ob_qmc6310.write(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_ONE,&data,1);
  179. if(ret == false)return -1;
  180. data = 0xFF;
  181. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_ONE,&data,1);
  182. if(ret == false || data != mag_odr)return -1;
  183. ob_qmc6310.cur_param.mag_odr = mag_odr;
  184. }
  185. return 0;
  186. }
  187. /**
  188. @brief 获取LSM配置参数
  189. @param param - [in] LSM配置参数
  190. @return 错误代码 - [out] -1失败,0成功
  191. */
  192. int drv_qmc6310_get_config_param(drv_qmc_config_param_t *p_param)
  193. {
  194. *p_param = ob_qmc6310.cur_param;
  195. return 0;
  196. }
  197. /**
  198. @brief 获取LSM的ACC数据
  199. @param p_data - [out] 返回的ACC三轴数据
  200. @return 错误代码 - [out] -1失败,0成功
  201. */
  202. int drv_qmc6310_get_mag_data(qmc_data_t *p_data)
  203. {
  204. bool ret;
  205. unsigned char mag_data[6];
  206. mag_data[0] = QMC6310_DATA_OUT_X_LSB_REG;
  207. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_DATA_OUT_X_LSB_REG,mag_data,6);
  208. if(ret == false)return -1;
  209. if(p_data != NULL)
  210. {
  211. p_data->mag[0] = (int16_t)(((mag_data[1]) << 8) | mag_data[0]);
  212. p_data->mag[1] = (int16_t)(((mag_data[3]) << 8) | mag_data[2]);
  213. p_data->mag[2] = (int16_t)(((mag_data[5]) << 8) | mag_data[4]);
  214. }
  215. return 0;
  216. }