drv_qmc6310_04.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. ******************************************************************************
  3. * @file qmc6310.c
  4. * @author STMicroelectronics
  5. * @version V1.0
  6. * @date 2013-xx-xx
  7. * @brief qmc6310动
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * 实验平台:秉火 指南者 开发板
  12. * 论坛 :http://www.firebbs.cn
  13. * 淘宝 :https://fire-stm32.taobao.com
  14. *
  15. ******************************************************************************
  16. */
  17. #include "drv_qmc6310.h"
  18. #include <math.h>
  19. #include "drv_iic_04.h"
  20. #include "nrf_delay.h"
  21. static uint8_t chipid = 0;
  22. static QMC6310_map c_map;
  23. //bool MPU9250_Write_Byte(uint8_t Device_Address,uint8_t REG_Address,uint8_t REG_data)
  24. //{
  25. // return IIC_WriteBytes(Device_Address,REG_Address,&REG_data,1);
  26. //}
  27. //bool MPU9250_Read_nBytes(uint8_t Device_Address,uint8_t REG_Address,uint8_t *readDataBuf,uint8_t readDataLen)
  28. //{
  29. // return IIC_ReadBytes(Device_Address,REG_Address,readDataBuf,readDataLen);
  30. //}
  31. //bool MPU9250_register_write_len(uint8_t Device_Address,uint8_t register_address, uint8_t len,uint8_t *buf)
  32. //{
  33. // return IIC_WriteBytes(Device_Address,register_address,buf,len);
  34. //}
  35. //bool MPU9250_register_read_len(uint8_t Device_Address,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  36. //{
  37. // return IIC_ReadBytes(Device_Address,register_address,destination,number_of_bytes);
  38. //}
  39. static int qmc6310_read_block(uint8_t addr, uint8_t *data, uint8_t len)
  40. {
  41. return IIC_04_ReadBytes(QMC6310U_IIC_ADDR,addr,data,len);
  42. }
  43. static int qmc6310_write_reg(uint8_t addr, uint8_t data)
  44. {
  45. return IIC_04_WriteBytes(QMC6310U_IIC_ADDR,addr,&data,1);
  46. }
  47. static void qmc6310_set_layout(int layout)
  48. {
  49. if(layout == 0)
  50. {
  51. c_map.sign[AXIS_X] = 1;
  52. c_map.sign[AXIS_Y] = 1;
  53. c_map.sign[AXIS_Z] = 1;
  54. c_map.map[AXIS_X] = AXIS_X;
  55. c_map.map[AXIS_Y] = AXIS_Y;
  56. c_map.map[AXIS_Z] = AXIS_Z;
  57. }
  58. else if(layout == 1)
  59. {
  60. c_map.sign[AXIS_X] = -1;
  61. c_map.sign[AXIS_Y] = 1;
  62. c_map.sign[AXIS_Z] = 1;
  63. c_map.map[AXIS_X] = AXIS_Y;
  64. c_map.map[AXIS_Y] = AXIS_X;
  65. c_map.map[AXIS_Z] = AXIS_Z;
  66. }
  67. else if(layout == 2)
  68. {
  69. c_map.sign[AXIS_X] = -1;
  70. c_map.sign[AXIS_Y] = -1;
  71. c_map.sign[AXIS_Z] = 1;
  72. c_map.map[AXIS_X] = AXIS_X;
  73. c_map.map[AXIS_Y] = AXIS_Y;
  74. c_map.map[AXIS_Z] = AXIS_Z;
  75. }
  76. else if(layout == 3)
  77. {
  78. c_map.sign[AXIS_X] = 1;
  79. c_map.sign[AXIS_Y] = -1;
  80. c_map.sign[AXIS_Z] = 1;
  81. c_map.map[AXIS_X] = AXIS_Y;
  82. c_map.map[AXIS_Y] = AXIS_X;
  83. c_map.map[AXIS_Z] = AXIS_Z;
  84. }
  85. else if(layout == 4)
  86. {
  87. c_map.sign[AXIS_X] = -1;
  88. c_map.sign[AXIS_Y] = 1;
  89. c_map.sign[AXIS_Z] = -1;
  90. c_map.map[AXIS_X] = AXIS_X;
  91. c_map.map[AXIS_Y] = AXIS_Y;
  92. c_map.map[AXIS_Z] = AXIS_Z;
  93. }
  94. else if(layout == 5)
  95. {
  96. c_map.sign[AXIS_X] = 1;
  97. c_map.sign[AXIS_Y] = 1;
  98. c_map.sign[AXIS_Z] = -1;
  99. c_map.map[AXIS_X] = AXIS_Y;
  100. c_map.map[AXIS_Y] = AXIS_X;
  101. c_map.map[AXIS_Z] = AXIS_Z;
  102. }
  103. else if(layout == 6)
  104. {
  105. c_map.sign[AXIS_X] = 1;
  106. c_map.sign[AXIS_Y] = -1;
  107. c_map.sign[AXIS_Z] = -1;
  108. c_map.map[AXIS_X] = AXIS_X;
  109. c_map.map[AXIS_Y] = AXIS_Y;
  110. c_map.map[AXIS_Z] = AXIS_Z;
  111. }
  112. else if(layout == 7)
  113. {
  114. c_map.sign[AXIS_X] = -1;
  115. c_map.sign[AXIS_Y] = -1;
  116. c_map.sign[AXIS_Z] = -1;
  117. c_map.map[AXIS_X] = AXIS_Y;
  118. c_map.map[AXIS_Y] = AXIS_X;
  119. c_map.map[AXIS_Z] = AXIS_Z;
  120. }
  121. else
  122. {
  123. c_map.sign[AXIS_X] = 1;
  124. c_map.sign[AXIS_Y] = 1;
  125. c_map.sign[AXIS_Z] = 1;
  126. c_map.map[AXIS_X] = AXIS_X;
  127. c_map.map[AXIS_Y] = AXIS_Y;
  128. c_map.map[AXIS_Z] = AXIS_Z;
  129. }
  130. }
  131. static int qmc6310_get_chipid(void)
  132. {
  133. int ret = 0;
  134. ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1);
  135. if(ret == 0){
  136. SEGGER_RTT_printf(0,"change_iic_addr = 0x3c\n");
  137. }
  138. else
  139. {
  140. SEGGER_RTT_printf(0,"QMC6308_get_chipid chipid = 0x%x,i2c_addr = 0x2c\n", chipid);
  141. return 1;
  142. }
  143. ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1);
  144. if(ret == 0){
  145. SEGGER_RTT_printf(0,"change_iic_addr = 0x1c\n");
  146. }
  147. else
  148. {
  149. SEGGER_RTT_printf(0,"QMC6310_get_chipid chipid = 0x%x,i2c_addr = 0x3c\n", chipid);
  150. return 1;
  151. }
  152. ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1);
  153. if(ret == 0){
  154. SEGGER_RTT_printf(0,"Get_Chip_ID_Failed!\n");
  155. return 0;
  156. }
  157. else
  158. {
  159. SEGGER_RTT_printf(0,"QMC6310_get_chipid chipid = 0x%x,i2c_addr = 0x1c\n", chipid);
  160. return 1;
  161. }
  162. }
  163. uint8_t qmc6310_04_read_mag_xyz(float *data)
  164. {
  165. int res;
  166. unsigned char mag_data[6];
  167. short hw_d[3] = {0};
  168. short raw_c[3];
  169. int t1 = 0;
  170. unsigned char rdy = 0;
  171. /* Check status register for data availability */
  172. while(!(rdy & 0x01) && (t1 < 5))
  173. {
  174. rdy = QMC6310_STATUS_REG;
  175. res = qmc6310_read_block(QMC6310_STATUS_REG, &rdy, 1);
  176. t1++;
  177. }
  178. mag_data[0] = QMC6310_DATA_OUT_X_LSB_REG;
  179. res = qmc6310_read_block(QMC6310_DATA_OUT_X_LSB_REG, mag_data, 6);
  180. if(res == 0)
  181. {
  182. return 0;
  183. }
  184. hw_d[0] = (short)(((mag_data[1]) << 8) | mag_data[0]);
  185. hw_d[1] = (short)(((mag_data[3]) << 8) | mag_data[2]);
  186. hw_d[2] = (short)(((mag_data[5]) << 8) | mag_data[4]);
  187. //Unit:mG 1G = 100uT = 1000mG
  188. //SEGGER_RTT_printf(0,"Hx=%d, Hy=%d, Hz=%d\n",hw_d[0],hw_d[1],hw_d[2]);
  189. raw_c[AXIS_X] = (int)(c_map.sign[AXIS_X]*hw_d[c_map.map[AXIS_X]]);
  190. raw_c[AXIS_Y] = (int)(c_map.sign[AXIS_Y]*hw_d[c_map.map[AXIS_Y]]);
  191. raw_c[AXIS_Z] = (int)(c_map.sign[AXIS_Z]*hw_d[c_map.map[AXIS_Z]]);
  192. // SEGGER_RTT_printf(0,"04 raw_c[0]=%d,raw_c[1]=%d,raw_c[2]=%d\n",raw_c[0],raw_c[1],raw_c[2]);
  193. data[0] = (float)raw_c[0] / 10.0f;
  194. data[1] = (float)raw_c[1] / 10.0f;
  195. data[2] = (float)raw_c[2] / 10.0f;
  196. return res;
  197. }
  198. ///* Set the sensor mode */
  199. //static int qmc6310_set_mode(unsigned char mode)
  200. //{
  201. // int err = 0;
  202. // unsigned char ctrl1_value = 0;
  203. //
  204. // err = qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl1_value, 1);
  205. // ctrl1_value = (ctrl1_value&(~0x03))|mode;
  206. // SEGGER_RTT_printf(0,"QMC6310_set_mode, 0x0A = [%02x]->[%02x] \r\n", QMC6310_CTL_REG_ONE,ctrl1_value);
  207. // err = qmc6310_write_reg(QMC6310_CTL_REG_ONE, ctrl1_value);
  208. // return err;
  209. //}
  210. //static int qmc6310_set_output_data_rate(unsigned char rate){
  211. //
  212. // int err = 0;
  213. // unsigned char ctrl1_value = 0;
  214. //
  215. // err = qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl1_value, 1);
  216. // ctrl1_value = (ctrl1_value& (~0xE8)) | (rate << 5);
  217. // SEGGER_RTT_printf(0,"QMC6310_set_output_data_rate, 0x0A = [%02x]->[%02x] \r\n", QMC6310_CTL_REG_ONE,ctrl1_value);
  218. // err = qmc6310_write_reg(QMC6310_CTL_REG_ONE, ctrl1_value);
  219. // return err;
  220. //}
  221. static int qmc6310_reset(void)
  222. {
  223. qmc6310_write_reg(0x0b, 0x80); //reset
  224. qmc6310_write_reg(0x0b, 0x00); //取消reset
  225. return 1;
  226. }
  227. static int qmc6310_04_CheckIsConfigure(uint8_t hz)
  228. {
  229. unsigned char ctrl_value;
  230. qmc6310_read_block(0x0d, &ctrl_value, 1);
  231. // SEGGER_RTT_printf(0,"QMC6310 01 0x%x=0x%x \r\n", 0x0d, ctrl_value);
  232. if(ctrl_value != 0x40) return 0;
  233. qmc6310_read_block(QMC6310_CTL_REG_TWO, &ctrl_value, 1);
  234. // SEGGER_RTT_printf(0,"QMC6310 01 0x%x=0x%x \r\n", QMC6310_CTL_REG_TWO, ctrl_value);
  235. if(ctrl_value != 0x00) return 0;
  236. qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl_value, 1);
  237. // SEGGER_RTT_printf(0,"QMC6310 01 0x%x=0x%x \r\n", QMC6310_CTL_REG_ONE, ctrl_value);
  238. if(ctrl_value != hz) return 0;
  239. return 1;
  240. }
  241. int qmc6310_04_ReConfigure(uint8_t hz)
  242. {
  243. if(!qmc6310_write_reg(0x29, 0x06))return 0;//(Define the sign for X Y and Z axis)
  244. if(!qmc6310_write_reg(0x0d, 0x40))return 0;
  245. if(!qmc6310_write_reg(0x0b, 0x00))return 0; //30 GS
  246. if(!qmc6310_write_reg(0x0a, hz))return 0;
  247. return 1;
  248. }
  249. int qmc6310_04_Suspend(void)
  250. {
  251. return qmc6310_reset();
  252. }
  253. int qmc6310_04_Init(uint8_t hz)
  254. {
  255. IIC_04_Init();
  256. int ret = 0;
  257. ret = qmc6310_get_chipid();
  258. if(ret==0)
  259. {
  260. return 0;
  261. }
  262. qmc6310_set_layout(0);
  263. qmc6310_reset();
  264. qmc6310_04_ReConfigure(hz);
  265. nrf_delay_ms(200);
  266. return qmc6310_04_CheckIsConfigure(hz);
  267. }