/******************** (C) COPYRIGHT 2016 STMicroelectronics ******************** * File Name : LSM303C_ACC_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" //EXAMPLE to fill LSM303C_ACC_ReadReg and LSM303C_ACC_WriteReg /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /************** Generic Function *******************/ /******************************************************************************* * Function Name : LSM303C_ACC_WriteReg * Description : Generic Writing function. It must be fullfilled with either * : I2C or SPI writing function * Input : Register Address, ptr to buffer to be written, * length of buffer * Output : None * Return : None *******************************************************************************/ status_t LSM303C_ACC_WriteReg(void *handle, u8_t Reg, u8_t *Bufp, u16_t len) { // HAL_I2C_Mem_Write(&hi2c1, LSM303C_ACC_MEMS_I2C_ADDRESS, Reg, I2C_MEMADD_SIZE_8BIT, // Bufp, len, 1000); // return MEMS_SUCCESS; //OperationI2C(LSM303C_ACC_MEMS_I2C_ADDRESS,&Reg,1); #if 0 IIC_Start(); IIC_Send_Byte(LSM303C_ACC_MEMS_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(); #else unsigned short addr; unsigned short i; MOTION_CS_XL_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_XL_LOW; DelayNs(5); //写 for(i=0;i<8;i++) { DelayNs(2); MOTION_CLK_LOW; DelayNs(2); if((addr<0x7F status_t LSM303C_ACC_SetInterrupt1_Duration(void *handle, u8_t buff) { u8_t value; buff |= 0x7F; //coerce if( !LSM303C_ACC_ReadReg(handle, LSM303C_ACC_IG_DUR1, &value, 1) ) return MEMS_ERROR; value &= ~0x7F; //mask value |= buff; if( !LSM303C_ACC_WriteReg(handle, LSM303C_ACC_IG_DUR1, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_ACC_SetInterrupt2_Duration * Description : Set Interrupt Duration data row * Input : Duration row value [u8_t] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ //WARNING: value from 0x00->0x7F status_t LSM303C_ACC_SetInterrupt2_Duration(void *handle, u8_t buff) { u8_t value; buff |= 0x7F; //coerce if( !LSM303C_ACC_ReadReg(handle, LSM303C_ACC_IG_DUR2, &value, 1) ) return MEMS_ERROR; value &= ~0x7F; //mask value |= buff; if( !LSM303C_ACC_WriteReg(handle, LSM303C_ACC_IG_DUR2, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_ACC_SetReferenceValue * Description : Set Reference data row * Input : Reference row value [AxesRaw_t] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_ACC_SetReferenceValue(void *handle, u8_t *buff) { u8_t i; for (i=0; i<6;i++ ) { if( !LSM303C_ACC_WriteReg(handle, LSM303C_ACC_XL_REFERENCE+i, &buff[i], 1) ) return MEMS_ERROR; } return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_ACC_GetReferenceValue * Description : Get Reference data row * Input : Reference row value [AxesRaw_t] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LSM303C_ACC_GetReferenceValue(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; j0x1F status_t LSM303C_ACC_SetFIFO_threshold(void *handle, u8_t buff) { u8_t value; buff &= 0x1F; //coerce if (!LSM303C_ACC_ReadReg(handle, LSM303C_ACC_ACT_DUR, &value, 1) ) return MEMS_ERROR; value &= ~0x1F; //mask value |= buff; if( !LSM303C_ACC_WriteReg(handle, LSM303C_ACC_ACT_DUR, &value, 1) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LSM303C_ACC_GetFIFO_StoredData * Description : FIFO Stored data row * Input : Stored data row value [u8_t] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ //WARNING: value from 0x00->0x1F status_t LSM303C_ACC_GetFIFO_StoredData(void *handle, u8_t buff) { if (!LSM303C_ACC_ReadReg(handle, LSM303C_ACC_FIFO_SRC, &buff, 1) ) return MEMS_ERROR; buff &= 0x1F; //coerce return MEMS_SUCCESS; } /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/