/*
***********************************************************************************************
* LIS2MDL磁力计驱动
*
* 实现LIS2MDL底层驱动
*
* Filename : LIS2MDL.c
* Version : V1.00
* Programmer(s) : FengDuo
*
***********************************************************************************************
*/
#define LIS2MDL_MOUDLE
//文件声明
/*
***********************************************************************************************
* INCLUDE FILES
***********************************************************************************************
*/
#include "includes.h"
/*
***********************************************************************************************
* LOCAL DEFINES
***********************************************************************************************
*/
static LIS2MDL_SAMPLE_DEF LIS2MDLSample;
/*
***********************************************************************************************
* LOCAL FUNCTION PROTOTYPES
***********************************************************************************************
*/
static void LIS2MDL_IOInit(void);
static CPU_INT08U LIS2MDL_Register_Read(CPU_INT08U addr);
static void LIS2MDL_Register_Write(CPU_INT08U addr,CPU_INT08U val);
static void LIS2MDL_Read_XYZ(CPU_INT16S *x,CPU_INT16S *y,CPU_INT16S *z);
static void LIS2MDL_Read_Offset(CPU_INT16S *x_offset,CPU_INT16S *y_offset,CPU_INT16S *z_offset);
static void LIS2MDL_Write_Offset(CPU_INT16S x_offset,CPU_INT16S y_offset,CPU_INT16S z_offset);
static void LIS2MDL_Clean_Offset(void);
static CPU_BOOLEAN LIS2MDL_Wait_Data_Update(CPU_INT08U max_cnt);
static CPU_BOOLEAN LIS2MDL_Read_Average(CPU_INT16S *x,CPU_INT16S *y,CPU_INT16S *z,CPU_INT08U times);
#ifdef LIS2MDL_READ_TEMP
static void LIS2MDL_Read_TEMP(CPU_INT16S *temp);
#endif
/*
***********************************************************************************************
LOCAL FUNCTIONS
***********************************************************************************************
*/
/*
***********************************************************************************************
* LIS2MDL_IOInit
*
* Description: LIS2MDL磁力计 IO初始化
*
* Argument(s): 无
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_IOInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = LIS2MDL_PIN_SCL |LIS2MDL_PIN_SDA;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(LIS2MDL_PORT_SDA_SCL, &GPIO_InitStruct);
}
/*
***********************************************************************************************
* LIS2MDL_Register_Write
*
* Description: 写LIS2MDL寄存器
*
* Argument(s):
* addr: IN, 寄存器地址
* val: IN, 要写入的值
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Register_Write(CPU_INT08U addr,CPU_INT08U val)
{
CPU_INT08U len = 0x01;
LIS2MDL_HardI2C_Register_Write(LIS2MDL_ADDR_WRITE, addr, &val, len);
}
/*
***********************************************************************************************
* LIS2MDL_Register_Read
*
* Description: 读LIS2MDL寄存器
*
* Argument(s):
* addr: IN, 寄存器地址
*
* Return(s) : 寄存器读取值
*
* Caller(s) :
*
***********************************************************************************************
*/
static CPU_INT08U LIS2MDL_Register_Read(CPU_INT08U addr)
{
CPU_INT08U temp = 0x00, len = 0x01;
LIS2MDL_HardI2C_Register_Read(LIS2MDL_ADDR_READ, addr, &temp, len);
return temp;
}
/*
***********************************************************************************************
* LIS2MDL_Read_XYZ
*
* Description: LIS2MDL读取XYZ三轴磁场强度
*
* Argument(s):
* x: OUT, X轴数据地址
* y: OUT, Y轴数据地址
* z: OUT, Z轴数据地址
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Read_XYZ(CPU_INT16S *x,CPU_INT16S *y,CPU_INT16S *z)
{
CPU_INT08U buf[LIS2MDL_XYZBUF_LEN];
LIS2MDL_HardI2C_Register_Read(LIS2MDL_ADDR_READ, LIS2MDL_ADDR_XOUTL, buf, LIS2MDL_XYZBUF_LEN);
*x=(CPU_INT16S)((buf[1]<<8)+buf[0]);
*y=(CPU_INT16S)((buf[3]<<8)+buf[2]);
*z=(CPU_INT16S)((buf[5]<<8)+buf[4]);
}
#ifdef LIS2MDL_READ_TEMP
/*
***********************************************************************************************
* LIS2MDL_Read_TEMP
*
* Description: LIS2MDL读取温度值
*
* Argument(s):
* temp: OUT, 温度数据地址
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Read_TEMP(CPU_INT16S *temp)
{
CPU_INT08U buf[LIS2MDL_TEMBUF_LEN];
CPU_INT16S dataT;
LIS2MDL_HardI2C_Register_Read(LIS2MDL_ADDR_READ, LIS2MDL_ADDR_TEMPL, buf, LIS2MDL_TEMBUF_LEN);
dataT = (CPU_INT16S)((buf[1]<<8)+buf[0]);
*temp= dataT/8+25;
}
#endif
/*
***********************************************************************************************
* LIS2MDL_Read_Offset
*
* Description: LIS2MDL读取XYZ三轴OFFSET
*
* Argument(s):
* x_offset: OUT, X轴OFFSET数据地址
* y_offset: OUT, Y轴OFFSET数据地址
* z_offset: OUT, Z轴OFFSET数据地址
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Read_Offset(CPU_INT16S *x_offset,CPU_INT16S *y_offset,CPU_INT16S *z_offset)
{
CPU_INT08U buf[LIS2MDL_OFFSETBUF_LEN];
LIS2MDL_HardI2C_Register_Read(LIS2MDL_ADDR_READ, LIS2MDL_ADDR_OFFSETX_L, buf, LIS2MDL_OFFSETBUF_LEN);
*x_offset = (CPU_INT16S)((buf[1]<<8)+buf[0]);
*y_offset = (CPU_INT16S)((buf[3]<<8)+buf[2]);
*z_offset = (CPU_INT16S)((buf[5]<<8)+buf[4]);
}
/*
***********************************************************************************************
* LIS2MDL_Clean_Offset
*
* Description: 将LIS2MDL XYZ三轴OFFSET清0
*
* Argument(s): 无
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Clean_Offset(void)
{
CPU_INT08U i;
for(i = 0x00;i < LIS2MDL_OFFSETBUF_LEN;i++)
{
LIS2MDL_Register_Write(LIS2MDL_ADDR_OFFSETX_L+i,0x00);
}
HAL_Delay(100);
}
/*
***********************************************************************************************
* LIS2MDL_Write_Offset
*
* Description: 写入LIS2MDL XYZ三轴OFFSET
*
* Argument(s):
* x_offset: OUT, X轴OFFSET
* y_offset: OUT, Y轴OFFSET
* z_offset: OUT, Z轴OFFSET
*
* Return(s) : 无
*
* Caller(s) :
*
***********************************************************************************************
*/
static void LIS2MDL_Write_Offset(CPU_INT16S x_offset,CPU_INT16S y_offset,CPU_INT16S z_offset)
{
CPU_INT08U buf[LIS2MDL_OFFSETBUF_LEN];
buf[0] = x_offset & 0xFF;
buf[1] = (x_offset >> 8) & 0xFF;
buf[2] = y_offset & 0xFF;
buf[3] = (y_offset >> 8) & 0xFF