Gian Paolo Donnarumma / LPS22HH
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HH.h Source File

LPS22HH.h

00001 #ifndef __LPS22HH_H__
00002 #define __LPS22HH_H__
00003 
00004 
00005 /* Includes ------------------------------------------------------------------*/
00006 
00007 #include "mbed.h"
00008 /* Defines -------------------------------------------------------------------*/
00009 /** I2C Device Address 8 bit format  if SA0=0 -> B9 if SA0=1 -> BB **/
00010 #define LPS22HH_I2C_ADD_H                       0xBBU
00011 #define LPS22HH_I2C_ADD_L                       0xB9U
00012 
00013 /** Device Identification (Who am I) **/
00014 #define LPS22HH_ID                              0xB3U
00015 
00016 /**
00017   * @}
00018   *
00019   */
00020 
00021 /**
00022   * @addtogroup  LPS22HH_Sensitivity
00023   * @brief       These macro are maintained for back compatibility.
00024   *              in order to convert data into engineering units please
00025   *              use functions:
00026   *                -> _from_lsb_to_hpa(int16_t lsb)
00027   *                -> _from_lsb_to_celsius(int16_t lsb);
00028   *
00029   *              REMOVING the MACRO you are compliant with:
00030   *              MISRA-C 2012 [Dir 4.9] -> " avoid function-like macros "
00031   * @{
00032   *
00033   */
00034 
00035 #define LPS22HH_FROM_LSB_TO_hPa(lsb)     (float)( lsb / 4096.0f )
00036 #define LPS22HH_FROM_LSB_TO_degC(lsb)    (float)( lsb / 100.0f )
00037 
00038 #define LPS22HH_WHO_AM_I                        0x0FU
00039 
00040 #define PROPERTY_DISABLE                (0U)
00041 #define PROPERTY_ENABLE                 (1U)
00042 
00043 
00044 /* Typedefs ------------------------------------------------------------------*/
00045 
00046 typedef enum
00047 {
00048   LPS22HH_OK = 0,
00049   LPS22HH_ERROR =-1
00050 } LPS22HHStatusTypeDef;
00051 
00052 typedef enum {
00053   LPS22HH_POWER_DOWN          = 0x00,
00054   LPS22HH_ONE_SHOOT           = 0x08,
00055   LPS22HH_1_Hz                = 0x01,
00056   LPS22HH_10_Hz               = 0x02,
00057   LPS22HH_25_Hz               = 0x03,
00058   LPS22HH_50_Hz               = 0x04,
00059   LPS22HH_75_Hz               = 0x05,
00060   LPS22HH_1_Hz_LOW_NOISE      = 0x11,
00061   LPS22HH_10_Hz_LOW_NOISE     = 0x12,
00062   LPS22HH_25_Hz_LOW_NOISE     = 0x13,
00063   LPS22HH_50_Hz_LOW_NOISE     = 0x14,
00064   LPS22HH_75_Hz_LOW_NOISE     = 0x15,
00065   LPS22HH_100_Hz              = 0x06,
00066   LPS22HH_200_Hz              = 0x07,
00067 } lps22hh_odr_t;
00068 
00069 typedef int32_t (*lps22hh_write_ptr)(void *, uint8_t, uint8_t*, uint16_t);
00070 typedef int32_t (*lps22hh_read_ptr) (void *, uint8_t, uint8_t*, uint16_t);
00071 
00072 typedef struct {
00073   /** Component mandatory fields **/
00074   lps22hh_write_ptr  write_reg;
00075   lps22hh_read_ptr   read_reg;
00076   /** Customizable optional pointer **/
00077   void *handle;
00078 } lps22hh_ctx_t;
00079 
00080 typedef enum {
00081   LPS22HH_LPF_ODR_DIV_2    = 0,
00082   LPS22HH_LPF_ODR_DIV_9    = 2,
00083   LPS22HH_LPF_ODR_DIV_20   = 3,
00084 } lps22hh_lpfp_cfg_t;
00085 
00086 typedef union{
00087   int16_t i16bit[3];
00088   uint8_t u8bit[6];
00089 } axis3bit16_t;
00090 
00091 typedef union{
00092   int16_t i16bit;
00093   uint8_t u8bit[2];
00094 } axis1bit16_t;
00095 
00096 typedef union{
00097   int32_t i32bit[3];
00098   uint8_t u8bit[12];
00099 } axis3bit32_t;
00100 
00101 typedef union{
00102   int32_t i32bit;
00103   uint8_t u8bit[4];
00104 } axis1bit32_t;
00105 
00106 /**
00107   * @}
00108   *
00109   */
00110 
00111 typedef struct{
00112   uint8_t bit0       : 1;
00113   uint8_t bit1       : 1;
00114   uint8_t bit2       : 1;
00115   uint8_t bit3       : 1;
00116   uint8_t bit4       : 1;
00117   uint8_t bit5       : 1;
00118   uint8_t bit6       : 1;
00119   uint8_t bit7       : 1;
00120 } bitwise_t;
00121 
00122 #define PROPERTY_DISABLE                (0U)
00123 #define PROPERTY_ENABLE                 (1U)
00124 
00125 
00126 
00127 /**
00128   * @}
00129   *
00130   */
00131 
00132 /** @addtogroup  LPS22HH_Interfaces_Functions
00133   * @brief       This section provide a set of functions used to read and
00134   *              write a generic register of the device.
00135   *              MANDATORY: return 0 -> no Error.
00136   * @{
00137   *
00138   */
00139 
00140 typedef int32_t (*lps22hh_write_ptr)(void *, uint8_t, uint8_t*, uint16_t);
00141 typedef int32_t (*lps22hh_read_ptr) (void *, uint8_t, uint8_t*, uint16_t);
00142 
00143 
00144 /**
00145   * @}
00146   *
00147   */
00148 
00149 /** @defgroup LPS22HH_Infos
00150   * @{
00151   *
00152   */
00153 
00154 /** I2C Device Address 8 bit format  if SA0=0 -> B9 if SA0=1 -> BB **/
00155 #define LPS22HH_I2C_ADD_H                       0xBBU
00156 #define LPS22HH_I2C_ADD_L                       0xB9U
00157 
00158 /** Device Identification (Who am I) **/
00159 #define LPS22HH_ID                              0xB3U
00160 
00161 /**
00162   * @}
00163   *
00164   */
00165 
00166 /**
00167   * @addtogroup  LPS22HH_Sensitivity
00168   * @brief       These macro are maintained for back compatibility.
00169   *              in order to convert data into engineering units please
00170   *              use functions:
00171   *                -> _from_lsb_to_hpa(int16_t lsb)
00172   *                -> _from_lsb_to_celsius(int16_t lsb);
00173   *
00174   *              REMOVING the MACRO you are compliant with:
00175   *              MISRA-C 2012 [Dir 4.9] -> " avoid function-like macros "
00176   * @{
00177   *
00178   */
00179 
00180 #define LPS22HH_FROM_LSB_TO_hPa(lsb)     (float)( lsb / 4096.0f )
00181 #define LPS22HH_FROM_LSB_TO_degC(lsb)    (float)( lsb / 100.0f )
00182 
00183 /**
00184   * @}
00185   *
00186   */
00187 
00188 #define LPS22HH_INTERRUPT_CFG                   0x0BU
00189 typedef struct {
00190   uint8_t pe                              : 2;  /* ple + phe */
00191   uint8_t lir                             : 1;
00192   uint8_t diff_en                         : 1;
00193   uint8_t reset_az                        : 1;
00194   uint8_t autozero                        : 1;
00195   uint8_t reset_arp                       : 1;
00196   uint8_t autorefp                        : 1;
00197 } lps22hh_interrupt_cfg_t;
00198 
00199 #define LPS22HH_THS_P_L                         0x0CU
00200 typedef struct {
00201   uint8_t ths                             : 8;
00202 } lps22hh_ths_p_l_t;
00203 
00204 #define LPS22HH_THS_P_H                         0x0DU
00205 typedef struct {
00206   uint8_t ths                             : 7;
00207   uint8_t not_used_01                     : 1;
00208 } lps22hh_ths_p_h_t;
00209 
00210 #define LPS22HH_IF_CTRL                         0x0EU
00211 typedef struct {
00212   uint8_t i2c_disable                     : 1;
00213   uint8_t i3c_disable                     : 1;
00214   uint8_t pd_dis_int1                     : 1;
00215   uint8_t sdo_pu_en                       : 1;
00216   uint8_t sda_pu_en                       : 1;
00217   uint8_t not_used_01                     : 2;
00218   uint8_t int_en_i3c                      : 1;
00219 } lps22hh_if_ctrl_t;
00220 
00221 #define LPS22HH_WHO_AM_I                        0x0FU
00222 #define LPS22HH_CTRL_REG1                       0x10U
00223 typedef struct {
00224   uint8_t sim                             : 1;
00225   uint8_t bdu                             : 1;
00226   uint8_t lpfp_cfg                        : 2;  /* en_lpfp + lpfp_cfg */
00227   uint8_t odr                             : 3;
00228   uint8_t not_used_01                     : 1;
00229 } lps22hh_ctrl_reg1_t;
00230 
00231 #define LPS22HH_CTRL_REG2                       0x11U
00232 typedef struct {
00233   uint8_t one_shot                        : 1;
00234   uint8_t low_noise_en                    : 1;
00235   uint8_t swreset                         : 1;
00236   uint8_t not_used_01                     : 1;
00237   uint8_t if_add_inc                      : 1;
00238   uint8_t pp_od                           : 1;
00239   uint8_t int_h_l                         : 1;
00240   uint8_t boot                            : 1;
00241 } lps22hh_ctrl_reg2_t;
00242 
00243 #define LPS22HH_CTRL_REG3                       0x12U
00244 typedef struct {
00245   uint8_t int_s                           : 2;
00246   uint8_t drdy                            : 1;
00247   uint8_t int_f_ovr                       : 1;
00248   uint8_t int_f_wtm                       : 1;
00249   uint8_t int_f_full                      : 1;
00250   uint8_t not_used_01                     : 2;
00251 } lps22hh_ctrl_reg3_t;
00252 
00253 #define LPS22HH_FIFO_CTRL                       0x13U
00254 typedef struct {
00255   uint8_t f_mode                          : 3;  /* f_mode + trig_modes */
00256   uint8_t stop_on_wtm                     : 1;
00257   uint8_t not_used_01                     : 4;
00258 } lps22hh_fifo_ctrl_t;
00259 
00260 #define LPS22HH_FIFO_WTM                        0x14U
00261 typedef struct {
00262   uint8_t wtm                             : 7;
00263   uint8_t not_used_01                     : 1;
00264 } lps22hh_fifo_wtm_t;
00265 
00266 #define LPS22HH_REF_P_XL                        0x15U
00267 #define LPS22HH_REF_P_L                         0x16U
00268 #define LPS22HH_RPDS_L                          0x18U
00269 #define LPS22HH_RPDS_H                          0x19U
00270 #define LPS22HH_INT_SOURCE                      0x24U
00271 typedef struct {
00272   uint8_t ph                              : 1;
00273   uint8_t pl                              : 1;
00274   uint8_t ia                              : 1;
00275   uint8_t not_used_01                     : 5;
00276 } lps22hh_int_source_t;
00277 
00278 #define LPS22HH_FIFO_STATUS1                    0x25U
00279 #define LPS22HH_FIFO_STATUS2                    0x26U
00280 typedef struct {
00281   uint8_t not_used_01                     : 5;
00282   uint8_t fifo_full_ia                    : 1;
00283   uint8_t fifo_ovr_ia                     : 1;
00284   uint8_t fifo_wtm_ia                     : 1;
00285 } lps22hh_fifo_status2_t;
00286 
00287 #define LPS22HH_STATUS                          0x27U
00288 typedef struct {
00289   uint8_t p_da                            : 1;
00290   uint8_t t_da                            : 1;
00291   uint8_t not_used_01                     : 2;
00292   uint8_t p_or                            : 1;
00293   uint8_t t_or                            : 1;
00294   uint8_t not_used_02                     : 2;
00295 } lps22hh_status_t;
00296 
00297 #define LPS22HH_PRESSURE_OUT_XL                 0x28U
00298 #define LPS22HH_PRESSURE_OUT_L                  0x29U
00299 #define LPS22HH_PRESSURE_OUT_H                  0x2AU
00300 #define LPS22HH_TEMP_OUT_L                      0x2BU
00301 #define LPS22HH_TEMP_OUT_H                      0x2CU
00302 #define LPS22HH_FIFO_DATA_OUT_PRESS_XL          0x78U
00303 #define LPS22HH_FIFO_DATA_OUT_PRESS_L           0x79U
00304 #define LPS22HH_FIFO_DATA_OUT_PRESS_H           0x7AU
00305 #define LPS22HH_FIFO_DATA_OUT_TEMP_L            0x7BU
00306 #define LPS22HH_FIFO_DATA_OUT_TEMP_H            0x7CU
00307 
00308 /**
00309   * @defgroup LPS22HH_Register_Union
00310   * @brief    This union group all the registers that has a bitfield
00311   *           description.
00312   *           This union is useful but not need by the driver.
00313   *
00314   *           REMOVING this union you are compliant with:
00315   *           MISRA-C 2012 [Rule 19.2] -> " Union are not allowed "
00316   *
00317   * @{
00318   *
00319   */
00320 typedef union{
00321   lps22hh_interrupt_cfg_t        interrupt_cfg;
00322   lps22hh_if_ctrl_t              if_ctrl;
00323   lps22hh_ctrl_reg1_t            ctrl_reg1;
00324   lps22hh_ctrl_reg2_t            ctrl_reg2;
00325   lps22hh_ctrl_reg3_t            ctrl_reg3;
00326   lps22hh_fifo_ctrl_t            fifo_ctrl;
00327   lps22hh_fifo_wtm_t             fifo_wtm;
00328   lps22hh_int_source_t           int_source;
00329   lps22hh_fifo_status2_t         fifo_status2;
00330   lps22hh_status_t               status;
00331   bitwise_t                      bitwise;
00332   uint8_t                        byte;
00333 } lps22hh_reg_t;
00334 
00335 typedef enum {
00336   LPS22HH_BYPASS_MODE            = 0,
00337   LPS22HH_FIFO_MODE              = 1,
00338   LPS22HH_STREAM_MODE            = 2,
00339   LPS22HH_DYNAMIC_STREAM_MODE    = 3,
00340   LPS22HH_BYPASS_TO_FIFO_MODE    = 5,
00341   LPS22HH_BYPASS_TO_STREAM_MODE  = 6,
00342   LPS22HH_STREAM_TO_FIFO_MODE    = 7,
00343 } lps22hh_f_mode_t;
00344 
00345 /**
00346   * @}
00347   *
00348   */
00349 
00350 
00351 /* Class Declaration ---------------------------------------------------------*/
00352    
00353 /**
00354  * Abstract class of a LPS22HH pressure sensor.
00355  */
00356 class LPS22HH
00357 {
00358   public:
00359     LPS22HH(PinName sda, PinName scl);
00360     
00361     LPS22HHStatusTypeDef begin();
00362     LPS22HHStatusTypeDef end();
00363     LPS22HHStatusTypeDef ReadID(uint8_t *Id);
00364     LPS22HHStatusTypeDef Enable();
00365     LPS22HHStatusTypeDef Disable();
00366     LPS22HHStatusTypeDef GetOutputDataRate(float *Odr);
00367     LPS22HHStatusTypeDef SetOutputDataRate(float Odr);
00368     ////LPS22HHStatusTypeDef GetPressure(float *Value);
00369     float GetPressure();
00370     LPS22HHStatusTypeDef Get_PRESS_DRDY_Status(uint8_t *Status);
00371     
00372     LPS22HHStatusTypeDef GetTemperature(float *Value);
00373     LPS22HHStatusTypeDef Get_TEMP_DRDY_Status(uint8_t *Status);
00374     
00375     LPS22HHStatusTypeDef Read_Reg(uint8_t reg, uint8_t *Data);
00376     LPS22HHStatusTypeDef Write_Reg(uint8_t reg, uint8_t Data);
00377     
00378     LPS22HHStatusTypeDef Get_FIFO_Data(float *Press, float *Temp);
00379     LPS22HHStatusTypeDef Get_FIFO_FTh_Status(uint8_t *Status);
00380     LPS22HHStatusTypeDef Get_FIFO_Full_Status(uint8_t *Status);
00381     LPS22HHStatusTypeDef Get_FIFO_Ovr_Status(uint8_t *Status);
00382     LPS22HHStatusTypeDef Get_FIFO_Level(uint8_t *Status);
00383     LPS22HHStatusTypeDef Reset_FIFO_Interrupt(uint8_t interrupt);
00384     LPS22HHStatusTypeDef Set_FIFO_Interrupt(uint8_t interrupt);
00385     LPS22HHStatusTypeDef Set_FIFO_Mode(uint8_t Mode);
00386     LPS22HHStatusTypeDef Set_FIFO_Watermark_Level(uint8_t Watermark);
00387     LPS22HHStatusTypeDef Stop_FIFO_On_Watermark(uint8_t Stop);
00388     
00389     LPS22HHStatusTypeDef Set_One_Shot();
00390     LPS22HHStatusTypeDef Get_One_Shot_Status(uint8_t *Status);
00391 
00392     
00393     /**
00394      * @brief Utility function to read data.
00395      * @param  pBuffer: pointer to data to be read.
00396      * @param  RegisterAddr: specifies internal address register to be read.
00397      * @param  NumByteToRead: number of bytes to be read.
00398      * @retval 0 if ok, an error code otherwise.
00399      */
00400     uint8_t IO_Read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00401     {   
00402         char tempBuff[NumByteToRead];
00403         uint8_t devAddr = (uint8_t)(((address) >> 1) & 0x7F);
00404         char regAddr = (char)RegisterAddr;
00405         _i2c.write((int)devAddr, &regAddr, 1);    
00406         //_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F)));
00407         //dev_i2c->write(RegisterAddr);
00408         //dev_i2c->endTransmission(false);
00409         _i2c.read(devAddr,tempBuff,NumByteToRead);
00410         
00411         memcpy(pBuffer,tempBuff,NumByteToRead);
00412         //dev_i2c->requestFrom(((uint8_t)(((address) >> 1) & 0x7F)), (uint8_t) NumByteToRead);
00413 
00414         /*
00415         int i=0;
00416         while (dev_i2c->available()) {
00417           pBuffer[i] = dev_i2c->read();
00418           i++;
00419         }
00420         */
00421         return 0;
00422       
00423 
00424       
00425     }
00426     
00427     /**
00428      * @brief Utility function to write data.
00429      * @param  pBuffer: pointer to data to be written.
00430      * @param  RegisterAddr: specifies internal address register to be written.
00431      * @param  NumByteToWrite: number of bytes to write.
00432      * @retval 0 if ok, an error code otherwise.
00433      */
00434     uint8_t IO_Write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00435     {  
00436       uint8_t devAddr = (uint8_t)(((address) >> 1) & 0x7F);
00437       char tData[NumByteToWrite+1];      
00438       tData[0] = RegisterAddr;
00439       memcpy(tData+1,pBuffer,NumByteToWrite);
00440       
00441       _i2c.write(devAddr,tData,NumByteToWrite+1);
00442 
00443         return 0;
00444       
00445     }
00446 
00447   private:
00448     LPS22HHStatusTypeDef SetOutputDataRate_When_Enabled(float Odr);
00449     LPS22HHStatusTypeDef SetOutputDataRate_When_Disabled(float Odr);
00450 
00451     /* Helper classes. */
00452     I2C _i2c;
00453     
00454     /* Configuration */
00455     uint8_t address;
00456         
00457     lps22hh_odr_t last_odr;
00458     uint8_t enabled;
00459     
00460     lps22hh_ctx_t reg_ctx;
00461     
00462 };
00463 
00464 #ifdef __cplusplus
00465  extern "C" {
00466 #endif
00467 int32_t LPS22HH_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
00468 int32_t LPS22HH_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
00469 #ifdef __cplusplus
00470   }
00471 #endif
00472 
00473 #endif