Beta

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A2 by ST

Committer:
nikapov
Date:
Mon Nov 28 13:16:22 2016 +0000
Revision:
0:cad5dab2b21d
Child:
1:bd2a01e81e6f
X_NUCLEO_IKS01A2 lib created.

Who changed what in which revision?

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