MEMS nano pressure sensor: 260-1260 hPa absolute digital output barometer.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LPS22HB_JS ... more

Committer:
cparata
Date:
Mon Mar 04 13:07:24 2019 +0000
Revision:
3:3971e9431200
Parent:
2:f7b403c70f6b
Fix issue in SPI write

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:3de9cb936fd1 1 /**
nikapov 0:3de9cb936fd1 2 ******************************************************************************
nikapov 0:3de9cb936fd1 3 * @file LPS22HBSensor.h
nikapov 0:3de9cb936fd1 4 * @author CLab
nikapov 0:3de9cb936fd1 5 * @version V1.0.0
nikapov 0:3de9cb936fd1 6 * @date 5 August 2016
nikapov 0:3de9cb936fd1 7 * @brief Abstract Class of an LPS22HB Pressure sensor.
nikapov 0:3de9cb936fd1 8 ******************************************************************************
nikapov 0:3de9cb936fd1 9 * @attention
nikapov 0:3de9cb936fd1 10 *
nikapov 0:3de9cb936fd1 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
nikapov 0:3de9cb936fd1 12 *
nikapov 0:3de9cb936fd1 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:3de9cb936fd1 14 * are permitted provided that the following conditions are met:
nikapov 0:3de9cb936fd1 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:3de9cb936fd1 16 * this list of conditions and the following disclaimer.
nikapov 0:3de9cb936fd1 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:3de9cb936fd1 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:3de9cb936fd1 19 * and/or other materials provided with the distribution.
nikapov 0:3de9cb936fd1 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:3de9cb936fd1 21 * may be used to endorse or promote products derived from this software
nikapov 0:3de9cb936fd1 22 * without specific prior written permission.
nikapov 0:3de9cb936fd1 23 *
nikapov 0:3de9cb936fd1 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:3de9cb936fd1 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:3de9cb936fd1 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:3de9cb936fd1 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:3de9cb936fd1 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:3de9cb936fd1 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:3de9cb936fd1 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:3de9cb936fd1 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:3de9cb936fd1 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:3de9cb936fd1 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:3de9cb936fd1 34 *
nikapov 0:3de9cb936fd1 35 ******************************************************************************
nikapov 0:3de9cb936fd1 36 */
nikapov 0:3de9cb936fd1 37
nikapov 0:3de9cb936fd1 38
nikapov 0:3de9cb936fd1 39 /* Prevent recursive inclusion -----------------------------------------------*/
nikapov 0:3de9cb936fd1 40
nikapov 0:3de9cb936fd1 41 #ifndef __LPS22HBSensor_H__
nikapov 0:3de9cb936fd1 42 #define __LPS22HBSensor_H__
nikapov 0:3de9cb936fd1 43
nikapov 0:3de9cb936fd1 44
nikapov 0:3de9cb936fd1 45 /* Includes ------------------------------------------------------------------*/
nikapov 0:3de9cb936fd1 46
nikapov 0:3de9cb936fd1 47 #include "DevI2C.h"
nikapov 0:3de9cb936fd1 48 #include "LPS22HB_driver.h"
nikapov 0:3de9cb936fd1 49 #include "PressureSensor.h"
nikapov 0:3de9cb936fd1 50 #include "TempSensor.h"
mapellil 1:33ddb62b31fd 51 #include <assert.h>
nikapov 0:3de9cb936fd1 52
nikapov 0:3de9cb936fd1 53 /* Class Declaration ---------------------------------------------------------*/
nikapov 0:3de9cb936fd1 54
nikapov 0:3de9cb936fd1 55 /**
nikapov 0:3de9cb936fd1 56 * Abstract class of an LPS22HB Pressure sensor.
nikapov 0:3de9cb936fd1 57 */
nikapov 0:3de9cb936fd1 58 class LPS22HBSensor : public PressureSensor, public TempSensor
nikapov 0:3de9cb936fd1 59 {
nikapov 0:3de9cb936fd1 60 public:
mapellil 2:f7b403c70f6b 61
mapellil 2:f7b403c70f6b 62 enum SPI_type_t {SPI3W, SPI4W};
mapellil 2:f7b403c70f6b 63 LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
mapellil 1:33ddb62b31fd 64 LPS22HBSensor(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC);
nikapov 0:3de9cb936fd1 65 virtual int init(void *init);
nikapov 0:3de9cb936fd1 66 virtual int read_id(uint8_t *id);
nikapov 0:3de9cb936fd1 67 virtual int get_pressure(float *pfData);
nikapov 0:3de9cb936fd1 68 virtual int get_temperature(float *pfData);
nikapov 0:3de9cb936fd1 69 int enable(void);
nikapov 0:3de9cb936fd1 70 int disable(void);
nikapov 0:3de9cb936fd1 71 int reset(void);
nikapov 0:3de9cb936fd1 72 int get_odr(float *odr);
nikapov 0:3de9cb936fd1 73 int set_odr(float odr);
nikapov 0:3de9cb936fd1 74 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:3de9cb936fd1 75 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:3de9cb936fd1 76
nikapov 0:3de9cb936fd1 77 /**
nikapov 0:3de9cb936fd1 78 * @brief Utility function to read data.
nikapov 0:3de9cb936fd1 79 * @param pBuffer: pointer to data to be read.
nikapov 0:3de9cb936fd1 80 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:3de9cb936fd1 81 * @param NumByteToRead: number of bytes to be read.
nikapov 0:3de9cb936fd1 82 * @retval 0 if ok, an error code otherwise.
nikapov 0:3de9cb936fd1 83 */
nikapov 0:3de9cb936fd1 84 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:3de9cb936fd1 85 {
mapellil 2:f7b403c70f6b 86 if (_dev_spi) {
mapellil 2:f7b403c70f6b 87 /* Write Reg Address */
mapellil 2:f7b403c70f6b 88 _dev_spi->lock();
mapellil 2:f7b403c70f6b 89 _cs_pin = 0;
mapellil 2:f7b403c70f6b 90 if (_spi_type == SPI4W) {
mapellil 2:f7b403c70f6b 91 _dev_spi->write(RegisterAddr | 0x80);
mapellil 2:f7b403c70f6b 92 for (int i=0; i<NumByteToRead; i++) {
mapellil 2:f7b403c70f6b 93 *(pBuffer+i) = _dev_spi->write(0x00);
mapellil 2:f7b403c70f6b 94 }
mapellil 2:f7b403c70f6b 95 } else if (_spi_type == SPI3W){
mapellil 2:f7b403c70f6b 96 /* Write RD Reg Address with RD bit*/
mapellil 2:f7b403c70f6b 97 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 2:f7b403c70f6b 98 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 2:f7b403c70f6b 99 }
mapellil 2:f7b403c70f6b 100 _cs_pin = 1;
mapellil 2:f7b403c70f6b 101 _dev_spi->unlock();
mapellil 2:f7b403c70f6b 102 return 0;
mapellil 2:f7b403c70f6b 103 }
mapellil 2:f7b403c70f6b 104 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
mapellil 2:f7b403c70f6b 105 return 1;
nikapov 0:3de9cb936fd1 106 }
nikapov 0:3de9cb936fd1 107
nikapov 0:3de9cb936fd1 108 /**
nikapov 0:3de9cb936fd1 109 * @brief Utility function to write data.
nikapov 0:3de9cb936fd1 110 * @param pBuffer: pointer to data to be written.
nikapov 0:3de9cb936fd1 111 * @param RegisterAddr: specifies internal address register to be written.
nikapov 0:3de9cb936fd1 112 * @param NumByteToWrite: number of bytes to write.
nikapov 0:3de9cb936fd1 113 * @retval 0 if ok, an error code otherwise.
nikapov 0:3de9cb936fd1 114 */
nikapov 0:3de9cb936fd1 115 uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:3de9cb936fd1 116 {
mapellil 2:f7b403c70f6b 117 if (_dev_spi) {
mapellil 2:f7b403c70f6b 118 _dev_spi->lock();
mapellil 2:f7b403c70f6b 119 _cs_pin = 0;
cparata 3:3971e9431200 120 _dev_spi->write(RegisterAddr);
mapellil 2:f7b403c70f6b 121 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
mapellil 2:f7b403c70f6b 122 _cs_pin = 1;
mapellil 2:f7b403c70f6b 123 _dev_spi->unlock();
cparata 3:3971e9431200 124 return 0;
mapellil 2:f7b403c70f6b 125 }
mapellil 2:f7b403c70f6b 126 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
mapellil 2:f7b403c70f6b 127 return 1;
nikapov 0:3de9cb936fd1 128 }
nikapov 0:3de9cb936fd1 129
nikapov 0:3de9cb936fd1 130 private:
nikapov 0:3de9cb936fd1 131 int Set_ODR_When_Enabled(float odr);
nikapov 0:3de9cb936fd1 132 int Set_ODR_When_Disabled(float odr);
nikapov 0:3de9cb936fd1 133
nikapov 0:3de9cb936fd1 134 /* Helper classes. */
mapellil 1:33ddb62b31fd 135 DevI2C *_dev_i2c;
mapellil 2:f7b403c70f6b 136 SPI *_dev_spi;
nikapov 0:3de9cb936fd1 137
nikapov 0:3de9cb936fd1 138 /* Configuration */
nikapov 0:3de9cb936fd1 139 uint8_t _address;
mapellil 1:33ddb62b31fd 140 DigitalOut _cs_pin;
mapellil 1:33ddb62b31fd 141 InterruptIn _int_pin;
mapellil 2:f7b403c70f6b 142 SPI_type_t _spi_type;
nikapov 0:3de9cb936fd1 143
nikapov 0:3de9cb936fd1 144 uint8_t _is_enabled;
nikapov 0:3de9cb936fd1 145 float _last_odr;
nikapov 0:3de9cb936fd1 146 };
nikapov 0:3de9cb936fd1 147
nikapov 0:3de9cb936fd1 148 #ifdef __cplusplus
nikapov 0:3de9cb936fd1 149 extern "C" {
nikapov 0:3de9cb936fd1 150 #endif
nikapov 0:3de9cb936fd1 151 uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
nikapov 0:3de9cb936fd1 152 uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
nikapov 0:3de9cb936fd1 153 #ifdef __cplusplus
nikapov 0:3de9cb936fd1 154 }
nikapov 0:3de9cb936fd1 155 #endif
nikapov 0:3de9cb936fd1 156
nikapov 0:3de9cb936fd1 157 #endif