Capacitive digital sensor for relative humidity and temperature.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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

Committer:
cparata
Date:
Wed Jul 24 14:17:21 2019 +0000
Revision:
5:ccf7f36492ae
Parent:
2:312ee2694a77
Format with Astyle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:7917d6d00a6e 1 /**
nikapov 0:7917d6d00a6e 2 ******************************************************************************
nikapov 0:7917d6d00a6e 3 * @file HTS221Sensor.cpp
nikapov 0:7917d6d00a6e 4 * @author CLab
nikapov 0:7917d6d00a6e 5 * @version V1.0.0
nikapov 0:7917d6d00a6e 6 * @date 5 August 2016
nikapov 0:7917d6d00a6e 7 * @brief Implementation of an HTS221 Humidity and Temperature sensor.
nikapov 0:7917d6d00a6e 8 ******************************************************************************
nikapov 0:7917d6d00a6e 9 * @attention
nikapov 0:7917d6d00a6e 10 *
nikapov 0:7917d6d00a6e 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
nikapov 0:7917d6d00a6e 12 *
nikapov 0:7917d6d00a6e 13 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:7917d6d00a6e 14 * are permitted provided that the following conditions are met:
nikapov 0:7917d6d00a6e 15 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:7917d6d00a6e 16 * this list of conditions and the following disclaimer.
nikapov 0:7917d6d00a6e 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:7917d6d00a6e 18 * this list of conditions and the following disclaimer in the documentation
nikapov 0:7917d6d00a6e 19 * and/or other materials provided with the distribution.
nikapov 0:7917d6d00a6e 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:7917d6d00a6e 21 * may be used to endorse or promote products derived from this software
nikapov 0:7917d6d00a6e 22 * without specific prior written permission.
nikapov 0:7917d6d00a6e 23 *
nikapov 0:7917d6d00a6e 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:7917d6d00a6e 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:7917d6d00a6e 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:7917d6d00a6e 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:7917d6d00a6e 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:7917d6d00a6e 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:7917d6d00a6e 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:7917d6d00a6e 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:7917d6d00a6e 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:7917d6d00a6e 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:7917d6d00a6e 34 *
nikapov 0:7917d6d00a6e 35 ******************************************************************************
nikapov 0:7917d6d00a6e 36 */
nikapov 0:7917d6d00a6e 37
nikapov 0:7917d6d00a6e 38
nikapov 0:7917d6d00a6e 39 /* Includes ------------------------------------------------------------------*/
nikapov 0:7917d6d00a6e 40
nikapov 0:7917d6d00a6e 41
nikapov 0:7917d6d00a6e 42 #include "HTS221Sensor.h"
nikapov 0:7917d6d00a6e 43
nikapov 0:7917d6d00a6e 44
nikapov 0:7917d6d00a6e 45 /* Class Implementation ------------------------------------------------------*/
nikapov 0:7917d6d00a6e 46
cparata 5:ccf7f36492ae 47 HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) :
cparata 5:ccf7f36492ae 48 _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin) // SPI3W ONLY
cparata 5:ccf7f36492ae 49 {
cparata 5:ccf7f36492ae 50 assert(spi);
mapellil 2:312ee2694a77 51 _dev_i2c = NULL;
mapellil 2:312ee2694a77 52 };
mapellil 2:312ee2694a77 53
nikapov 0:7917d6d00a6e 54 /** Constructor
nikapov 0:7917d6d00a6e 55 * @param i2c object of an helper class which handles the I2C peripheral
nikapov 0:7917d6d00a6e 56 * @param address the address of the component's instance
nikapov 0:7917d6d00a6e 57 */
mapellil 2:312ee2694a77 58 HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) :
cparata 5:ccf7f36492ae 59 _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin)
nikapov 0:7917d6d00a6e 60 {
mapellil 2:312ee2694a77 61 assert(i2c);
mapellil 2:312ee2694a77 62 _dev_spi = NULL;
nikapov 0:7917d6d00a6e 63 };
nikapov 0:7917d6d00a6e 64
nikapov 0:7917d6d00a6e 65 /**
nikapov 0:7917d6d00a6e 66 * @brief Initializing the component.
nikapov 0:7917d6d00a6e 67 * @param[in] init pointer to device specific initalization structure.
nikapov 0:7917d6d00a6e 68 * @retval "0" in case of success, an error code otherwise.
nikapov 0:7917d6d00a6e 69 */
nikapov 0:7917d6d00a6e 70 int HTS221Sensor::init(void *init)
nikapov 0:7917d6d00a6e 71 {
cparata 5:ccf7f36492ae 72 /* Power down the device */
cparata 5:ccf7f36492ae 73 if (HTS221_DeActivate((void *)this) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 74 return 1;
cparata 5:ccf7f36492ae 75 }
nikapov 0:7917d6d00a6e 76
cparata 5:ccf7f36492ae 77 /* Enable BDU */
cparata 5:ccf7f36492ae 78 if (HTS221_Set_BduMode((void *)this, HTS221_ENABLE) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 79 return 1;
cparata 5:ccf7f36492ae 80 }
cparata 5:ccf7f36492ae 81
cparata 5:ccf7f36492ae 82 if (set_odr(1.0f) == 1) {
cparata 5:ccf7f36492ae 83 return 1;
cparata 5:ccf7f36492ae 84 }
cparata 5:ccf7f36492ae 85
cparata 5:ccf7f36492ae 86 return 0;
nikapov 0:7917d6d00a6e 87 }
nikapov 0:7917d6d00a6e 88
nikapov 0:7917d6d00a6e 89 /**
nikapov 0:7917d6d00a6e 90 * @brief Enable HTS221
nikapov 0:7917d6d00a6e 91 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 92 */
nikapov 0:7917d6d00a6e 93 int HTS221Sensor::enable(void)
nikapov 0:7917d6d00a6e 94 {
cparata 5:ccf7f36492ae 95 /* Power up the device */
cparata 5:ccf7f36492ae 96 if (HTS221_Activate((void *)this) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 97 return 1;
cparata 5:ccf7f36492ae 98 }
nikapov 0:7917d6d00a6e 99
cparata 5:ccf7f36492ae 100 return 0;
nikapov 0:7917d6d00a6e 101 }
nikapov 0:7917d6d00a6e 102
nikapov 0:7917d6d00a6e 103 /**
nikapov 0:7917d6d00a6e 104 * @brief Disable HTS221
nikapov 0:7917d6d00a6e 105 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 106 */
nikapov 0:7917d6d00a6e 107 int HTS221Sensor::disable(void)
nikapov 0:7917d6d00a6e 108 {
cparata 5:ccf7f36492ae 109 /* Power up the device */
cparata 5:ccf7f36492ae 110 if (HTS221_DeActivate((void *)this) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 111 return 1;
cparata 5:ccf7f36492ae 112 }
nikapov 0:7917d6d00a6e 113
cparata 5:ccf7f36492ae 114 return 0;
nikapov 0:7917d6d00a6e 115 }
nikapov 0:7917d6d00a6e 116
nikapov 0:7917d6d00a6e 117 /**
nikapov 0:7917d6d00a6e 118 * @brief Read ID address of HTS221
nikapov 0:7917d6d00a6e 119 * @param id the pointer where the ID of the device is stored
nikapov 0:7917d6d00a6e 120 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 121 */
nikapov 0:7917d6d00a6e 122 int HTS221Sensor::read_id(uint8_t *id)
nikapov 0:7917d6d00a6e 123 {
cparata 5:ccf7f36492ae 124 if (!id) {
cparata 5:ccf7f36492ae 125 return 1;
cparata 5:ccf7f36492ae 126 }
nikapov 0:7917d6d00a6e 127
cparata 5:ccf7f36492ae 128 /* Read WHO AM I register */
cparata 5:ccf7f36492ae 129 if (HTS221_Get_DeviceID((void *)this, id) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 130 return 1;
cparata 5:ccf7f36492ae 131 }
cparata 5:ccf7f36492ae 132
cparata 5:ccf7f36492ae 133 return 0;
nikapov 0:7917d6d00a6e 134 }
nikapov 0:7917d6d00a6e 135
nikapov 0:7917d6d00a6e 136 /**
nikapov 0:7917d6d00a6e 137 * @brief Reboot memory content of HTS221
nikapov 0:7917d6d00a6e 138 * @param None
nikapov 0:7917d6d00a6e 139 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 140 */
nikapov 0:7917d6d00a6e 141 int HTS221Sensor::reset(void)
nikapov 0:7917d6d00a6e 142 {
nikapov 0:7917d6d00a6e 143 uint8_t tmpreg;
nikapov 0:7917d6d00a6e 144
nikapov 0:7917d6d00a6e 145 /* Read CTRL_REG2 register */
cparata 5:ccf7f36492ae 146 if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0) {
cparata 5:ccf7f36492ae 147 return 1;
nikapov 0:7917d6d00a6e 148 }
nikapov 0:7917d6d00a6e 149
nikapov 0:7917d6d00a6e 150 /* Enable or Disable the reboot memory */
nikapov 0:7917d6d00a6e 151 tmpreg |= (0x01 << HTS221_BOOT_BIT);
nikapov 0:7917d6d00a6e 152
nikapov 0:7917d6d00a6e 153 /* Write value to MEMS CTRL_REG2 regsister */
cparata 5:ccf7f36492ae 154 if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0) {
cparata 5:ccf7f36492ae 155 return 1;
nikapov 0:7917d6d00a6e 156 }
cparata 5:ccf7f36492ae 157
nikapov 0:7917d6d00a6e 158 return 0;
nikapov 0:7917d6d00a6e 159 }
nikapov 0:7917d6d00a6e 160
nikapov 0:7917d6d00a6e 161 /**
nikapov 0:7917d6d00a6e 162 * @brief Read HTS221 output register, and calculate the humidity
nikapov 0:7917d6d00a6e 163 * @param pfData the pointer to data output
nikapov 0:7917d6d00a6e 164 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 165 */
cparata 5:ccf7f36492ae 166 int HTS221Sensor::get_humidity(float *pfData)
nikapov 0:7917d6d00a6e 167 {
cparata 5:ccf7f36492ae 168 uint16_t uint16data = 0;
nikapov 0:7917d6d00a6e 169
cparata 5:ccf7f36492ae 170 /* Read data from HTS221. */
cparata 5:ccf7f36492ae 171 if (HTS221_Get_Humidity((void *)this, &uint16data) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 172 return 1;
cparata 5:ccf7f36492ae 173 }
nikapov 0:7917d6d00a6e 174
cparata 5:ccf7f36492ae 175 *pfData = (float)uint16data / 10.0f;
nikapov 0:7917d6d00a6e 176
cparata 5:ccf7f36492ae 177 return 0;
nikapov 0:7917d6d00a6e 178 }
nikapov 0:7917d6d00a6e 179
nikapov 0:7917d6d00a6e 180 /**
nikapov 0:7917d6d00a6e 181 * @brief Read HTS221 output register, and calculate the temperature
nikapov 0:7917d6d00a6e 182 * @param pfData the pointer to data output
nikapov 0:7917d6d00a6e 183 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 184 */
cparata 5:ccf7f36492ae 185 int HTS221Sensor::get_temperature(float *pfData)
nikapov 0:7917d6d00a6e 186 {
cparata 5:ccf7f36492ae 187 int16_t int16data = 0;
nikapov 0:7917d6d00a6e 188
cparata 5:ccf7f36492ae 189 /* Read data from HTS221. */
cparata 5:ccf7f36492ae 190 if (HTS221_Get_Temperature((void *)this, &int16data) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 191 return 1;
cparata 5:ccf7f36492ae 192 }
nikapov 0:7917d6d00a6e 193
cparata 5:ccf7f36492ae 194 *pfData = (float)int16data / 10.0f;
nikapov 0:7917d6d00a6e 195
cparata 5:ccf7f36492ae 196 return 0;
nikapov 0:7917d6d00a6e 197 }
nikapov 0:7917d6d00a6e 198
nikapov 0:7917d6d00a6e 199 /**
nikapov 0:7917d6d00a6e 200 * @brief Read HTS221 output register, and calculate the humidity
nikapov 0:7917d6d00a6e 201 * @param odr the pointer to the output data rate
nikapov 0:7917d6d00a6e 202 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 203 */
cparata 5:ccf7f36492ae 204 int HTS221Sensor::get_odr(float *odr)
nikapov 0:7917d6d00a6e 205 {
cparata 5:ccf7f36492ae 206 HTS221_Odr_et odr_low_level;
nikapov 0:7917d6d00a6e 207
cparata 5:ccf7f36492ae 208 if (HTS221_Get_Odr((void *)this, &odr_low_level) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 209 return 1;
cparata 5:ccf7f36492ae 210 }
nikapov 0:7917d6d00a6e 211
cparata 5:ccf7f36492ae 212 switch (odr_low_level) {
cparata 5:ccf7f36492ae 213 case HTS221_ODR_ONE_SHOT:
cparata 5:ccf7f36492ae 214 *odr = 0.0f;
cparata 5:ccf7f36492ae 215 break;
cparata 5:ccf7f36492ae 216 case HTS221_ODR_1HZ :
cparata 5:ccf7f36492ae 217 *odr = 1.0f;
cparata 5:ccf7f36492ae 218 break;
cparata 5:ccf7f36492ae 219 case HTS221_ODR_7HZ :
cparata 5:ccf7f36492ae 220 *odr = 7.0f;
cparata 5:ccf7f36492ae 221 break;
cparata 5:ccf7f36492ae 222 case HTS221_ODR_12_5HZ :
cparata 5:ccf7f36492ae 223 *odr = 12.5f;
cparata 5:ccf7f36492ae 224 break;
cparata 5:ccf7f36492ae 225 default :
cparata 5:ccf7f36492ae 226 *odr = -1.0f;
cparata 5:ccf7f36492ae 227 return 1;
cparata 5:ccf7f36492ae 228 }
nikapov 0:7917d6d00a6e 229
cparata 5:ccf7f36492ae 230 return 0;
nikapov 0:7917d6d00a6e 231 }
nikapov 0:7917d6d00a6e 232
nikapov 0:7917d6d00a6e 233 /**
nikapov 0:7917d6d00a6e 234 * @brief Set ODR
nikapov 0:7917d6d00a6e 235 * @param odr the output data rate to be set
nikapov 0:7917d6d00a6e 236 * @retval 0 in case of success, an error code otherwise
nikapov 0:7917d6d00a6e 237 */
nikapov 0:7917d6d00a6e 238 int HTS221Sensor::set_odr(float odr)
nikapov 0:7917d6d00a6e 239 {
cparata 5:ccf7f36492ae 240 HTS221_Odr_et new_odr;
nikapov 0:7917d6d00a6e 241
cparata 5:ccf7f36492ae 242 new_odr = (odr <= 1.0f) ? HTS221_ODR_1HZ
cparata 5:ccf7f36492ae 243 : (odr <= 7.0f) ? HTS221_ODR_7HZ
cparata 5:ccf7f36492ae 244 : HTS221_ODR_12_5HZ;
nikapov 0:7917d6d00a6e 245
cparata 5:ccf7f36492ae 246 if (HTS221_Set_Odr((void *)this, new_odr) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 247 return 1;
cparata 5:ccf7f36492ae 248 }
nikapov 0:7917d6d00a6e 249
cparata 5:ccf7f36492ae 250 return 0;
nikapov 0:7917d6d00a6e 251 }
nikapov 0:7917d6d00a6e 252
nikapov 0:7917d6d00a6e 253
nikapov 0:7917d6d00a6e 254 /**
nikapov 0:7917d6d00a6e 255 * @brief Read the data from register
nikapov 0:7917d6d00a6e 256 * @param reg register address
nikapov 0:7917d6d00a6e 257 * @param data register data
nikapov 0:7917d6d00a6e 258 * @retval 0 in case of success
nikapov 0:7917d6d00a6e 259 * @retval 1 in case of failure
nikapov 0:7917d6d00a6e 260 */
cparata 5:ccf7f36492ae 261 int HTS221Sensor::read_reg(uint8_t reg, uint8_t *data)
nikapov 0:7917d6d00a6e 262 {
nikapov 0:7917d6d00a6e 263
cparata 5:ccf7f36492ae 264 if (HTS221_read_reg((void *)this, reg, 1, data) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 265 return 1;
cparata 5:ccf7f36492ae 266 }
nikapov 0:7917d6d00a6e 267
cparata 5:ccf7f36492ae 268 return 0;
nikapov 0:7917d6d00a6e 269 }
nikapov 0:7917d6d00a6e 270
nikapov 0:7917d6d00a6e 271 /**
nikapov 0:7917d6d00a6e 272 * @brief Write the data to register
nikapov 0:7917d6d00a6e 273 * @param reg register address
nikapov 0:7917d6d00a6e 274 * @param data register data
nikapov 0:7917d6d00a6e 275 * @retval 0 in case of success
nikapov 0:7917d6d00a6e 276 * @retval 1 in case of failure
nikapov 0:7917d6d00a6e 277 */
cparata 5:ccf7f36492ae 278 int HTS221Sensor::write_reg(uint8_t reg, uint8_t data)
nikapov 0:7917d6d00a6e 279 {
nikapov 0:7917d6d00a6e 280
cparata 5:ccf7f36492ae 281 if (HTS221_write_reg((void *)this, reg, 1, &data) == HTS221_ERROR) {
cparata 5:ccf7f36492ae 282 return 1;
cparata 5:ccf7f36492ae 283 }
nikapov 0:7917d6d00a6e 284
cparata 5:ccf7f36492ae 285 return 0;
nikapov 0:7917d6d00a6e 286 }
nikapov 0:7917d6d00a6e 287
cparata 5:ccf7f36492ae 288 uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
nikapov 0:7917d6d00a6e 289 {
cparata 5:ccf7f36492ae 290 return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
nikapov 0:7917d6d00a6e 291 }
nikapov 0:7917d6d00a6e 292
cparata 5:ccf7f36492ae 293 uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
nikapov 0:7917d6d00a6e 294 {
cparata 5:ccf7f36492ae 295 return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
nikapov 0:7917d6d00a6e 296 }