Workshop example
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
HTS221Sensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file HTS221Sensor.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Implementation of an HTS221 Humidity and Temperature sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© 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 00039 /* Includes ------------------------------------------------------------------*/ 00040 00041 00042 #include "HTS221Sensor.h" 00043 00044 00045 /* Class Implementation ------------------------------------------------------*/ 00046 00047 HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) : 00048 _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin) // SPI3W ONLY 00049 { 00050 assert(spi); 00051 _dev_i2c = NULL; 00052 }; 00053 00054 /** Constructor 00055 * @param i2c object of an helper class which handles the I2C peripheral 00056 * @param address the address of the component's instance 00057 */ 00058 HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) : 00059 _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin) 00060 { 00061 assert(i2c); 00062 _dev_spi = NULL; 00063 }; 00064 00065 /** 00066 * @brief Initializing the component. 00067 * @param[in] init pointer to device specific initalization structure. 00068 * @retval "0" in case of success, an error code otherwise. 00069 */ 00070 int HTS221Sensor::init(void *init) 00071 { 00072 /* Power down the device */ 00073 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR ) 00074 { 00075 return 1; 00076 } 00077 00078 /* Enable BDU */ 00079 if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR ) 00080 { 00081 return 1; 00082 } 00083 00084 if(set_odr(1.0f) == 1) 00085 { 00086 return 1; 00087 } 00088 00089 return 0; 00090 } 00091 00092 /** 00093 * @brief Enable HTS221 00094 * @retval 0 in case of success, an error code otherwise 00095 */ 00096 int HTS221Sensor::enable(void) 00097 { 00098 /* Power up the device */ 00099 if ( HTS221_Activate( (void *)this ) == HTS221_ERROR ) 00100 { 00101 return 1; 00102 } 00103 00104 return 0; 00105 } 00106 00107 /** 00108 * @brief Disable HTS221 00109 * @retval 0 in case of success, an error code otherwise 00110 */ 00111 int HTS221Sensor::disable(void) 00112 { 00113 /* Power up the device */ 00114 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR ) 00115 { 00116 return 1; 00117 } 00118 00119 return 0; 00120 } 00121 00122 /** 00123 * @brief Read ID address of HTS221 00124 * @param id the pointer where the ID of the device is stored 00125 * @retval 0 in case of success, an error code otherwise 00126 */ 00127 int HTS221Sensor::read_id(uint8_t *id) 00128 { 00129 if(!id) 00130 { 00131 return 1; 00132 } 00133 00134 /* Read WHO AM I register */ 00135 if ( HTS221_Get_DeviceID( (void *)this, id ) == HTS221_ERROR ) 00136 { 00137 return 1; 00138 } 00139 00140 return 0; 00141 } 00142 00143 /** 00144 * @brief Reboot memory content of HTS221 00145 * @param None 00146 * @retval 0 in case of success, an error code otherwise 00147 */ 00148 int HTS221Sensor::reset(void) 00149 { 00150 uint8_t tmpreg; 00151 00152 /* Read CTRL_REG2 register */ 00153 if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0) 00154 { 00155 return 1; 00156 } 00157 00158 /* Enable or Disable the reboot memory */ 00159 tmpreg |= (0x01 << HTS221_BOOT_BIT); 00160 00161 /* Write value to MEMS CTRL_REG2 regsister */ 00162 if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0) 00163 { 00164 return 1; 00165 } 00166 00167 return 0; 00168 } 00169 00170 /** 00171 * @brief Read HTS221 output register, and calculate the humidity 00172 * @param pfData the pointer to data output 00173 * @retval 0 in case of success, an error code otherwise 00174 */ 00175 int HTS221Sensor::get_humidity(float* pfData) 00176 { 00177 uint16_t uint16data = 0; 00178 00179 /* Read data from HTS221. */ 00180 if ( HTS221_Get_Humidity( (void *)this, &uint16data ) == HTS221_ERROR ) 00181 { 00182 return 1; 00183 } 00184 00185 *pfData = ( float )uint16data / 10.0f; 00186 00187 return 0; 00188 } 00189 00190 /** 00191 * @brief Read HTS221 output register, and calculate the temperature 00192 * @param pfData the pointer to data output 00193 * @retval 0 in case of success, an error code otherwise 00194 */ 00195 int HTS221Sensor::get_temperature(float* pfData) 00196 { 00197 int16_t int16data = 0; 00198 00199 /* Read data from HTS221. */ 00200 if ( HTS221_Get_Temperature( (void *)this, &int16data ) == HTS221_ERROR ) 00201 { 00202 return 1; 00203 } 00204 00205 *pfData = ( float )int16data / 10.0f; 00206 00207 return 0; 00208 } 00209 00210 /** 00211 * @brief Read HTS221 output register, and calculate the humidity 00212 * @param odr the pointer to the output data rate 00213 * @retval 0 in case of success, an error code otherwise 00214 */ 00215 int HTS221Sensor::get_odr(float* odr) 00216 { 00217 HTS221_Odr_et odr_low_level; 00218 00219 if ( HTS221_Get_Odr( (void *)this, &odr_low_level ) == HTS221_ERROR ) 00220 { 00221 return 1; 00222 } 00223 00224 switch( odr_low_level ) 00225 { 00226 case HTS221_ODR_ONE_SHOT : 00227 *odr = 0.0f; 00228 break; 00229 case HTS221_ODR_1HZ : 00230 *odr = 1.0f; 00231 break; 00232 case HTS221_ODR_7HZ : 00233 *odr = 7.0f; 00234 break; 00235 case HTS221_ODR_12_5HZ : 00236 *odr = 12.5f; 00237 break; 00238 default : 00239 *odr = -1.0f; 00240 return 1; 00241 } 00242 00243 return 0; 00244 } 00245 00246 /** 00247 * @brief Set ODR 00248 * @param odr the output data rate to be set 00249 * @retval 0 in case of success, an error code otherwise 00250 */ 00251 int HTS221Sensor::set_odr(float odr) 00252 { 00253 HTS221_Odr_et new_odr; 00254 00255 new_odr = ( odr <= 1.0f ) ? HTS221_ODR_1HZ 00256 : ( odr <= 7.0f ) ? HTS221_ODR_7HZ 00257 : HTS221_ODR_12_5HZ ; 00258 00259 if ( HTS221_Set_Odr( (void *)this, new_odr ) == HTS221_ERROR ) 00260 { 00261 return 1; 00262 } 00263 00264 return 0; 00265 } 00266 00267 00268 /** 00269 * @brief Read the data from register 00270 * @param reg register address 00271 * @param data register data 00272 * @retval 0 in case of success 00273 * @retval 1 in case of failure 00274 */ 00275 int HTS221Sensor::read_reg( uint8_t reg, uint8_t *data ) 00276 { 00277 00278 if ( HTS221_read_reg( (void *)this, reg, 1, data ) == HTS221_ERROR ) 00279 { 00280 return 1; 00281 } 00282 00283 return 0; 00284 } 00285 00286 /** 00287 * @brief Write the data to register 00288 * @param reg register address 00289 * @param data register data 00290 * @retval 0 in case of success 00291 * @retval 1 in case of failure 00292 */ 00293 int HTS221Sensor::write_reg( uint8_t reg, uint8_t data ) 00294 { 00295 00296 if ( HTS221_write_reg( (void *)this, reg, 1, &data ) == HTS221_ERROR ) 00297 { 00298 return 1; 00299 } 00300 00301 return 0; 00302 } 00303 00304 uint8_t HTS221_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) 00305 { 00306 return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00307 } 00308 00309 uint8_t HTS221_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) 00310 { 00311 return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00312 }
Generated on Tue Jul 12 2022 22:34:14 by 1.7.2