Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
LPS22HHSensor.h
00001 /** 00002 ****************************************************************************** 00003 * @file LPS22HHSensor.h 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date February 2019 00007 * @brief Abstract Class of a LPS22HH pressure sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2019 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 __LPS22HHSensor_H__ 00042 #define __LPS22HHSensor_H__ 00043 00044 00045 /* Includes ------------------------------------------------------------------*/ 00046 00047 #include "DevI2C.h" 00048 #include "lps22hh_reg.h" 00049 #include "PressureSensor.h" 00050 #include "TempSensor.h" 00051 #include <assert.h> 00052 00053 /* Defines -------------------------------------------------------------------*/ 00054 /* Typedefs ------------------------------------------------------------------*/ 00055 /* Class Declaration ---------------------------------------------------------*/ 00056 00057 /** 00058 * Abstract class of a LPS22HH pressure sensor. 00059 */ 00060 class LPS22HHSensor : public PressureSensor, public TempSensor { 00061 public: 00062 enum SPI_type_t {SPI3W, SPI4W}; 00063 LPS22HHSensor(SPI *spi, PinName cs_pin, PinName int_pin = NC, SPI_type_t spi_type = SPI4W); 00064 LPS22HHSensor(DevI2C *i2c, uint8_t address = LPS22HH_I2C_ADD_H, PinName int_pin = NC); 00065 virtual int init(void *init); 00066 virtual int read_id(uint8_t *id); 00067 virtual int get_pressure(float *value); 00068 virtual int get_temperature(float *value); 00069 int enable(void); 00070 int disable(void); 00071 int get_odr(float *odr); 00072 int set_odr(float odr); 00073 int read_reg(uint8_t reg, uint8_t *data); 00074 int write_reg(uint8_t reg, uint8_t data); 00075 int get_press_drdy_status(uint8_t *status); 00076 int get_temp_drdy_status(uint8_t *status); 00077 int get_fifo_data(float *press, float *temp); 00078 int get_fifo_fth_status(uint8_t *status); 00079 int get_fifo_full_status(uint8_t *status); 00080 int get_fifo_ovr_status(uint8_t *status); 00081 int get_fifo_level(uint8_t *status); 00082 int reset_fifo_interrupt(uint8_t interrupt); 00083 int set_fifo_interrupt(uint8_t interrupt); 00084 int set_fifo_mode(uint8_t mode); 00085 int set_fifo_watermark_level(uint8_t watermark); 00086 int stop_fifo_on_watermark(uint8_t stop); 00087 int set_one_shot(); 00088 int get_one_shot_status(uint8_t *status); 00089 00090 /** 00091 * @brief Attaching an interrupt handler to the INT interrupt. 00092 * @param fptr an interrupt handler. 00093 * @retval None. 00094 */ 00095 void attach_int_irq(void (*fptr)(void)) 00096 { 00097 _int_irq.rise(fptr); 00098 } 00099 00100 /** 00101 * @brief Enabling the INT interrupt handling. 00102 * @param None. 00103 * @retval None. 00104 */ 00105 void enable_int_irq(void) 00106 { 00107 _int_irq.enable_irq(); 00108 } 00109 00110 /** 00111 * @brief Disabling the INT interrupt handling. 00112 * @param None. 00113 * @retval None. 00114 */ 00115 void disable_int_irq(void) 00116 { 00117 _int_irq.disable_irq(); 00118 } 00119 00120 00121 /** 00122 * @brief Utility function to read data. 00123 * @param pBuffer: pointer to data to be read. 00124 * @param RegisterAddr: specifies internal address register to be read. 00125 * @param NumByteToRead: number of bytes to be read. 00126 * @retval 0 if ok, an error code otherwise. 00127 */ 00128 uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) 00129 { 00130 if (_dev_spi) { 00131 /* Write Reg Address */ 00132 _dev_spi->lock(); 00133 _cs_pin = 0; 00134 if (_spi_type == SPI4W) { 00135 _dev_spi->write(RegisterAddr | 0x80); 00136 for (int i = 0; i < NumByteToRead; i++) { 00137 *(pBuffer + i) = _dev_spi->write(0x00); 00138 } 00139 } else if (_spi_type == SPI3W) { 00140 /* Write RD Reg Address with RD bit*/ 00141 uint8_t TxByte = RegisterAddr | 0x80; 00142 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); 00143 } 00144 _cs_pin = 1; 00145 _dev_spi->unlock(); 00146 return 0; 00147 } 00148 if (_dev_i2c) { 00149 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); 00150 } 00151 return 1; 00152 } 00153 00154 /** 00155 * @brief Utility function to write data. 00156 * @param pBuffer: pointer to data to be written. 00157 * @param RegisterAddr: specifies internal address register to be written. 00158 * @param NumByteToWrite: number of bytes to write. 00159 * @retval 0 if ok, an error code otherwise. 00160 */ 00161 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) 00162 { 00163 if (_dev_spi) { 00164 _dev_spi->lock(); 00165 _cs_pin = 0; 00166 _dev_spi->write(RegisterAddr); 00167 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); 00168 _cs_pin = 1; 00169 _dev_spi->unlock(); 00170 return 0; 00171 } 00172 if (_dev_i2c) { 00173 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); 00174 } 00175 return 1; 00176 } 00177 00178 private: 00179 int set_odr_when_enabled(float odr); 00180 int set_odr_when_disabled(float odr); 00181 00182 /* Helper classes. */ 00183 DevI2C *_dev_i2c; 00184 SPI *_dev_spi; 00185 00186 /* Configuration */ 00187 uint8_t _address; 00188 DigitalOut _cs_pin; 00189 InterruptIn _int_irq; 00190 SPI_type_t _spi_type; 00191 00192 uint8_t _is_enabled; 00193 lps22hh_odr_t _last_odr; 00194 00195 lps22hh_ctx_t _reg_ctx; 00196 00197 }; 00198 00199 #ifdef __cplusplus 00200 extern "C" { 00201 #endif 00202 int32_t LPS22HH_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); 00203 int32_t LPS22HH_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); 00204 #ifdef __cplusplus 00205 } 00206 #endif 00207 00208 #endif
Generated on Wed Jul 13 2022 04:53:54 by
