drv_qmc6310_v2.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 QMC6310断电
  102. @param 无
  103. @return 错误代码 - [out] -1失败,0成功
  104. */
  105. int drv_qmc6310_power_off(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. return 0;
  121. }
  122. /**
  123. @brief QMC6310上电(默认配置挂起)
  124. @param 无
  125. @return 错误代码 - [out] -1失败,0成功
  126. */
  127. int drv_qmc6310_power_on(void)
  128. {
  129. nrf_gpio_pin_write(PIN_BACK_SENSE_POWER,1);
  130. //初始化IIC
  131. IIC_BACK_Init();
  132. memset(&ob_qmc6310.cur_data,0,sizeof(ob_qmc6310.cur_data));
  133. ob_qmc6310.cur_param.mag_fs = QMC_MAG_FS_30GS;
  134. ob_qmc6310.cur_param.mag_odr = QMC_MAG_ODR_OFF;
  135. return 0;
  136. }
  137. /**
  138. @brief 设置MAG量程
  139. @param mag_fs - [in] MAG量程
  140. @return 错误代码 - [out] -1失败,0成功
  141. */
  142. int drv_qmc6310_set_mag_fs(QMC_MAG_FS_e mag_fs)
  143. {
  144. bool ret;
  145. uint8_t data;
  146. if(ob_qmc6310.cur_param.mag_fs != mag_fs)
  147. {
  148. switch(mag_fs)
  149. {
  150. case QMC_MAG_FS_30GS:
  151. data = QMC_MAG_FS_30GS;
  152. ret = ob_qmc6310.write(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_TWO,&data,1);
  153. if(ret == false)return -1;
  154. data = 0xFF;
  155. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_TWO,&data,1);
  156. if(ret == false || data != QMC_MAG_FS_30GS)return -1;
  157. ob_qmc6310.cur_param.mag_fs = QMC_MAG_FS_30GS;
  158. break;
  159. }
  160. }
  161. return 0;
  162. }
  163. /**
  164. @brief 获取配置MAG采样频率需要的步骤数量
  165. @return 错误代码 - [out] 配置MAG采样频率需要的步骤数量
  166. */
  167. int drv_qmc6310_get_mag_odr_flow(void)
  168. {
  169. return 4;
  170. }
  171. /**
  172. @brief 设置MAG采样频率
  173. @param mag_odr - [in] MAG采样频率
  174. @param flow - [in] 当前需要处理的步骤
  175. @return 错误代码 - [out] -1失败,0成功
  176. */
  177. int drv_qmc6310_set_mag_odr(QMC_MAG_ODR_e mag_odr, int flow)
  178. {
  179. int ret = -1;
  180. bool err;
  181. uint8_t data;
  182. if(flow <= 0 || flow >= 5)return -1;
  183. if(ob_qmc6310.cur_param.mag_odr != mag_odr)
  184. {
  185. switch(flow)
  186. {
  187. case 1:
  188. //define the sign for x y and z axis
  189. if(mag_odr != QMC_MAG_ODR_OFF)
  190. {
  191. data = 0x06;
  192. err = ob_qmc6310.write(QMC6310U_IIC_ADDR,0x29,&data,1);
  193. if(err == false)return -1;
  194. else ret = 0;
  195. }
  196. else
  197. {
  198. ret = 0;
  199. }
  200. break;
  201. case 2:
  202. //define the sign for x y and z axis
  203. if(mag_odr != QMC_MAG_ODR_OFF)
  204. {
  205. data = 0xFF;
  206. err = ob_qmc6310.read(QMC6310U_IIC_ADDR,0x29,&data,1);
  207. if(err == false || data != 0x06)return -1;
  208. else ret = 0;
  209. }
  210. else
  211. {
  212. ret = 0;
  213. }
  214. break;
  215. case 3:
  216. //set odr
  217. data = mag_odr;
  218. err = ob_qmc6310.write(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_ONE,&data,1);
  219. if(err == false)return -1;
  220. else ret = 0;
  221. break;
  222. case 4:
  223. data = 0xFF;
  224. err = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_CTL_REG_ONE,&data,1);
  225. if(err == false || data != mag_odr)return -1;
  226. else{
  227. ret = 0;
  228. ob_qmc6310.cur_param.mag_odr = mag_odr;
  229. }
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. return 0;
  236. }
  237. return ret;
  238. }
  239. /**
  240. @brief 获取LSM配置参数
  241. @param param - [in] LSM配置参数
  242. @return 错误代码 - [out] -1失败,0成功
  243. */
  244. int drv_qmc6310_get_config_param(drv_qmc_config_param_t *p_param)
  245. {
  246. *p_param = ob_qmc6310.cur_param;
  247. return 0;
  248. }
  249. /**
  250. @brief 获取LSM的ACC数据
  251. @param p_data - [out] 返回的ACC三轴数据
  252. @return 错误代码 - [out] -1失败,0成功
  253. */
  254. int drv_qmc6310_get_mag_data(qmc_data_t *p_data)
  255. {
  256. bool ret;
  257. unsigned char mag_data[6];
  258. mag_data[0] = QMC6310_DATA_OUT_X_LSB_REG;
  259. ret = ob_qmc6310.read(QMC6310U_IIC_ADDR,QMC6310_DATA_OUT_X_LSB_REG,mag_data,6);
  260. if(ret == false)return -1;
  261. if(p_data != NULL)
  262. {
  263. p_data->mag[0] = (int16_t)(((mag_data[1]) << 8) | mag_data[0]);
  264. p_data->mag[1] = (int16_t)(((mag_data[3]) << 8) | mag_data[2]);
  265. p_data->mag[2] = (int16_t)(((mag_data[5]) << 8) | mag_data[4]);
  266. }
  267. return 0;
  268. }