drv_qmc6310_03.c 7.5 KB

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