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: X_NUCLEO_COMMON ST_INTERFACES
Dependents: X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3
STTS751Sensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file STTS751Sensor.cpp 00004 * @author SRA 00005 * @version V1.0.0 00006 * @date February 2019 00007 * @brief Implementation of a STTS751 temperature sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2019 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 "STTS751Sensor.h" 00042 00043 00044 /* Class Implementation ------------------------------------------------------*/ 00045 00046 /** Constructor 00047 * @param i2c object of an helper class which handles the I2C peripheral 00048 * @param address the address of the component's instance 00049 * @param int_pin the interrupt pin 00050 */ 00051 STTS751Sensor::STTS751Sensor(DevI2C *i2c, uint8_t address, PinName int_pin) : _dev_i2c(i2c), _address(address), _int_irq(int_pin) 00052 { 00053 assert(i2c); 00054 _reg_ctx.write_reg = STTS751_io_write; 00055 _reg_ctx.read_reg = STTS751_io_read; 00056 _reg_ctx.handle = (void *)this; 00057 } 00058 00059 /** 00060 * @brief Initializing the component 00061 * @param init pointer to device specific initalization structure 00062 * @retval 0 in case of success, an error code otherwise 00063 */ 00064 int STTS751Sensor::init(void *init) 00065 { 00066 /* Disable EVENT pin of SMBus. */ 00067 if (stts751_pin_event_route_set(&_reg_ctx, PROPERTY_ENABLE) != 0) { 00068 return 1; 00069 } 00070 00071 /* Set default ODR */ 00072 _last_odr = 1.0f; 00073 00074 /* Set the resolution to the maximum allowed value */ 00075 if (stts751_resolution_set(&_reg_ctx, STTS751_12bit) != 0) { 00076 return 1; 00077 } 00078 00079 /* Put the component in standby mode. */ 00080 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0) { 00081 return 1; 00082 } 00083 00084 _is_enabled = 0; 00085 00086 return 0; 00087 } 00088 00089 /** 00090 * @brief Get WHO_AM_I value 00091 * @param id the WHO_AM_I value 00092 * @retval 0 in case of success, an error code otherwise 00093 */ 00094 int STTS751Sensor::read_id(uint8_t *id) 00095 { 00096 stts751_id_t buf; 00097 00098 if (stts751_device_id_get(&_reg_ctx, &buf) != 0) { 00099 return 1; 00100 } 00101 00102 *id = buf.manufacturer_id; 00103 00104 return 0; 00105 } 00106 00107 /** 00108 * @brief Enable the STTS751 temperature sensor 00109 * @retval 0 in case of success, an error code otherwise 00110 */ 00111 int STTS751Sensor::enable() 00112 { 00113 /* Check if the component is already enabled */ 00114 if (_is_enabled == 1U) { 00115 return 0; 00116 } 00117 00118 /* Power on the component and set the odr. */ 00119 if (set_odr(_last_odr) != 0) { 00120 return 1; 00121 } 00122 00123 _is_enabled = 1; 00124 00125 return 0; 00126 } 00127 00128 /** 00129 * @brief Disable the STTS751 temperature sensor 00130 * @retval 0 in case of success, an error code otherwise 00131 */ 00132 int STTS751Sensor::disable() 00133 { 00134 /* Check if the component is already disabled */ 00135 if (_is_enabled == 0U) { 00136 return 0; 00137 } 00138 00139 /* Save the current odr. */ 00140 if (get_odr(&_last_odr) != 0) { 00141 return 1; 00142 } 00143 00144 /* Put the component in standby mode. */ 00145 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0) { 00146 return 1; 00147 } 00148 00149 _is_enabled = 0; 00150 00151 return 0; 00152 } 00153 00154 /** 00155 * @brief Get the STTS751 temperature sensor output data rate 00156 * @param odr pointer where the output data rate is written 00157 * @retval 0 in case of success, an error code otherwise 00158 */ 00159 int STTS751Sensor::get_odr(float *odr) 00160 { 00161 int ret = 0; 00162 stts751_odr_t odr_low_level; 00163 00164 if (stts751_temp_data_rate_get(&_reg_ctx, &odr_low_level) != 0) { 00165 return 1; 00166 } 00167 00168 switch (odr_low_level) { 00169 case STTS751_TEMP_ODR_OFF: 00170 case STTS751_TEMP_ODR_ONE_SHOT: 00171 *odr = 0.0f; 00172 break; 00173 00174 case STTS751_TEMP_ODR_62mHz5: 00175 *odr = 0.0625f; 00176 break; 00177 00178 case STTS751_TEMP_ODR_125mHz: 00179 *odr = 0.125f; 00180 break; 00181 00182 case STTS751_TEMP_ODR_250mHz: 00183 *odr = 0.250f; 00184 break; 00185 00186 case STTS751_TEMP_ODR_500mHz: 00187 *odr = 0.500f; 00188 break; 00189 00190 case STTS751_TEMP_ODR_1Hz: 00191 *odr = 1.0f; 00192 break; 00193 00194 case STTS751_TEMP_ODR_2Hz: 00195 *odr = 2.0f; 00196 break; 00197 00198 case STTS751_TEMP_ODR_4Hz: 00199 *odr = 4.0f; 00200 break; 00201 00202 case STTS751_TEMP_ODR_8Hz: 00203 *odr = 8.0f; 00204 break; 00205 00206 case STTS751_TEMP_ODR_16Hz: 00207 *odr = 16.0f; 00208 break; 00209 00210 case STTS751_TEMP_ODR_32Hz: 00211 *odr = 32.0f; 00212 break; 00213 00214 default: 00215 ret = 1; 00216 break; 00217 } 00218 00219 return ret; 00220 } 00221 00222 /** 00223 * @brief Set the STTS751 temperature sensor output data rate 00224 * @param odr the output data rate value to be set 00225 * @retval 0 in case of success, an error code otherwise 00226 */ 00227 int STTS751Sensor::set_odr(float odr) 00228 { 00229 stts751_odr_t new_odr; 00230 stts751_tres_t res; 00231 00232 /* Get the current resolution */ 00233 if (stts751_resolution_get(&_reg_ctx, &res) != 0) { 00234 return 1; 00235 } 00236 00237 /* If the requested odr is 16Hz we cannot use the 12 bits resolution */ 00238 if (odr == 16.0f && res == STTS751_12bit) { 00239 /* We force resolution to the maximum allowed value */ 00240 if (stts751_resolution_set(&_reg_ctx, STTS751_11bit) != 0) { 00241 return 1; 00242 } 00243 } 00244 00245 /* If the requested odr is 32Hz we cannot use the 12 bits and 11 bits resolutions */ 00246 if (odr == 32.0f && (res == STTS751_12bit || res == STTS751_11bit)) { 00247 /* We force resolution to the maximum allowed value */ 00248 if (stts751_resolution_set(&_reg_ctx, STTS751_10bit) != 0) { 00249 return 1; 00250 } 00251 } 00252 00253 new_odr = (odr <= 0.0625f) ? STTS751_TEMP_ODR_62mHz5 00254 : (odr <= 0.125f) ? STTS751_TEMP_ODR_125mHz 00255 : (odr <= 0.25f) ? STTS751_TEMP_ODR_250mHz 00256 : (odr <= 0.5f) ? STTS751_TEMP_ODR_500mHz 00257 : (odr <= 1.0f) ? STTS751_TEMP_ODR_1Hz 00258 : (odr <= 2.0f) ? STTS751_TEMP_ODR_2Hz 00259 : (odr <= 4.0f) ? STTS751_TEMP_ODR_4Hz 00260 : (odr <= 8.0f) ? STTS751_TEMP_ODR_8Hz 00261 : (odr <= 16.0f) ? STTS751_TEMP_ODR_16Hz 00262 : STTS751_TEMP_ODR_32Hz; 00263 00264 if (stts751_temp_data_rate_set(&_reg_ctx, new_odr) != 0) { 00265 return 1; 00266 } 00267 00268 return 0; 00269 } 00270 00271 /** 00272 * @brief Get the STTS751 temperature value 00273 * @param value pointer where the temperature value is written 00274 * @retval 0 in case of success, an error code otherwise 00275 */ 00276 int STTS751Sensor::get_temperature(float *value) 00277 { 00278 int16_t raw_value = 0; 00279 00280 /* Get the temperature */ 00281 if (stts751_temperature_raw_get(&_reg_ctx, &raw_value) != 0) { 00282 return 1; 00283 } 00284 00285 *value = stts751_from_lsb_to_celsius(raw_value); 00286 00287 return 0; 00288 } 00289 00290 /** 00291 * @brief Get the STTS751 temperature data ready bit value 00292 * @param status the status of data ready bit 00293 * @retval 0 in case of success, an error code otherwise 00294 */ 00295 int STTS751Sensor::get_temp_drdy_status(uint8_t *status) 00296 { 00297 uint8_t val; 00298 00299 if (stts751_flag_busy_get(&_reg_ctx, &val) != 0) { 00300 return 1; 00301 } 00302 00303 if (val) { 00304 *status = 0; 00305 } else { 00306 *status = 1; 00307 } 00308 00309 return 0; 00310 } 00311 00312 /** 00313 * @brief Set the STTS751 low temperature threshold value 00314 * @param value the low temperature threshold to be set 00315 * @retval 0 in case of success, an error code otherwise 00316 */ 00317 int STTS751Sensor::set_low_temp_thr(float value) 00318 { 00319 int16_t raw_value; 00320 00321 raw_value = stts751_from_celsius_to_lsb(value); 00322 00323 /* Set the temperature threshold */ 00324 if (stts751_low_temperature_threshold_set(&_reg_ctx, raw_value) != 0) { 00325 return 1; 00326 } 00327 00328 return 0; 00329 } 00330 00331 /** 00332 * @brief Set the STTS751 high temperature threshold value 00333 * @param value the high temperature threshold to be set 00334 * @retval 0 in case of success, an error code otherwise 00335 */ 00336 int STTS751Sensor::set_high_temp_thr(float value) 00337 { 00338 int16_t raw_value; 00339 00340 raw_value = stts751_from_celsius_to_lsb(value); 00341 00342 /* Set the temperature threshold */ 00343 if (stts751_high_temperature_threshold_set(&_reg_ctx, raw_value) != 0) { 00344 return 1; 00345 } 00346 00347 return 0; 00348 } 00349 00350 /** 00351 * @brief Get the STTS751 temperature limits status 00352 * @param high_limit indicates that high temperature limit has been exceeded 00353 * @param low_limit indicates that low temperature limit has been exceeded 00354 * @param therm_limit indicates that therm temperature limit has been exceeded 00355 * @retval 0 in case of success, an error code otherwise 00356 */ 00357 int STTS751Sensor::get_temp_limit_status(uint8_t *high_limit, uint8_t *low_limit, uint8_t *therm_limit) 00358 { 00359 stts751_status_t status; 00360 00361 /* Read status register */ 00362 if (stts751_status_reg_get(&_reg_ctx, &status) != 0) { 00363 return 1; 00364 } 00365 00366 if(high_limit) { 00367 *high_limit = status.t_high; 00368 } 00369 00370 if(low_limit) { 00371 *low_limit = status.t_low; 00372 } 00373 00374 if(therm_limit) { 00375 *therm_limit = status.thrm; 00376 } 00377 00378 return 0; 00379 } 00380 00381 /** 00382 * @brief Enable or disable interrupt on EVENT pin 00383 * @param enable 0 disable the EVENT pin, 1 enable EVENT pin 00384 * @retval 0 in case of success, an error code otherwise 00385 */ 00386 int STTS751Sensor::set_event_pin(uint8_t enable) 00387 { 00388 uint8_t state; 00389 00390 /* The MASK1 bit in configuration register has inverted logic */ 00391 if (enable == 0) { 00392 state = PROPERTY_ENABLE; 00393 } else { 00394 state = PROPERTY_DISABLE; 00395 } 00396 00397 if (stts751_pin_event_route_set(&_reg_ctx, state) != 0) { 00398 return 1; 00399 } 00400 00401 return 0; 00402 } 00403 00404 /** 00405 * @brief Get the STTS751 register value 00406 * @param reg address to be read 00407 * @param data pointer where the value is written 00408 * @retval 0 in case of success, an error code otherwise 00409 */ 00410 int STTS751Sensor::read_reg(uint8_t reg, uint8_t *data) 00411 { 00412 if (stts751_read_reg(&_reg_ctx, reg, data, 1) != 0) { 00413 return 1; 00414 } 00415 00416 return 0; 00417 } 00418 00419 /** 00420 * @brief Set the STTS751 register value 00421 * @param reg address to be written 00422 * @param data value to be written 00423 * @retval 0 in case of success, an error code otherwise 00424 */ 00425 int STTS751Sensor::write_reg(uint8_t reg, uint8_t data) 00426 { 00427 if (stts751_write_reg(&_reg_ctx, reg, &data, 1) != 0) { 00428 return 1; 00429 } 00430 00431 return 0; 00432 } 00433 00434 /** 00435 * @brief Set the STTS751 One Shot Mode 00436 * @retval 0 in case of success, an error code otherwise 00437 */ 00438 int STTS751Sensor::set_one_shot() 00439 { 00440 /* Start One Shot Measurement */ 00441 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_ONE_SHOT) != 0) { 00442 return 1; 00443 } 00444 00445 return 0; 00446 } 00447 00448 /** 00449 * @brief Get the STTS751 One Shot Status 00450 * @param status pointer to the one shot status (1 means measurements available, 0 means measurements not available yet) 00451 * @retval 0 in case of success, an error code otherwise 00452 */ 00453 int STTS751Sensor::get_one_shot_status(uint8_t *status) 00454 { 00455 uint8_t busy; 00456 00457 /* Get Busy flag */ 00458 if (stts751_flag_busy_get(&_reg_ctx, &busy) != 0) { 00459 return 1; 00460 } 00461 00462 if (busy) { 00463 *status = 0; 00464 } else { 00465 *status = 1; 00466 } 00467 00468 return 0; 00469 } 00470 00471 00472 int32_t STTS751_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) 00473 { 00474 return ((STTS751Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00475 } 00476 00477 int32_t STTS751_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) 00478 { 00479 return ((STTS751Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00480 }
Generated on Fri Jul 15 2022 18:33:31 by
1.7.2