MEMS nano pressure sensor: 260-1260 hPa absolute digital output barometer.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LPS22HHSensor.h@0:c761bc6186e8, 2019-03-06 (annotated)
- Committer:
- cparata
- Date:
- Wed Mar 06 09:18:20 2019 +0000
- Revision:
- 0:c761bc6186e8
- Child:
- 1:978cae936ddb
First version of LPS22HH library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:c761bc6186e8 | 1 | /** |
cparata | 0:c761bc6186e8 | 2 | ****************************************************************************** |
cparata | 0:c761bc6186e8 | 3 | * @file LPS22HHSensor.h |
cparata | 0:c761bc6186e8 | 4 | * @author SRA |
cparata | 0:c761bc6186e8 | 5 | * @version V1.0.0 |
cparata | 0:c761bc6186e8 | 6 | * @date February 2019 |
cparata | 0:c761bc6186e8 | 7 | * @brief Abstract Class of a LPS22HH pressure sensor. |
cparata | 0:c761bc6186e8 | 8 | ****************************************************************************** |
cparata | 0:c761bc6186e8 | 9 | * @attention |
cparata | 0:c761bc6186e8 | 10 | * |
cparata | 0:c761bc6186e8 | 11 | * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> |
cparata | 0:c761bc6186e8 | 12 | * |
cparata | 0:c761bc6186e8 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:c761bc6186e8 | 14 | * are permitted provided that the following conditions are met: |
cparata | 0:c761bc6186e8 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:c761bc6186e8 | 16 | * this list of conditions and the following disclaimer. |
cparata | 0:c761bc6186e8 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:c761bc6186e8 | 18 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:c761bc6186e8 | 19 | * and/or other materials provided with the distribution. |
cparata | 0:c761bc6186e8 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:c761bc6186e8 | 21 | * may be used to endorse or promote products derived from this software |
cparata | 0:c761bc6186e8 | 22 | * without specific prior written permission. |
cparata | 0:c761bc6186e8 | 23 | * |
cparata | 0:c761bc6186e8 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:c761bc6186e8 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:c761bc6186e8 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:c761bc6186e8 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:c761bc6186e8 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:c761bc6186e8 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:c761bc6186e8 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:c761bc6186e8 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:c761bc6186e8 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:c761bc6186e8 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:c761bc6186e8 | 34 | * |
cparata | 0:c761bc6186e8 | 35 | ****************************************************************************** |
cparata | 0:c761bc6186e8 | 36 | */ |
cparata | 0:c761bc6186e8 | 37 | |
cparata | 0:c761bc6186e8 | 38 | |
cparata | 0:c761bc6186e8 | 39 | /* Prevent recursive inclusion -----------------------------------------------*/ |
cparata | 0:c761bc6186e8 | 40 | |
cparata | 0:c761bc6186e8 | 41 | #ifndef __LPS22HHSensor_H__ |
cparata | 0:c761bc6186e8 | 42 | #define __LPS22HHSensor_H__ |
cparata | 0:c761bc6186e8 | 43 | |
cparata | 0:c761bc6186e8 | 44 | |
cparata | 0:c761bc6186e8 | 45 | /* Includes ------------------------------------------------------------------*/ |
cparata | 0:c761bc6186e8 | 46 | |
cparata | 0:c761bc6186e8 | 47 | #include "DevI2C.h" |
cparata | 0:c761bc6186e8 | 48 | #include "lps22hh_reg.h" |
cparata | 0:c761bc6186e8 | 49 | #include "PressureSensor.h" |
cparata | 0:c761bc6186e8 | 50 | #include "TempSensor.h" |
cparata | 0:c761bc6186e8 | 51 | #include <assert.h> |
cparata | 0:c761bc6186e8 | 52 | |
cparata | 0:c761bc6186e8 | 53 | /* Defines -------------------------------------------------------------------*/ |
cparata | 0:c761bc6186e8 | 54 | /* Typedefs ------------------------------------------------------------------*/ |
cparata | 0:c761bc6186e8 | 55 | /* Class Declaration ---------------------------------------------------------*/ |
cparata | 0:c761bc6186e8 | 56 | |
cparata | 0:c761bc6186e8 | 57 | /** |
cparata | 0:c761bc6186e8 | 58 | * Abstract class of a LPS22HH pressure sensor. |
cparata | 0:c761bc6186e8 | 59 | */ |
cparata | 0:c761bc6186e8 | 60 | class LPS22HHSensor : public PressureSensor, public TempSensor |
cparata | 0:c761bc6186e8 | 61 | { |
cparata | 0:c761bc6186e8 | 62 | public: |
cparata | 0:c761bc6186e8 | 63 | enum SPI_type_t {SPI3W, SPI4W}; |
cparata | 0:c761bc6186e8 | 64 | LPS22HHSensor(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W); |
cparata | 0:c761bc6186e8 | 65 | LPS22HHSensor(DevI2C *i2c, uint8_t address=LPS22HH_I2C_ADD_H, PinName int_pin=NC); |
cparata | 0:c761bc6186e8 | 66 | virtual int init(void *init); |
cparata | 0:c761bc6186e8 | 67 | virtual int read_id(uint8_t *id); |
cparata | 0:c761bc6186e8 | 68 | virtual int get_pressure(float *value); |
cparata | 0:c761bc6186e8 | 69 | virtual int get_temperature(float *value); |
cparata | 0:c761bc6186e8 | 70 | int enable(void); |
cparata | 0:c761bc6186e8 | 71 | int disable(void); |
cparata | 0:c761bc6186e8 | 72 | int get_odr(float *odr); |
cparata | 0:c761bc6186e8 | 73 | int set_odr(float odr); |
cparata | 0:c761bc6186e8 | 74 | int read_reg(uint8_t reg, uint8_t *data); |
cparata | 0:c761bc6186e8 | 75 | int write_reg(uint8_t reg, uint8_t data); |
cparata | 0:c761bc6186e8 | 76 | int get_press_drdy_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 77 | int get_temp_drdy_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 78 | int get_fifo_data(float *press, float *temp); |
cparata | 0:c761bc6186e8 | 79 | int get_fifo_fth_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 80 | int get_fifo_full_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 81 | int get_fifo_ovr_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 82 | int get_fifo_level(uint8_t *status); |
cparata | 0:c761bc6186e8 | 83 | int reset_fifo_interrupt(uint8_t interrupt); |
cparata | 0:c761bc6186e8 | 84 | int set_fifo_interrupt(uint8_t interrupt); |
cparata | 0:c761bc6186e8 | 85 | int set_fifo_mode(uint8_t mode); |
cparata | 0:c761bc6186e8 | 86 | int set_fifo_watermark_level(uint8_t watermark); |
cparata | 0:c761bc6186e8 | 87 | int stop_fifo_on_watermark(uint8_t stop); |
cparata | 0:c761bc6186e8 | 88 | int set_one_shot(); |
cparata | 0:c761bc6186e8 | 89 | int get_one_shot_status(uint8_t *status); |
cparata | 0:c761bc6186e8 | 90 | |
cparata | 0:c761bc6186e8 | 91 | /** |
cparata | 0:c761bc6186e8 | 92 | * @brief Attaching an interrupt handler to the INT interrupt. |
cparata | 0:c761bc6186e8 | 93 | * @param fptr an interrupt handler. |
cparata | 0:c761bc6186e8 | 94 | * @retval None. |
cparata | 0:c761bc6186e8 | 95 | */ |
cparata | 0:c761bc6186e8 | 96 | void attach_int_irq(void (*fptr)(void)) |
cparata | 0:c761bc6186e8 | 97 | { |
cparata | 0:c761bc6186e8 | 98 | _int_irq.rise(fptr); |
cparata | 0:c761bc6186e8 | 99 | } |
cparata | 0:c761bc6186e8 | 100 | |
cparata | 0:c761bc6186e8 | 101 | /** |
cparata | 0:c761bc6186e8 | 102 | * @brief Enabling the INT interrupt handling. |
cparata | 0:c761bc6186e8 | 103 | * @param None. |
cparata | 0:c761bc6186e8 | 104 | * @retval None. |
cparata | 0:c761bc6186e8 | 105 | */ |
cparata | 0:c761bc6186e8 | 106 | void enable_int_irq(void) |
cparata | 0:c761bc6186e8 | 107 | { |
cparata | 0:c761bc6186e8 | 108 | _int_irq.enable_irq(); |
cparata | 0:c761bc6186e8 | 109 | } |
cparata | 0:c761bc6186e8 | 110 | |
cparata | 0:c761bc6186e8 | 111 | /** |
cparata | 0:c761bc6186e8 | 112 | * @brief Disabling the INT interrupt handling. |
cparata | 0:c761bc6186e8 | 113 | * @param None. |
cparata | 0:c761bc6186e8 | 114 | * @retval None. |
cparata | 0:c761bc6186e8 | 115 | */ |
cparata | 0:c761bc6186e8 | 116 | void disable_int_irq(void) |
cparata | 0:c761bc6186e8 | 117 | { |
cparata | 0:c761bc6186e8 | 118 | _int_irq.disable_irq(); |
cparata | 0:c761bc6186e8 | 119 | } |
cparata | 0:c761bc6186e8 | 120 | |
cparata | 0:c761bc6186e8 | 121 | |
cparata | 0:c761bc6186e8 | 122 | /** |
cparata | 0:c761bc6186e8 | 123 | * @brief Utility function to read data. |
cparata | 0:c761bc6186e8 | 124 | * @param pBuffer: pointer to data to be read. |
cparata | 0:c761bc6186e8 | 125 | * @param RegisterAddr: specifies internal address register to be read. |
cparata | 0:c761bc6186e8 | 126 | * @param NumByteToRead: number of bytes to be read. |
cparata | 0:c761bc6186e8 | 127 | * @retval 0 if ok, an error code otherwise. |
cparata | 0:c761bc6186e8 | 128 | */ |
cparata | 0:c761bc6186e8 | 129 | uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) |
cparata | 0:c761bc6186e8 | 130 | { |
cparata | 0:c761bc6186e8 | 131 | if (_dev_spi) { |
cparata | 0:c761bc6186e8 | 132 | /* Write Reg Address */ |
cparata | 0:c761bc6186e8 | 133 | _dev_spi->lock(); |
cparata | 0:c761bc6186e8 | 134 | _cs_pin = 0; |
cparata | 0:c761bc6186e8 | 135 | if (_spi_type == SPI4W) { |
cparata | 0:c761bc6186e8 | 136 | _dev_spi->write(RegisterAddr | 0x80); |
cparata | 0:c761bc6186e8 | 137 | for (int i=0; i<NumByteToRead; i++) { |
cparata | 0:c761bc6186e8 | 138 | *(pBuffer+i) = _dev_spi->write(0x00); |
cparata | 0:c761bc6186e8 | 139 | } |
cparata | 0:c761bc6186e8 | 140 | } else if (_spi_type == SPI3W){ |
cparata | 0:c761bc6186e8 | 141 | /* Write RD Reg Address with RD bit*/ |
cparata | 0:c761bc6186e8 | 142 | uint8_t TxByte = RegisterAddr | 0x80; |
cparata | 0:c761bc6186e8 | 143 | _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); |
cparata | 0:c761bc6186e8 | 144 | } |
cparata | 0:c761bc6186e8 | 145 | _cs_pin = 1; |
cparata | 0:c761bc6186e8 | 146 | _dev_spi->unlock(); |
cparata | 0:c761bc6186e8 | 147 | return 0; |
cparata | 0:c761bc6186e8 | 148 | } |
cparata | 0:c761bc6186e8 | 149 | if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); |
cparata | 0:c761bc6186e8 | 150 | return 1; |
cparata | 0:c761bc6186e8 | 151 | } |
cparata | 0:c761bc6186e8 | 152 | |
cparata | 0:c761bc6186e8 | 153 | /** |
cparata | 0:c761bc6186e8 | 154 | * @brief Utility function to write data. |
cparata | 0:c761bc6186e8 | 155 | * @param pBuffer: pointer to data to be written. |
cparata | 0:c761bc6186e8 | 156 | * @param RegisterAddr: specifies internal address register to be written. |
cparata | 0:c761bc6186e8 | 157 | * @param NumByteToWrite: number of bytes to write. |
cparata | 0:c761bc6186e8 | 158 | * @retval 0 if ok, an error code otherwise. |
cparata | 0:c761bc6186e8 | 159 | */ |
cparata | 0:c761bc6186e8 | 160 | uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) |
cparata | 0:c761bc6186e8 | 161 | { |
cparata | 0:c761bc6186e8 | 162 | if (_dev_spi) { |
cparata | 0:c761bc6186e8 | 163 | _dev_spi->lock(); |
cparata | 0:c761bc6186e8 | 164 | _cs_pin = 0; |
cparata | 0:c761bc6186e8 | 165 | _dev_spi->write(RegisterAddr); |
cparata | 0:c761bc6186e8 | 166 | _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); |
cparata | 0:c761bc6186e8 | 167 | _cs_pin = 1; |
cparata | 0:c761bc6186e8 | 168 | _dev_spi->unlock(); |
cparata | 0:c761bc6186e8 | 169 | return 0; |
cparata | 0:c761bc6186e8 | 170 | } |
cparata | 0:c761bc6186e8 | 171 | if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); |
cparata | 0:c761bc6186e8 | 172 | return 1; |
cparata | 0:c761bc6186e8 | 173 | } |
cparata | 0:c761bc6186e8 | 174 | |
cparata | 0:c761bc6186e8 | 175 | private: |
cparata | 0:c761bc6186e8 | 176 | int set_odr_when_enabled(float odr); |
cparata | 0:c761bc6186e8 | 177 | int set_odr_when_disabled(float odr); |
cparata | 0:c761bc6186e8 | 178 | |
cparata | 0:c761bc6186e8 | 179 | /* Helper classes. */ |
cparata | 0:c761bc6186e8 | 180 | DevI2C *_dev_i2c; |
cparata | 0:c761bc6186e8 | 181 | SPI *_dev_spi; |
cparata | 0:c761bc6186e8 | 182 | |
cparata | 0:c761bc6186e8 | 183 | /* Configuration */ |
cparata | 0:c761bc6186e8 | 184 | uint8_t _address; |
cparata | 0:c761bc6186e8 | 185 | DigitalOut _cs_pin; |
cparata | 0:c761bc6186e8 | 186 | InterruptIn _int_irq; |
cparata | 0:c761bc6186e8 | 187 | SPI_type_t _spi_type; |
cparata | 0:c761bc6186e8 | 188 | |
cparata | 0:c761bc6186e8 | 189 | uint8_t _is_enabled; |
cparata | 0:c761bc6186e8 | 190 | lps22hh_odr_t _last_odr; |
cparata | 0:c761bc6186e8 | 191 | |
cparata | 0:c761bc6186e8 | 192 | lps22hh_ctx_t _reg_ctx; |
cparata | 0:c761bc6186e8 | 193 | |
cparata | 0:c761bc6186e8 | 194 | }; |
cparata | 0:c761bc6186e8 | 195 | |
cparata | 0:c761bc6186e8 | 196 | #ifdef __cplusplus |
cparata | 0:c761bc6186e8 | 197 | extern "C" { |
cparata | 0:c761bc6186e8 | 198 | #endif |
cparata | 0:c761bc6186e8 | 199 | int32_t LPS22HH_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ); |
cparata | 0:c761bc6186e8 | 200 | int32_t LPS22HH_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ); |
cparata | 0:c761bc6186e8 | 201 | #ifdef __cplusplus |
cparata | 0:c761bc6186e8 | 202 | } |
cparata | 0:c761bc6186e8 | 203 | #endif |
cparata | 0:c761bc6186e8 | 204 | |
cparata | 0:c761bc6186e8 | 205 | #endif |