Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
wobetz
Date:
Wed Jun 03 13:10:04 2015 +0000
Revision:
26:e8bca468b3a6
Parent:
24:92cc9c6e4b2b
Child:
38:e06de1c26727
Use floating point in calls to function 'pow()'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 4:566f2c41dc1d 1 /**
Wolfgang Betz 4:566f2c41dc1d 2 ******************************************************************************
Wolfgang Betz 4:566f2c41dc1d 3 * @file hts221.cpp
Wolfgang Betz 4:566f2c41dc1d 4 * @author AST / EST
Wolfgang Betz 4:566f2c41dc1d 5 * @version V0.0.1
Wolfgang Betz 4:566f2c41dc1d 6 * @date 14-April-2015
Wolfgang Betz 4:566f2c41dc1d 7 * @brief Implementation file for the HTS221 driver class
Wolfgang Betz 4:566f2c41dc1d 8 ******************************************************************************
Wolfgang Betz 4:566f2c41dc1d 9 * @attention
Wolfgang Betz 4:566f2c41dc1d 10 *
Wolfgang Betz 4:566f2c41dc1d 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 4:566f2c41dc1d 12 *
Wolfgang Betz 4:566f2c41dc1d 13 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 4:566f2c41dc1d 14 * are permitted provided that the following conditions are met:
Wolfgang Betz 4:566f2c41dc1d 15 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 4:566f2c41dc1d 16 * this list of conditions and the following disclaimer.
Wolfgang Betz 4:566f2c41dc1d 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 4:566f2c41dc1d 18 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 4:566f2c41dc1d 19 * and/or other materials provided with the distribution.
Wolfgang Betz 4:566f2c41dc1d 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 4:566f2c41dc1d 21 * may be used to endorse or promote products derived from this software
Wolfgang Betz 4:566f2c41dc1d 22 * without specific prior written permission.
Wolfgang Betz 4:566f2c41dc1d 23 *
Wolfgang Betz 4:566f2c41dc1d 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 4:566f2c41dc1d 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 4:566f2c41dc1d 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 4:566f2c41dc1d 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 4:566f2c41dc1d 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 4:566f2c41dc1d 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 4:566f2c41dc1d 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 4:566f2c41dc1d 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 4:566f2c41dc1d 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 4:566f2c41dc1d 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 4:566f2c41dc1d 34 *
Wolfgang Betz 4:566f2c41dc1d 35 ******************************************************************************
Wolfgang Betz 4:566f2c41dc1d 36 */
Wolfgang Betz 4:566f2c41dc1d 37
Wolfgang Betz 4:566f2c41dc1d 38 /* betzw - based on:
Wolfgang Betz 24:92cc9c6e4b2b 39 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/hts221/hts221.c: revision #395,
Wolfgang Betz 24:92cc9c6e4b2b 40 X-CUBE-MEMS1/trunk: revision #402
Wolfgang Betz 4:566f2c41dc1d 41 */
Wolfgang Betz 4:566f2c41dc1d 42
Wolfgang Betz 4:566f2c41dc1d 43 /* Includes ------------------------------------------------------------------*/
Wolfgang Betz 4:566f2c41dc1d 44 #include "mbed.h"
Wolfgang Betz 4:566f2c41dc1d 45 #include "hts221_class.h"
Wolfgang Betz 4:566f2c41dc1d 46 #include "hts221.h"
Wolfgang Betz 4:566f2c41dc1d 47 #include "../../x_nucleo_iks01a1_targets.h"
Wolfgang Betz 4:566f2c41dc1d 48
Wolfgang Betz 4:566f2c41dc1d 49 /* Methods -------------------------------------------------------------------*/
Wolfgang Betz 4:566f2c41dc1d 50 /**
Wolfgang Betz 4:566f2c41dc1d 51 * @brief HTS221 Calibration procedure
Wolfgang Betz 4:566f2c41dc1d 52 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 53 */
Wolfgang Betz 4:566f2c41dc1d 54 HUM_TEMP_StatusTypeDef HTS221::HTS221_Calibration(void)
Wolfgang Betz 4:566f2c41dc1d 55 {
Wolfgang Betz 24:92cc9c6e4b2b 56 /* Temperature Calibration */
Wolfgang Betz 24:92cc9c6e4b2b 57 /* Temperature in degree for calibration ( "/8" to obtain float) */
Wolfgang Betz 24:92cc9c6e4b2b 58 uint16_t T0_degC_x8_L, T0_degC_x8_H, T1_degC_x8_L, T1_degC_x8_H;
Wolfgang Betz 24:92cc9c6e4b2b 59 uint8_t H0_rh_x2, H1_rh_x2;
Wolfgang Betz 24:92cc9c6e4b2b 60 uint8_t tempReg[2] = {0, 0};
Wolfgang Betz 24:92cc9c6e4b2b 61
Wolfgang Betz 24:92cc9c6e4b2b 62 if(HTS221_IO_Read(tempReg, HTS221_T0_degC_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 63 {
Wolfgang Betz 24:92cc9c6e4b2b 64 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 65 }
Wolfgang Betz 24:92cc9c6e4b2b 66
Wolfgang Betz 24:92cc9c6e4b2b 67 T0_degC_x8_L = (uint16_t)tempReg[0];
Wolfgang Betz 24:92cc9c6e4b2b 68
Wolfgang Betz 24:92cc9c6e4b2b 69 if(HTS221_IO_Read(tempReg, HTS221_T1_T0_MSB_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 70 {
Wolfgang Betz 24:92cc9c6e4b2b 71 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 72 }
Wolfgang Betz 24:92cc9c6e4b2b 73
Wolfgang Betz 24:92cc9c6e4b2b 74 T0_degC_x8_H = (uint16_t) (tempReg[0] & 0x03);
Wolfgang Betz 24:92cc9c6e4b2b 75 T0_degC = ((float)((T0_degC_x8_H << 8) | (T0_degC_x8_L))) / 8;
Wolfgang Betz 24:92cc9c6e4b2b 76
Wolfgang Betz 24:92cc9c6e4b2b 77 if(HTS221_IO_Read(tempReg, HTS221_T1_degC_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 78 {
Wolfgang Betz 24:92cc9c6e4b2b 79 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 80 }
Wolfgang Betz 24:92cc9c6e4b2b 81
Wolfgang Betz 24:92cc9c6e4b2b 82 T1_degC_x8_L = (uint16_t)tempReg[0];
Wolfgang Betz 24:92cc9c6e4b2b 83
Wolfgang Betz 24:92cc9c6e4b2b 84 if(HTS221_IO_Read(tempReg, HTS221_T1_T0_MSB_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 85 {
Wolfgang Betz 24:92cc9c6e4b2b 86 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 87 }
Wolfgang Betz 24:92cc9c6e4b2b 88
Wolfgang Betz 24:92cc9c6e4b2b 89 T1_degC_x8_H = (uint16_t) (tempReg[0] & 0x0C);
Wolfgang Betz 24:92cc9c6e4b2b 90 T1_degC_x8_H = T1_degC_x8_H >> 2;
Wolfgang Betz 24:92cc9c6e4b2b 91 T1_degC = ((float)((T1_degC_x8_H << 8) | (T1_degC_x8_L))) / 8;
Wolfgang Betz 24:92cc9c6e4b2b 92
Wolfgang Betz 24:92cc9c6e4b2b 93 if(HTS221_IO_Read(tempReg, (HTS221_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 94 {
Wolfgang Betz 24:92cc9c6e4b2b 95 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 96 }
Wolfgang Betz 24:92cc9c6e4b2b 97
Wolfgang Betz 24:92cc9c6e4b2b 98 T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 99
Wolfgang Betz 24:92cc9c6e4b2b 100 if(HTS221_IO_Read(tempReg, (HTS221_T1_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 101 {
Wolfgang Betz 24:92cc9c6e4b2b 102 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 103 }
Wolfgang Betz 24:92cc9c6e4b2b 104
Wolfgang Betz 24:92cc9c6e4b2b 105 T1_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 106
Wolfgang Betz 24:92cc9c6e4b2b 107 /* Humidity Calibration */
Wolfgang Betz 24:92cc9c6e4b2b 108 /* Humidity in degree for calibration ( "/2" to obtain float) */
Wolfgang Betz 24:92cc9c6e4b2b 109
Wolfgang Betz 24:92cc9c6e4b2b 110 if(HTS221_IO_Read(&H0_rh_x2, HTS221_H0_RH_X2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 111 {
Wolfgang Betz 24:92cc9c6e4b2b 112 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 113 }
Wolfgang Betz 24:92cc9c6e4b2b 114
Wolfgang Betz 24:92cc9c6e4b2b 115 if(HTS221_IO_Read(&H1_rh_x2, HTS221_H1_RH_X2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 116 {
Wolfgang Betz 24:92cc9c6e4b2b 117 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 118 }
Wolfgang Betz 24:92cc9c6e4b2b 119
Wolfgang Betz 24:92cc9c6e4b2b 120 if(HTS221_IO_Read(&tempReg[0], (HTS221_H0_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 121 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 122 {
Wolfgang Betz 24:92cc9c6e4b2b 123 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 124 }
Wolfgang Betz 24:92cc9c6e4b2b 125
Wolfgang Betz 24:92cc9c6e4b2b 126 H0_T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 127
Wolfgang Betz 24:92cc9c6e4b2b 128 if(HTS221_IO_Read(&tempReg[0], (HTS221_H1_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 129 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 130 {
Wolfgang Betz 24:92cc9c6e4b2b 131 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 132 }
Wolfgang Betz 24:92cc9c6e4b2b 133
Wolfgang Betz 24:92cc9c6e4b2b 134 H1_T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 135
Wolfgang Betz 24:92cc9c6e4b2b 136 H0_rh = ((float)H0_rh_x2) / 2;
Wolfgang Betz 24:92cc9c6e4b2b 137 H1_rh = ((float)H1_rh_x2) / 2;
Wolfgang Betz 24:92cc9c6e4b2b 138
Wolfgang Betz 24:92cc9c6e4b2b 139 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 140 }
Wolfgang Betz 4:566f2c41dc1d 141
Wolfgang Betz 4:566f2c41dc1d 142
Wolfgang Betz 4:566f2c41dc1d 143 /**
Wolfgang Betz 4:566f2c41dc1d 144 * @brief Set HTS221 Initialization
Wolfgang Betz 4:566f2c41dc1d 145 * @param HTS221_Init the configuration setting for the HTS221
Wolfgang Betz 4:566f2c41dc1d 146 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 147 */
Wolfgang Betz 4:566f2c41dc1d 148 HUM_TEMP_StatusTypeDef HTS221::HTS221_Init(HUM_TEMP_InitTypeDef *HTS221_Init)
Wolfgang Betz 24:92cc9c6e4b2b 149 {
Wolfgang Betz 24:92cc9c6e4b2b 150 uint8_t tmp = 0x00;
Wolfgang Betz 24:92cc9c6e4b2b 151
Wolfgang Betz 24:92cc9c6e4b2b 152 /* Configure the low level interface ---------------------------------------*/
Wolfgang Betz 24:92cc9c6e4b2b 153 if(HTS221_IO_Init() != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 154 {
Wolfgang Betz 24:92cc9c6e4b2b 155 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 156 }
Wolfgang Betz 24:92cc9c6e4b2b 157
Wolfgang Betz 24:92cc9c6e4b2b 158 if(HTS221_Power_On() != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 159 {
Wolfgang Betz 24:92cc9c6e4b2b 160 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 161 }
Wolfgang Betz 24:92cc9c6e4b2b 162
Wolfgang Betz 24:92cc9c6e4b2b 163 if(HTS221_Calibration() != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 164 {
Wolfgang Betz 24:92cc9c6e4b2b 165 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 166 }
Wolfgang Betz 24:92cc9c6e4b2b 167
Wolfgang Betz 24:92cc9c6e4b2b 168 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 169 {
Wolfgang Betz 24:92cc9c6e4b2b 170 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 171 }
Wolfgang Betz 24:92cc9c6e4b2b 172
Wolfgang Betz 24:92cc9c6e4b2b 173 /* Output Data Rate selection */
Wolfgang Betz 24:92cc9c6e4b2b 174 tmp &= ~(HTS221_ODR_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 175 tmp |= HTS221_Init->OutputDataRate;
Wolfgang Betz 24:92cc9c6e4b2b 176
Wolfgang Betz 24:92cc9c6e4b2b 177 if(HTS221_IO_Write(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 178 {
Wolfgang Betz 24:92cc9c6e4b2b 179 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 180 }
Wolfgang Betz 24:92cc9c6e4b2b 181
Wolfgang Betz 24:92cc9c6e4b2b 182 HTS221_IO_ITConfig();
Wolfgang Betz 24:92cc9c6e4b2b 183
Wolfgang Betz 24:92cc9c6e4b2b 184 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 185 }
Wolfgang Betz 4:566f2c41dc1d 186
Wolfgang Betz 4:566f2c41dc1d 187 /**
Wolfgang Betz 4:566f2c41dc1d 188 * @brief Read ID address of HTS221
Wolfgang Betz 4:566f2c41dc1d 189 * @param ht_id the pointer where the ID of the device is stored
Wolfgang Betz 4:566f2c41dc1d 190 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 191 */
Wolfgang Betz 4:566f2c41dc1d 192 HUM_TEMP_StatusTypeDef HTS221::HTS221_ReadID(uint8_t *ht_id)
Wolfgang Betz 4:566f2c41dc1d 193 {
Wolfgang Betz 24:92cc9c6e4b2b 194 if(!ht_id)
Wolfgang Betz 24:92cc9c6e4b2b 195 {
Wolfgang Betz 24:92cc9c6e4b2b 196 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 197 }
Wolfgang Betz 24:92cc9c6e4b2b 198
Wolfgang Betz 24:92cc9c6e4b2b 199 return HTS221_IO_Read(ht_id, HTS221_WHO_AM_I_ADDR, 1);
Wolfgang Betz 4:566f2c41dc1d 200 }
Wolfgang Betz 4:566f2c41dc1d 201
Wolfgang Betz 4:566f2c41dc1d 202 /**
Wolfgang Betz 4:566f2c41dc1d 203 * @brief Reboot memory content of HTS221
Wolfgang Betz 4:566f2c41dc1d 204 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 205 */
Wolfgang Betz 4:566f2c41dc1d 206 HUM_TEMP_StatusTypeDef HTS221::HTS221_RebootCmd(void)
Wolfgang Betz 4:566f2c41dc1d 207 {
Wolfgang Betz 24:92cc9c6e4b2b 208 uint8_t tmpreg;
Wolfgang Betz 24:92cc9c6e4b2b 209
Wolfgang Betz 24:92cc9c6e4b2b 210 /* Read CTRL_REG2 register */
Wolfgang Betz 24:92cc9c6e4b2b 211 if(HTS221_IO_Read(&tmpreg, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 212 {
Wolfgang Betz 24:92cc9c6e4b2b 213 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 214 }
Wolfgang Betz 24:92cc9c6e4b2b 215
Wolfgang Betz 24:92cc9c6e4b2b 216 /* Enable or Disable the reboot memory */
Wolfgang Betz 24:92cc9c6e4b2b 217 tmpreg |= HTS221_BOOT_REBOOTMEMORY;
Wolfgang Betz 24:92cc9c6e4b2b 218
Wolfgang Betz 24:92cc9c6e4b2b 219 /* Write value to MEMS CTRL_REG2 regsister */
Wolfgang Betz 24:92cc9c6e4b2b 220 if(HTS221_IO_Write(&tmpreg, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 221 {
Wolfgang Betz 24:92cc9c6e4b2b 222 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 223 }
Wolfgang Betz 24:92cc9c6e4b2b 224
Wolfgang Betz 24:92cc9c6e4b2b 225 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 226 }
Wolfgang Betz 4:566f2c41dc1d 227
Wolfgang Betz 4:566f2c41dc1d 228
Wolfgang Betz 4:566f2c41dc1d 229 /**
Wolfgang Betz 4:566f2c41dc1d 230 * @brief Read HTS221 output register, and calculate the humidity
Wolfgang Betz 4:566f2c41dc1d 231 * @param pfData the pointer to data output
Wolfgang Betz 4:566f2c41dc1d 232 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 233 */
Wolfgang Betz 4:566f2c41dc1d 234 HUM_TEMP_StatusTypeDef HTS221::HTS221_GetHumidity(float* pfData)
Wolfgang Betz 4:566f2c41dc1d 235 {
Wolfgang Betz 24:92cc9c6e4b2b 236 int16_t H_T_out, humidity_t;
Wolfgang Betz 24:92cc9c6e4b2b 237 uint8_t tempReg[2] = {0, 0};
Wolfgang Betz 24:92cc9c6e4b2b 238 uint8_t tmp = 0x00;
Wolfgang Betz 24:92cc9c6e4b2b 239 float H_rh;
Wolfgang Betz 24:92cc9c6e4b2b 240
Wolfgang Betz 24:92cc9c6e4b2b 241 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 242 {
Wolfgang Betz 24:92cc9c6e4b2b 243 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 244 }
Wolfgang Betz 24:92cc9c6e4b2b 245
Wolfgang Betz 24:92cc9c6e4b2b 246 /* Output Data Rate selection */
Wolfgang Betz 24:92cc9c6e4b2b 247 tmp &= (HTS221_ODR_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 248
Wolfgang Betz 24:92cc9c6e4b2b 249 if(tmp == 0x00)
Wolfgang Betz 24:92cc9c6e4b2b 250 {
Wolfgang Betz 24:92cc9c6e4b2b 251 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 252 {
Wolfgang Betz 4:566f2c41dc1d 253 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 254 }
Wolfgang Betz 4:566f2c41dc1d 255
Wolfgang Betz 24:92cc9c6e4b2b 256 /* Serial Interface Mode selection */
Wolfgang Betz 24:92cc9c6e4b2b 257 tmp &= ~(HTS221_ONE_SHOT_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 258 tmp |= HTS221_ONE_SHOT_START;
Wolfgang Betz 4:566f2c41dc1d 259
Wolfgang Betz 24:92cc9c6e4b2b 260 if(HTS221_IO_Write(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 261 {
Wolfgang Betz 4:566f2c41dc1d 262 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 263 }
Wolfgang Betz 4:566f2c41dc1d 264
Wolfgang Betz 24:92cc9c6e4b2b 265 do
Wolfgang Betz 24:92cc9c6e4b2b 266 {
Wolfgang Betz 4:566f2c41dc1d 267
Wolfgang Betz 24:92cc9c6e4b2b 268 if(HTS221_IO_Read(&tmp, HTS221_STATUS_REG_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 269 {
Wolfgang Betz 24:92cc9c6e4b2b 270 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 271 }
Wolfgang Betz 24:92cc9c6e4b2b 272
Wolfgang Betz 24:92cc9c6e4b2b 273 }
Wolfgang Betz 24:92cc9c6e4b2b 274 while(!(tmp && 0x02));
Wolfgang Betz 24:92cc9c6e4b2b 275 }
Wolfgang Betz 24:92cc9c6e4b2b 276
Wolfgang Betz 24:92cc9c6e4b2b 277
Wolfgang Betz 24:92cc9c6e4b2b 278 if(HTS221_IO_Read(&tempReg[0], (HTS221_HUMIDITY_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 279 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 280 {
Wolfgang Betz 24:92cc9c6e4b2b 281 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 282 }
Wolfgang Betz 24:92cc9c6e4b2b 283
Wolfgang Betz 24:92cc9c6e4b2b 284 H_T_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 285
Wolfgang Betz 24:92cc9c6e4b2b 286 H_rh = ( float )(((( H_T_out - H0_T0_out ) * ( H1_rh - H0_rh )) / ( H1_T0_out - H0_T0_out )) + H0_rh );
Wolfgang Betz 24:92cc9c6e4b2b 287
Wolfgang Betz 24:92cc9c6e4b2b 288 // Truncate to specific number of decimal digits
wobetz 26:e8bca468b3a6 289 humidity_t = (uint16_t)(H_rh * pow(10.0f, HUM_DECIMAL_DIGITS));
wobetz 26:e8bca468b3a6 290 *pfData = ((float)humidity_t) / pow(10.0f, HUM_DECIMAL_DIGITS);
Wolfgang Betz 24:92cc9c6e4b2b 291
Wolfgang Betz 24:92cc9c6e4b2b 292 // Prevent data going below 0% and above 100% due to linear interpolation
Wolfgang Betz 24:92cc9c6e4b2b 293 if ( *pfData < 0.0f ) *pfData = 0.0f;
Wolfgang Betz 24:92cc9c6e4b2b 294 if ( *pfData > 100.0f ) *pfData = 100.0f;
Wolfgang Betz 24:92cc9c6e4b2b 295
Wolfgang Betz 24:92cc9c6e4b2b 296 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 297 }
Wolfgang Betz 4:566f2c41dc1d 298
Wolfgang Betz 4:566f2c41dc1d 299 /**
Wolfgang Betz 4:566f2c41dc1d 300 * @brief Read HTS221 output register, and calculate the temperature
Wolfgang Betz 4:566f2c41dc1d 301 * @param pfData the pointer to data output
Wolfgang Betz 4:566f2c41dc1d 302 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 303 */
Wolfgang Betz 4:566f2c41dc1d 304 HUM_TEMP_StatusTypeDef HTS221::HTS221_GetTemperature(float* pfData)
Wolfgang Betz 4:566f2c41dc1d 305 {
Wolfgang Betz 24:92cc9c6e4b2b 306 int16_t T_out, temperature_t;
Wolfgang Betz 24:92cc9c6e4b2b 307 uint8_t tempReg[2] = {0, 0};
Wolfgang Betz 24:92cc9c6e4b2b 308 uint8_t tmp = 0x00;
Wolfgang Betz 24:92cc9c6e4b2b 309 float T_degC;
Wolfgang Betz 24:92cc9c6e4b2b 310
Wolfgang Betz 24:92cc9c6e4b2b 311 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 312 {
Wolfgang Betz 24:92cc9c6e4b2b 313 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 314 }
Wolfgang Betz 24:92cc9c6e4b2b 315
Wolfgang Betz 24:92cc9c6e4b2b 316 /* Output Data Rate selection */
Wolfgang Betz 24:92cc9c6e4b2b 317 tmp &= (HTS221_ODR_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 318
Wolfgang Betz 24:92cc9c6e4b2b 319 if(tmp == 0x00)
Wolfgang Betz 24:92cc9c6e4b2b 320 {
Wolfgang Betz 24:92cc9c6e4b2b 321 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 322 {
Wolfgang Betz 4:566f2c41dc1d 323 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 324 }
Wolfgang Betz 4:566f2c41dc1d 325
Wolfgang Betz 24:92cc9c6e4b2b 326 /* Serial Interface Mode selection */
Wolfgang Betz 24:92cc9c6e4b2b 327 tmp &= ~(HTS221_ONE_SHOT_MASK);
Wolfgang Betz 24:92cc9c6e4b2b 328 tmp |= HTS221_ONE_SHOT_START;
Wolfgang Betz 24:92cc9c6e4b2b 329
Wolfgang Betz 24:92cc9c6e4b2b 330 if(HTS221_IO_Write(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 331 {
Wolfgang Betz 24:92cc9c6e4b2b 332 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 333 }
Wolfgang Betz 24:92cc9c6e4b2b 334
Wolfgang Betz 24:92cc9c6e4b2b 335 do
Wolfgang Betz 24:92cc9c6e4b2b 336 {
Wolfgang Betz 4:566f2c41dc1d 337
Wolfgang Betz 24:92cc9c6e4b2b 338 if(HTS221_IO_Read(&tmp, HTS221_STATUS_REG_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 339 {
Wolfgang Betz 24:92cc9c6e4b2b 340 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 341 }
Wolfgang Betz 24:92cc9c6e4b2b 342
Wolfgang Betz 24:92cc9c6e4b2b 343 }
Wolfgang Betz 24:92cc9c6e4b2b 344 while(!(tmp && 0x01));
Wolfgang Betz 24:92cc9c6e4b2b 345 }
Wolfgang Betz 24:92cc9c6e4b2b 346
Wolfgang Betz 24:92cc9c6e4b2b 347 if(HTS221_IO_Read(&tempReg[0], (HTS221_TEMP_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD),
Wolfgang Betz 24:92cc9c6e4b2b 348 2) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 349 {
Wolfgang Betz 24:92cc9c6e4b2b 350 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 351 }
Wolfgang Betz 24:92cc9c6e4b2b 352
Wolfgang Betz 24:92cc9c6e4b2b 353 T_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
Wolfgang Betz 24:92cc9c6e4b2b 354
Wolfgang Betz 24:92cc9c6e4b2b 355 T_degC = ((float)(T_out - T0_out)) / (T1_out - T0_out) * (T1_degC - T0_degC) + T0_degC;
Wolfgang Betz 24:92cc9c6e4b2b 356
wobetz 26:e8bca468b3a6 357 temperature_t = (int16_t)(T_degC * pow(10.0f, TEMP_DECIMAL_DIGITS));
Wolfgang Betz 24:92cc9c6e4b2b 358
wobetz 26:e8bca468b3a6 359 *pfData = ((float)temperature_t) / pow(10.0f, TEMP_DECIMAL_DIGITS);
Wolfgang Betz 24:92cc9c6e4b2b 360
Wolfgang Betz 24:92cc9c6e4b2b 361 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 362 }
Wolfgang Betz 4:566f2c41dc1d 363
Wolfgang Betz 4:566f2c41dc1d 364
Wolfgang Betz 4:566f2c41dc1d 365 /**
Wolfgang Betz 4:566f2c41dc1d 366 * @brief Exit the shutdown mode for HTS221
Wolfgang Betz 4:566f2c41dc1d 367 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 368 */
Wolfgang Betz 4:566f2c41dc1d 369 HUM_TEMP_StatusTypeDef HTS221::HTS221_Power_On(void)
Wolfgang Betz 4:566f2c41dc1d 370 {
Wolfgang Betz 24:92cc9c6e4b2b 371 uint8_t tmpReg;
Wolfgang Betz 24:92cc9c6e4b2b 372
Wolfgang Betz 24:92cc9c6e4b2b 373 /* Read the register content */
Wolfgang Betz 24:92cc9c6e4b2b 374 if(HTS221_IO_Read(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 375 {
Wolfgang Betz 24:92cc9c6e4b2b 376 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 377 }
Wolfgang Betz 24:92cc9c6e4b2b 378
Wolfgang Betz 24:92cc9c6e4b2b 379 /* Set the power down bit */
Wolfgang Betz 24:92cc9c6e4b2b 380 tmpReg |= HTS221_MODE_ACTIVE;
Wolfgang Betz 24:92cc9c6e4b2b 381
Wolfgang Betz 24:92cc9c6e4b2b 382 /* Write register */
Wolfgang Betz 24:92cc9c6e4b2b 383 if(HTS221_IO_Write(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 384 {
Wolfgang Betz 24:92cc9c6e4b2b 385 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 386 }
Wolfgang Betz 24:92cc9c6e4b2b 387
Wolfgang Betz 24:92cc9c6e4b2b 388 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 389 }
Wolfgang Betz 4:566f2c41dc1d 390
Wolfgang Betz 4:566f2c41dc1d 391 /**
Wolfgang Betz 4:566f2c41dc1d 392 * @brief Enter the shutdown mode for HTS221
Wolfgang Betz 4:566f2c41dc1d 393 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 394 */
Wolfgang Betz 4:566f2c41dc1d 395 HUM_TEMP_StatusTypeDef HTS221::HTS221_Power_OFF(void)
Wolfgang Betz 4:566f2c41dc1d 396 {
Wolfgang Betz 24:92cc9c6e4b2b 397 uint8_t tmpReg;
Wolfgang Betz 24:92cc9c6e4b2b 398
Wolfgang Betz 24:92cc9c6e4b2b 399 /* Read the register content */
Wolfgang Betz 24:92cc9c6e4b2b 400 if(HTS221_IO_Read(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 401 {
Wolfgang Betz 24:92cc9c6e4b2b 402 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 403 }
Wolfgang Betz 24:92cc9c6e4b2b 404
Wolfgang Betz 24:92cc9c6e4b2b 405 /* Reset the power down bit */
Wolfgang Betz 24:92cc9c6e4b2b 406 tmpReg &= ~(HTS221_MODE_ACTIVE);
Wolfgang Betz 24:92cc9c6e4b2b 407
Wolfgang Betz 24:92cc9c6e4b2b 408 /* Write register */
Wolfgang Betz 24:92cc9c6e4b2b 409 if(HTS221_IO_Write(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 24:92cc9c6e4b2b 410 {
Wolfgang Betz 24:92cc9c6e4b2b 411 return HUM_TEMP_ERROR;
Wolfgang Betz 24:92cc9c6e4b2b 412 }
Wolfgang Betz 24:92cc9c6e4b2b 413
Wolfgang Betz 24:92cc9c6e4b2b 414 return HUM_TEMP_OK;
Wolfgang Betz 24:92cc9c6e4b2b 415 }
Wolfgang Betz 4:566f2c41dc1d 416
Wolfgang Betz 24:92cc9c6e4b2b 417 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/