/** ****************************************************************************** * @file qmc6310.c * @author STMicroelectronics * @version V1.0 * @date 2013-xx-xx * @brief qmc6310动 ****************************************************************************** * @attention * * 实验平台:秉火 指南者 开发板 * 论坛 :http://www.firebbs.cn * 淘宝 :https://fire-stm32.taobao.com * ****************************************************************************** */ #include "drv_qmc6310.h" #include #include "drv_iic_01.h" static uint8_t chipid = 0; static QMC6310_map c_map; //bool MPU9250_Write_Byte(uint8_t Device_Address,uint8_t REG_Address,uint8_t REG_data) //{ // return IIC_WriteBytes(Device_Address,REG_Address,®_data,1); //} //bool MPU9250_Read_nBytes(uint8_t Device_Address,uint8_t REG_Address,uint8_t *readDataBuf,uint8_t readDataLen) //{ // return IIC_ReadBytes(Device_Address,REG_Address,readDataBuf,readDataLen); //} //bool MPU9250_register_write_len(uint8_t Device_Address,uint8_t register_address, uint8_t len,uint8_t *buf) //{ // return IIC_WriteBytes(Device_Address,register_address,buf,len); //} //bool MPU9250_register_read_len(uint8_t Device_Address,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination ) //{ // return IIC_ReadBytes(Device_Address,register_address,destination,number_of_bytes); //} static int qmc6310_read_block(uint8_t addr, uint8_t *data, uint8_t len) { return IIC_01_ReadBytes(QMC6310U_IIC_ADDR,addr,data,len); } static int qmc6310_write_reg(uint8_t addr, uint8_t data) { return IIC_01_WriteBytes(QMC6310U_IIC_ADDR,addr,&data,1); } static void qmc6310_set_layout(int layout) { if(layout == 0) { c_map.sign[AXIS_X] = 1; c_map.sign[AXIS_Y] = 1; c_map.sign[AXIS_Z] = 1; c_map.map[AXIS_X] = AXIS_X; c_map.map[AXIS_Y] = AXIS_Y; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 1) { c_map.sign[AXIS_X] = -1; c_map.sign[AXIS_Y] = 1; c_map.sign[AXIS_Z] = 1; c_map.map[AXIS_X] = AXIS_Y; c_map.map[AXIS_Y] = AXIS_X; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 2) { c_map.sign[AXIS_X] = -1; c_map.sign[AXIS_Y] = -1; c_map.sign[AXIS_Z] = 1; c_map.map[AXIS_X] = AXIS_X; c_map.map[AXIS_Y] = AXIS_Y; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 3) { c_map.sign[AXIS_X] = 1; c_map.sign[AXIS_Y] = -1; c_map.sign[AXIS_Z] = 1; c_map.map[AXIS_X] = AXIS_Y; c_map.map[AXIS_Y] = AXIS_X; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 4) { c_map.sign[AXIS_X] = -1; c_map.sign[AXIS_Y] = 1; c_map.sign[AXIS_Z] = -1; c_map.map[AXIS_X] = AXIS_X; c_map.map[AXIS_Y] = AXIS_Y; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 5) { c_map.sign[AXIS_X] = 1; c_map.sign[AXIS_Y] = 1; c_map.sign[AXIS_Z] = -1; c_map.map[AXIS_X] = AXIS_Y; c_map.map[AXIS_Y] = AXIS_X; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 6) { c_map.sign[AXIS_X] = 1; c_map.sign[AXIS_Y] = -1; c_map.sign[AXIS_Z] = -1; c_map.map[AXIS_X] = AXIS_X; c_map.map[AXIS_Y] = AXIS_Y; c_map.map[AXIS_Z] = AXIS_Z; } else if(layout == 7) { c_map.sign[AXIS_X] = -1; c_map.sign[AXIS_Y] = -1; c_map.sign[AXIS_Z] = -1; c_map.map[AXIS_X] = AXIS_Y; c_map.map[AXIS_Y] = AXIS_X; c_map.map[AXIS_Z] = AXIS_Z; } else { c_map.sign[AXIS_X] = 1; c_map.sign[AXIS_Y] = 1; c_map.sign[AXIS_Z] = 1; c_map.map[AXIS_X] = AXIS_X; c_map.map[AXIS_Y] = AXIS_Y; c_map.map[AXIS_Z] = AXIS_Z; } } static int qmc6310_get_chipid(void) { int ret = 0; ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1); if(ret == 0){ SEGGER_RTT_printf(0,"change_iic_addr = 0x3c\n"); } else { SEGGER_RTT_printf(0,"QMC6308_get_chipid chipid = 0x%x,i2c_addr = 0x2c\n", chipid); return 1; } ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1); if(ret == 0){ SEGGER_RTT_printf(0,"change_iic_addr = 0x1c\n"); } else { SEGGER_RTT_printf(0,"QMC6310_get_chipid chipid = 0x%x,i2c_addr = 0x3c\n", chipid); return 1; } ret = qmc6310_read_block(QMC6310_CHIP_ID_REG, &chipid , 1); if(ret == 0){ SEGGER_RTT_printf(0,"Get_Chip_ID_Failed!\n"); return 0; } else { SEGGER_RTT_printf(0,"QMC6310_get_chipid chipid = 0x%x,i2c_addr = 0x1c\n", chipid); return 1; } } uint8_t qmc6310_01_read_mag_xyz(float *data) { int res; unsigned char mag_data[6]; short hw_d[3] = {0}; short raw_c[3]; int t1 = 0; unsigned char rdy = 0; /* Check status register for data availability */ while(!(rdy & 0x01) && (t1 < 5)) { rdy = QMC6310_STATUS_REG; res = qmc6310_read_block(QMC6310_STATUS_REG, &rdy, 1); t1++; } mag_data[0] = QMC6310_DATA_OUT_X_LSB_REG; res = qmc6310_read_block(QMC6310_DATA_OUT_X_LSB_REG, mag_data, 6); if(res == 0) { return 0; } hw_d[0] = (short)(((mag_data[1]) << 8) | mag_data[0]); hw_d[1] = (short)(((mag_data[3]) << 8) | mag_data[2]); hw_d[2] = (short)(((mag_data[5]) << 8) | mag_data[4]); //Unit:mG 1G = 100uT = 1000mG //SEGGER_RTT_printf(0,"Hx=%d, Hy=%d, Hz=%d\n",hw_d[0],hw_d[1],hw_d[2]); raw_c[AXIS_X] = (int)(c_map.sign[AXIS_X]*hw_d[c_map.map[AXIS_X]]); raw_c[AXIS_Y] = (int)(c_map.sign[AXIS_Y]*hw_d[c_map.map[AXIS_Y]]); raw_c[AXIS_Z] = (int)(c_map.sign[AXIS_Z]*hw_d[c_map.map[AXIS_Z]]); 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]); data[0] = (float)raw_c[0] / 10.0f; data[1] = (float)raw_c[1] / 10.0f; data[2] = (float)raw_c[2] / 10.0f; return res; } ///* Set the sensor mode */ //static int qmc6310_set_mode(unsigned char mode) //{ // int err = 0; // unsigned char ctrl1_value = 0; // // err = qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl1_value, 1); // ctrl1_value = (ctrl1_value&(~0x03))|mode; // SEGGER_RTT_printf(0,"QMC6310_set_mode, 0x0A = [%02x]->[%02x] \r\n", QMC6310_CTL_REG_ONE,ctrl1_value); // err = qmc6310_write_reg(QMC6310_CTL_REG_ONE, ctrl1_value); // return err; //} //static int qmc6310_set_output_data_rate(unsigned char rate){ // // int err = 0; // unsigned char ctrl1_value = 0; // // err = qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl1_value, 1); // ctrl1_value = (ctrl1_value& (~0xE8)) | (rate << 5); // SEGGER_RTT_printf(0,"QMC6310_set_output_data_rate, 0x0A = [%02x]->[%02x] \r\n", QMC6310_CTL_REG_ONE,ctrl1_value); // err = qmc6310_write_reg(QMC6310_CTL_REG_ONE, ctrl1_value); // return err; //} static int qmc6310_reset(void) { qmc6310_write_reg(0x0b, 0x80); qmc6310_write_reg(0x0b, 0x00); //30 GS return 1; } int qmc6310_01_enable(void) { qmc6310_reset(); if(!qmc6310_write_reg(0x0d, 0x40))return 0; if(!qmc6310_write_reg(0x29, 0x06))return 0; // if(!qmc6310_write_reg(0x0a, 0x31))return 0;//10hz normal mode if(!qmc6310_write_reg(0x0a, 0x39))return 0;//100hz normal mode if(!qmc6310_write_reg(0x0b, 0x00))return 0; //30 GS return 1; } int qmc6310_01_disable(void) { if(!qmc6310_write_reg(0x0d, 0x40))return 0; if(!qmc6310_write_reg(0x29, 0x06))return 0; if(!qmc6310_write_reg(0x0a, 0x00))return 0;//suspend if(!qmc6310_write_reg(0x0b, 0x00))return 0; //30 GS return 1; } int qmc6310_01_init(void) { IIC_01_Init(); qmc6310_reset(); int ret = 0; ret = qmc6310_get_chipid(); if(ret==0) { return 0; } qmc6310_set_layout(0); qmc6310_01_enable(); // unsigned char ctrl_value; // qmc6310_read_block(QMC6310_CTL_REG_ONE, &ctrl_value, 1); // SEGGER_RTT_printf(0,"QMC6310 0x%x=0x%x \r\n", QMC6310_CTL_REG_ONE, ctrl_value); // qmc6310_read_block(QMC6310_CTL_REG_TWO, &ctrl_value, 1); // SEGGER_RTT_printf(0,"QMC6310 0x%x=0x%x \r\n", QMC6310_CTL_REG_TWO, ctrl_value); // qmc6310_read_block(0x0d, &ctrl_value, 1); // SEGGER_RTT_printf(0,"QMC6310 0x%x=0x%x \r\n", 0x0d, ctrl_value); return 1; }