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