Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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