Contains the BSP driver for the B-L475E-IOT01 board.
Dependents: mbed-os-example-ble-Thermometer DISCO_L475VG_IOT01-Telegram-BOT DISCO_L475VG_IOT01-sche_cheveux DISCO_L475VG_IOT01-QSPI_FLASH_FILE_SYSTEM ... more
https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/
Drivers/BSP/Components/hts221/hts221.c@4:137bb367ba50, 2017-04-14 (annotated)
- Committer:
- bcostm
- Date:
- Fri Apr 14 13:09:54 2017 +0200
- Revision:
- 4:137bb367ba50
- Child:
- 7:bfe8272ced90
Add remaining Components files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 4:137bb367ba50 | 1 | /** |
bcostm | 4:137bb367ba50 | 2 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 3 | * @file hts221.c |
bcostm | 4:137bb367ba50 | 4 | * @author MCD Application Team |
bcostm | 4:137bb367ba50 | 5 | * @version V1.0.0 |
bcostm | 4:137bb367ba50 | 6 | * @date 14-February-2017 |
bcostm | 4:137bb367ba50 | 7 | * @brief This file provides a set of functions needed to manage the HTS221 |
bcostm | 4:137bb367ba50 | 8 | * humidity and temperature devices |
bcostm | 4:137bb367ba50 | 9 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 10 | * @attention |
bcostm | 4:137bb367ba50 | 11 | * |
bcostm | 4:137bb367ba50 | 12 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
bcostm | 4:137bb367ba50 | 13 | * |
bcostm | 4:137bb367ba50 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
bcostm | 4:137bb367ba50 | 15 | * are permitted provided that the following conditions are met: |
bcostm | 4:137bb367ba50 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
bcostm | 4:137bb367ba50 | 17 | * this list of conditions and the following disclaimer. |
bcostm | 4:137bb367ba50 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
bcostm | 4:137bb367ba50 | 19 | * this list of conditions and the following disclaimer in the documentation |
bcostm | 4:137bb367ba50 | 20 | * and/or other materials provided with the distribution. |
bcostm | 4:137bb367ba50 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
bcostm | 4:137bb367ba50 | 22 | * may be used to endorse or promote products derived from this software |
bcostm | 4:137bb367ba50 | 23 | * without specific prior written permission. |
bcostm | 4:137bb367ba50 | 24 | * |
bcostm | 4:137bb367ba50 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bcostm | 4:137bb367ba50 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bcostm | 4:137bb367ba50 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
bcostm | 4:137bb367ba50 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
bcostm | 4:137bb367ba50 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
bcostm | 4:137bb367ba50 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
bcostm | 4:137bb367ba50 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
bcostm | 4:137bb367ba50 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
bcostm | 4:137bb367ba50 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
bcostm | 4:137bb367ba50 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bcostm | 4:137bb367ba50 | 35 | * |
bcostm | 4:137bb367ba50 | 36 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 37 | */ |
bcostm | 4:137bb367ba50 | 38 | |
bcostm | 4:137bb367ba50 | 39 | /* Includes ------------------------------------------------------------------*/ |
bcostm | 4:137bb367ba50 | 40 | #include "hts221.h" |
bcostm | 4:137bb367ba50 | 41 | |
bcostm | 4:137bb367ba50 | 42 | /** @addtogroup BSP |
bcostm | 4:137bb367ba50 | 43 | * @{ |
bcostm | 4:137bb367ba50 | 44 | */ |
bcostm | 4:137bb367ba50 | 45 | |
bcostm | 4:137bb367ba50 | 46 | /** @addtogroup Component |
bcostm | 4:137bb367ba50 | 47 | * @{ |
bcostm | 4:137bb367ba50 | 48 | */ |
bcostm | 4:137bb367ba50 | 49 | |
bcostm | 4:137bb367ba50 | 50 | /** @defgroup HTS221 HTS221 |
bcostm | 4:137bb367ba50 | 51 | * @{ |
bcostm | 4:137bb367ba50 | 52 | */ |
bcostm | 4:137bb367ba50 | 53 | |
bcostm | 4:137bb367ba50 | 54 | /** @defgroup HTS221_Private_Variables HTS221 Private Variables |
bcostm | 4:137bb367ba50 | 55 | * @{ |
bcostm | 4:137bb367ba50 | 56 | */ |
bcostm | 4:137bb367ba50 | 57 | /* HTS221 Humidity Private Variables */ |
bcostm | 4:137bb367ba50 | 58 | HSENSOR_DrvTypeDef HTS221_H_Drv = |
bcostm | 4:137bb367ba50 | 59 | { |
bcostm | 4:137bb367ba50 | 60 | HTS221_H_Init, |
bcostm | 4:137bb367ba50 | 61 | HTS221_H_ReadID, |
bcostm | 4:137bb367ba50 | 62 | HTS221_H_ReadHumidity |
bcostm | 4:137bb367ba50 | 63 | }; |
bcostm | 4:137bb367ba50 | 64 | |
bcostm | 4:137bb367ba50 | 65 | /* HTS221_Temperature_Private_Variables */ |
bcostm | 4:137bb367ba50 | 66 | TSENSOR_DrvTypeDef HTS221_T_Drv = |
bcostm | 4:137bb367ba50 | 67 | { |
bcostm | 4:137bb367ba50 | 68 | HTS221_T_Init, |
bcostm | 4:137bb367ba50 | 69 | 0, |
bcostm | 4:137bb367ba50 | 70 | 0, |
bcostm | 4:137bb367ba50 | 71 | HTS221_T_ReadTemp |
bcostm | 4:137bb367ba50 | 72 | }; |
bcostm | 4:137bb367ba50 | 73 | /** |
bcostm | 4:137bb367ba50 | 74 | * @} |
bcostm | 4:137bb367ba50 | 75 | */ |
bcostm | 4:137bb367ba50 | 76 | |
bcostm | 4:137bb367ba50 | 77 | /** @defgroup HTS221_Humidity_Private_Functions HTS221 Humidity Private Functions |
bcostm | 4:137bb367ba50 | 78 | * @{ |
bcostm | 4:137bb367ba50 | 79 | */ |
bcostm | 4:137bb367ba50 | 80 | /** |
bcostm | 4:137bb367ba50 | 81 | * @brief Set HTS221 humidity sensor Initialization. |
bcostm | 4:137bb367ba50 | 82 | */ |
bcostm | 4:137bb367ba50 | 83 | void HTS221_H_Init(uint16_t DeviceAddr) |
bcostm | 4:137bb367ba50 | 84 | { |
bcostm | 4:137bb367ba50 | 85 | uint8_t tmp; |
bcostm | 4:137bb367ba50 | 86 | |
bcostm | 4:137bb367ba50 | 87 | /* Read CTRL_REG1 */ |
bcostm | 4:137bb367ba50 | 88 | tmp = SENSOR_IO_Read(DeviceAddr, HTS221_CTRL_REG1); |
bcostm | 4:137bb367ba50 | 89 | |
bcostm | 4:137bb367ba50 | 90 | /* Enable BDU */ |
bcostm | 4:137bb367ba50 | 91 | tmp &= ~HTS221_BDU_MASK; |
bcostm | 4:137bb367ba50 | 92 | tmp |= (1 << HTS221_BDU_BIT); |
bcostm | 4:137bb367ba50 | 93 | |
bcostm | 4:137bb367ba50 | 94 | /* Set default ODR */ |
bcostm | 4:137bb367ba50 | 95 | tmp &= ~HTS221_ODR_MASK; |
bcostm | 4:137bb367ba50 | 96 | tmp |= (uint8_t)0x01; /* Set ODR to 1Hz */ |
bcostm | 4:137bb367ba50 | 97 | |
bcostm | 4:137bb367ba50 | 98 | /* Activate the device */ |
bcostm | 4:137bb367ba50 | 99 | tmp |= HTS221_PD_MASK; |
bcostm | 4:137bb367ba50 | 100 | |
bcostm | 4:137bb367ba50 | 101 | /* Apply settings to CTRL_REG1 */ |
bcostm | 4:137bb367ba50 | 102 | SENSOR_IO_Write(DeviceAddr, HTS221_CTRL_REG1, tmp); |
bcostm | 4:137bb367ba50 | 103 | } |
bcostm | 4:137bb367ba50 | 104 | |
bcostm | 4:137bb367ba50 | 105 | /** |
bcostm | 4:137bb367ba50 | 106 | * @brief Read HTS221 ID. |
bcostm | 4:137bb367ba50 | 107 | * @retval ID |
bcostm | 4:137bb367ba50 | 108 | */ |
bcostm | 4:137bb367ba50 | 109 | uint8_t HTS221_H_ReadID(uint16_t DeviceAddr) |
bcostm | 4:137bb367ba50 | 110 | { |
bcostm | 4:137bb367ba50 | 111 | uint8_t ctrl = 0x00; |
bcostm | 4:137bb367ba50 | 112 | |
bcostm | 4:137bb367ba50 | 113 | /* IO interface initialization */ |
bcostm | 4:137bb367ba50 | 114 | SENSOR_IO_Init(); |
bcostm | 4:137bb367ba50 | 115 | |
bcostm | 4:137bb367ba50 | 116 | /* Read value at Who am I register address */ |
bcostm | 4:137bb367ba50 | 117 | ctrl = SENSOR_IO_Read(DeviceAddr, HTS221_WHO_AM_I_REG); |
bcostm | 4:137bb367ba50 | 118 | |
bcostm | 4:137bb367ba50 | 119 | return ctrl; |
bcostm | 4:137bb367ba50 | 120 | } |
bcostm | 4:137bb367ba50 | 121 | |
bcostm | 4:137bb367ba50 | 122 | /** |
bcostm | 4:137bb367ba50 | 123 | * @brief Read humidity value of HTS221 |
bcostm | 4:137bb367ba50 | 124 | * @retval humidity value; |
bcostm | 4:137bb367ba50 | 125 | */ |
bcostm | 4:137bb367ba50 | 126 | float HTS221_H_ReadHumidity(uint16_t DeviceAddr) |
bcostm | 4:137bb367ba50 | 127 | { |
bcostm | 4:137bb367ba50 | 128 | int16_t H0_T0_out, H1_T0_out, H_T_out; |
bcostm | 4:137bb367ba50 | 129 | int16_t H0_rh, H1_rh; |
bcostm | 4:137bb367ba50 | 130 | uint8_t buffer[2]; |
bcostm | 4:137bb367ba50 | 131 | float tmp_f; |
bcostm | 4:137bb367ba50 | 132 | |
bcostm | 4:137bb367ba50 | 133 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H0_RH_X2 | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 134 | |
bcostm | 4:137bb367ba50 | 135 | H0_rh = buffer[0] >> 1; |
bcostm | 4:137bb367ba50 | 136 | H1_rh = buffer[1] >> 1; |
bcostm | 4:137bb367ba50 | 137 | |
bcostm | 4:137bb367ba50 | 138 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H0_T0_OUT_L | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 139 | |
bcostm | 4:137bb367ba50 | 140 | H0_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; |
bcostm | 4:137bb367ba50 | 141 | |
bcostm | 4:137bb367ba50 | 142 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H1_T0_OUT_L | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 143 | |
bcostm | 4:137bb367ba50 | 144 | H1_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; |
bcostm | 4:137bb367ba50 | 145 | |
bcostm | 4:137bb367ba50 | 146 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_HR_OUT_L_REG | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 147 | |
bcostm | 4:137bb367ba50 | 148 | H_T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; |
bcostm | 4:137bb367ba50 | 149 | |
bcostm | 4:137bb367ba50 | 150 | tmp_f = (float)(H_T_out - H0_T0_out) * (float)(H1_rh - H0_rh) / (float)(H1_T0_out - H0_T0_out) + H0_rh; |
bcostm | 4:137bb367ba50 | 151 | tmp_f *= 10.0f; |
bcostm | 4:137bb367ba50 | 152 | |
bcostm | 4:137bb367ba50 | 153 | tmp_f = ( tmp_f > 1000.0f ) ? 1000.0f |
bcostm | 4:137bb367ba50 | 154 | : ( tmp_f < 0.0f ) ? 0.0f |
bcostm | 4:137bb367ba50 | 155 | : tmp_f; |
bcostm | 4:137bb367ba50 | 156 | |
bcostm | 4:137bb367ba50 | 157 | return (tmp_f / 10.0f); |
bcostm | 4:137bb367ba50 | 158 | } |
bcostm | 4:137bb367ba50 | 159 | |
bcostm | 4:137bb367ba50 | 160 | |
bcostm | 4:137bb367ba50 | 161 | /** |
bcostm | 4:137bb367ba50 | 162 | * @} |
bcostm | 4:137bb367ba50 | 163 | */ |
bcostm | 4:137bb367ba50 | 164 | |
bcostm | 4:137bb367ba50 | 165 | /** @defgroup HTS221_Temperature_Private_Functions HTS221 Temperature Private Functions |
bcostm | 4:137bb367ba50 | 166 | * @{ |
bcostm | 4:137bb367ba50 | 167 | */ |
bcostm | 4:137bb367ba50 | 168 | |
bcostm | 4:137bb367ba50 | 169 | /** |
bcostm | 4:137bb367ba50 | 170 | * @brief Set HTS221 temperature sensor Initialization. |
bcostm | 4:137bb367ba50 | 171 | * @param DeviceAddr: I2C device address |
bcostm | 4:137bb367ba50 | 172 | * @param InitStruct: pointer to a TSENSOR_InitTypeDef structure |
bcostm | 4:137bb367ba50 | 173 | * that contains the configuration setting for the HTS221. |
bcostm | 4:137bb367ba50 | 174 | */ |
bcostm | 4:137bb367ba50 | 175 | void HTS221_T_Init(uint16_t DeviceAddr, TSENSOR_InitTypeDef *pInitStruct) |
bcostm | 4:137bb367ba50 | 176 | { |
bcostm | 4:137bb367ba50 | 177 | uint8_t tmp; |
bcostm | 4:137bb367ba50 | 178 | |
bcostm | 4:137bb367ba50 | 179 | /* Read CTRL_REG1 */ |
bcostm | 4:137bb367ba50 | 180 | tmp = SENSOR_IO_Read(DeviceAddr, HTS221_CTRL_REG1); |
bcostm | 4:137bb367ba50 | 181 | |
bcostm | 4:137bb367ba50 | 182 | /* Enable BDU */ |
bcostm | 4:137bb367ba50 | 183 | tmp &= ~HTS221_BDU_MASK; |
bcostm | 4:137bb367ba50 | 184 | tmp |= (1 << HTS221_BDU_BIT); |
bcostm | 4:137bb367ba50 | 185 | |
bcostm | 4:137bb367ba50 | 186 | /* Set default ODR */ |
bcostm | 4:137bb367ba50 | 187 | tmp &= ~HTS221_ODR_MASK; |
bcostm | 4:137bb367ba50 | 188 | tmp |= (uint8_t)0x01; /* Set ODR to 1Hz */ |
bcostm | 4:137bb367ba50 | 189 | |
bcostm | 4:137bb367ba50 | 190 | /* Activate the device */ |
bcostm | 4:137bb367ba50 | 191 | tmp |= HTS221_PD_MASK; |
bcostm | 4:137bb367ba50 | 192 | |
bcostm | 4:137bb367ba50 | 193 | /* Apply settings to CTRL_REG1 */ |
bcostm | 4:137bb367ba50 | 194 | SENSOR_IO_Write(DeviceAddr, HTS221_CTRL_REG1, tmp); |
bcostm | 4:137bb367ba50 | 195 | } |
bcostm | 4:137bb367ba50 | 196 | |
bcostm | 4:137bb367ba50 | 197 | /** |
bcostm | 4:137bb367ba50 | 198 | * @brief Read temperature value of HTS221 |
bcostm | 4:137bb367ba50 | 199 | * @param DeviceAddr: I2C device address |
bcostm | 4:137bb367ba50 | 200 | * @retval temperature value |
bcostm | 4:137bb367ba50 | 201 | */ |
bcostm | 4:137bb367ba50 | 202 | float HTS221_T_ReadTemp(uint16_t DeviceAddr) |
bcostm | 4:137bb367ba50 | 203 | { |
bcostm | 4:137bb367ba50 | 204 | int16_t T0_out, T1_out, T_out, T0_degC_x8_u16, T1_degC_x8_u16; |
bcostm | 4:137bb367ba50 | 205 | int16_t T0_degC, T1_degC; |
bcostm | 4:137bb367ba50 | 206 | uint8_t buffer[4], tmp; |
bcostm | 4:137bb367ba50 | 207 | float tmp_f; |
bcostm | 4:137bb367ba50 | 208 | |
bcostm | 4:137bb367ba50 | 209 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_T0_DEGC_X8 | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 210 | tmp = SENSOR_IO_Read(DeviceAddr, HTS221_T0_T1_DEGC_H2); |
bcostm | 4:137bb367ba50 | 211 | |
bcostm | 4:137bb367ba50 | 212 | T0_degC_x8_u16 = (((uint16_t)(tmp & 0x03)) << 8) | ((uint16_t)buffer[0]); |
bcostm | 4:137bb367ba50 | 213 | T1_degC_x8_u16 = (((uint16_t)(tmp & 0x0C)) << 6) | ((uint16_t)buffer[1]); |
bcostm | 4:137bb367ba50 | 214 | T0_degC = T0_degC_x8_u16 >> 3; |
bcostm | 4:137bb367ba50 | 215 | T1_degC = T1_degC_x8_u16 >> 3; |
bcostm | 4:137bb367ba50 | 216 | |
bcostm | 4:137bb367ba50 | 217 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_T0_OUT_L | 0x80), buffer, 4); |
bcostm | 4:137bb367ba50 | 218 | |
bcostm | 4:137bb367ba50 | 219 | T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; |
bcostm | 4:137bb367ba50 | 220 | T1_out = (((uint16_t)buffer[3]) << 8) | (uint16_t)buffer[2]; |
bcostm | 4:137bb367ba50 | 221 | |
bcostm | 4:137bb367ba50 | 222 | SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_TEMP_OUT_L_REG | 0x80), buffer, 2); |
bcostm | 4:137bb367ba50 | 223 | |
bcostm | 4:137bb367ba50 | 224 | T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; |
bcostm | 4:137bb367ba50 | 225 | |
bcostm | 4:137bb367ba50 | 226 | tmp_f = (float)(T_out - T0_out) * (float)(T1_degC - T0_degC) / (float)(T1_out - T0_out) + T0_degC; |
bcostm | 4:137bb367ba50 | 227 | |
bcostm | 4:137bb367ba50 | 228 | return tmp_f; |
bcostm | 4:137bb367ba50 | 229 | } |
bcostm | 4:137bb367ba50 | 230 | |
bcostm | 4:137bb367ba50 | 231 | /** |
bcostm | 4:137bb367ba50 | 232 | * @} |
bcostm | 4:137bb367ba50 | 233 | */ |
bcostm | 4:137bb367ba50 | 234 | |
bcostm | 4:137bb367ba50 | 235 | /** |
bcostm | 4:137bb367ba50 | 236 | * @} |
bcostm | 4:137bb367ba50 | 237 | */ |
bcostm | 4:137bb367ba50 | 238 | |
bcostm | 4:137bb367ba50 | 239 | /** |
bcostm | 4:137bb367ba50 | 240 | * @} |
bcostm | 4:137bb367ba50 | 241 | */ |
bcostm | 4:137bb367ba50 | 242 | |
bcostm | 4:137bb367ba50 | 243 | /** |
bcostm | 4:137bb367ba50 | 244 | * @} |
bcostm | 4:137bb367ba50 | 245 | */ |
bcostm | 4:137bb367ba50 | 246 | |
bcostm | 4:137bb367ba50 | 247 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |