Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
J_Satchell
Date:
Thu Aug 17 06:58:29 2017 +0000
Revision:
91:cd9fcd45ecf6
Parent:
61:8eb4c09ae4cb
Added mutex

Who changed what in which revision?

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