Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
wobetz
Date:
Fri May 22 09:39:04 2015 +0000
Revision:
11:001a21c6ac1d
Parent:
4:566f2c41dc1d
Child:
24:92cc9c6e4b2b
Make it compile also in mbed compiler;

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 4:566f2c41dc1d 39 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/hts221/hts221.c: revision #270,
Wolfgang Betz 4:566f2c41dc1d 40 X-CUBE-MEMS1/trunk: revision #293
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 * @param None
Wolfgang Betz 4:566f2c41dc1d 53 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 54 */
Wolfgang Betz 4:566f2c41dc1d 55 HUM_TEMP_StatusTypeDef HTS221::HTS221_Calibration(void)
Wolfgang Betz 4:566f2c41dc1d 56 {
Wolfgang Betz 4:566f2c41dc1d 57 /* Temperature Calibration */
Wolfgang Betz 4:566f2c41dc1d 58 /* Temperature in degree for calibration ( "/8" to obtain float) */
Wolfgang Betz 4:566f2c41dc1d 59 uint16_t T0_degC_x8_L, T0_degC_x8_H, T1_degC_x8_L, T1_degC_x8_H;
Wolfgang Betz 4:566f2c41dc1d 60 uint8_t H0_rh_x2, H1_rh_x2;
Wolfgang Betz 4:566f2c41dc1d 61 uint8_t tempReg[2] = {0,0};
Wolfgang Betz 4:566f2c41dc1d 62
Wolfgang Betz 4:566f2c41dc1d 63 if(HTS221_IO_Read(tempReg, HTS221_T0_degC_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 64 {
Wolfgang Betz 4:566f2c41dc1d 65 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 66 }
Wolfgang Betz 4:566f2c41dc1d 67
Wolfgang Betz 4:566f2c41dc1d 68 T0_degC_x8_L = (uint16_t)tempReg[0];
Wolfgang Betz 4:566f2c41dc1d 69
Wolfgang Betz 4:566f2c41dc1d 70 if(HTS221_IO_Read(tempReg, HTS221_T1_T0_MSB_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 71 {
Wolfgang Betz 4:566f2c41dc1d 72 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 73 }
Wolfgang Betz 4:566f2c41dc1d 74
Wolfgang Betz 4:566f2c41dc1d 75 T0_degC_x8_H = (uint16_t) (tempReg[0] & 0x03);
Wolfgang Betz 4:566f2c41dc1d 76 T0_degC = ((float)((T0_degC_x8_H<<8) | (T0_degC_x8_L)))/8;
Wolfgang Betz 4:566f2c41dc1d 77
Wolfgang Betz 4:566f2c41dc1d 78 if(HTS221_IO_Read(tempReg, HTS221_T1_degC_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 79 {
Wolfgang Betz 4:566f2c41dc1d 80 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 81 }
Wolfgang Betz 4:566f2c41dc1d 82
Wolfgang Betz 4:566f2c41dc1d 83 T1_degC_x8_L = (uint16_t)tempReg[0];
Wolfgang Betz 4:566f2c41dc1d 84
Wolfgang Betz 4:566f2c41dc1d 85 if(HTS221_IO_Read(tempReg, HTS221_T1_T0_MSB_X8_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 86 {
Wolfgang Betz 4:566f2c41dc1d 87 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 88 }
Wolfgang Betz 4:566f2c41dc1d 89
Wolfgang Betz 4:566f2c41dc1d 90 T1_degC_x8_H = (uint16_t) (tempReg[0] & 0x0C);
Wolfgang Betz 4:566f2c41dc1d 91 T1_degC_x8_H = T1_degC_x8_H >> 2;
Wolfgang Betz 4:566f2c41dc1d 92 T1_degC = ((float)((T1_degC_x8_H<<8) | (T1_degC_x8_L)))/8;
Wolfgang Betz 4:566f2c41dc1d 93
Wolfgang Betz 4:566f2c41dc1d 94 if(HTS221_IO_Read(tempReg, (HTS221_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 95 {
Wolfgang Betz 4:566f2c41dc1d 96 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 97 }
Wolfgang Betz 4:566f2c41dc1d 98
Wolfgang Betz 4:566f2c41dc1d 99 T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 100
Wolfgang Betz 4:566f2c41dc1d 101 if(HTS221_IO_Read(tempReg, (HTS221_T1_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 102 {
Wolfgang Betz 4:566f2c41dc1d 103 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 104 }
Wolfgang Betz 4:566f2c41dc1d 105
Wolfgang Betz 4:566f2c41dc1d 106 T1_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 107
Wolfgang Betz 4:566f2c41dc1d 108 /* Humidity Calibration */
Wolfgang Betz 4:566f2c41dc1d 109 /* Humidity in degree for calibration ( "/2" to obtain float) */
Wolfgang Betz 4:566f2c41dc1d 110
Wolfgang Betz 4:566f2c41dc1d 111 if(HTS221_IO_Read(&H0_rh_x2, HTS221_H0_RH_X2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 112 {
Wolfgang Betz 4:566f2c41dc1d 113 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 114 }
Wolfgang Betz 4:566f2c41dc1d 115
Wolfgang Betz 4:566f2c41dc1d 116 if(HTS221_IO_Read(&H1_rh_x2, HTS221_H1_RH_X2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 117 {
Wolfgang Betz 4:566f2c41dc1d 118 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 119 }
Wolfgang Betz 4:566f2c41dc1d 120
Wolfgang Betz 4:566f2c41dc1d 121 if(HTS221_IO_Read(&tempReg[0], (HTS221_H0_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 122 {
Wolfgang Betz 4:566f2c41dc1d 123 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 124 }
Wolfgang Betz 4:566f2c41dc1d 125
Wolfgang Betz 4:566f2c41dc1d 126 H0_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 127
Wolfgang Betz 4:566f2c41dc1d 128 if(HTS221_IO_Read(&tempReg[0], (HTS221_H1_T0_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 129 {
Wolfgang Betz 4:566f2c41dc1d 130 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 131 }
Wolfgang Betz 4:566f2c41dc1d 132
Wolfgang Betz 4:566f2c41dc1d 133 H1_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 134
Wolfgang Betz 4:566f2c41dc1d 135 H0_rh = ((float)H0_rh_x2)/2;
Wolfgang Betz 4:566f2c41dc1d 136 H1_rh = ((float)H1_rh_x2)/2;
Wolfgang Betz 4:566f2c41dc1d 137
Wolfgang Betz 4:566f2c41dc1d 138 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 139 }
Wolfgang Betz 4:566f2c41dc1d 140
Wolfgang Betz 4:566f2c41dc1d 141
Wolfgang Betz 4:566f2c41dc1d 142 /**
Wolfgang Betz 4:566f2c41dc1d 143 * @brief Set HTS221 Initialization
Wolfgang Betz 4:566f2c41dc1d 144 * @param HTS221_Init the configuration setting for the HTS221
Wolfgang Betz 4:566f2c41dc1d 145 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 146 */
Wolfgang Betz 4:566f2c41dc1d 147 HUM_TEMP_StatusTypeDef HTS221::HTS221_Init(HUM_TEMP_InitTypeDef *HTS221_Init)
Wolfgang Betz 4:566f2c41dc1d 148 {
Wolfgang Betz 4:566f2c41dc1d 149 uint8_t tmp = 0x00;
Wolfgang Betz 4:566f2c41dc1d 150
Wolfgang Betz 4:566f2c41dc1d 151 /* Configure the low level interface ---------------------------------------*/
Wolfgang Betz 4:566f2c41dc1d 152 if(HTS221_IO_Init() != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 153 {
Wolfgang Betz 4:566f2c41dc1d 154 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 155 }
Wolfgang Betz 4:566f2c41dc1d 156
Wolfgang Betz 4:566f2c41dc1d 157 if(HTS221_Power_On() != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 158 {
Wolfgang Betz 4:566f2c41dc1d 159 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 160 }
Wolfgang Betz 4:566f2c41dc1d 161
Wolfgang Betz 4:566f2c41dc1d 162 if(HTS221_Calibration() != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 163 {
Wolfgang Betz 4:566f2c41dc1d 164 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 165 }
Wolfgang Betz 4:566f2c41dc1d 166
Wolfgang Betz 4:566f2c41dc1d 167 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 168 {
Wolfgang Betz 4:566f2c41dc1d 169 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 170 }
Wolfgang Betz 4:566f2c41dc1d 171
Wolfgang Betz 4:566f2c41dc1d 172 /* Output Data Rate selection */
Wolfgang Betz 4:566f2c41dc1d 173 tmp &= ~(HTS221_ODR_MASK);
Wolfgang Betz 4:566f2c41dc1d 174 tmp |= HTS221_Init->OutputDataRate;
Wolfgang Betz 4:566f2c41dc1d 175
Wolfgang Betz 4:566f2c41dc1d 176 if(HTS221_IO_Write(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 177 {
Wolfgang Betz 4:566f2c41dc1d 178 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 179 }
Wolfgang Betz 4:566f2c41dc1d 180
Wolfgang Betz 4:566f2c41dc1d 181 HTS221_IO_ITConfig();
Wolfgang Betz 4:566f2c41dc1d 182
Wolfgang Betz 4:566f2c41dc1d 183 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 184 }
Wolfgang Betz 4:566f2c41dc1d 185
Wolfgang Betz 4:566f2c41dc1d 186 /**
Wolfgang Betz 4:566f2c41dc1d 187 * @brief Read ID address of HTS221
Wolfgang Betz 4:566f2c41dc1d 188 * @param ht_id the pointer where the ID of the device is stored
Wolfgang Betz 4:566f2c41dc1d 189 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 190 */
Wolfgang Betz 4:566f2c41dc1d 191 HUM_TEMP_StatusTypeDef HTS221::HTS221_ReadID(uint8_t *ht_id)
Wolfgang Betz 4:566f2c41dc1d 192 {
Wolfgang Betz 4:566f2c41dc1d 193 if(!ht_id)
Wolfgang Betz 4:566f2c41dc1d 194 {
Wolfgang Betz 4:566f2c41dc1d 195 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 196 }
Wolfgang Betz 4:566f2c41dc1d 197
Wolfgang Betz 4:566f2c41dc1d 198 return HTS221_IO_Read(ht_id, HTS221_WHO_AM_I_ADDR, 1);
Wolfgang Betz 4:566f2c41dc1d 199 }
Wolfgang Betz 4:566f2c41dc1d 200
Wolfgang Betz 4:566f2c41dc1d 201 /**
Wolfgang Betz 4:566f2c41dc1d 202 * @brief Reboot memory content of HTS221
Wolfgang Betz 4:566f2c41dc1d 203 * @param None
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 4:566f2c41dc1d 208 uint8_t tmpreg;
Wolfgang Betz 4:566f2c41dc1d 209
Wolfgang Betz 4:566f2c41dc1d 210 /* Read CTRL_REG2 register */
Wolfgang Betz 4:566f2c41dc1d 211 if(HTS221_IO_Read(&tmpreg, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 212 {
Wolfgang Betz 4:566f2c41dc1d 213 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 214 }
Wolfgang Betz 4:566f2c41dc1d 215
Wolfgang Betz 4:566f2c41dc1d 216 /* Enable or Disable the reboot memory */
Wolfgang Betz 4:566f2c41dc1d 217 tmpreg |= HTS221_BOOT_REBOOTMEMORY;
Wolfgang Betz 4:566f2c41dc1d 218
Wolfgang Betz 4:566f2c41dc1d 219 /* Write value to MEMS CTRL_REG2 regsister */
Wolfgang Betz 4:566f2c41dc1d 220 if(HTS221_IO_Write(&tmpreg, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 221 {
Wolfgang Betz 4:566f2c41dc1d 222 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 223 }
Wolfgang Betz 4:566f2c41dc1d 224
Wolfgang Betz 4:566f2c41dc1d 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 4:566f2c41dc1d 236 int16_t H_T_out, humidity_t;
Wolfgang Betz 4:566f2c41dc1d 237 uint8_t tempReg[2] = {0,0};
Wolfgang Betz 4:566f2c41dc1d 238 uint8_t tmp = 0x00;
Wolfgang Betz 4:566f2c41dc1d 239 float H_rh;
Wolfgang Betz 4:566f2c41dc1d 240
Wolfgang Betz 4:566f2c41dc1d 241 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 242 {
Wolfgang Betz 4:566f2c41dc1d 243 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 244 }
Wolfgang Betz 4:566f2c41dc1d 245
Wolfgang Betz 4:566f2c41dc1d 246 /* Output Data Rate selection */
Wolfgang Betz 4:566f2c41dc1d 247 tmp &= (HTS221_ODR_MASK);
Wolfgang Betz 4:566f2c41dc1d 248
Wolfgang Betz 4:566f2c41dc1d 249 if(tmp == 0x00)
Wolfgang Betz 4:566f2c41dc1d 250 {
Wolfgang Betz 4:566f2c41dc1d 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 4:566f2c41dc1d 256 /* Serial Interface Mode selection */
Wolfgang Betz 4:566f2c41dc1d 257 tmp &= ~(HTS221_ONE_SHOT_MASK);
Wolfgang Betz 4:566f2c41dc1d 258 tmp |= HTS221_ONE_SHOT_START;
Wolfgang Betz 4:566f2c41dc1d 259
Wolfgang Betz 4:566f2c41dc1d 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 4:566f2c41dc1d 265 do{
Wolfgang Betz 4:566f2c41dc1d 266
Wolfgang Betz 4:566f2c41dc1d 267 if(HTS221_IO_Read(&tmp, HTS221_STATUS_REG_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 268 {
Wolfgang Betz 4:566f2c41dc1d 269 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 270 }
Wolfgang Betz 4:566f2c41dc1d 271
Wolfgang Betz 4:566f2c41dc1d 272 }while(!(tmp&&0x02));
Wolfgang Betz 4:566f2c41dc1d 273 }
Wolfgang Betz 4:566f2c41dc1d 274
Wolfgang Betz 4:566f2c41dc1d 275
Wolfgang Betz 4:566f2c41dc1d 276 if(HTS221_IO_Read(&tempReg[0], (HTS221_HUMIDITY_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 277 {
Wolfgang Betz 4:566f2c41dc1d 278 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 279 }
Wolfgang Betz 4:566f2c41dc1d 280
Wolfgang Betz 4:566f2c41dc1d 281 H_T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 282
Wolfgang Betz 4:566f2c41dc1d 283 H_rh = ( float )(((( H_T_out - H0_T0_out ) * ( H1_rh - H0_rh )) / ( H1_T0_out - H0_T0_out )) + H0_rh );
Wolfgang Betz 4:566f2c41dc1d 284
Wolfgang Betz 4:566f2c41dc1d 285 // Truncate to specific number of decimal digits
wobetz 11:001a21c6ac1d 286 humidity_t = (uint16_t)(H_rh * pow(10.0f,HUM_DECIMAL_DIGITS));
wobetz 11:001a21c6ac1d 287 *pfData = ((float)humidity_t)/pow(10.0f,HUM_DECIMAL_DIGITS);
Wolfgang Betz 4:566f2c41dc1d 288
Wolfgang Betz 4:566f2c41dc1d 289 // Prevent data going below 0% and above 100% due to linear interpolation
Wolfgang Betz 4:566f2c41dc1d 290 if ( *pfData < 0.0f ) *pfData = 0.0f;
Wolfgang Betz 4:566f2c41dc1d 291 if ( *pfData > 100.0f ) *pfData = 100.0f;
Wolfgang Betz 4:566f2c41dc1d 292
Wolfgang Betz 4:566f2c41dc1d 293 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 294 }
Wolfgang Betz 4:566f2c41dc1d 295
Wolfgang Betz 4:566f2c41dc1d 296 /**
Wolfgang Betz 4:566f2c41dc1d 297 * @brief Read HTS221 output register, and calculate the temperature
Wolfgang Betz 4:566f2c41dc1d 298 * @param pfData the pointer to data output
Wolfgang Betz 4:566f2c41dc1d 299 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 300 */
Wolfgang Betz 4:566f2c41dc1d 301 HUM_TEMP_StatusTypeDef HTS221::HTS221_GetTemperature(float* pfData)
Wolfgang Betz 4:566f2c41dc1d 302 {
Wolfgang Betz 4:566f2c41dc1d 303 int16_t T_out, temperature_t;
Wolfgang Betz 4:566f2c41dc1d 304 uint8_t tempReg[2] = {0,0};
Wolfgang Betz 4:566f2c41dc1d 305 uint8_t tmp = 0x00;
Wolfgang Betz 4:566f2c41dc1d 306 float T_degC;
Wolfgang Betz 4:566f2c41dc1d 307
Wolfgang Betz 4:566f2c41dc1d 308 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 309 {
Wolfgang Betz 4:566f2c41dc1d 310 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 311 }
Wolfgang Betz 4:566f2c41dc1d 312
Wolfgang Betz 4:566f2c41dc1d 313 /* Output Data Rate selection */
Wolfgang Betz 4:566f2c41dc1d 314 tmp &= (HTS221_ODR_MASK);
Wolfgang Betz 4:566f2c41dc1d 315
Wolfgang Betz 4:566f2c41dc1d 316 if(tmp == 0x00)
Wolfgang Betz 4:566f2c41dc1d 317 {
Wolfgang Betz 4:566f2c41dc1d 318 if(HTS221_IO_Read(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 319 {
Wolfgang Betz 4:566f2c41dc1d 320 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 321 }
Wolfgang Betz 4:566f2c41dc1d 322
Wolfgang Betz 4:566f2c41dc1d 323 /* Serial Interface Mode selection */
Wolfgang Betz 4:566f2c41dc1d 324 tmp &= ~(HTS221_ONE_SHOT_MASK);
Wolfgang Betz 4:566f2c41dc1d 325 tmp |= HTS221_ONE_SHOT_START;
Wolfgang Betz 4:566f2c41dc1d 326
Wolfgang Betz 4:566f2c41dc1d 327 if(HTS221_IO_Write(&tmp, HTS221_CTRL_REG2_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 328 {
Wolfgang Betz 4:566f2c41dc1d 329 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 330 }
Wolfgang Betz 4:566f2c41dc1d 331
Wolfgang Betz 4:566f2c41dc1d 332 do{
Wolfgang Betz 4:566f2c41dc1d 333
Wolfgang Betz 4:566f2c41dc1d 334 if(HTS221_IO_Read(&tmp, HTS221_STATUS_REG_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 335 {
Wolfgang Betz 4:566f2c41dc1d 336 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 337 }
Wolfgang Betz 4:566f2c41dc1d 338
Wolfgang Betz 4:566f2c41dc1d 339 }while(!(tmp&&0x01));
Wolfgang Betz 4:566f2c41dc1d 340 }
Wolfgang Betz 4:566f2c41dc1d 341
Wolfgang Betz 4:566f2c41dc1d 342 if(HTS221_IO_Read(&tempReg[0], (HTS221_TEMP_OUT_L_ADDR | HTS221_I2C_MULTIPLEBYTE_CMD), 2) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 343 {
Wolfgang Betz 4:566f2c41dc1d 344 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 345 }
Wolfgang Betz 4:566f2c41dc1d 346
Wolfgang Betz 4:566f2c41dc1d 347 T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]);
Wolfgang Betz 4:566f2c41dc1d 348
Wolfgang Betz 4:566f2c41dc1d 349 T_degC = ((float)(T_out - T0_out))/(T1_out - T0_out) * (T1_degC - T0_degC) + T0_degC;
Wolfgang Betz 4:566f2c41dc1d 350
wobetz 11:001a21c6ac1d 351 temperature_t = (int16_t)(T_degC * pow(10.0f,TEMP_DECIMAL_DIGITS));
Wolfgang Betz 4:566f2c41dc1d 352
wobetz 11:001a21c6ac1d 353 *pfData = ((float)temperature_t)/pow(10.0f,TEMP_DECIMAL_DIGITS);
Wolfgang Betz 4:566f2c41dc1d 354
Wolfgang Betz 4:566f2c41dc1d 355 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 356 }
Wolfgang Betz 4:566f2c41dc1d 357
Wolfgang Betz 4:566f2c41dc1d 358
Wolfgang Betz 4:566f2c41dc1d 359 /**
Wolfgang Betz 4:566f2c41dc1d 360 * @brief Exit the shutdown mode for HTS221
Wolfgang Betz 4:566f2c41dc1d 361 * @param None
Wolfgang Betz 4:566f2c41dc1d 362 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 363 */
Wolfgang Betz 4:566f2c41dc1d 364 HUM_TEMP_StatusTypeDef HTS221::HTS221_Power_On(void)
Wolfgang Betz 4:566f2c41dc1d 365 {
Wolfgang Betz 4:566f2c41dc1d 366 uint8_t tmpReg;
Wolfgang Betz 4:566f2c41dc1d 367
Wolfgang Betz 4:566f2c41dc1d 368 /* Read the register content */
Wolfgang Betz 4:566f2c41dc1d 369 if(HTS221_IO_Read(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 370 {
Wolfgang Betz 4:566f2c41dc1d 371 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 372 }
Wolfgang Betz 4:566f2c41dc1d 373
Wolfgang Betz 4:566f2c41dc1d 374 /* Set the power down bit */
Wolfgang Betz 4:566f2c41dc1d 375 tmpReg |= HTS221_MODE_ACTIVE;
Wolfgang Betz 4:566f2c41dc1d 376
Wolfgang Betz 4:566f2c41dc1d 377 /* Write register */
Wolfgang Betz 4:566f2c41dc1d 378 if(HTS221_IO_Write(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 379 {
Wolfgang Betz 4:566f2c41dc1d 380 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 381 }
Wolfgang Betz 4:566f2c41dc1d 382
Wolfgang Betz 4:566f2c41dc1d 383 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 384 }
Wolfgang Betz 4:566f2c41dc1d 385
Wolfgang Betz 4:566f2c41dc1d 386 /**
Wolfgang Betz 4:566f2c41dc1d 387 * @brief Enter the shutdown mode for HTS221
Wolfgang Betz 4:566f2c41dc1d 388 * @param None
Wolfgang Betz 4:566f2c41dc1d 389 * @retval HUM_TEMP_OK in case of success, an error code otherwise
Wolfgang Betz 4:566f2c41dc1d 390 */
Wolfgang Betz 4:566f2c41dc1d 391 HUM_TEMP_StatusTypeDef HTS221::HTS221_Power_OFF(void)
Wolfgang Betz 4:566f2c41dc1d 392 {
Wolfgang Betz 4:566f2c41dc1d 393 uint8_t tmpReg;
Wolfgang Betz 4:566f2c41dc1d 394
Wolfgang Betz 4:566f2c41dc1d 395 /* Read the register content */
Wolfgang Betz 4:566f2c41dc1d 396 if(HTS221_IO_Read(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 397 {
Wolfgang Betz 4:566f2c41dc1d 398 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 399 }
Wolfgang Betz 4:566f2c41dc1d 400
Wolfgang Betz 4:566f2c41dc1d 401 /* Reset the power down bit */
Wolfgang Betz 4:566f2c41dc1d 402 tmpReg &= ~(HTS221_MODE_ACTIVE);
Wolfgang Betz 4:566f2c41dc1d 403
Wolfgang Betz 4:566f2c41dc1d 404 /* Write register */
Wolfgang Betz 4:566f2c41dc1d 405 if(HTS221_IO_Write(&tmpReg, HTS221_CTRL_REG1_ADDR, 1) != HUM_TEMP_OK)
Wolfgang Betz 4:566f2c41dc1d 406 {
Wolfgang Betz 4:566f2c41dc1d 407 return HUM_TEMP_ERROR;
Wolfgang Betz 4:566f2c41dc1d 408 }
Wolfgang Betz 4:566f2c41dc1d 409
Wolfgang Betz 4:566f2c41dc1d 410 return HUM_TEMP_OK;
Wolfgang Betz 4:566f2c41dc1d 411 }