Capacitive digital sensor for relative humidity and temperature.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTS221Sensor.cpp Source File

HTS221Sensor.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    HTS221Sensor.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    5 August 2016
00007  * @brief   Implementation of an HTS221 Humidity and Temperature 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 /* Includes ------------------------------------------------------------------*/
00040 
00041 
00042 #include "HTS221Sensor.h"
00043 
00044 
00045 /* Class Implementation ------------------------------------------------------*/
00046 
00047 HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) :
00048     _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin)  // SPI3W ONLY
00049 {
00050     assert(spi);
00051     _dev_i2c = NULL;
00052 };
00053 
00054 /** Constructor
00055  * @param i2c object of an helper class which handles the I2C peripheral
00056  * @param address the address of the component's instance
00057  */
00058 HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) :
00059     _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin)
00060 {
00061     assert(i2c);
00062     _dev_spi = NULL;
00063 };
00064 
00065 /**
00066  * @brief     Initializing the component.
00067  * @param[in] init pointer to device specific initalization structure.
00068  * @retval    "0" in case of success, an error code otherwise.
00069  */
00070 int HTS221Sensor::init(void *init)
00071 {
00072     /* Power down the device */
00073     if (HTS221_DeActivate((void *)this) == HTS221_ERROR) {
00074         return 1;
00075     }
00076 
00077     /* Enable BDU */
00078     if (HTS221_Set_BduMode((void *)this, HTS221_ENABLE) == HTS221_ERROR) {
00079         return 1;
00080     }
00081 
00082     if (set_odr(1.0f) == 1) {
00083         return 1;
00084     }
00085 
00086     return 0;
00087 }
00088 
00089 /**
00090  * @brief  Enable HTS221
00091  * @retval 0 in case of success, an error code otherwise
00092  */
00093 int HTS221Sensor::enable(void)
00094 {
00095     /* Power up the device */
00096     if (HTS221_Activate((void *)this) == HTS221_ERROR) {
00097         return 1;
00098     }
00099 
00100     return 0;
00101 }
00102 
00103 /**
00104  * @brief  Disable HTS221
00105  * @retval 0 in case of success, an error code otherwise
00106  */
00107 int HTS221Sensor::disable(void)
00108 {
00109     /* Power up the device */
00110     if (HTS221_DeActivate((void *)this) == HTS221_ERROR) {
00111         return 1;
00112     }
00113 
00114     return 0;
00115 }
00116 
00117 /**
00118  * @brief  Read ID address of HTS221
00119  * @param  id the pointer where the ID of the device is stored
00120  * @retval 0 in case of success, an error code otherwise
00121  */
00122 int HTS221Sensor::read_id(uint8_t *id)
00123 {
00124     if (!id) {
00125         return 1;
00126     }
00127 
00128     /* Read WHO AM I register */
00129     if (HTS221_Get_DeviceID((void *)this, id) == HTS221_ERROR) {
00130         return 1;
00131     }
00132 
00133     return 0;
00134 }
00135 
00136 /**
00137  * @brief  Reboot memory content of HTS221
00138  * @param  None
00139  * @retval 0 in case of success, an error code otherwise
00140  */
00141 int HTS221Sensor::reset(void)
00142 {
00143     uint8_t tmpreg;
00144 
00145     /* Read CTRL_REG2 register */
00146     if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0) {
00147         return 1;
00148     }
00149 
00150     /* Enable or Disable the reboot memory */
00151     tmpreg |= (0x01 << HTS221_BOOT_BIT);
00152 
00153     /* Write value to MEMS CTRL_REG2 regsister */
00154     if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0) {
00155         return 1;
00156     }
00157 
00158     return 0;
00159 }
00160 
00161 /**
00162  * @brief  Read HTS221 output register, and calculate the humidity
00163  * @param  pfData the pointer to data output
00164  * @retval 0 in case of success, an error code otherwise
00165  */
00166 int HTS221Sensor::get_humidity(float *pfData)
00167 {
00168     uint16_t uint16data = 0;
00169 
00170     /* Read data from HTS221. */
00171     if (HTS221_Get_Humidity((void *)this, &uint16data) == HTS221_ERROR) {
00172         return 1;
00173     }
00174 
00175     *pfData = (float)uint16data / 10.0f;
00176 
00177     return 0;
00178 }
00179 
00180 /**
00181  * @brief  Read HTS221 output register, and calculate the temperature
00182  * @param  pfData the pointer to data output
00183  * @retval 0 in case of success, an error code otherwise
00184  */
00185 int HTS221Sensor::get_temperature(float *pfData)
00186 {
00187     int16_t int16data = 0;
00188 
00189     /* Read data from HTS221. */
00190     if (HTS221_Get_Temperature((void *)this, &int16data) == HTS221_ERROR) {
00191         return 1;
00192     }
00193 
00194     *pfData = (float)int16data / 10.0f;
00195 
00196     return 0;
00197 }
00198 
00199 /**
00200  * @brief  Read HTS221 output register, and calculate the humidity
00201  * @param  odr the pointer to the output data rate
00202  * @retval 0 in case of success, an error code otherwise
00203  */
00204 int HTS221Sensor::get_odr(float *odr)
00205 {
00206     HTS221_Odr_et odr_low_level;
00207 
00208     if (HTS221_Get_Odr((void *)this, &odr_low_level) == HTS221_ERROR) {
00209         return 1;
00210     }
00211 
00212     switch (odr_low_level) {
00213         case HTS221_ODR_ONE_SHOT :
00214             *odr =  0.0f;
00215             break;
00216         case HTS221_ODR_1HZ      :
00217             *odr =  1.0f;
00218             break;
00219         case HTS221_ODR_7HZ      :
00220             *odr =  7.0f;
00221             break;
00222         case HTS221_ODR_12_5HZ   :
00223             *odr = 12.5f;
00224             break;
00225         default                 :
00226             *odr = -1.0f;
00227             return 1;
00228     }
00229 
00230     return 0;
00231 }
00232 
00233 /**
00234  * @brief  Set ODR
00235  * @param  odr the output data rate to be set
00236  * @retval 0 in case of success, an error code otherwise
00237  */
00238 int HTS221Sensor::set_odr(float odr)
00239 {
00240     HTS221_Odr_et new_odr;
00241 
00242     new_odr = (odr <= 1.0f) ? HTS221_ODR_1HZ 
00243               : (odr <= 7.0f) ? HTS221_ODR_7HZ 
00244               :                   HTS221_ODR_12_5HZ ;
00245 
00246     if (HTS221_Set_Odr((void *)this, new_odr) == HTS221_ERROR) {
00247         return 1;
00248     }
00249 
00250     return 0;
00251 }
00252 
00253 
00254 /**
00255  * @brief Read the data from register
00256  * @param reg register address
00257  * @param data register data
00258  * @retval 0 in case of success
00259  * @retval 1 in case of failure
00260  */
00261 int HTS221Sensor::read_reg(uint8_t reg, uint8_t *data)
00262 {
00263 
00264     if (HTS221_read_reg((void *)this, reg, 1, data) == HTS221_ERROR) {
00265         return 1;
00266     }
00267 
00268     return 0;
00269 }
00270 
00271 /**
00272  * @brief Write the data to register
00273  * @param reg register address
00274  * @param data register data
00275  * @retval 0 in case of success
00276  * @retval 1 in case of failure
00277  */
00278 int HTS221Sensor::write_reg(uint8_t reg, uint8_t data)
00279 {
00280 
00281     if (HTS221_write_reg((void *)this, reg, 1, &data) == HTS221_ERROR) {
00282         return 1;
00283     }
00284 
00285     return 0;
00286 }
00287 
00288 uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
00289 {
00290     return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
00291 }
00292 
00293 uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
00294 {
00295     return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
00296 }