Capacitive digital sensor for relative humidity and temperature.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client HTS221_JS ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTS221_driver.c Source File

HTS221_driver.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    HTS221_driver.c
00004  * @author  HESA Application Team
00005  * @version V1.1
00006  * @date    10-August-2016
00007  * @brief   HTS221 driver file
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "HTS221_driver.h"
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #ifdef  USE_FULL_ASSERT_HTS221
00046 #include <stdio.h>
00047 #endif
00048 
00049 
00050 /** @addtogroup Environmental_Sensor
00051 * @{
00052 */
00053 
00054 /** @defgroup HTS221_DRIVER
00055 * @brief HTS221 DRIVER
00056 * @{
00057 */
00058 
00059 /** @defgroup HTS221_Imported_Function_Prototypes
00060 * @{
00061 */
00062 
00063 extern uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00064 extern uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00065 
00066 /**
00067 * @}
00068 */
00069 
00070 /** @defgroup HTS221_Private_Function_Prototypes
00071 * @{
00072 */
00073 
00074 /**
00075 * @}
00076 */
00077 
00078 /** @defgroup HTS221_Private_Functions
00079 * @{
00080 */
00081 
00082 /**
00083 * @}
00084 */
00085 
00086 /** @defgroup HTS221_Public_Functions
00087 * @{
00088 */
00089 
00090 /*******************************************************************************
00091 * Function Name   : HTS221_read_reg
00092 * Description   : Generic Reading function. It must be fullfilled with either
00093 *         : I2C or SPI reading functions
00094 * Input       : Register Address
00095 * Output      : Data Read
00096 * Return      : None
00097 *******************************************************************************/
00098 HTS221_Error_et HTS221_read_reg(void *handle, uint8_t RegAddr, uint16_t NumByteToRead, uint8_t *Data)
00099 {
00100 
00101     if (NumByteToRead > 1) {
00102         RegAddr |= 0x80;
00103     }
00104 
00105     if (HTS221_io_read(handle, RegAddr, Data, NumByteToRead)) {
00106         return HTS221_ERROR;
00107     } else {
00108         return HTS221_OK;
00109     }
00110 }
00111 
00112 /*******************************************************************************
00113 * Function Name   : HTS221_write_reg
00114 * Description   : Generic Writing function. It must be fullfilled with either
00115 *         : I2C or SPI writing function
00116 * Input       : Register Address, Data to be written
00117 * Output      : None
00118 * Return      : None
00119 *******************************************************************************/
00120 HTS221_Error_et HTS221_write_reg(void *handle, uint8_t RegAddr, uint16_t NumByteToWrite, uint8_t *Data)
00121 {
00122 
00123     if (NumByteToWrite > 1) {
00124         RegAddr |= 0x80;
00125     }
00126 
00127     if (HTS221_io_write(handle, RegAddr, Data, NumByteToWrite)) {
00128         return HTS221_ERROR;
00129     } else {
00130         return HTS221_OK;
00131     }
00132 }
00133 
00134 /**
00135 * @brief  Get the version of this driver.
00136 * @param  pxVersion pointer to a HTS221_DriverVersion_st structure that contains the version information.
00137 *         This parameter is a pointer to @ref HTS221_DriverVersion_st.
00138 * @retval Error code [HTS221_OK, HTS221_ERROR].
00139 */
00140 HTS221_Error_et HTS221_Get_DriverVersion(HTS221_DriverVersion_st *version)
00141 {
00142     version->Major = HTS221_DRIVER_VERSION_MAJOR;
00143     version->Minor = HTS221_DRIVER_VERSION_MINOR;
00144     version->Point = HTS221_DRIVER_VERSION_POINT;
00145 
00146     return HTS221_OK;
00147 }
00148 
00149 /**
00150 * @brief  Get device type ID.
00151 * @param  *handle Device handle.
00152 * @param  deviceid pointer to the returned device type ID.
00153 * @retval Error code [HTS221_OK, HTS221_ERROR].
00154 */
00155 HTS221_Error_et HTS221_Get_DeviceID(void *handle, uint8_t *deviceid)
00156 {
00157     if (HTS221_read_reg(handle, HTS221_WHO_AM_I_REG, 1, deviceid)) {
00158         return HTS221_ERROR;
00159     }
00160 
00161     return HTS221_OK;
00162 }
00163 
00164 /**
00165 * @brief  Initializes the HTS221 with the specified parameters in HTS221_Init_st struct.
00166 * @param  *handle Device handle.
00167 * @param  pxInit pointer to a HTS221_Init_st structure that contains the configuration.
00168 *         This parameter is a pointer to @ref HTS221_Init_st.
00169 * @retval Error code [HTS221_OK, HTS221_ERROR].
00170 */
00171 HTS221_Error_et HTS221_Set_InitConfig(void *handle, HTS221_Init_st *pxInit)
00172 {
00173     uint8_t buffer[3];
00174 
00175     HTS221_assert_param(IS_HTS221_AVGH(pxInit->avg_h ));
00176     HTS221_assert_param(IS_HTS221_AVGT(pxInit->avg_t ));
00177     HTS221_assert_param(IS_HTS221_ODR(pxInit->odr ));
00178     HTS221_assert_param(IS_HTS221_State(pxInit->bdu_status ));
00179     HTS221_assert_param(IS_HTS221_State(pxInit->heater_status ));
00180 
00181     HTS221_assert_param(IS_HTS221_DrdyLevelType(pxInit->irq_level ));
00182     HTS221_assert_param(IS_HTS221_OutputType(pxInit->irq_output_type ));
00183     HTS221_assert_param(IS_HTS221_State(pxInit->irq_enable ));
00184 
00185     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, buffer)) {
00186         return HTS221_ERROR;
00187     }
00188 
00189     buffer[0] &= ~(HTS221_AVGH_MASK | HTS221_AVGT_MASK);
00190     buffer[0] |= (uint8_t)pxInit->avg_h ;
00191     buffer[0] |= (uint8_t)pxInit->avg_t ;
00192 
00193     if (HTS221_write_reg(handle, HTS221_AV_CONF_REG, 1, buffer)) {
00194         return HTS221_ERROR;
00195     }
00196 
00197     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 3, buffer)) {
00198         return HTS221_ERROR;
00199     }
00200 
00201     buffer[0] &= ~(HTS221_BDU_MASK | HTS221_ODR_MASK);
00202     buffer[0] |= (uint8_t)pxInit->odr ;
00203     buffer[0] |= ((uint8_t)pxInit->bdu_status ) << HTS221_BDU_BIT;
00204 
00205     buffer[1] &= ~HTS221_HEATHER_BIT;
00206     buffer[1] |= ((uint8_t)pxInit->heater_status ) << HTS221_HEATHER_BIT;
00207 
00208     buffer[2] &= ~(HTS221_DRDY_H_L_MASK | HTS221_PP_OD_MASK | HTS221_DRDY_MASK);
00209     buffer[2] |= ((uint8_t)pxInit->irq_level ) << HTS221_DRDY_H_L_BIT;
00210     buffer[2] |= (uint8_t)pxInit->irq_output_type ;
00211     buffer[2] |= ((uint8_t)pxInit->irq_enable ) << HTS221_DRDY_BIT;
00212 
00213     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 3, buffer)) {
00214         return HTS221_ERROR;
00215     }
00216 
00217     return HTS221_OK;
00218 }
00219 
00220 /**
00221 * @brief  Returns a HTS221_Init_st struct with the actual configuration.
00222 * @param  *handle Device handle.
00223 * @param  pxInit pointer to a HTS221_Init_st structure.
00224 *         This parameter is a pointer to @ref HTS221_Init_st.
00225 * @retval Error code [HTS221_OK, HTS221_ERROR].
00226 */
00227 HTS221_Error_et HTS221_Get_InitConfig(void *handle, HTS221_Init_st *pxInit)
00228 {
00229     uint8_t buffer[3];
00230 
00231     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, buffer)) {
00232         return HTS221_ERROR;
00233     }
00234 
00235     pxInit->avg_h  = (HTS221_Avgh_et)(buffer[0] & HTS221_AVGH_MASK);
00236     pxInit->avg_t  = (HTS221_Avgt_et)(buffer[0] & HTS221_AVGT_MASK);
00237 
00238     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 3, buffer)) {
00239         return HTS221_ERROR;
00240     }
00241 
00242     pxInit->odr  = (HTS221_Odr_et)(buffer[0] & HTS221_ODR_MASK);
00243     pxInit->bdu_status  = (HTS221_State_et)((buffer[0] & HTS221_BDU_MASK) >> HTS221_BDU_BIT);
00244     pxInit->heater_status  = (HTS221_State_et)((buffer[1] & HTS221_HEATHER_MASK) >> HTS221_HEATHER_BIT);
00245 
00246     pxInit->irq_level  = (HTS221_DrdyLevel_et)(buffer[2] & HTS221_DRDY_H_L_MASK);
00247     pxInit->irq_output_type  = (HTS221_OutputType_et)(buffer[2] & HTS221_PP_OD_MASK);
00248     pxInit->irq_enable  = (HTS221_State_et)((buffer[2] & HTS221_DRDY_MASK) >> HTS221_DRDY_BIT);
00249 
00250     return HTS221_OK;
00251 }
00252 
00253 /**
00254 * @brief  De initialization function for HTS221.
00255 *         This function put the HTS221 in power down, make a memory boot and clear the data output flags.
00256 * @param  *handle Device handle.
00257 * @retval Error code [HTS221_OK, HTS221_ERROR].
00258 */
00259 HTS221_Error_et HTS221_DeInit(void *handle)
00260 {
00261     uint8_t buffer[4];
00262 
00263     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 2, buffer)) {
00264         return HTS221_ERROR;
00265     }
00266 
00267     /* HTS221 in power down */
00268     buffer[0] |= 0x01 << HTS221_PD_BIT;
00269 
00270     /* Make HTS221 boot */
00271     buffer[1] |= 0x01 << HTS221_BOOT_BIT;
00272 
00273     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 2, buffer)) {
00274         return HTS221_ERROR;
00275     }
00276 
00277     /* Dump of data output */
00278     if (HTS221_read_reg(handle, HTS221_HR_OUT_L_REG, 4, buffer)) {
00279         return HTS221_ERROR;
00280     }
00281 
00282     return HTS221_OK;
00283 }
00284 
00285 /**
00286 * @brief  Read HTS221 output registers, and calculate humidity and temperature.
00287 * @param  *handle Device handle.
00288 * @param  humidity pointer to the returned humidity value that must be divided by 10 to get the value in [%].
00289 * @param  temperature pointer to the returned temperature value that must be divided by 10 to get the value in ['C].
00290 * @retval Error code [HTS221_OK, HTS221_ERROR].
00291 */
00292 HTS221_Error_et HTS221_Get_Measurement(void *handle, uint16_t *humidity, int16_t *temperature)
00293 {
00294     if (HTS221_Get_Temperature(handle, temperature) == HTS221_ERROR) {
00295         return HTS221_ERROR;
00296     }
00297     if (HTS221_Get_Humidity(handle, humidity) == HTS221_ERROR) {
00298         return HTS221_ERROR;
00299     }
00300 
00301     return HTS221_OK;
00302 }
00303 
00304 /**
00305 * @brief  Read HTS221 output registers. Humidity and temperature.
00306 * @param  *handle Device handle.
00307 * @param  humidity pointer to the returned humidity raw value.
00308 * @param  temperature pointer to the returned temperature raw value.
00309 * @retval Error code [HTS221_OK, HTS221_ERROR].
00310 */
00311 HTS221_Error_et HTS221_Get_RawMeasurement(void *handle, int16_t *humidity, int16_t *temperature)
00312 {
00313     uint8_t buffer[4];
00314 
00315     if (HTS221_read_reg(handle, HTS221_HR_OUT_L_REG, 4, buffer)) {
00316         return HTS221_ERROR;
00317     }
00318 
00319     *humidity = (int16_t)((((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]);
00320     *temperature = (int16_t)((((uint16_t)buffer[3]) << 8) | (uint16_t)buffer[2]);
00321 
00322     return HTS221_OK;
00323 }
00324 
00325 /**
00326 * @brief  Read HTS221 Humidity output registers, and calculate humidity.
00327 * @param  *handle Device handle.
00328 * @param  Pointer to the returned humidity value that must be divided by 10 to get the value in [%].
00329 * @retval Error code [HTS221_OK, HTS221_ERROR].
00330 */
00331 HTS221_Error_et HTS221_Get_Humidity(void *handle, uint16_t *value)
00332 {
00333     int16_t H0_T0_out, H1_T0_out, H_T_out;
00334     int16_t H0_rh, H1_rh;
00335     uint8_t buffer[2];
00336     float   tmp_f;
00337 
00338     if (HTS221_read_reg(handle, HTS221_H0_RH_X2, 2, buffer)) {
00339         return HTS221_ERROR;
00340     }
00341     H0_rh = buffer[0] >> 1;
00342     H1_rh = buffer[1] >> 1;
00343 
00344     if (HTS221_read_reg(handle, HTS221_H0_T0_OUT_L, 2, buffer)) {
00345         return HTS221_ERROR;
00346     }
00347     H0_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
00348 
00349     if (HTS221_read_reg(handle, HTS221_H1_T0_OUT_L, 2, buffer)) {
00350         return HTS221_ERROR;
00351     }
00352     H1_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
00353 
00354     if (HTS221_read_reg(handle, HTS221_HR_OUT_L_REG, 2, buffer)) {
00355         return HTS221_ERROR;
00356     }
00357     H_T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
00358 
00359     tmp_f = (float)(H_T_out - H0_T0_out) * (float)(H1_rh - H0_rh) / (float)(H1_T0_out - H0_T0_out)  +  H0_rh;
00360     tmp_f *= 10.0f;
00361 
00362     *value = (tmp_f > 1000.0f) ? 1000
00363              : (tmp_f <    0.0f) ?    0
00364              : (uint16_t)tmp_f;
00365 
00366     return HTS221_OK;
00367 }
00368 
00369 /**
00370 * @brief  Read HTS221 humidity output registers.
00371 * @param  *handle Device handle.
00372 * @param  Pointer to the returned humidity raw value.
00373 * @retval Error code [HTS221_OK, HTS221_ERROR].
00374 */
00375 HTS221_Error_et HTS221_Get_HumidityRaw(void *handle, int16_t *value)
00376 {
00377     uint8_t buffer[2];
00378 
00379     if (HTS221_read_reg(handle, HTS221_HR_OUT_L_REG, 2, buffer)) {
00380         return HTS221_ERROR;
00381     }
00382 
00383     *value = (int16_t)((((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]);
00384 
00385     return HTS221_OK;
00386 }
00387 
00388 /**
00389 * @brief  Read HTS221 temperature output registers, and calculate temperature.
00390 * @param  *handle Device handle.
00391 * @param  Pointer to the returned temperature value that must be divided by 10 to get the value in ['C].
00392 * @retval Error code [HTS221_OK, HTS221_ERROR].
00393 */
00394 HTS221_Error_et HTS221_Get_Temperature(void *handle, int16_t *value)
00395 {
00396     int16_t T0_out, T1_out, T_out, T0_degC_x8_u16, T1_degC_x8_u16;
00397     int16_t T0_degC, T1_degC;
00398     uint8_t buffer[4], tmp;
00399     float   tmp_f;
00400 
00401     if (HTS221_read_reg(handle, HTS221_T0_DEGC_X8, 2, buffer)) {
00402         return HTS221_ERROR;
00403     }
00404     if (HTS221_read_reg(handle, HTS221_T0_T1_DEGC_H2, 1, &tmp)) {
00405         return HTS221_ERROR;
00406     }
00407 
00408     T0_degC_x8_u16 = (((uint16_t)(tmp & 0x03)) << 8) | ((uint16_t)buffer[0]);
00409     T1_degC_x8_u16 = (((uint16_t)(tmp & 0x0C)) << 6) | ((uint16_t)buffer[1]);
00410     T0_degC = T0_degC_x8_u16 >> 3;
00411     T1_degC = T1_degC_x8_u16 >> 3;
00412 
00413     if (HTS221_read_reg(handle, HTS221_T0_OUT_L, 4, buffer)) {
00414         return HTS221_ERROR;
00415     }
00416 
00417     T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
00418     T1_out = (((uint16_t)buffer[3]) << 8) | (uint16_t)buffer[2];
00419 
00420     if (HTS221_read_reg(handle, HTS221_TEMP_OUT_L_REG, 2, buffer)) {
00421         return HTS221_ERROR;
00422     }
00423 
00424     T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
00425 
00426     tmp_f = (float)(T_out - T0_out) * (float)(T1_degC - T0_degC) / (float)(T1_out - T0_out)  +  T0_degC;
00427     tmp_f *= 10.0f;
00428 
00429     *value = (int16_t)tmp_f;
00430 
00431     return HTS221_OK;
00432 }
00433 
00434 /**
00435 * @brief  Read HTS221 temperature output registers.
00436 * @param  *handle Device handle.
00437 * @param  Pointer to the returned temperature raw value.
00438 * @retval Error code [HTS221_OK, HTS221_ERROR].
00439 */
00440 HTS221_Error_et HTS221_Get_TemperatureRaw(void *handle, int16_t *value)
00441 {
00442     uint8_t buffer[2];
00443 
00444     if (HTS221_read_reg(handle, HTS221_TEMP_OUT_L_REG, 2, buffer)) {
00445         return HTS221_ERROR;
00446     }
00447 
00448     *value = (int16_t)((((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]);
00449 
00450     return HTS221_OK;
00451 }
00452 
00453 /**
00454 * @brief  Get the availability of new data for humidity and temperature.
00455 * @param  *handle Device handle.
00456 * @param  humidity pointer to the returned humidity data status [HTS221_SET/HTS221_RESET].
00457 * @param  temperature pointer to the returned temperature data status [HTS221_SET/HTS221_RESET].
00458 *         This parameter is a pointer to @ref HTS221_BitStatus_et.
00459 * @retval Error code [HTS221_OK, HTS221_ERROR].
00460 */
00461 HTS221_Error_et HTS221_Get_DataStatus(void *handle, HTS221_BitStatus_et *humidity, HTS221_BitStatus_et *temperature)
00462 {
00463     uint8_t tmp;
00464 
00465     if (HTS221_read_reg(handle, HTS221_STATUS_REG, 1, &tmp)) {
00466         return HTS221_ERROR;
00467     }
00468 
00469     *humidity = (HTS221_BitStatus_et)((tmp & HTS221_HDA_MASK) >> HTS221_H_DA_BIT);
00470     *temperature = (HTS221_BitStatus_et)(tmp & HTS221_TDA_MASK);
00471 
00472     return HTS221_OK;
00473 }
00474 
00475 /**
00476 * @brief  Exit from power down mode.
00477 * @param  *handle Device handle.
00478 * @param  void.
00479 * @retval Error code [HTS221_OK, HTS221_ERROR].
00480 */
00481 HTS221_Error_et HTS221_Activate(void *handle)
00482 {
00483     uint8_t tmp;
00484 
00485     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00486         return HTS221_ERROR;
00487     }
00488 
00489     tmp |= HTS221_PD_MASK;
00490 
00491     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00492         return HTS221_ERROR;
00493     }
00494 
00495     return HTS221_OK;
00496 }
00497 
00498 /**
00499 * @brief  Put the sensor in power down mode.
00500 * @param  *handle Device handle.
00501 * @retval Error code [HTS221_OK, HTS221_ERROR].
00502 */
00503 HTS221_Error_et HTS221_DeActivate(void *handle)
00504 {
00505     uint8_t tmp;
00506 
00507     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00508         return HTS221_ERROR;
00509     }
00510 
00511     tmp &= ~HTS221_PD_MASK;
00512 
00513     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00514         return HTS221_ERROR;
00515     }
00516 
00517     return HTS221_OK;
00518 }
00519 
00520 
00521 
00522 /**
00523 * @brief  Check if the single measurement has completed.
00524 * @param  *handle Device handle.
00525 * @param  tmp is set to 1, when the measure is completed
00526 * @retval Status [HTS221_ERROR, HTS221_OK]
00527 */
00528 HTS221_Error_et HTS221_IsMeasurementCompleted(void *handle, HTS221_BitStatus_et *Is_Measurement_Completed)
00529 {
00530     uint8_t tmp;
00531 
00532     if (HTS221_read_reg(handle, HTS221_STATUS_REG, 1, &tmp)) {
00533         return HTS221_ERROR;
00534     }
00535 
00536     if ((tmp & (uint8_t)(HTS221_HDA_MASK | HTS221_TDA_MASK)) == (uint8_t)(HTS221_HDA_MASK | HTS221_TDA_MASK)) {
00537         *Is_Measurement_Completed = HTS221_SET;
00538     } else {
00539         *Is_Measurement_Completed = HTS221_RESET;
00540     }
00541 
00542     return HTS221_OK;
00543 }
00544 
00545 
00546 /**
00547 * @brief  Set_ humidity and temperature average mode.
00548 * @param  *handle Device handle.
00549 * @param  avgh is the average mode for humidity, this parameter is @ref HTS221_Avgh_et.
00550 * @param  avgt is the average mode for temperature, this parameter is @ref HTS221_Avgt_et.
00551 * @retval Error code [HTS221_OK, HTS221_ERROR].
00552 */
00553 HTS221_Error_et HTS221_Set_AvgHT(void *handle, HTS221_Avgh_et avgh, HTS221_Avgt_et avgt)
00554 {
00555     uint8_t tmp;
00556 
00557     HTS221_assert_param(IS_HTS221_AVGH(avgh));
00558     HTS221_assert_param(IS_HTS221_AVGT(avgt));
00559 
00560     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00561         return HTS221_ERROR;
00562     }
00563 
00564     tmp &= ~(HTS221_AVGH_MASK | HTS221_AVGT_MASK);
00565     tmp |= (uint8_t)avgh;
00566     tmp |= (uint8_t)avgt;
00567 
00568     if (HTS221_write_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00569         return HTS221_ERROR;
00570     }
00571 
00572     return HTS221_OK;
00573 }
00574 
00575 /**
00576 * @brief  Set humidity average mode.
00577 * @param  *handle Device handle.
00578 * @param  avgh is the average mode for humidity, this parameter is @ref HTS221_Avgh_et.
00579 * @retval Error code [HTS221_OK, HTS221_ERROR].
00580 */
00581 HTS221_Error_et HTS221_Set_AvgH(void *handle, HTS221_Avgh_et avgh)
00582 {
00583     uint8_t tmp;
00584 
00585     HTS221_assert_param(IS_HTS221_AVGH(avgh));
00586 
00587     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00588         return HTS221_ERROR;
00589     }
00590 
00591     tmp &= ~HTS221_AVGH_MASK;
00592     tmp |= (uint8_t)avgh;
00593 
00594     if (HTS221_write_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00595         return HTS221_ERROR;
00596     }
00597 
00598     return HTS221_OK;
00599 }
00600 
00601 /**
00602 * @brief  Set temperature average mode.
00603 * @param  *handle Device handle.
00604 * @param  avgt is the average mode for temperature, this parameter is @ref HTS221_Avgt_et.
00605 * @retval Error code [HTS221_OK, HTS221_ERROR].
00606 */
00607 HTS221_Error_et HTS221_Set_AvgT(void *handle, HTS221_Avgt_et avgt)
00608 {
00609     uint8_t tmp;
00610 
00611     HTS221_assert_param(IS_HTS221_AVGT(avgt));
00612 
00613     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00614         return HTS221_ERROR;
00615     }
00616 
00617     tmp &= ~HTS221_AVGT_MASK;
00618     tmp |= (uint8_t)avgt;
00619 
00620     if (HTS221_write_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00621         return HTS221_ERROR;
00622     }
00623 
00624     return HTS221_OK;
00625 }
00626 
00627 /**
00628 * @brief  Get humidity and temperature average mode.
00629 * @param  *handle Device handle.
00630 * @param  avgh pointer to the returned value with the humidity average mode.
00631 * @param  avgt pointer to the returned value with the temperature average mode.
00632 * @retval Error code [HTS221_OK, HTS221_ERROR].
00633 */
00634 HTS221_Error_et HTS221_Get_AvgHT(void *handle, HTS221_Avgh_et *avgh, HTS221_Avgt_et *avgt)
00635 {
00636     uint8_t tmp;
00637 
00638     if (HTS221_read_reg(handle, HTS221_AV_CONF_REG, 1, &tmp)) {
00639         return HTS221_ERROR;
00640     }
00641 
00642     *avgh = (HTS221_Avgh_et)(tmp & HTS221_AVGH_MASK);
00643     *avgt = (HTS221_Avgt_et)(tmp & HTS221_AVGT_MASK);
00644 
00645     return HTS221_OK;
00646 }
00647 
00648 /**
00649 * @brief  Set block data update mode.
00650 * @param  *handle Device handle.
00651 * @param  status can be HTS221_ENABLE: enable the block data update, output data registers are updated once both MSB and LSB are read.
00652 * @param  status can be HTS221_DISABLE: output data registers are continuously updated.
00653 *         This parameter is a @ref HTS221_BitStatus_et.
00654 * @retval Error code [HTS221_OK, HTS221_ERROR].
00655 */
00656 HTS221_Error_et HTS221_Set_BduMode(void *handle, HTS221_State_et status)
00657 {
00658     uint8_t tmp;
00659 
00660     HTS221_assert_param(IS_HTS221_State(status));
00661 
00662     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00663         return HTS221_ERROR;
00664     }
00665 
00666     tmp &= ~HTS221_BDU_MASK;
00667     tmp |= ((uint8_t)status) << HTS221_BDU_BIT;
00668 
00669     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00670         return HTS221_ERROR;
00671     }
00672 
00673     return HTS221_OK;
00674 }
00675 
00676 /**
00677 * @brief  Get block data update mode.
00678 * @param  *handle Device handle.
00679 * @param  Pointer to the returned value with block data update mode status.
00680 * @retval Error code [HTS221_OK, HTS221_ERROR].
00681 */
00682 HTS221_Error_et HTS221_Get_BduMode(void *handle, HTS221_State_et *status)
00683 {
00684     uint8_t tmp;
00685 
00686     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00687         return HTS221_ERROR;
00688     }
00689 
00690     *status = (HTS221_State_et)((tmp & HTS221_BDU_MASK) >> HTS221_BDU_BIT);
00691 
00692     return HTS221_OK;
00693 }
00694 
00695 /**
00696 * @brief  Enter or exit from power down mode.
00697 * @param  *handle Device handle.
00698 * @param  status can be HTS221_SET: HTS221 in power down mode.
00699 * @param  status can be HTS221_REET: HTS221 in active mode.
00700 *         This parameter is a @ref HTS221_BitStatus_et.
00701 * @retval Error code [HTS221_OK, HTS221_ERROR].
00702 */
00703 HTS221_Error_et HTS221_Set_PowerDownMode(void *handle, HTS221_BitStatus_et status)
00704 {
00705     uint8_t tmp;
00706 
00707     HTS221_assert_param(IS_HTS221_BitStatus(status));
00708 
00709     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00710         return HTS221_ERROR;
00711     }
00712 
00713     tmp &= ~HTS221_PD_MASK;
00714     tmp |= ((uint8_t)status) << HTS221_PD_BIT;
00715 
00716     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00717         return HTS221_ERROR;
00718     }
00719 
00720     return HTS221_OK;
00721 }
00722 
00723 /**
00724 * @brief  Get if HTS221 is in active mode or in power down mode.
00725 * @param  *handle Device handle.
00726 * @param  Pointer to the returned value with HTS221 status.
00727 * @retval Error code [HTS221_OK, HTS221_ERROR].
00728 */
00729 HTS221_Error_et HTS221_Get_PowerDownMode(void *handle, HTS221_BitStatus_et *status)
00730 {
00731     uint8_t tmp;
00732 
00733     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00734         return HTS221_ERROR;
00735     }
00736 
00737     *status = (HTS221_BitStatus_et)((tmp & HTS221_PD_MASK) >> HTS221_PD_BIT);
00738 
00739     return HTS221_OK;
00740 }
00741 
00742 /**
00743 * @brief  Set the output data rate mode.
00744 * @param  *handle Device handle.
00745 * @param  odr is the output data rate mode.
00746 *         This parameter is a @ref HTS221_Odr_et.
00747 * @retval Error code [HTS221_OK, HTS221_ERROR].
00748 */
00749 HTS221_Error_et HTS221_Set_Odr(void *handle, HTS221_Odr_et odr)
00750 {
00751     uint8_t tmp;
00752 
00753     HTS221_assert_param(IS_HTS221_ODR(odr));
00754 
00755     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00756         return HTS221_ERROR;
00757     }
00758 
00759     tmp &= ~HTS221_ODR_MASK;
00760     tmp |= (uint8_t)odr;
00761 
00762     if (HTS221_write_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00763         return HTS221_ERROR;
00764     }
00765 
00766     return HTS221_OK;
00767 }
00768 
00769 /**
00770 * @brief  Get the output data rate mode.
00771 * @param  *handle Device handle.
00772 * @param  Pointer to the returned value with output data rate mode.
00773 * @retval Error code [HTS221_OK, HTS221_ERROR].
00774 */
00775 HTS221_Error_et HTS221_Get_Odr(void *handle, HTS221_Odr_et *odr)
00776 {
00777     uint8_t tmp;
00778 
00779     if (HTS221_read_reg(handle, HTS221_CTRL_REG1, 1, &tmp)) {
00780         return HTS221_ERROR;
00781     }
00782 
00783     tmp &= HTS221_ODR_MASK;
00784     *odr = (HTS221_Odr_et)tmp;
00785 
00786     return HTS221_OK;
00787 }
00788 
00789 /**
00790 * @brief  Reboot Memory Content.
00791 * @param  *handle Device handle.
00792 * @retval Error code [HTS221_OK, HTS221_ERROR].
00793 */
00794 HTS221_Error_et HTS221_MemoryBoot(void *handle)
00795 {
00796     uint8_t tmp;
00797 
00798     if (HTS221_read_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00799         return HTS221_ERROR;
00800     }
00801 
00802     tmp |= HTS221_BOOT_MASK;
00803 
00804     if (HTS221_write_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00805         return HTS221_ERROR;
00806     }
00807 
00808     return HTS221_OK;
00809 }
00810 
00811 /**
00812 * @brief  Configure the internal heater.
00813 * @param  *handle Device handle.
00814 * @param  The status of the internal heater [HTS221_ENABLE/HTS221_DISABLE].
00815 *         This parameter is a @ref HTS221_State_et.
00816 * @retval Error code [HTS221_OK, HTS221_ERROR]
00817 */
00818 HTS221_Error_et HTS221_Set_HeaterState(void *handle, HTS221_State_et status)
00819 {
00820     uint8_t tmp;
00821 
00822     HTS221_assert_param(IS_HTS221_State(status));
00823 
00824     if (HTS221_read_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00825         return HTS221_ERROR;
00826     }
00827 
00828     tmp &= ~HTS221_HEATHER_MASK;
00829     tmp |= ((uint8_t)status) << HTS221_HEATHER_BIT;
00830 
00831     if (HTS221_write_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00832         return HTS221_ERROR;
00833     }
00834 
00835     return HTS221_OK;
00836 }
00837 
00838 /**
00839 * @brief  Get the internal heater.
00840 * @param  *handle Device handle.
00841 * @param  Pointer to the returned status of the internal heater [HTS221_ENABLE/HTS221_DISABLE].
00842 * @retval Error code [HTS221_OK, HTS221_ERROR].
00843 */
00844 HTS221_Error_et HTS221_Get_HeaterState(void *handle, HTS221_State_et *status)
00845 {
00846     uint8_t tmp;
00847 
00848     if (HTS221_read_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00849         return HTS221_ERROR;
00850     }
00851 
00852     *status = (HTS221_State_et)((tmp & HTS221_HEATHER_MASK) >> HTS221_HEATHER_BIT);
00853 
00854     return HTS221_OK;
00855 }
00856 
00857 /**
00858 * @brief  Set ONE_SHOT bit to start a new conversion (ODR mode has to be 00).
00859 *         Once the measurement is done, ONE_SHOT bit is self-cleared.
00860 * @param  *handle Device handle.
00861 * @retval Error code [HTS221_OK, HTS221_ERROR].
00862 */
00863 HTS221_Error_et HTS221_StartOneShotMeasurement(void *handle)
00864 {
00865     uint8_t tmp;
00866 
00867     if (HTS221_read_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00868         return HTS221_ERROR;
00869     }
00870 
00871     tmp |= HTS221_ONE_SHOT_MASK;
00872 
00873     if (HTS221_write_reg(handle, HTS221_CTRL_REG2, 1, &tmp)) {
00874         return HTS221_ERROR;
00875     }
00876 
00877     return HTS221_OK;
00878 
00879 }
00880 
00881 /**
00882 * @brief  Set level configuration of the interrupt pin DRDY.
00883 * @param  *handle Device handle.
00884 * @param  status can be HTS221_LOW_LVL: active level is LOW.
00885 * @param  status can be HTS221_HIGH_LVL: active level is HIGH.
00886 *         This parameter is a @ref HTS221_State_et.
00887 * @retval Error code [HTS221_OK, HTS221_ERROR].
00888 */
00889 HTS221_Error_et HTS221_Set_IrqActiveLevel(void *handle, HTS221_DrdyLevel_et value)
00890 {
00891     uint8_t tmp;
00892 
00893     HTS221_assert_param(IS_HTS221_DrdyLevelType(value));
00894 
00895     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00896         return HTS221_ERROR;
00897     }
00898 
00899     tmp &= ~HTS221_DRDY_H_L_MASK;
00900     tmp |= (uint8_t)value;
00901 
00902     if (HTS221_write_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00903         return HTS221_ERROR;
00904     }
00905 
00906     return HTS221_OK;
00907 }
00908 
00909 /**
00910 * @brief  Get level configuration of the interrupt pin DRDY.
00911 * @param  *handle Device handle.
00912 * @param  Pointer to the returned status of the level configuration [HTS221_ENABLE/HTS221_DISABLE].
00913 * @retval Error code [HTS221_OK, HTS221_ERROR].
00914 */
00915 HTS221_Error_et HTS221_Get_IrqActiveLevel(void *handle, HTS221_DrdyLevel_et *value)
00916 {
00917     uint8_t tmp;
00918 
00919     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00920         return HTS221_ERROR;
00921     }
00922 
00923     *value = (HTS221_DrdyLevel_et)(tmp & HTS221_DRDY_H_L_MASK);
00924 
00925     return HTS221_OK;
00926 }
00927 
00928 /**
00929 * @brief  Set Push-pull/open drain configuration for the interrupt pin DRDY.
00930 * @param  *handle Device handle.
00931 * @param  value is the output type configuration.
00932 *         This parameter is a @ref HTS221_OutputType_et.
00933 * @retval Error code [HTS221_OK, HTS221_ERROR].
00934 */
00935 HTS221_Error_et HTS221_Set_IrqOutputType(void *handle, HTS221_OutputType_et value)
00936 {
00937     uint8_t tmp;
00938 
00939     HTS221_assert_param(IS_HTS221_OutputType(value));
00940 
00941     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00942         return HTS221_ERROR;
00943     }
00944 
00945     tmp &= ~HTS221_PP_OD_MASK;
00946     tmp |= (uint8_t)value;
00947 
00948     if (HTS221_write_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00949         return HTS221_ERROR;
00950     }
00951 
00952     return HTS221_OK;
00953 }
00954 
00955 /**
00956 * @brief  Get the configuration for the interrupt pin DRDY.
00957 * @param  *handle Device handle.
00958 * @param  Pointer to the returned value with output type configuration.
00959 * @retval Error code [HTS221_OK, HTS221_ERROR].
00960 */
00961 HTS221_Error_et HTS221_Get_IrqOutputType(void *handle, HTS221_OutputType_et *value)
00962 {
00963     uint8_t tmp;
00964 
00965     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00966         return HTS221_ERROR;
00967     }
00968 
00969     *value = (HTS221_OutputType_et)(tmp & HTS221_PP_OD_MASK);
00970 
00971     return HTS221_OK;
00972 }
00973 
00974 /**
00975 * @brief  Enable/disable the interrupt mode.
00976 * @param  *handle Device handle.
00977 * @param  status is the enable/disable for the interrupt mode.
00978 *         This parameter is a @ref HTS221_State_et.
00979 * @retval Error code [HTS221_OK, HTS221_ERROR].
00980 */
00981 HTS221_Error_et HTS221_Set_IrqEnable(void *handle, HTS221_State_et status)
00982 {
00983     uint8_t tmp;
00984 
00985     HTS221_assert_param(IS_HTS221_State(status));
00986 
00987     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00988         return HTS221_ERROR;
00989     }
00990 
00991     tmp &= ~HTS221_DRDY_MASK;
00992     tmp |= ((uint8_t)status) << HTS221_DRDY_BIT;
00993 
00994     if (HTS221_write_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
00995         return HTS221_ERROR;
00996     }
00997 
00998     return HTS221_OK;
00999 }
01000 
01001 /**
01002 * @brief  Get the interrupt mode.
01003 * @param  *handle Device handle.
01004 * @param  Pointer to the returned status of the interrupt mode configuration [HTS221_ENABLE/HTS221_DISABLE].
01005 * @retval Error code [HTS221_OK, HTS221_ERROR].
01006 */
01007 HTS221_Error_et HTS221_Get_IrqEnable(void *handle, HTS221_State_et *status)
01008 {
01009     uint8_t tmp;
01010 
01011     if (HTS221_read_reg(handle, HTS221_CTRL_REG3, 1, &tmp)) {
01012         return HTS221_ERROR;
01013     }
01014 
01015     *status = (HTS221_State_et)((tmp & HTS221_DRDY_MASK) >> HTS221_DRDY_BIT);
01016 
01017     return HTS221_OK;
01018 }
01019 
01020 
01021 #ifdef  USE_FULL_ASSERT_HTS221
01022 /**
01023 * @brief  Reports the name of the source file and the source line number
01024 *         where the assert_param error has occurred.
01025 * @param file: pointer to the source file name
01026 * @param line: assert_param error line source number
01027 * @retval : None
01028 */
01029 void HTS221_assert_failed(uint8_t *file, uint32_t line)
01030 {
01031     /* User can add his own implementation to report the file name and line number */
01032     printf("Wrong parameters value: file %s on line %d\r\n", file, (int)line);
01033 
01034     /* Infinite loop */
01035     while (1) {
01036     }
01037 }
01038 #endif
01039 
01040 #ifdef __cplusplus
01041 }
01042 #endif
01043 
01044 /**
01045 * @}
01046 */
01047 
01048 /**
01049 * @}
01050 */
01051 
01052 /**
01053 * @}
01054 */
01055 
01056 /******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/