/******************** (C) COPYRIGHT 2016 STMicroelectronics ******************** * File Name : LSM303C_MAG_driver.c * Author : MEMS Application Team * Version : v1.1 * Date : 22 Nov 2016 * Description : LSM303C Platform Independent Driver * ******************************************************************************** * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS. *******************************************************************************/ /* Includes ------------------------------------------------------------------*/ #include "includes.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /************** Generic Function *******************/ /******************************************************************************* * Function Name : LSM303C_MAG_WriteReg * Description : Generic Writing function. It must be fullfilled with either * : I2C or SPI writing function * Input : Register Address, Data to be written * Output : None * Return : None *******************************************************************************/ u8_t LSM303C_MAG_WriteReg(void *handle, u8_t Reg, u8_t *Bufp, u16_t len) { /*Example*/ //HAL_I2C_Mem_Write(&hi2c1, LSM303C_MAG_I2C_ADDRESS, Reg, I2C_MEMADD_SIZE_8BIT, // Bufp, len, 1000); // OperationI2C(LSM303C_MAG_I2C_ADDRESS,&Reg,1); #if 0 IIC_Start(); IIC_Send_Byte(LSM303C_MAG_I2C_ADDRESS); if(IIC_Wait_Ack()) { IIC_Stop();//释放总线 return 1;//没应答则退出 } IIC_Send_Byte(Reg); IIC_Wait_Ack(); IIC_Send_Byte(Bufp[0]); IIC_Wait_Ack(); IIC_Stop(); return MEMS_SUCCESS; #else unsigned short addr; unsigned short i; MOTION_CS_MAG_LOW; DelayNs(5); //写0x23 addr=Reg; addr<<=8; addr|=*Bufp; for(i=0;i<16;i++) { MOTION_CLK_LOW; DelayNs(2); if((addr<0x7F)return 0; addr=0x80|Reg; MOTION_CS_MAG_LOW; DelayNs(5); //写 for(i=0;i<8;i++) { DelayNs(2); MOTION_CLK_LOW; DelayNs(2); if((addr<> LSM303C_MAG_WHO_AM_I_BIT_POSITION; //mask return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_W_SystemOperatingMode * Description : Write MD * Input : LSM303C_MAG_MD_t * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_W_SystemOperatingMode(void *handle, LSM303C_MAG_MD_t newValue) { u8_t value; if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) ) return MEMS_ERROR; value &= ~LSM303C_MAG_MD_MASK; value |= newValue; if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_R_SystemOperatingMode * Description : Read MD * Input : Pointer to LSM303C_MAG_MD_t * Output : Status of MD see LSM303C_MAG_MD_t * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_R_SystemOperatingMode(void *handle, LSM303C_MAG_MD_t *value) { if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, (u8_t *)value, 1) ) return MEMS_ERROR; *value &= LSM303C_MAG_MD_MASK; //mask return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_W_BlockDataUpdate * Description : Write BDU * Input : LSM303C_MAG_BDU_t * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_W_BlockDataUpdate(void *handle, LSM303C_MAG_BDU_t newValue) { u8_t value; if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG5, &value, 1) ) return MEMS_ERROR; value &= ~LSM303C_MAG_BDU_MASK; value |= newValue; if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG5, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_R_BlockDataUpdate * Description : Read BDU * Input : Pointer to LSM303C_MAG_BDU_t * Output : Status of BDU see LSM303C_MAG_BDU_t * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_R_BlockDataUpdate(void *handle, LSM303C_MAG_BDU_t *value) { if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG5, (u8_t *)value, 1) ) return MEMS_ERROR; *value &= LSM303C_MAG_BDU_MASK; //mask return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_W_FullScale * Description : Write FS * Input : LSM303C_MAG_FS_t * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_W_FullScale(void *handle, LSM303C_MAG_FS_t newValue) { u8_t value; if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) ) return MEMS_ERROR; value &= ~LSM303C_MAG_FS_MASK; value |= newValue; if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_R_FullScale * Description : Read FS * Input : Pointer to LSM303C_MAG_FS_t * Output : Status of FS see LSM303C_MAG_FS_t * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_R_FullScale(void *handle, LSM303C_MAG_FS_t *value) { if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, (u8_t *)value, 1) ) return MEMS_ERROR; *value &= LSM303C_MAG_FS_MASK; //mask return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_W_OutputDataRate * Description : Write DO * Input : LSM303C_MAG_DO_t * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_W_OutputDataRate(void *handle, LSM303C_MAG_DO_t newValue) { u8_t value; if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) ) return MEMS_ERROR; value &= ~LSM303C_MAG_DO_MASK; value |= newValue; if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_MAG_R_OutputDataRate * Description : Read DO * Input : Pointer to LSM303C_MAG_DO_t * Output : Status of DO see LSM303C_MAG_DO_t * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_R_OutputDataRate(void *handle, LSM303C_MAG_DO_t *value) { if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, (u8_t *)value, 1) ) return MEMS_ERROR; *value &= LSM303C_MAG_DO_MASK; //mask return MEMS_SUCCESS; } /******************************************************************************* * Function Name : status_t LSM303C_MAG_Get_Magnetic(u8_t *buff) * Description : Read Magnetic output register * Input : pointer to [u8_t] * Output : Magnetic buffer u8_t * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_MAG_Get_Magnetic(void *handle, u8_t *buff) { u8_t i, j, k; u8_t numberOfByteForDimension; numberOfByteForDimension=6/3; k=0; for (i=0; i<3;i++ ) { for (j=0; j buffer to swap * numberOfByte -> the buffer length in byte * dimension -> number of dimension * * Output : bufferToSwap -> buffer swapped * Return : None *******************************************************************************/ void LSM303C_MAG_SwapHighLowByte(u8_t *bufferToSwap, u8_t numberOfByte, u8_t dimension) { u8_t numberOfByteForDimension, i, j; u8_t tempValue[10]; numberOfByteForDimension=numberOfByte/dimension; for (i=0; i