Added SPI3w support through DevSPI

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON_3W

Fork of LPS22HB by ST

Committer:
mapellil
Date:
Tue Sep 26 08:18:06 2017 +0000
Revision:
2:fba15124c67b
Parent:
1:8813c9be1720
Removed DevSPI dependency, now SPI3W is supported directly through SPI

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 ------------------------------------------------------------------*/
mapellil 1:8813c9be1720 46 //#include "SPI3W.h"
nikapov 0:3de9cb936fd1 47 #include "DevI2C.h"
mapellil 1:8813c9be1720 48 #include "DevSPI.h"
nikapov 0:3de9cb936fd1 49 #include "LPS22HB_driver.h"
nikapov 0:3de9cb936fd1 50 #include "PressureSensor.h"
nikapov 0:3de9cb936fd1 51 #include "TempSensor.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:fba15124c67b 61 enum SPI_type_t {SPI3W, SPI4W};
mapellil 2:fba15124c67b 62 LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);
mapellil 1:8813c9be1720 63 LPS22HBSensor(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC);
nikapov 0:3de9cb936fd1 64 virtual int init(void *init);
nikapov 0:3de9cb936fd1 65 virtual int read_id(uint8_t *id);
nikapov 0:3de9cb936fd1 66 virtual int get_pressure(float *pfData);
nikapov 0:3de9cb936fd1 67 virtual int get_temperature(float *pfData);
nikapov 0:3de9cb936fd1 68 int enable(void);
nikapov 0:3de9cb936fd1 69 int disable(void);
nikapov 0:3de9cb936fd1 70 int reset(void);
nikapov 0:3de9cb936fd1 71 int get_odr(float *odr);
nikapov 0:3de9cb936fd1 72 int set_odr(float odr);
nikapov 0:3de9cb936fd1 73 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:3de9cb936fd1 74 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:3de9cb936fd1 75
nikapov 0:3de9cb936fd1 76 /**
nikapov 0:3de9cb936fd1 77 * @brief Utility function to read data.
nikapov 0:3de9cb936fd1 78 * @param pBuffer: pointer to data to be read.
nikapov 0:3de9cb936fd1 79 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:3de9cb936fd1 80 * @param NumByteToRead: number of bytes to be read.
nikapov 0:3de9cb936fd1 81 * @retval 0 if ok, an error code otherwise.
nikapov 0:3de9cb936fd1 82 */
nikapov 0:3de9cb936fd1 83 uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:3de9cb936fd1 84 {
mapellil 1:8813c9be1720 85 if (_dev_spi) {
mapellil 1:8813c9be1720 86 /* Write Reg Address */
mapellil 1:8813c9be1720 87 _dev_spi->lock();
mapellil 1:8813c9be1720 88 _cs_pin = 0;
mapellil 2:fba15124c67b 89 if (_spi_type == SPI4W) {
mapellil 1:8813c9be1720 90 _dev_spi->write(RegisterAddr | 0x80);
mapellil 1:8813c9be1720 91 for (int i=0; i<NumByteToRead; i++) {
mapellil 1:8813c9be1720 92 *(pBuffer+i) = _dev_spi->write(0x00);
mapellil 1:8813c9be1720 93 }
mapellil 2:fba15124c67b 94 } else if (_spi_type == SPI3W){
mapellil 1:8813c9be1720 95 /* Write RD Reg Address with RD bit*/
mapellil 1:8813c9be1720 96 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 1:8813c9be1720 97 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 1:8813c9be1720 98 }
mapellil 1:8813c9be1720 99 _cs_pin = 1;
mapellil 1:8813c9be1720 100 _dev_spi->unlock();
mapellil 1:8813c9be1720 101 return 0;
mapellil 1:8813c9be1720 102 }
mapellil 1:8813c9be1720 103
mapellil 1:8813c9be1720 104 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
mapellil 1:8813c9be1720 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 1:8813c9be1720 117 int data;
mapellil 1:8813c9be1720 118 if (_dev_spi) {
mapellil 1:8813c9be1720 119 _dev_spi->lock();
mapellil 1:8813c9be1720 120 _cs_pin = 0;
mapellil 1:8813c9be1720 121 data = _dev_spi->write(RegisterAddr);
mapellil 1:8813c9be1720 122 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
mapellil 1:8813c9be1720 123 _cs_pin = 1;
mapellil 1:8813c9be1720 124 _dev_spi->unlock();
mapellil 1:8813c9be1720 125 return data;
mapellil 1:8813c9be1720 126 }
mapellil 1:8813c9be1720 127
mapellil 1:8813c9be1720 128 if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
mapellil 1:8813c9be1720 129 return 1;
nikapov 0:3de9cb936fd1 130 }
nikapov 0:3de9cb936fd1 131
nikapov 0:3de9cb936fd1 132 private:
nikapov 0:3de9cb936fd1 133 int Set_ODR_When_Enabled(float odr);
nikapov 0:3de9cb936fd1 134 int Set_ODR_When_Disabled(float odr);
nikapov 0:3de9cb936fd1 135
nikapov 0:3de9cb936fd1 136 /* Helper classes. */
mapellil 1:8813c9be1720 137 DevI2C *_dev_i2c;
mapellil 2:fba15124c67b 138 SPI *_dev_spi;
nikapov 0:3de9cb936fd1 139
nikapov 0:3de9cb936fd1 140 /* Configuration */
nikapov 0:3de9cb936fd1 141 uint8_t _address;
mapellil 1:8813c9be1720 142 DigitalOut _cs_pin;
mapellil 1:8813c9be1720 143 InterruptIn _int_pin;
nikapov 0:3de9cb936fd1 144
nikapov 0:3de9cb936fd1 145 uint8_t _is_enabled;
nikapov 0:3de9cb936fd1 146 float _last_odr;
mapellil 2:fba15124c67b 147 SPI_type_t _spi_type;
nikapov 0:3de9cb936fd1 148 };
nikapov 0:3de9cb936fd1 149
nikapov 0:3de9cb936fd1 150 #ifdef __cplusplus
nikapov 0:3de9cb936fd1 151 extern "C" {
nikapov 0:3de9cb936fd1 152 #endif
nikapov 0:3de9cb936fd1 153 uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
nikapov 0:3de9cb936fd1 154 uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
nikapov 0:3de9cb936fd1 155 #ifdef __cplusplus
nikapov 0:3de9cb936fd1 156 }
nikapov 0:3de9cb936fd1 157 #endif
nikapov 0:3de9cb936fd1 158
nikapov 0:3de9cb936fd1 159 #endif