ST Expansion SW Team / X_NUCLEO_IKS01A2_SPI3W

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON_SPI3W

Dependents:   sensor-node-ble

Fork of X_NUCLEO_IKS01A2 by Licio Mapelli

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LPS22HBSensor.h Source File

LPS22HBSensor.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LPS22HBSensor.h
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    5 August 2016
00007  * @brief   Abstract Class of an LPS22HB Pressure sensor.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2016 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 __LPS22HBSensor_H__
00042 #define __LPS22HBSensor_H__
00043 
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 #include "SPI3W.h"
00047 #include "DevI2C.h"
00048 #include "LPS22HB_driver.h"
00049 #include "PressureSensor.h"
00050 #include "TempSensor.h"
00051 
00052 /* Class Declaration ---------------------------------------------------------*/
00053 
00054 /**
00055  * Abstract class of an LPS22HB Pressure sensor.
00056  */
00057 class LPS22HBSensor : public PressureSensor, public TempSensor
00058 {
00059   public:
00060     LPS22HBSensor(SPI3W *spi3w, PinName cs_pin, PinName int_pin=NC);        
00061     LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin=NC);            
00062     LPS22HBSensor(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC);
00063     virtual int init(void *init);
00064     virtual int read_id(uint8_t *id);
00065     virtual int get_pressure(float *pfData);
00066     virtual int get_temperature(float *pfData);
00067     int enable(void);
00068     int disable(void);
00069     int reset(void);
00070     int get_odr(float *odr);
00071     int set_odr(float odr);
00072     int read_reg(uint8_t reg, uint8_t *data);
00073     int write_reg(uint8_t reg, uint8_t data);
00074     
00075     /**
00076      * @brief Utility function to read data.
00077      * @param  pBuffer: pointer to data to be read.
00078      * @param  RegisterAddr: specifies internal address register to be read.
00079      * @param  NumByteToRead: number of bytes to be read.
00080      * @retval 0 if ok, an error code otherwise.
00081      */
00082     uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00083     {
00084         if (_dev_spi) {
00085         /* Write Reg Address */
00086             _dev_spi->lock();
00087             _cs_pin = 0;                      
00088             _dev_spi->write(RegisterAddr | 0x80);
00089 
00090             for (int i=0; i<NumByteToRead; i++) {
00091                 *(pBuffer+i) = _dev_spi->write(0x00);
00092             }
00093                         
00094             _cs_pin = 1;
00095             _dev_spi->unlock(); 
00096             return 0;
00097         }
00098             
00099         if (_dev_spi3w) return _dev_spi3w->spi3w_read(pBuffer, &_cs_pin, RegisterAddr, NumByteToRead);                                               
00100                        
00101         if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00102         return 1;
00103     }
00104     
00105     /**
00106      * @brief Utility function to write data.
00107      * @param  pBuffer: pointer to data to be written.
00108      * @param  RegisterAddr: specifies internal address register to be written.
00109      * @param  NumByteToWrite: number of bytes to write.
00110      * @retval 0 if ok, an error code otherwise.
00111      */
00112     uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00113     {
00114             int data;   
00115               if (_dev_spi) { 
00116                     _dev_spi->lock();
00117                     _cs_pin = 0;
00118                     data = _dev_spi->write(RegisterAddr);
00119                     
00120                     for (int i=0; i<NumByteToWrite; i++) {
00121                         data = _dev_spi->write(*(pBuffer+i));
00122                     }
00123                     
00124                     _cs_pin = 1;                    
00125                     _dev_spi->unlock();
00126                     return data;                    
00127                 }
00128             
00129               if (_dev_spi3w) return _dev_spi3w->spi3w_write(pBuffer, &_cs_pin, RegisterAddr, NumByteToWrite);
00130             
00131               if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);    
00132               return 1;
00133     }
00134 
00135   private:
00136     int Set_ODR_When_Enabled(float odr);
00137     int Set_ODR_When_Disabled(float odr);
00138 
00139     /* Helper classes. */
00140     DevI2C *_dev_i2c;
00141     SPI3W  *_dev_spi3w;
00142     SPI    *_dev_spi; 
00143     
00144     /* Configuration */
00145     uint8_t _address;
00146     DigitalOut  _cs_pin;        
00147     InterruptIn _int_pin;
00148     
00149     uint8_t _is_enabled;
00150     float _last_odr;
00151 };
00152 
00153 #ifdef __cplusplus
00154  extern "C" {
00155 #endif
00156 uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
00157 uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
00158 #ifdef __cplusplus
00159   }
00160 #endif
00161 
00162 #endif