[运动传感器] LIS3DH 官方驱动(STM32L1XX)

littleshrimp   2016-11-29 17:36 楼主



/** * @file HAL_LIS331DLH.h * @author AMS VMA RF application team * @version V2.2 * @date 01/11/2011 * @brief Hardware Abstraction Layer for LIS331DLH. * @details * * 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 SOURCE CODE IS PROTECTED BY A LICENSE. * FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED * IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE. * * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2> */ /* Define to prevent recursive inclusion*/ #ifndef __HAL_LIS331DLH_H #define __HAL_LIS331DLH_H #ifdef __cplusplus extern "C" { #endif /* Includes */ #include "stm32l1xx.h" /** * @addtogroup LIS331DLH * @{ */ /** * @addtogroup LIS331DLH_SPI_Define * @{ */ #define LIS_SPI SPI1 #define LIS_RCC_Periph_SPI RCC_APB2Periph_SPI1 #define LIS_SPI_Port GPIOA #define LIS_SPI_AF GPIO_AF_SPI1 #define LIS_SPI_MISO_Pin GPIO_Pin_6 #define LIS_SPI_MISO_SOURCE GPIO_PinSource6 #define LIS_SPI_M0SI_Pin GPIO_Pin_7 #define LIS_SPI_MOSI_SOURCE GPIO_PinSource7 #define LIS_SPI_SCK_Pin GPIO_Pin_5 #define LIS_SPI_SCK_SOURCE GPIO_PinSource5 #define LIS_SPI_CS_Pin GPIO_Pin_4 #define LIS_SPI_CS_Port GPIOA #define LIS_RCC_Port_SPI RCC_AHBPeriph_GPIOA /** *@} */ /* end of group LIS331DLH_SPI_Define */ /** * @addtogroup Accelerometer_Interrupt_Pin_Define * @{ */ #define LIS_A_INT2_PIN GPIO_Pin_11 #define LIS_A_INT2_GPIO_PORT GPIOB #define LIS_A_INT2_GPIO_CLK RCC_AHBPeriph_GPIOB #define LIS_A_INT2_EXTI_LINE EXTI_Line11 #define LIS_A_INT2_EXTI_PIN_SOURCE EXTI_PinSource11 #define LIS_A_INT2_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB #define LIS_A_INT2_EXTI_IRQn EXTI15_10_IRQn #define LIS3DH_IRQHandler EXTI15_10_IRQHandler /** *@} */ /* end of group Accelerometer_Interrupt_Pin_Define */ /** *@} */ /* end of group LIS331DLH */ #endif /* __HAL_LIS331DLH_H */ /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/ /******************** (C) COPYRIGHT 2012 STMicroelectronics ******************** * File Name : lis3dh_driver.h * Author : MSH Application Team * Author : Abhishek Anand, Fabio Tota * Version : $Revision:$ * Date : $Date:$ * Description : Descriptor Header for lis3dh_driver.c driver file * * HISTORY: * Date | Modification | Author * 24/06/2011 | Initial Revision | Fabio Tota * 07/06/2012 | Support for multiple drivers in the same program | Abhishek Anand * ******************************************************************************** * 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. * *******************************************************************************/ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __LIS3DH_DRIVER__H #define __LIS3DH_DRIVER__H /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ //these could change accordingly with the architecture #ifndef __ARCHDEP__TYPES #define __ARCHDEP__TYPES typedef unsigned char u8_t; typedef unsigned short int u16_t; typedef short int i16_t; typedef signed char i8_t; #endif /*__ARCHDEP__TYPES*/ typedef u8_t LIS3DH_IntPinConf_t; typedef u8_t LIS3DH_Axis_t; typedef u8_t LIS3DH_Int1Conf_t; //define structure #ifndef __SHARED__TYPES #define __SHARED__TYPES typedef enum { MEMS_SUCCESS = 0x01, MEMS_ERROR = 0x00 } status_t; typedef enum { MEMS_ENABLE = 0x01, MEMS_DISABLE = 0x00 } State_t; typedef struct { i16_t AXIS_X; i16_t AXIS_Y; i16_t AXIS_Z; } AxesRaw_t; #endif /*__SHARED__TYPES*/ typedef enum { LIS3DH_ODR_1Hz = 0x01, LIS3DH_ODR_10Hz = 0x02, LIS3DH_ODR_25Hz = 0x03, LIS3DH_ODR_50Hz = 0x04, LIS3DH_ODR_100Hz = 0x05, LIS3DH_ODR_200Hz = 0x06, LIS3DH_ODR_400Hz = 0x07, LIS3DH_ODR_1620Hz_LP = 0x08, LIS3DH_ODR_1344Hz_NP_5367HZ_LP = 0x09 } LIS3DH_ODR_t; typedef enum { LIS3DH_POWER_DOWN = 0x00, LIS3DH_LOW_POWER = 0x01, LIS3DH_NORMAL = 0x02 } LIS3DH_Mode_t; typedef enum { LIS3DH_HPM_NORMAL_MODE_RES = 0x00, LIS3DH_HPM_REF_SIGNAL = 0x01, LIS3DH_HPM_NORMAL_MODE = 0x02, LIS3DH_HPM_AUTORESET_INT = 0x03 } LIS3DH_HPFMode_t; typedef enum { LIS3DH_HPFCF_0 = 0x00, LIS3DH_HPFCF_1 = 0x01, LIS3DH_HPFCF_2 = 0x02, LIS3DH_HPFCF_3 = 0x03 } LIS3DH_HPFCutOffFreq_t; typedef struct { u16_t AUX_1; u16_t AUX_2; u16_t AUX_3; } LIS3DH_Aux123Raw_t; typedef enum { LIS3DH_FULLSCALE_2 = 0x00, LIS3DH_FULLSCALE_4 = 0x01, LIS3DH_FULLSCALE_8 = 0x02, LIS3DH_FULLSCALE_16 = 0x03 } LIS3DH_Fullscale_t; typedef enum { LIS3DH_BLE_LSB = 0x00, LIS3DH_BLE_MSB = 0x01 } LIS3DH_Endianess_t; typedef enum { LIS3DH_SELF_TEST_DISABLE = 0x00, LIS3DH_SELF_TEST_0 = 0x01, LIS3DH_SELF_TEST_1 = 0x02 } LIS3DH_SelfTest_t; typedef enum { LIS3DH_FIFO_BYPASS_MODE = 0x00, LIS3DH_FIFO_MODE = 0x01, LIS3DH_FIFO_STREAM_MODE = 0x02, LIS3DH_FIFO_TRIGGER_MODE = 0x03, LIS3DH_FIFO_DISABLE = 0x04 } LIS3DH_FifoMode_t; typedef enum { LIS3DH_TRIG_INT1 = 0x00, LIS3DH_TRIG_INT2 = 0x01 } LIS3DH_TrigInt_t; typedef enum { LIS3DH_SPI_4_WIRE = 0x00, LIS3DH_SPI_3_WIRE = 0x01 } LIS3DH_SPIMode_t; typedef enum { LIS3DH_X_ENABLE = 0x01, LIS3DH_X_DISABLE = 0x00, LIS3DH_Y_ENABLE = 0x02, LIS3DH_Y_DISABLE = 0x00, LIS3DH_Z_ENABLE = 0x04, LIS3DH_Z_DISABLE = 0x00 } LIS3DH_AXISenable_t; typedef enum { LIS3DH_INT1_6D_4D_DISABLE = 0x00, LIS3DH_INT1_6D_ENABLE = 0x01, LIS3DH_INT1_4D_ENABLE = 0x02 } LIS3DH_INT_6D_4D_t; typedef enum { LIS3DH_UP_SX = 0x44, LIS3DH_UP_DX = 0x42, LIS3DH_DW_SX = 0x41, LIS3DH_DW_DX = 0x48, LIS3DH_TOP = 0x60, LIS3DH_BOTTOM = 0x50 } LIS3DH_POSITION_6D_t; typedef enum { LIS3DH_INT_MODE_OR = 0x00, LIS3DH_INT_MODE_6D_MOVEMENT = 0x01, LIS3DH_INT_MODE_AND = 0x02, LIS3DH_INT_MODE_6D_POSITION = 0x03 } LIS3DH_Int1Mode_t; //interrupt click response // b7 = don't care b6 = IA b5 = DClick b4 = Sclick b3 = Sign // b2 = z b1 = y b0 = x typedef enum { LIS3DH_DCLICK_Z_P = 0x24, LIS3DH_DCLICK_Z_N = 0x2C, LIS3DH_SCLICK_Z_P = 0x14, LIS3DH_SCLICK_Z_N = 0x1C, LIS3DH_DCLICK_Y_P = 0x22, LIS3DH_DCLICK_Y_N = 0x2A, LIS3DH_SCLICK_Y_P = 0x12, LIS3DH_SCLICK_Y_N = 0x1A, LIS3DH_DCLICK_X_P = 0x21, LIS3DH_DCLICK_X_N = 0x29, LIS3DH_SCLICK_X_P = 0x11, LIS3DH_SCLICK_X_N = 0x19, LIS3DH_NO_CLICK = 0x00 } LIS3DH_Click_Response; //TODO: start from here and manage the shared macros etc before this /* Exported constants --------------------------------------------------------*/ #ifndef __SHARED__CONSTANTS #define __SHARED__CONSTANTS #define MEMS_SET 0x01 #define MEMS_RESET 0x00 #endif /*__SHARED__CONSTANTS*/ //Register Definition #define LIS3DH_WHO_AM_I 0x0F // device identification register // CONTROL REGISTER 1 #define LIS3DH_CTRL_REG1 0x20 #define LIS3DH_ODR_BIT BIT(4) #define LIS3DH_LPEN BIT(3) #define LIS3DH_ZEN BIT(2) #define LIS3DH_YEN BIT(1) #define LIS3DH_XEN BIT(0) //CONTROL REGISTER 2 #define LIS3DH_CTRL_REG2 0x21 #define LIS3DH_HPM BIT(6) #define LIS3DH_HPCF BIT(4) #define LIS3DH_FDS BIT(3) #define LIS3DH_HPCLICK BIT(2) #define LIS3DH_HPIS2 BIT(1) #define LIS3DH_HPIS1 BIT(0) //CONTROL REGISTER 3 #define LIS3DH_CTRL_REG3 0x22 #define LIS3DH_I1_CLICK BIT(7) #define LIS3DH_I1_AOI1 BIT(6) #define LIS3DH_I1_AOI2 BIT(5) #define LIS3DH_I1_DRDY1 BIT(4) #define LIS3DH_I1_DRDY2 BIT(3) #define LIS3DH_I1_WTM BIT(2) #define LIS3DH_I1_ORUN BIT(1) //CONTROL REGISTER 6 #define LIS3DH_CTRL_REG6 0x25 #define LIS3DH_I2_CLICK BIT(7) #define LIS3DH_I2_INT1 BIT(6) #define LIS3DH_I2_BOOT BIT(4) #define LIS3DH_H_LACTIVE BIT(1) //TEMPERATURE CONFIG REGISTER #define LIS3DH_TEMP_CFG_REG 0x1F #define LIS3DH_ADC_PD BIT(7) #define LIS3DH_TEMP_EN BIT(6) //CONTROL REGISTER 4 #define LIS3DH_CTRL_REG4 0x23 #define LIS3DH_BDU BIT(7) #define LIS3DH_BLE BIT(6) #define LIS3DH_FS BIT(4) #define LIS3DH_HR BIT(3) #define LIS3DH_ST BIT(1) #define LIS3DH_SIM BIT(0) //CONTROL REGISTER 5 #define LIS3DH_CTRL_REG5 0x24 #define LIS3DH_BOOT BIT(7) #define LIS3DH_FIFO_EN BIT(6) #define LIS3DH_LIR_INT1 BIT(3) #define LIS3DH_D4D_INT1 BIT(2) //REFERENCE/DATA_CAPTURE #define LIS3DH_REFERENCE_REG 0x26 #define LIS3DH_REF BIT(0) //STATUS_REG_AXIES #define LIS3DH_STATUS_REG 0x27 #define LIS3DH_ZYXOR BIT(7) #define LIS3DH_ZOR BIT(6) #define LIS3DH_YOR BIT(5) #define LIS3DH_XOR BIT(4) #define LIS3DH_ZYXDA BIT(3) #define LIS3DH_ZDA BIT(2) #define LIS3DH_YDA BIT(1) #define LIS3DH_XDA BIT(0) //STATUS_REG_AUX #define LIS3DH_STATUS_AUX 0x07 //INTERRUPT 1 CONFIGURATION #define LIS3DH_INT1_CFG 0x30 #define LIS3DH_ANDOR BIT(7) #define LIS3DH_INT_6D BIT(6) #define LIS3DH_ZHIE BIT(5) #define LIS3DH_ZLIE BIT(4) #define LIS3DH_YHIE BIT(3) #define LIS3DH_YLIE BIT(2) #define LIS3DH_XHIE BIT(1) #define LIS3DH_XLIE BIT(0) //FIFO CONTROL REGISTER #define LIS3DH_FIFO_CTRL_REG 0x2E #define LIS3DH_FM BIT(6) #define LIS3DH_TR BIT(5) #define LIS3DH_FTH BIT(0) //CONTROL REG3 bit mask #define LIS3DH_CLICK_ON_PIN_INT1_ENABLE 0x80 #define LIS3DH_CLICK_ON_PIN_INT1_DISABLE 0x00 #define LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE 0x40 #define LIS3DH_I1_INT1_ON_PIN_INT1_DISABLE 0x00 #define LIS3DH_I1_INT2_ON_PIN_INT1_ENABLE 0x20 #define LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE 0x00 #define LIS3DH_I1_DRDY1_ON_INT1_ENABLE 0x10 #define LIS3DH_I1_DRDY1_ON_INT1_DISABLE 0x00 #define LIS3DH_I1_DRDY2_ON_INT1_ENABLE 0x08 #define LIS3DH_I1_DRDY2_ON_INT1_DISABLE 0x00 #define LIS3DH_WTM_ON_INT1_ENABLE 0x04 #define LIS3DH_WTM_ON_INT1_DISABLE 0x00 #define LIS3DH_INT1_OVERRUN_ENABLE 0x02 #define LIS3DH_INT1_OVERRUN_DISABLE 0x00 //CONTROL REG6 bit mask #define LIS3DH_CLICK_ON_PIN_INT2_ENABLE 0x80 #define LIS3DH_CLICK_ON_PIN_INT2_DISABLE 0x00 #define LIS3DH_I2_INT1_ON_PIN_INT2_ENABLE 0x40 #define LIS3DH_I2_INT1_ON_PIN_INT2_DISABLE 0x00 #define LIS3DH_I2_INT2_ON_PIN_INT2_ENABLE 0x20 #define LIS3DH_I2_INT2_ON_PIN_INT2_DISABLE 0x00 #define LIS3DH_I2_BOOT_ON_INT2_ENABLE 0x10 #define LIS3DH_I2_BOOT_ON_INT2_DISABLE 0x00 #define LIS3DH_INT_ACTIVE_HIGH 0x00 #define LIS3DH_INT_ACTIVE_LOW 0x02 //INT1_CFG bit mask #define LIS3DH_INT1_AND 0x80 #define LIS3DH_INT1_OR 0x00 #define LIS3DH_INT1_ZHIE_ENABLE 0x20 #define LIS3DH_INT1_ZHIE_DISABLE 0x00 #define LIS3DH_INT1_ZLIE_ENABLE 0x10 #define LIS3DH_INT1_ZLIE_DISABLE 0x00 #define LIS3DH_INT1_YHIE_ENABLE 0x08 #define LIS3DH_INT1_YHIE_DISABLE 0x00 #define LIS3DH_INT1_YLIE_ENABLE 0x04 #define LIS3DH_INT1_YLIE_DISABLE 0x00 #define LIS3DH_INT1_XHIE_ENABLE 0x02 #define LIS3DH_INT1_XHIE_DISABLE 0x00 #define LIS3DH_INT1_XLIE_ENABLE 0x01 #define LIS3DH_INT1_XLIE_DISABLE 0x00 //INT1_SRC bit mask #define LIS3DH_INT1_SRC_IA 0x40 #define LIS3DH_INT1_SRC_ZH 0x20 #define LIS3DH_INT1_SRC_ZL 0x10 #define LIS3DH_INT1_SRC_YH 0x08 #define LIS3DH_INT1_SRC_YL 0x04 #define LIS3DH_INT1_SRC_XH 0x02 #define LIS3DH_INT1_SRC_XL 0x01 //INT1 REGISTERS #define LIS3DH_INT1_THS 0x32 #define LIS3DH_INT1_DURATION 0x33 //INTERRUPT 1 SOURCE REGISTER #define LIS3DH_INT1_SRC 0x31 //FIFO Source Register bit Mask #define LIS3DH_FIFO_SRC_WTM 0x80 #define LIS3DH_FIFO_SRC_OVRUN 0x40 #define LIS3DH_FIFO_SRC_EMPTY 0x20 //INTERRUPT CLICK REGISTER #define LIS3DH_CLICK_CFG 0x38 //INTERRUPT CLICK CONFIGURATION bit mask #define LIS3DH_ZD_ENABLE 0x20 #define LIS3DH_ZD_DISABLE 0x00 #define LIS3DH_ZS_ENABLE 0x10 #define LIS3DH_ZS_DISABLE 0x00 #define LIS3DH_YD_ENABLE 0x08 #define LIS3DH_YD_DISABLE 0x00 #define LIS3DH_YS_ENABLE 0x04 #define LIS3DH_YS_DISABLE 0x00 #define LIS3DH_XD_ENABLE 0x02 #define LIS3DH_XD_DISABLE 0x00 #define LIS3DH_XS_ENABLE 0x01 #define LIS3DH_XS_DISABLE 0x00 //INTERRUPT CLICK SOURCE REGISTER #define LIS3DH_CLICK_SRC 0x39 //INTERRUPT CLICK SOURCE REGISTER bit mask #define LIS3DH_IA 0x40 #define LIS3DH_DCLICK 0x20 #define LIS3DH_SCLICK 0x10 #define LIS3DH_CLICK_SIGN 0x08 #define LIS3DH_CLICK_Z 0x04 #define LIS3DH_CLICK_Y 0x02 #define LIS3DH_CLICK_X 0x01 //Click-click Register #define LIS3DH_CLICK_THS 0x3A #define LIS3DH_TIME_LIMIT 0x3B #define LIS3DH_TIME_LATENCY 0x3C #define LIS3DH_TIME_WINDOW 0x3D //OUTPUT REGISTER #define LIS3DH_OUT_X_L 0x28 #define LIS3DH_OUT_X_H 0x29 #define LIS3DH_OUT_Y_L 0x2A #define LIS3DH_OUT_Y_H 0x2B #define LIS3DH_OUT_Z_L 0x2C #define LIS3DH_OUT_Z_H 0x2D //AUX REGISTER #define LIS3DH_OUT_1_L 0x08 #define LIS3DH_OUT_1_H 0x09 #define LIS3DH_OUT_2_L 0x0A #define LIS3DH_OUT_2_H 0x0B #define LIS3DH_OUT_3_L 0x0C #define LIS3DH_OUT_3_H 0x0D //STATUS REGISTER bit mask #define LIS3DH_STATUS_REG_ZYXOR 0x80 // 1 : new data set has over written the previous one // 0 : no overrun has occurred (default) #define LIS3DH_STATUS_REG_ZOR 0x40 // 0 : no overrun has occurred (default) // 1 : new Z-axis data has over written the previous one #define LIS3DH_STATUS_REG_YOR 0x20 // 0 : no overrun has occurred (default) // 1 : new Y-axis data has over written the previous one #define LIS3DH_STATUS_REG_XOR 0x10 // 0 : no overrun has occurred (default) // 1 : new X-axis data has over written the previous one #define LIS3DH_STATUS_REG_ZYXDA 0x08 // 0 : a new set of data is not yet avvious one // 1 : a new set of data is available #define LIS3DH_STATUS_REG_ZDA 0x04 // 0 : a new data for the Z-Axis is not availvious one // 1 : a new data for the Z-Axis is available #define LIS3DH_STATUS_REG_YDA 0x02 // 0 : a new data for the Y-Axis is not available // 1 : a new data for the Y-Axis is available #define LIS3DH_STATUS_REG_XDA 0x01 // 0 : a new data for the X-Axis is not available #define LIS3DH_DATAREADY_BIT LIS3DH_STATUS_REG_ZYXDA //STATUS AUX REGISTER bit mask #define LIS3DH_STATUS_AUX_321OR 0x80 #define LIS3DH_STATUS_AUX_3OR 0x40 #define LIS3DH_STATUS_AUX_2OR 0x20 #define LIS3DH_STATUS_AUX_1OR 0x10 #define LIS3DH_STATUS_AUX_321DA 0x08 #define LIS3DH_STATUS_AUX_3DA 0x04 #define LIS3DH_STATUS_AUX_2DA 0x02 #define LIS3DH_STATUS_AUX_1DA 0x01 #define LIS3DH_MEMS_I2C_ADDRESS 0x33 //FIFO REGISTERS #define LIS3DH_FIFO_CTRL_REG 0x2E #define LIS3DH_FIFO_SRC_REG 0x2F //Sensitivity #define LIS3DH_Acc_Sensitivity_2g 1 /*!< accelerometer sensitivity with 2 g full scale [mg/LSB] */ #define LIS3DH_Acc_Sensitivity_4g 2 /*!< accelerometer sensitivity with 4 g full scale [mg/LSB] */ #define LIS3DH_Acc_Sensitivity_8g 4 /*!< accelerometer sensitivity with 8 g full scale [mg/LSB] */ #define LIS3DH_Acc_Sensitivity_16g 12 /*!< accelerometer sensitivity with 8 g full scale [mg/LSB] */ /* Exported macro ------------------------------------------------------------*/ #ifndef __SHARED__MACROS #define __SHARED__MACROS #define ValBit(VAR,Place) (VAR & (1<<Place)) #define BIT(x) ( (x) ) #endif /*__SHARED__MACROS*/ /* Exported functions --------------------------------------------------------*/ //Sensor Configuration Functions void LIS3DH_SpiInit(void); status_t LIS3DH_SetODR(LIS3DH_ODR_t ov); status_t LIS3DH_SetMode(LIS3DH_Mode_t md); status_t LIS3DH_SetAxis(LIS3DH_Axis_t axis); status_t LIS3DH_SetFullScale(LIS3DH_Fullscale_t fs); status_t LIS3DH_SetBDU(State_t bdu); status_t LIS3DH_SetBLE(LIS3DH_Endianess_t ble); status_t LIS3DH_SetSelfTest(LIS3DH_SelfTest_t st); status_t LIS3DH_SetTemperature(State_t state); status_t LIS3DH_SetADCAux(State_t state); //Filtering Functions status_t LIS3DH_HPFClickEnable(State_t hpfe); status_t LIS3DH_HPFAOI1Enable(State_t hpfe); status_t LIS3DH_HPFAOI2Enable(State_t hpfe); status_t LIS3DH_SetHPFMode(LIS3DH_HPFMode_t hpf); status_t LIS3DH_SetHPFCutOFF(LIS3DH_HPFCutOffFreq_t hpf); status_t LIS3DH_SetFilterDataSel(State_t state); //Interrupt Functions status_t LIS3DH_SetInt1Pin(LIS3DH_IntPinConf_t pinConf); status_t LIS3DH_SetInt2Pin(LIS3DH_IntPinConf_t pinConf); status_t LIS3DH_Int1LatchEnable(State_t latch); status_t LIS3DH_ResetInt1Latch(void); status_t LIS3DH_SetIntConfiguration(LIS3DH_Int1Conf_t ic); status_t LIS3DH_SetInt1Threshold(u8_t ths); status_t LIS3DH_SetInt1Duration(LIS3DH_Int1Conf_t id); status_t LIS3DH_SetIntMode(LIS3DH_Int1Mode_t ic); status_t LIS3DH_SetClickCFG(u8_t status); status_t LIS3DH_SetInt6D4DConfiguration(LIS3DH_INT_6D_4D_t ic); status_t LIS3DH_GetInt1Src(u8_t* val); status_t LIS3DH_GetInt1SrcBit(u8_t statusBIT, u8_t* val); //FIFO Functions status_t LIS3DH_FIFOModeEnable(LIS3DH_FifoMode_t fm); status_t LIS3DH_SetWaterMark(u8_t wtm); status_t LIS3DH_SetTriggerInt(LIS3DH_TrigInt_t tr); status_t LIS3DH_GetFifoSourceReg(u8_t* val); status_t LIS3DH_GetFifoSourceBit(u8_t statusBIT, u8_t* val); status_t LIS3DH_GetFifoSourceFSS(u8_t* val); //Other Reading Functions status_t LIS3DH_GetStatusReg(u8_t* val); status_t LIS3DH_GetStatusBit(u8_t statusBIT, u8_t* val); status_t LIS3DH_GetStatusAUXBit(u8_t statusBIT, u8_t* val); status_t LIS3DH_GetStatusAUX(u8_t* val); status_t LIS3DH_GetAccAxesRaw(AxesRaw_t* buff); status_t LIS3DH_ConvAccValue(AxesRaw_t* data); status_t LIS3DH_GetAuxRaw(LIS3DH_Aux123Raw_t* buff); status_t LIS3DH_GetClickResponse(u8_t* val); status_t LIS3DH_GetTempRaw(i8_t* val); status_t LIS3DH_GetWHO_AM_I(u8_t* val); status_t LIS3DH_Get6DPosition(u8_t* val); //Generic // i.e. u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data); // i.e. u8_t LIS3DH_WriteReg(u8_t Reg, u8_t Data); #endif /* __LIS3DH_H */ /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/ /******************** (C) COPYRIGHT 2012 STMicroelectronics ******************** * File Name : LIS3DH_driver.c * Author : MSH Application Team * Version : $Revision:$ * Date : $Date:$ * Description : LIS3DH driver file * * HISTORY: * Date | Modification | Author ******************************************************************************** * 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 "lis3dh_driver.h" #include "HAL_lis3dh.h" #include "stm32l1xx.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ #define LIS3DH_CS_LOW() GPIO_ResetBits(LIS_SPI_CS_Port, LIS_SPI_CS_Pin ) #define LIS3DH_CS_HIGH() GPIO_SetBits(LIS_SPI_CS_Port, LIS_SPI_CS_Pin) /******************************************************************************* * Function Name : LIS3DH_SpiInit * Description : Generic Reading function. It must be fullfilled with either * : I2C or SPI reading functions * Input : Register Address * Output : Data Read * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ void LIS3DH_SpiInit(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI1 and GPIO clocks */ RCC_AHBPeriphClockCmd(LIS_RCC_Port_SPI, ENABLE); RCC_APB2PeriphClockCmd(LIS_RCC_Periph_SPI , ENABLE); /* Connect PXx to SPI_SCK */ GPIO_PinAFConfig(LIS_SPI_Port, LIS_SPI_SCK_SOURCE, LIS_SPI_AF); /* Connect PXx to SPI_MISO */ GPIO_PinAFConfig(LIS_SPI_Port, LIS_SPI_MISO_SOURCE, LIS_SPI_AF); /* Connect PXx to SPI_MOSI */ GPIO_PinAFConfig(LIS_SPI_Port, LIS_SPI_MOSI_SOURCE, LIS_SPI_AF); /* Configure SPI1 pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = LIS_SPI_MISO_Pin | LIS_SPI_M0SI_Pin | LIS_SPI_SCK_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(LIS_SPI_Port, &GPIO_InitStructure); /* SPI1 configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(LIS_SPI, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(LIS_SPI, ENABLE); /* Configure GPIO PIN for Lis Chip select */ GPIO_InitStructure.GPIO_Pin = LIS_SPI_CS_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(LIS_SPI_CS_Port, &GPIO_InitStructure); /* Deselect : Chip Select high */ GPIO_SetBits(LIS_SPI_Port, LIS_SPI_CS_Pin); } /******************************************************************************* * Function Name : LIS3DH_SPI_SendByte * Description : Sends a Byte through the SPI interface and return the * Byte received from the SPI bus. * Input : Byte : Byte send. * Output : None * Return : The received byte value *******************************************************************************/ unsigned char LIS3DH_SPI_SendByte(unsigned char byte) { /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(LIS_SPI, SPI_I2S_FLAG_TXE) == RESET); /* Send Half Word through the SPI1 peripheral */ SPI_I2S_SendData(LIS_SPI, byte); /* Wait to receive a Half Word */ while (SPI_I2S_GetFlagStatus(LIS_SPI, SPI_I2S_FLAG_RXNE) == RESET); /* Return the Half Word read from the SPI bus */ return (unsigned char)SPI_I2S_ReceiveData(LIS_SPI); } /******************************************************************************* * Function Name : LIS3DH_ReadReg * Description : Generic Reading function. It must be fullfilled with either * : I2C or SPI reading functions * Input : Register Address * Output : Data REad * Return : None *******************************************************************************/ u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data) { Reg |= 0x80; LIS3DH_CS_LOW(); LIS3DH_SPI_SendByte(Reg); *Data=LIS3DH_SPI_SendByte(0x00); LIS3DH_CS_HIGH(); return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_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 LIS3DH_WriteReg(u8_t WriteAddr, u8_t Data) { LIS3DH_CS_LOW(); LIS3DH_SPI_SendByte(WriteAddr); LIS3DH_SPI_SendByte(Data); LIS3DH_CS_HIGH(); return MEMS_SUCCESS; } /* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : LIS3DH_GetWHO_AM_I * Description : Read identification code by WHO_AM_I register * Input : Char to empty by Device identification Value * Output : None * Return : Status [value of FSS] *******************************************************************************/ status_t LIS3DH_GetWHO_AM_I(u8_t* val){ if( !LIS3DH_ReadReg(LIS3DH_WHO_AM_I, val) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetStatusAUX * Description : Read the AUX status register * Input : Char to empty by status register buffer * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetStatusAUX(u8_t* val) { if( !LIS3DH_ReadReg(LIS3DH_STATUS_AUX, val) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetStatusAUXBIT * Description : Read the AUX status register BIT * Input : LIS3DH_STATUS_AUX_321OR, LIS3DH_STATUS_AUX_3OR, LIS3DH_STATUS_AUX_2OR, LIS3DH_STATUS_AUX_1OR, LIS3DH_STATUS_AUX_321DA, LIS3DH_STATUS_AUX_3DA, LIS3DH_STATUS_AUX_2DA, LIS3DH_STATUS_AUX_1DA * Output : None * Return : Status of BIT [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetStatusAUXBit(u8_t statusBIT, u8_t* val) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_STATUS_AUX, &value) ) return MEMS_ERROR; if(statusBIT == LIS3DH_STATUS_AUX_321OR){ if(value &= LIS3DH_STATUS_AUX_321OR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_3OR){ if(value &= LIS3DH_STATUS_AUX_3OR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_2OR){ if(value &= LIS3DH_STATUS_AUX_2OR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_1OR){ if(value &= LIS3DH_STATUS_AUX_1OR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_321DA){ if(value &= LIS3DH_STATUS_AUX_321DA) { *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_3DA){ if(value &= LIS3DH_STATUS_AUX_3DA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_2DA){ if(value &= LIS3DH_STATUS_AUX_2DA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_STATUS_AUX_1DA){ if(value &= LIS3DH_STATUS_AUX_1DA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } return MEMS_ERROR; } /******************************************************************************* * Function Name : LIS3DH_SetODR * Description : Sets LIS3DH Output Data Rate * Input : Output Data Rate * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetODR(LIS3DH_ODR_t ov){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG1, &value) ) return MEMS_ERROR; value &= 0x0f; value |= ov<<LIS3DH_ODR_BIT; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG1, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetTemperature * Description : Sets LIS3DH Output Temperature * Input : MEMS_ENABLE, MEMS_DISABLE * Output : None * Note : For Read Temperature by LIS3DH_OUT_AUX_3, LIS3DH_SetADCAux and LIS3DH_SetBDU functions must be ENABLE * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetTemperature(State_t state){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_TEMP_CFG_REG, &value) ) return MEMS_ERROR; value &= 0xBF; value |= state<<LIS3DH_TEMP_EN; if( !LIS3DH_WriteReg(LIS3DH_TEMP_CFG_REG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetADCAux * Description : Sets LIS3DH Output ADC * Input : MEMS_ENABLE, MEMS_DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetADCAux(State_t state){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_TEMP_CFG_REG, &value) ) return MEMS_ERROR; value &= 0x7F; value |= state<<LIS3DH_ADC_PD; if( !LIS3DH_WriteReg(LIS3DH_TEMP_CFG_REG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetAuxRaw * Description : Read the Aux Values Output Registers * Input : Buffer to empty * Output : Aux Values Registers buffer * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetAuxRaw(LIS3DH_Aux123Raw_t* buff) { u8_t valueL; u8_t valueH; if( !LIS3DH_ReadReg(LIS3DH_OUT_1_L, &valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_1_H, &valueH) ) return MEMS_ERROR; buff->AUX_1 = (u16_t)( (valueH << 8) | valueL )/16; if( !LIS3DH_ReadReg(LIS3DH_OUT_2_L, &valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_2_H, &valueH) ) return MEMS_ERROR; buff->AUX_2 = (u16_t)( (valueH << 8) | valueL )/16; if( !LIS3DH_ReadReg(LIS3DH_OUT_3_L, &valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_3_H, &valueH) ) return MEMS_ERROR; buff->AUX_3 = (u16_t)( (valueH << 8) | valueL )/16; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetTempRaw * Description : Read the Temperature Values by AUX Output Registers OUT_3_H * Input : Buffer to empty * Output : Temperature Values Registers buffer * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetTempRaw(i8_t* buff) { u8_t valueL; u8_t valueH; if( !LIS3DH_ReadReg(LIS3DH_OUT_3_L, &valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_3_H, &valueH) ) return MEMS_ERROR; *buff = (i8_t)( valueH ); return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetMode * Description : Sets LIS3DH Operating Mode * Input : Modality (LIS3DH_NORMAL, LIS3DH_LOW_POWER, LIS3DH_POWER_DOWN) * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetMode(LIS3DH_Mode_t md) { u8_t value; u8_t value2; static u8_t ODR_old_value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG1, &value) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value2) ) return MEMS_ERROR; if((value & 0xF0)==0) value = value | (ODR_old_value & 0xF0); //if it comes from POWERDOWN switch(md) { case LIS3DH_POWER_DOWN: ODR_old_value = value; value &= 0x0F; break; case LIS3DH_NORMAL: value &= 0xF7; value |= (MEMS_RESET<<LIS3DH_LPEN); value2 &= 0xF7; value2 |= (MEMS_SET<<LIS3DH_HR); //set HighResolution_BIT break; case LIS3DH_LOW_POWER: value &= 0xF7; value |= (MEMS_SET<<LIS3DH_LPEN); value2 &= 0xF7; value2 |= (MEMS_RESET<<LIS3DH_HR); //reset HighResolution_BIT break; default: return MEMS_ERROR; } if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG1, value) ) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value2) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetAxis * Description : Enable/Disable LIS3DH Axis * Input : LIS3DH_X_ENABLE/DISABLE | LIS3DH_Y_ENABLE/DISABLE | LIS3DH_Z_ENABLE/DISABLE * Output : None * Note : You MUST use all input variable in the argument, as example * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetAxis(LIS3DH_Axis_t axis) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG1, &value) ) return MEMS_ERROR; value &= 0xF8; value |= (0x07 & axis); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG1, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetFullScale * Description : Sets the LIS3DH FullScale * Input : LIS3DH_FULLSCALE_2/LIS3DH_FULLSCALE_4/LIS3DH_FULLSCALE_8/LIS3DH_FULLSCALE_16 * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetFullScale(LIS3DH_Fullscale_t fs) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value) ) return MEMS_ERROR; value &= 0xCF; value |= (fs<<LIS3DH_FS); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetBDU * Description : Enable/Disable Block Data Update Functionality * Input : ENABLE/DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetBDU(State_t bdu) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value) ) return MEMS_ERROR; value &= 0x7F; value |= (bdu<<LIS3DH_BDU); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetBLE * Description : Set Endianess (MSB/LSB) * Input : BLE_LSB / BLE_MSB * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetBLE(LIS3DH_Endianess_t ble) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value) ) return MEMS_ERROR; value &= 0xBF; value |= (ble<<LIS3DH_BLE); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetSelfTest * Description : Set Self Test Modality * Input : LIS3DH_SELF_TEST_DISABLE/ST_0/ST_1 * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetSelfTest(LIS3DH_SelfTest_t st) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value) ) return MEMS_ERROR; value &= 0xF9; value |= (st<<LIS3DH_ST); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_HPFClick * Description : Enable/Disable High Pass Filter for click * Input : MEMS_ENABLE/MEMS_DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_HPFClickEnable(State_t hpfe) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0xFB; value |= (hpfe<<LIS3DH_HPCLICK); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_HPFAOI1 * Description : Enable/Disable High Pass Filter for AOI on INT_1 * Input : MEMS_ENABLE/MEMS_DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_HPFAOI1Enable(State_t hpfe) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0xFE; value |= (hpfe<<LIS3DH_HPIS1); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_HPFAOI2 * Description : Enable/Disable High Pass Filter for AOI on INT_2 * Input : MEMS_ENABLE/MEMS_DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_HPFAOI2Enable(State_t hpfe) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0xFD; value |= (hpfe<<LIS3DH_HPIS2); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetHPFMode * Description : Set High Pass Filter Modality * Input : LIS3DH_HPM_NORMAL_MODE_RES/LIS3DH_HPM_REF_SIGNAL/ LIS3DH_HPM_NORMAL_MODE/LIS3DH_HPM_AUTORESET_INT * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetHPFMode(LIS3DH_HPFMode_t hpm) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0x3F; value |= (hpm<<LIS3DH_HPM); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetHPFCutOFF * Description : Set High Pass CUT OFF Freq * Input : HPFCF [0,3] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetHPFCutOFF(LIS3DH_HPFCutOffFreq_t hpf) { u8_t value; if (hpf > 3) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0xCF; value |= (hpf<<LIS3DH_HPCF); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetFilterDataSel * Description : Set Filter Data Selection bypassed or sent to FIFO OUT register * Input : MEMS_SET, MEMS_RESET * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetFilterDataSel(State_t state) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG2, &value) ) return MEMS_ERROR; value &= 0xF7; value |= (state<<LIS3DH_FDS); if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG2, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetInt1Pin * Description : Set Interrupt1 pin Function * Input : LIS3DH_CLICK_ON_PIN_INT1_ENABLE/DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE/DISABLE | LIS3DH_I1_INT2_ON_PIN_INT1_ENABLE/DISABLE | LIS3DH_I1_DRDY1_ON_INT1_ENABLE/DISABLE | LIS3DH_I1_DRDY2_ON_INT1_ENABLE/DISABLE | LIS3DH_WTM_ON_INT1_ENABLE/DISABLE | LIS3DH_INT1_OVERRUN_ENABLE/DISABLE * example : SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_ENABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE | LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE | LIS3DH_I1_DRDY1_ON_INT1_ENABLE | LIS3DH_I1_DRDY2_ON_INT1_ENABLE | LIS3DH_WTM_ON_INT1_DISABLE | LIS3DH_INT1_OVERRUN_DISABLE ) * Note : To enable Interrupt signals on INT1 Pad (You MUST use all input variable in the argument, as example) * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetInt1Pin(LIS3DH_IntPinConf_t pinConf) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG3, &value) ) return MEMS_ERROR; value &= 0x00; value |= pinConf; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG3, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetInt2Pin * Description : Set Interrupt2 pin Function * Input : LIS3DH_CLICK_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_INT1_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_INT2_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_BOOT_ON_INT2_ENABLE/DISABLE | LIS3DH_INT_ACTIVE_HIGH/LOW * example : LIS3DH_SetInt2Pin(LIS3DH_CLICK_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_INT1_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_INT2_ON_PIN_INT2_ENABLE/DISABLE | LIS3DH_I2_BOOT_ON_INT2_ENABLE/DISABLE | LIS3DH_INT_ACTIVE_HIGH/LOW) * Note : To enable Interrupt signals on INT2 Pad (You MUST use all input variable in the argument, as example) * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetInt2Pin(LIS3DH_IntPinConf_t pinConf) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG6, &value) ) return MEMS_ERROR; value &= 0x00; value |= pinConf; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG6, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetClickCFG * Description : Set Click Interrupt config Function * Input : LIS3DH_ZD_ENABLE/DISABLE | LIS3DH_ZS_ENABLE/DISABLE | LIS3DH_YD_ENABLE/DISABLE | LIS3DH_YS_ENABLE/DISABLE | LIS3DH_XD_ENABLE/DISABLE | LIS3DH_XS_ENABLE/DISABLE * example : LIS3DH_SetClickCFG( LIS3DH_ZD_ENABLE | LIS3DH_ZS_DISABLE | LIS3DH_YD_ENABLE | LIS3DH_YS_DISABLE | LIS3DH_XD_ENABLE | LIS3DH_XS_ENABLE) * Note : You MUST use all input variable in the argument, as example * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetClickCFG(u8_t status) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CLICK_CFG, &value) ) return MEMS_ERROR; value &= 0xC0; value |= status; if( !LIS3DH_WriteReg(LIS3DH_CLICK_CFG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetClickTHS * Description : Set Click Interrupt threshold * Input : Click-click Threshold value [0-127] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetClickTHS(u8_t ths) { if(ths>127) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_CLICK_THS, ths) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetClickLIMIT * Description : Set Click Interrupt Time Limit * Input : Click-click Time Limit value [0-127] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetClickLIMIT(u8_t t_limit) { if(t_limit>127) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_TIME_LIMIT, t_limit) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetClickLATENCY * Description : Set Click Interrupt Time Latency * Input : Click-click Time Latency value [0-255] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetClickLATENCY(u8_t t_latency) { if( !LIS3DH_WriteReg(LIS3DH_TIME_LATENCY, t_latency) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetClickWINDOW * Description : Set Click Interrupt Time Window * Input : Click-click Time Window value [0-255] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetClickWINDOW(u8_t t_window) { if( !LIS3DH_WriteReg(LIS3DH_TIME_WINDOW, t_window) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetClickResponse * Description : Get Click Interrupt Response by CLICK_SRC REGISTER * Input : char to empty by Click Response Typedef * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetClickResponse(u8_t* res) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CLICK_SRC, &value) ) return MEMS_ERROR; value &= 0x7F; if((value & LIS3DH_IA)==0) { *res = LIS3DH_NO_CLICK; return MEMS_SUCCESS; } else { if (value & LIS3DH_DCLICK){ if (value & LIS3DH_CLICK_SIGN){ if (value & LIS3DH_CLICK_Z) { *res = LIS3DH_DCLICK_Z_N; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_Y) { *res = LIS3DH_DCLICK_Y_N; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_X) { *res = LIS3DH_DCLICK_X_N; return MEMS_SUCCESS; } } else{ if (value & LIS3DH_CLICK_Z) { *res = LIS3DH_DCLICK_Z_P; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_Y) { *res = LIS3DH_DCLICK_Y_P; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_X) { *res = LIS3DH_DCLICK_X_P; return MEMS_SUCCESS; } } } else{ if (value & LIS3DH_CLICK_SIGN){ if (value & LIS3DH_CLICK_Z) { *res = LIS3DH_SCLICK_Z_N; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_Y) { *res = LIS3DH_SCLICK_Y_N; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_X) { *res = LIS3DH_SCLICK_X_N; return MEMS_SUCCESS; } } else{ if (value & LIS3DH_CLICK_Z) { *res = LIS3DH_SCLICK_Z_P; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_Y) { *res = LIS3DH_SCLICK_Y_P; return MEMS_SUCCESS; } if (value & LIS3DH_CLICK_X) { *res = LIS3DH_SCLICK_X_P; return MEMS_SUCCESS; } } } } return MEMS_ERROR; } /******************************************************************************* * Function Name : LIS3DH_Int1LatchEnable * Description : Enable Interrupt 1 Latching function * Input : ENABLE/DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_Int1LatchEnable(State_t latch) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xF7; value |= latch<<LIS3DH_LIR_INT1; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_ResetInt1Latch * Description : Reset Interrupt 1 Latching function * Input : None * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_ResetInt1Latch(void) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_INT1_SRC, &value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetIntConfiguration * Description : Interrupt 1 Configuration (without LIS3DH_6D_INT) * Input : LIS3DH_INT1_AND/OR | LIS3DH_INT1_ZHIE_ENABLE/DISABLE | LIS3DH_INT1_ZLIE_ENABLE/DISABLE... * Output : None * Note : You MUST use all input variable in the argument, as example * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetIntConfiguration(LIS3DH_Int1Conf_t ic) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_INT1_CFG, &value) ) return MEMS_ERROR; value &= 0x40; value |= ic; if( !LIS3DH_WriteReg(LIS3DH_INT1_CFG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetIntMode * Description : Interrupt 1 Configuration mode (OR, 6D Movement, AND, 6D Position) * Input : LIS3DH_INT_MODE_OR, LIS3DH_INT_MODE_6D_MOVEMENT, LIS3DH_INT_MODE_AND, LIS3DH_INT_MODE_6D_POSITION * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetIntMode(LIS3DH_Int1Mode_t int_mode) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_INT1_CFG, &value) ) return MEMS_ERROR; value &= 0x3F; value |= (int_mode<<LIS3DH_INT_6D); if( !LIS3DH_WriteReg(LIS3DH_INT1_CFG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetInt6D4DConfiguration * Description : 6D, 4D Interrupt Configuration * Input : LIS3DH_INT1_6D_ENABLE, LIS3DH_INT1_4D_ENABLE, LIS3DH_INT1_6D_4D_DISABLE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetInt6D4DConfiguration(LIS3DH_INT_6D_4D_t ic) { u8_t value; u8_t value2; if( !LIS3DH_ReadReg(LIS3DH_INT1_CFG, &value) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value2) ) return MEMS_ERROR; if(ic == LIS3DH_INT1_6D_ENABLE){ value &= 0xBF; value |= (MEMS_ENABLE<<LIS3DH_INT_6D); value2 &= 0xFB; value2 |= (MEMS_DISABLE<<LIS3DH_D4D_INT1); } if(ic == LIS3DH_INT1_4D_ENABLE){ value &= 0xBF; value |= (MEMS_ENABLE<<LIS3DH_INT_6D); value2 &= 0xFB; value2 |= (MEMS_ENABLE<<LIS3DH_D4D_INT1); } if(ic == LIS3DH_INT1_6D_4D_DISABLE){ value &= 0xBF; value |= (MEMS_DISABLE<<LIS3DH_INT_6D); value2 &= 0xFB; value2 |= (MEMS_DISABLE<<LIS3DH_D4D_INT1); } if( !LIS3DH_WriteReg(LIS3DH_INT1_CFG, value) ) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value2) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_Get6DPosition * Description : 6D, 4D Interrupt Position Detect * Input : Byte to empty by POSITION_6D_t Typedef * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_Get6DPosition(u8_t* val){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_INT1_SRC, &value) ) return MEMS_ERROR; value &= 0x7F; switch (value){ case LIS3DH_UP_SX: *val = LIS3DH_UP_SX; break; case LIS3DH_UP_DX: *val = LIS3DH_UP_DX; break; case LIS3DH_DW_SX: *val = LIS3DH_DW_SX; break; case LIS3DH_DW_DX: *val = LIS3DH_DW_DX; break; case LIS3DH_TOP: *val = LIS3DH_TOP; break; case LIS3DH_BOTTOM: *val = LIS3DH_BOTTOM; break; } return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetInt1Threshold * Description : Sets Interrupt 1 Threshold * Input : Threshold = [0,31] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetInt1Threshold(u8_t ths) { if (ths > 127) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_INT1_THS, ths) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetInt1Duration * Description : Sets Interrupt 1 Duration * Input : Duration value * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetInt1Duration(LIS3DH_Int1Conf_t id) { if (id > 127) return MEMS_ERROR; if( !LIS3DH_WriteReg(LIS3DH_INT1_DURATION, id) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_FIFOModeEnable * Description : Sets Fifo Modality * Input : LIS3DH_FIFO_DISABLE, LIS3DH_FIFO_BYPASS_MODE, LIS3DH_FIFO_MODE, LIS3DH_FIFO_STREAM_MODE, LIS3DH_FIFO_TRIGGER_MODE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_FIFOModeEnable(LIS3DH_FifoMode_t fm) { u8_t value; if(fm == LIS3DH_FIFO_DISABLE) { if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0x1F; value |= (LIS3DH_FIFO_BYPASS_MODE<<LIS3DH_FM); if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) //fifo mode bypass return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xBF; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) //fifo disable return MEMS_ERROR; } if(fm == LIS3DH_FIFO_BYPASS_MODE) { if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xBF; value |= MEMS_SET<<LIS3DH_FIFO_EN; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) //fifo enable return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0x1f; value |= (fm<<LIS3DH_FM); //fifo mode configuration if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; } if(fm == LIS3DH_FIFO_MODE) { if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xBF; value |= MEMS_SET<<LIS3DH_FIFO_EN; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) //fifo enable return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0x1f; value |= (fm<<LIS3DH_FM); //fifo mode configuration if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; } if(fm == LIS3DH_FIFO_STREAM_MODE) { if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xBF; value |= MEMS_SET<<LIS3DH_FIFO_EN; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) //fifo enable return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0x1f; value |= (fm<<LIS3DH_FM); //fifo mode configuration if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; } if(fm == LIS3DH_FIFO_TRIGGER_MODE) { if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG5, &value) ) return MEMS_ERROR; value &= 0xBF; value |= MEMS_SET<<LIS3DH_FIFO_EN; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG5, value) ) //fifo enable return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0x1f; value |= (fm<<LIS3DH_FM); //fifo mode configuration if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; } return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetTriggerInt * Description : Trigger event liked to trigger signal INT1/INT2 * Input : LIS3DH_TRIG_INT1/LIS3DH_TRIG_INT2 * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetTriggerInt(LIS3DH_TrigInt_t tr) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0xDF; value |= (tr<<LIS3DH_TR); if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetWaterMark * Description : Sets Watermark Value * Input : Watermark = [0,31] * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetWaterMark(u8_t wtm) { u8_t value; if(wtm > 31) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) return MEMS_ERROR; value &= 0xE0; value |= wtm; if( !LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetStatusReg * Description : Read the status register * Input : char to empty by Status Reg Value * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetStatusReg(u8_t* val) { if( !LIS3DH_ReadReg(LIS3DH_STATUS_REG, val) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetStatusBIT * Description : Read the status register BIT * Input : LIS3DH_STATUS_REG_ZYXOR, LIS3DH_STATUS_REG_ZOR, LIS3DH_STATUS_REG_YOR, LIS3DH_STATUS_REG_XOR, LIS3DH_STATUS_REG_ZYXDA, LIS3DH_STATUS_REG_ZDA, LIS3DH_STATUS_REG_YDA, LIS3DH_STATUS_REG_XDA, LIS3DH_DATAREADY_BIT val: Byte to be filled with the status bit * Output : status register BIT * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetStatusBit(u8_t statusBIT, u8_t* val) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_STATUS_REG, &value) ) return MEMS_ERROR; switch (statusBIT){ case LIS3DH_STATUS_REG_ZYXOR: if(value &= LIS3DH_STATUS_REG_ZYXOR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_ZOR: if(value &= LIS3DH_STATUS_REG_ZOR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_YOR: if(value &= LIS3DH_STATUS_REG_YOR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_XOR: if(value &= LIS3DH_STATUS_REG_XOR){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_ZYXDA: if(value &= LIS3DH_STATUS_REG_ZYXDA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_ZDA: if(value &= LIS3DH_STATUS_REG_ZDA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_YDA: if(value &= LIS3DH_STATUS_REG_YDA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } case LIS3DH_STATUS_REG_XDA: if(value &= LIS3DH_STATUS_REG_XDA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } return MEMS_ERROR; } /******************************************************************************* * Function Name : LIS3DH_GetAccAxesRaw * Description : Read the Acceleration Values Output Registers * Input : buffer to empity by AxesRaw_t Typedef * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetAccAxesRaw(AxesRaw_t* buff) { i16_t value; u8_t *valueL = (u8_t *)(&value); u8_t *valueH = ((u8_t *)(&value)+1); if( !LIS3DH_ReadReg(LIS3DH_OUT_X_L, valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_X_H, valueH) ) return MEMS_ERROR; buff->AXIS_X = value; if( !LIS3DH_ReadReg(LIS3DH_OUT_Y_L, valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_Y_H, valueH) ) return MEMS_ERROR; buff->AXIS_Y = value; if( !LIS3DH_ReadReg(LIS3DH_OUT_Z_L, valueL) ) return MEMS_ERROR; if( !LIS3DH_ReadReg(LIS3DH_OUT_Z_H, valueH) ) return MEMS_ERROR; buff->AXIS_Z = value; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_ConvAccValue * Description : Convert the raw acceleration data to value in mg units * Input : raw data to be converted * Output : converted data with value in mg units * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_ConvAccValue(AxesRaw_t* data) { u8_t crtl4; u8_t sensitivity; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &crtl4) ) return MEMS_ERROR; /* switch the sensitivity value set in the CRTL4*/ switch(crtl4 & 0x30) { case 0x00: sensitivity = LIS3DH_Acc_Sensitivity_2g; break; case 0x10: sensitivity = LIS3DH_Acc_Sensitivity_4g; break; case 0x20: sensitivity = LIS3DH_Acc_Sensitivity_8g; break; case 0x30: sensitivity = LIS3DH_Acc_Sensitivity_16g; break; } data->AXIS_X = (data->AXIS_X >> 4)*sensitivity; data->AXIS_Y = (data->AXIS_Y >> 4)*sensitivity; data->AXIS_Z = (data->AXIS_Z >> 4)*sensitivity; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetInt1Src * Description : Reset Interrupt 1 Latching function * Input : Char to empty by Int1 source value * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetInt1Src(u8_t* val) { if( !LIS3DH_ReadReg(LIS3DH_INT1_SRC, val) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetInt1SrcBit * Description : Reset Interrupt 1 Latching function * Input : statusBIT: LIS3DH_INT_SRC_IA, LIS3DH_INT_SRC_ZH, LIS3DH_INT_SRC_ZL..... * val: Byte to be filled with the status bit * Output : None * Return : Status of BIT [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetInt1SrcBit(u8_t statusBIT, u8_t* val) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_INT1_SRC, &value) ) return MEMS_ERROR; if(statusBIT == LIS3DH_INT1_SRC_IA){ if(value &= LIS3DH_INT1_SRC_IA){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_ZH){ if(value &= LIS3DH_INT1_SRC_ZH){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_ZL){ if(value &= LIS3DH_INT1_SRC_ZL){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_YH){ if(value &= LIS3DH_INT1_SRC_YH){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_YL){ if(value &= LIS3DH_INT1_SRC_YL){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_XH){ if(value &= LIS3DH_INT1_SRC_XH){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_INT1_SRC_XL){ if(value &= LIS3DH_INT1_SRC_XL){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } return MEMS_ERROR; } /******************************************************************************* * Function Name : LIS3DH_GetFifoSourceReg * Description : Read Fifo source Register * Input : Byte to empty by FIFO source register value * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetFifoSourceReg(u8_t* val) { if( !LIS3DH_ReadReg(LIS3DH_FIFO_SRC_REG, val) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_GetFifoSourceBit * Description : Read Fifo WaterMark source bit * Input : statusBIT: LIS3DH_FIFO_SRC_WTM, LIS3DH_FIFO_SRC_OVRUN, LIS3DH_FIFO_SRC_EMPTY * val: Byte to fill with the bit value * Output : None * Return : Status of BIT [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_GetFifoSourceBit(u8_t statusBIT, u8_t* val){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_FIFO_SRC_REG, &value) ) return MEMS_ERROR; if(statusBIT == LIS3DH_FIFO_SRC_WTM){ if(value &= LIS3DH_FIFO_SRC_WTM){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_FIFO_SRC_OVRUN){ if(value &= LIS3DH_FIFO_SRC_OVRUN){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } if(statusBIT == LIS3DH_FIFO_SRC_EMPTY){ if(value &= statusBIT == LIS3DH_FIFO_SRC_EMPTY){ *val = MEMS_SET; return MEMS_SUCCESS; } else{ *val = MEMS_RESET; return MEMS_SUCCESS; } } return MEMS_ERROR; } /******************************************************************************* * Function Name : LIS3DH_GetFifoSourceFSS * Description : Read current number of unread samples stored in FIFO * Input : Byte to empty by FIFO unread sample value * Output : None * Return : Status [value of FSS] *******************************************************************************/ status_t LIS3DH_GetFifoSourceFSS(u8_t* val){ u8_t value; if( !LIS3DH_ReadReg(LIS3DH_FIFO_SRC_REG, &value) ) return MEMS_ERROR; value &= 0x1F; *val = value; return MEMS_SUCCESS; } /******************************************************************************* * Function Name : LIS3DH_SetSPIInterface * Description : Set SPI mode: 3 Wire Interface OR 4 Wire Interface * Input : LIS3DH_SPI_3_WIRE, LIS3DH_SPI_4_WIRE * Output : None * Return : Status [MEMS_ERROR, MEMS_SUCCESS] *******************************************************************************/ status_t LIS3DH_SetSPIInterface(LIS3DH_SPIMode_t spi) { u8_t value; if( !LIS3DH_ReadReg(LIS3DH_CTRL_REG4, &value) ) return MEMS_ERROR; value &= 0xFE; value |= spi<<LIS3DH_SIM; if( !LIS3DH_WriteReg(LIS3DH_CTRL_REG4, value) ) return MEMS_ERROR; return MEMS_SUCCESS; } /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/












本帖最后由 littleshrimp 于 2016-11-29 19:08 编辑
虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复评论 (10)

头晕了,全是代码
点赞  2016-11-29 18:51
是LIS3DH吧!
点赞  2016-11-29 19:02

谢谢大神提醒我改回来了

对了,惯性导航的资料大神找到没?
QQ图片20161129191427.png
不管什么DR了,先弄来点学习学习呗?
file:///

虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2016-11-29 19:15
这个太复杂了吧!
其实就是配置几个寄存器而已
点赞  2016-11-30 21:44
代码蛮长的, 到时候试试!
点赞  2016-12-1 21:15
引用: melau 发表于 2016-11-30 21:44
这个太复杂了吧!
其实就是配置几个寄存器而已

你还没把这个料充分玩起来
ST MEMS技术讨论群:415506792
点赞  2016-12-2 07:01
引用: littleshrimp 发表于 2016-11-29 19:15
谢谢大神提醒我改回来了

对了,惯性导航的资料大神找到没?

不管什么DR了,先弄来点学习学习呗?
...

等一下吧先
点赞  2016-12-2 09:59
费老大劲登陆,就是为了告诉各位,这个代码是好用的,who am i=0x33,谢谢
点赞  2017-11-14 16:14
引用: yunjie167 发表于 2017-11-14 16:14
费老大劲登陆,就是为了告诉各位,这个代码是好用的,who am i=0x33,谢谢

虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2017-11-14 17:20
引用: yunjie167 发表于 2017-11-14 16:14
费老大劲登陆,就是为了告诉各位,这个代码是好用的,who am i=0x33,谢谢

欢迎记住密码,也常来论坛分享和提问
点赞  2017-11-14 22:12
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复