LPS22HH pressure sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HHSensor.h Source File

LPS22HHSensor.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LPS22HHSensor.h
00004  * @author  SRA
00005  * @version V1.0.0
00006  * @date    February 2019
00007  * @brief   Abstract Class of a LPS22HH pressure sensor.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 /* Prevent recursive inclusion -----------------------------------------------*/
00040 
00041 #ifndef __LPS22HHSensor_H__
00042 #define __LPS22HHSensor_H__
00043 
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 
00047 #include "DevI2C.h"
00048 #include "lps22hh_reg.h"
00049 #include "PressureSensor.h"
00050 #include "TempSensor.h"
00051 #include <assert.h>
00052 
00053 /* Defines -------------------------------------------------------------------*/
00054 /* Typedefs ------------------------------------------------------------------*/
00055 /* Class Declaration ---------------------------------------------------------*/
00056 
00057 /**
00058  * Abstract class of a LPS22HH pressure sensor.
00059  */
00060 class LPS22HHSensor : public PressureSensor, public TempSensor {
00061 public:
00062     enum SPI_type_t {SPI3W, SPI4W};
00063     LPS22HHSensor(SPI *spi, PinName cs_pin, PinName int_pin = NC, SPI_type_t spi_type = SPI4W);
00064     LPS22HHSensor(DevI2C *i2c, uint8_t address = LPS22HH_I2C_ADD_H, PinName int_pin = NC);
00065     virtual int init(void *init);
00066     virtual int read_id(uint8_t *id);
00067     virtual int get_pressure(float *value);
00068     virtual int get_temperature(float *value);
00069     int enable(void);
00070     int disable(void);
00071     int get_odr(float *odr, uint8_t *low_noise_en);
00072     int set_odr(float odr, uint8_t low_noise_en);
00073     int get_lpfp_cfg(uint8_t *lpfp_cfg);
00074     int set_lpfp_cfg(uint8_t lpfp_cfg);
00075     int read_reg(uint8_t reg, uint8_t *data);
00076     int write_reg(uint8_t reg, uint8_t data);
00077     int get_press_drdy_status(uint8_t *status);
00078     int get_temp_drdy_status(uint8_t *status);
00079     int get_fifo_data(float *press, float *temp);
00080     int get_fifo_fth_status(uint8_t *status);
00081     int get_fifo_full_status(uint8_t *status);
00082     int get_fifo_ovr_status(uint8_t *status);
00083     int get_fifo_level(uint8_t *status);
00084     int reset_fifo_interrupt(uint8_t interrupt);
00085     int set_fifo_interrupt(uint8_t interrupt);
00086     int set_fifo_mode(uint8_t mode);
00087     int set_fifo_watermark_level(uint8_t watermark);
00088     int stop_fifo_on_watermark(uint8_t stop);
00089     int set_one_shot();
00090     int get_one_shot_status(uint8_t *status);
00091 
00092     /**
00093      * @brief  Attaching an interrupt handler to the INT interrupt.
00094      * @param  fptr an interrupt handler.
00095      * @retval None.
00096      */
00097     void attach_int_irq(void (*fptr)(void))
00098     {
00099         _int_irq.rise(fptr);
00100     }
00101 
00102     /**
00103      * @brief  Enabling the INT interrupt handling.
00104      * @param  None.
00105      * @retval None.
00106      */
00107     void enable_int_irq(void)
00108     {
00109         _int_irq.enable_irq();
00110     }
00111 
00112     /**
00113      * @brief  Disabling the INT interrupt handling.
00114      * @param  None.
00115      * @retval None.
00116      */
00117     void disable_int_irq(void)
00118     {
00119         _int_irq.disable_irq();
00120     }
00121 
00122 
00123     /**
00124      * @brief Utility function to read data.
00125      * @param  pBuffer: pointer to data to be read.
00126      * @param  RegisterAddr: specifies internal address register to be read.
00127      * @param  NumByteToRead: number of bytes to be read.
00128      * @retval 0 if ok, an error code otherwise.
00129      */
00130     uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00131     {
00132         if (_dev_spi) {
00133             /* Write Reg Address */
00134             _dev_spi->lock();
00135             _cs_pin = 0;
00136             if (_spi_type == SPI4W) {
00137                 _dev_spi->write(RegisterAddr | 0x80);
00138                 for (int i = 0; i < NumByteToRead; i++) {
00139                     *(pBuffer + i) = _dev_spi->write(0x00);
00140                 }
00141             } else if (_spi_type == SPI3W) {
00142                 /* Write RD Reg Address with RD bit*/
00143                 uint8_t TxByte = RegisterAddr | 0x80;
00144                 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00145             }
00146             _cs_pin = 1;
00147             _dev_spi->unlock();
00148             return 0;
00149         }
00150         if (_dev_i2c) {
00151             return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00152         }
00153         return 1;
00154     }
00155 
00156     /**
00157      * @brief Utility function to write data.
00158      * @param  pBuffer: pointer to data to be written.
00159      * @param  RegisterAddr: specifies internal address register to be written.
00160      * @param  NumByteToWrite: number of bytes to write.
00161      * @retval 0 if ok, an error code otherwise.
00162      */
00163     uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00164     {
00165         if (_dev_spi) {
00166             _dev_spi->lock();
00167             _cs_pin = 0;
00168             _dev_spi->write(RegisterAddr);
00169             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
00170             _cs_pin = 1;
00171             _dev_spi->unlock();
00172             return 0;
00173         }
00174         if (_dev_i2c) {
00175             return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00176         }
00177         return 1;
00178     }
00179 
00180 private:
00181     int set_odr_when_enabled(float odr, uint8_t low_noise_en);
00182     int set_odr_when_disabled(float odr, uint8_t low_noise_en);
00183 
00184     /* Helper classes. */
00185     DevI2C *_dev_i2c;
00186     SPI    *_dev_spi;
00187 
00188     /* Configuration */
00189     uint8_t _address;
00190     DigitalOut  _cs_pin;
00191     InterruptIn _int_irq;
00192     SPI_type_t _spi_type;
00193 
00194     uint8_t _is_enabled;
00195     lps22hh_odr_t _last_odr;
00196 
00197     lps22hh_ctx_t _reg_ctx;
00198 
00199 };
00200 
00201 #ifdef __cplusplus
00202 extern "C" {
00203 #endif
00204 int32_t LPS22HH_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00205 int32_t LPS22HH_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00206 #ifdef __cplusplus
00207 }
00208 #endif
00209 
00210 #endif