Low voltage digital temperature sensor

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3 X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Wed Mar 06 10:18:41 2019 +0000
Revision:
0:128dae13c732
Child:
1:5859badee052
First version of STTS751 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:128dae13c732 1 /**
cparata 0:128dae13c732 2 ******************************************************************************
cparata 0:128dae13c732 3 * @file STTS751Sensor.cpp
cparata 0:128dae13c732 4 * @author SRA
cparata 0:128dae13c732 5 * @version V1.0.0
cparata 0:128dae13c732 6 * @date February 2019
cparata 0:128dae13c732 7 * @brief Implementation of a STTS751 temperature sensor.
cparata 0:128dae13c732 8 ******************************************************************************
cparata 0:128dae13c732 9 * @attention
cparata 0:128dae13c732 10 *
cparata 0:128dae13c732 11 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:128dae13c732 12 *
cparata 0:128dae13c732 13 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:128dae13c732 14 * are permitted provided that the following conditions are met:
cparata 0:128dae13c732 15 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:128dae13c732 16 * this list of conditions and the following disclaimer.
cparata 0:128dae13c732 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:128dae13c732 18 * this list of conditions and the following disclaimer in the documentation
cparata 0:128dae13c732 19 * and/or other materials provided with the distribution.
cparata 0:128dae13c732 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:128dae13c732 21 * may be used to endorse or promote products derived from this software
cparata 0:128dae13c732 22 * without specific prior written permission.
cparata 0:128dae13c732 23 *
cparata 0:128dae13c732 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:128dae13c732 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:128dae13c732 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:128dae13c732 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:128dae13c732 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:128dae13c732 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:128dae13c732 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:128dae13c732 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:128dae13c732 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:128dae13c732 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:128dae13c732 34 *
cparata 0:128dae13c732 35 ******************************************************************************
cparata 0:128dae13c732 36 */
cparata 0:128dae13c732 37
cparata 0:128dae13c732 38
cparata 0:128dae13c732 39 /* Includes ------------------------------------------------------------------*/
cparata 0:128dae13c732 40
cparata 0:128dae13c732 41 #include "STTS751Sensor.h"
cparata 0:128dae13c732 42
cparata 0:128dae13c732 43
cparata 0:128dae13c732 44 /* Class Implementation ------------------------------------------------------*/
cparata 0:128dae13c732 45
cparata 0:128dae13c732 46 /** Constructor
cparata 0:128dae13c732 47 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:128dae13c732 48 * @param address the address of the component's instance
cparata 0:128dae13c732 49 * @param int_pin the interrupt pin
cparata 0:128dae13c732 50 */
cparata 0:128dae13c732 51 STTS751Sensor::STTS751Sensor(DevI2C *i2c, uint8_t address, PinName int_pin) : _dev_i2c(i2c), _address(address), _int_irq(int_pin)
cparata 0:128dae13c732 52 {
cparata 0:128dae13c732 53 assert (i2c);
cparata 0:128dae13c732 54 _reg_ctx.write_reg = STTS751_io_write;
cparata 0:128dae13c732 55 _reg_ctx.read_reg = STTS751_io_read;
cparata 0:128dae13c732 56 _reg_ctx.handle = (void *)this;
cparata 0:128dae13c732 57 }
cparata 0:128dae13c732 58
cparata 0:128dae13c732 59 /**
cparata 0:128dae13c732 60 * @brief Initializing the component
cparata 0:128dae13c732 61 * @param init pointer to device specific initalization structure
cparata 0:128dae13c732 62 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 63 */
cparata 0:128dae13c732 64 int STTS751Sensor::init(void *init)
cparata 0:128dae13c732 65 {
cparata 0:128dae13c732 66 /* Disable EVENT pin of SMBus. */
cparata 0:128dae13c732 67 if (stts751_pin_event_route_set(&_reg_ctx, PROPERTY_ENABLE) != 0)
cparata 0:128dae13c732 68 {
cparata 0:128dae13c732 69 return 1;
cparata 0:128dae13c732 70 }
cparata 0:128dae13c732 71
cparata 0:128dae13c732 72 /* Set default ODR */
cparata 0:128dae13c732 73 _last_odr = 1.0f;
cparata 0:128dae13c732 74
cparata 0:128dae13c732 75 /* Set the resolution to the maximum allowed value */
cparata 0:128dae13c732 76 if (stts751_resolution_set(&_reg_ctx, STTS751_12bit) != 0)
cparata 0:128dae13c732 77 {
cparata 0:128dae13c732 78 return 1;
cparata 0:128dae13c732 79 }
cparata 0:128dae13c732 80
cparata 0:128dae13c732 81 /* Put the component in standby mode. */
cparata 0:128dae13c732 82 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0)
cparata 0:128dae13c732 83 {
cparata 0:128dae13c732 84 return 1;
cparata 0:128dae13c732 85 }
cparata 0:128dae13c732 86
cparata 0:128dae13c732 87 _is_enabled = 0;
cparata 0:128dae13c732 88
cparata 0:128dae13c732 89 return 0;
cparata 0:128dae13c732 90 }
cparata 0:128dae13c732 91
cparata 0:128dae13c732 92 /**
cparata 0:128dae13c732 93 * @brief Get WHO_AM_I value
cparata 0:128dae13c732 94 * @param id the WHO_AM_I value
cparata 0:128dae13c732 95 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 96 */
cparata 0:128dae13c732 97 int STTS751Sensor::read_id(uint8_t *id)
cparata 0:128dae13c732 98 {
cparata 0:128dae13c732 99 stts751_id_t buf;
cparata 0:128dae13c732 100
cparata 0:128dae13c732 101 if (stts751_device_id_get(&_reg_ctx, &buf) != 0)
cparata 0:128dae13c732 102 {
cparata 0:128dae13c732 103 return 1;
cparata 0:128dae13c732 104 }
cparata 0:128dae13c732 105
cparata 0:128dae13c732 106 *id = buf.manufacturer_id;
cparata 0:128dae13c732 107
cparata 0:128dae13c732 108 return 0;
cparata 0:128dae13c732 109 }
cparata 0:128dae13c732 110
cparata 0:128dae13c732 111 /**
cparata 0:128dae13c732 112 * @brief Enable the STTS751 temperature sensor
cparata 0:128dae13c732 113 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 114 */
cparata 0:128dae13c732 115 int STTS751Sensor::enable()
cparata 0:128dae13c732 116 {
cparata 0:128dae13c732 117 /* Check if the component is already enabled */
cparata 0:128dae13c732 118 if (_is_enabled == 1U)
cparata 0:128dae13c732 119 {
cparata 0:128dae13c732 120 return 0;
cparata 0:128dae13c732 121 }
cparata 0:128dae13c732 122
cparata 0:128dae13c732 123 /* Power on the component and set the odr. */
cparata 0:128dae13c732 124 if (set_odr(_last_odr) != 0)
cparata 0:128dae13c732 125 {
cparata 0:128dae13c732 126 return 1;
cparata 0:128dae13c732 127 }
cparata 0:128dae13c732 128
cparata 0:128dae13c732 129 _is_enabled = 1;
cparata 0:128dae13c732 130
cparata 0:128dae13c732 131 return 0;
cparata 0:128dae13c732 132 }
cparata 0:128dae13c732 133
cparata 0:128dae13c732 134 /**
cparata 0:128dae13c732 135 * @brief Disable the STTS751 temperature sensor
cparata 0:128dae13c732 136 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 137 */
cparata 0:128dae13c732 138 int STTS751Sensor::disable()
cparata 0:128dae13c732 139 {
cparata 0:128dae13c732 140 /* Check if the component is already disabled */
cparata 0:128dae13c732 141 if (_is_enabled == 0U)
cparata 0:128dae13c732 142 {
cparata 0:128dae13c732 143 return 0;
cparata 0:128dae13c732 144 }
cparata 0:128dae13c732 145
cparata 0:128dae13c732 146 /* Save the current odr. */
cparata 0:128dae13c732 147 if (get_odr(&_last_odr) != 0)
cparata 0:128dae13c732 148 {
cparata 0:128dae13c732 149 return 1;
cparata 0:128dae13c732 150 }
cparata 0:128dae13c732 151
cparata 0:128dae13c732 152 /* Put the component in standby mode. */
cparata 0:128dae13c732 153 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0)
cparata 0:128dae13c732 154 {
cparata 0:128dae13c732 155 return 1;
cparata 0:128dae13c732 156 }
cparata 0:128dae13c732 157
cparata 0:128dae13c732 158 _is_enabled = 0;
cparata 0:128dae13c732 159
cparata 0:128dae13c732 160 return 0;
cparata 0:128dae13c732 161 }
cparata 0:128dae13c732 162
cparata 0:128dae13c732 163 /**
cparata 0:128dae13c732 164 * @brief Get the STTS751 temperature sensor output data rate
cparata 0:128dae13c732 165 * @param odr pointer where the output data rate is written
cparata 0:128dae13c732 166 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 167 */
cparata 0:128dae13c732 168 int STTS751Sensor::get_odr(float *odr)
cparata 0:128dae13c732 169 {
cparata 0:128dae13c732 170 int ret = 0;
cparata 0:128dae13c732 171 stts751_odr_t odr_low_level;
cparata 0:128dae13c732 172
cparata 0:128dae13c732 173 if (stts751_temp_data_rate_get(&_reg_ctx, &odr_low_level) != 0)
cparata 0:128dae13c732 174 {
cparata 0:128dae13c732 175 return 1;
cparata 0:128dae13c732 176 }
cparata 0:128dae13c732 177
cparata 0:128dae13c732 178 switch (odr_low_level)
cparata 0:128dae13c732 179 {
cparata 0:128dae13c732 180 case STTS751_TEMP_ODR_OFF:
cparata 0:128dae13c732 181 case STTS751_TEMP_ODR_ONE_SHOT:
cparata 0:128dae13c732 182 *odr = 0.0f;
cparata 0:128dae13c732 183 break;
cparata 0:128dae13c732 184
cparata 0:128dae13c732 185 case STTS751_TEMP_ODR_62mHz5:
cparata 0:128dae13c732 186 *odr = 0.0625f;
cparata 0:128dae13c732 187 break;
cparata 0:128dae13c732 188
cparata 0:128dae13c732 189 case STTS751_TEMP_ODR_125mHz:
cparata 0:128dae13c732 190 *odr = 0.125f;
cparata 0:128dae13c732 191 break;
cparata 0:128dae13c732 192
cparata 0:128dae13c732 193 case STTS751_TEMP_ODR_250mHz:
cparata 0:128dae13c732 194 *odr = 0.250f;
cparata 0:128dae13c732 195 break;
cparata 0:128dae13c732 196
cparata 0:128dae13c732 197 case STTS751_TEMP_ODR_500mHz:
cparata 0:128dae13c732 198 *odr = 0.500f;
cparata 0:128dae13c732 199 break;
cparata 0:128dae13c732 200
cparata 0:128dae13c732 201 case STTS751_TEMP_ODR_1Hz:
cparata 0:128dae13c732 202 *odr = 1.0f;
cparata 0:128dae13c732 203 break;
cparata 0:128dae13c732 204
cparata 0:128dae13c732 205 case STTS751_TEMP_ODR_2Hz:
cparata 0:128dae13c732 206 *odr = 2.0f;
cparata 0:128dae13c732 207 break;
cparata 0:128dae13c732 208
cparata 0:128dae13c732 209 case STTS751_TEMP_ODR_4Hz:
cparata 0:128dae13c732 210 *odr = 4.0f;
cparata 0:128dae13c732 211 break;
cparata 0:128dae13c732 212
cparata 0:128dae13c732 213 case STTS751_TEMP_ODR_8Hz:
cparata 0:128dae13c732 214 *odr = 8.0f;
cparata 0:128dae13c732 215 break;
cparata 0:128dae13c732 216
cparata 0:128dae13c732 217 case STTS751_TEMP_ODR_16Hz:
cparata 0:128dae13c732 218 *odr = 16.0f;
cparata 0:128dae13c732 219 break;
cparata 0:128dae13c732 220
cparata 0:128dae13c732 221 case STTS751_TEMP_ODR_32Hz:
cparata 0:128dae13c732 222 *odr = 32.0f;
cparata 0:128dae13c732 223 break;
cparata 0:128dae13c732 224
cparata 0:128dae13c732 225 default:
cparata 0:128dae13c732 226 ret = 1;
cparata 0:128dae13c732 227 break;
cparata 0:128dae13c732 228 }
cparata 0:128dae13c732 229
cparata 0:128dae13c732 230 return ret;
cparata 0:128dae13c732 231 }
cparata 0:128dae13c732 232
cparata 0:128dae13c732 233 /**
cparata 0:128dae13c732 234 * @brief Set the STTS751 temperature sensor output data rate
cparata 0:128dae13c732 235 * @param odr the output data rate value to be set
cparata 0:128dae13c732 236 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 237 */
cparata 0:128dae13c732 238 int STTS751Sensor::set_odr(float odr)
cparata 0:128dae13c732 239 {
cparata 0:128dae13c732 240 stts751_odr_t new_odr;
cparata 0:128dae13c732 241 stts751_tres_t res;
cparata 0:128dae13c732 242
cparata 0:128dae13c732 243 /* Get the current resolution */
cparata 0:128dae13c732 244 if (stts751_resolution_get(&_reg_ctx, &res) != 0)
cparata 0:128dae13c732 245 {
cparata 0:128dae13c732 246 return 1;
cparata 0:128dae13c732 247 }
cparata 0:128dae13c732 248
cparata 0:128dae13c732 249 /* If the requested odr is 16Hz we cannot use the 12 bits resolution */
cparata 0:128dae13c732 250 if(odr == 16.0f && res == STTS751_12bit)
cparata 0:128dae13c732 251 {
cparata 0:128dae13c732 252 /* We force resolution to the maximum allowed value */
cparata 0:128dae13c732 253 if (stts751_resolution_set(&_reg_ctx, STTS751_11bit) != 0)
cparata 0:128dae13c732 254 {
cparata 0:128dae13c732 255 return 1;
cparata 0:128dae13c732 256 }
cparata 0:128dae13c732 257 }
cparata 0:128dae13c732 258
cparata 0:128dae13c732 259 /* If the requested odr is 32Hz we cannot use the 12 bits and 11 bits resolutions */
cparata 0:128dae13c732 260 if(odr == 32.0f && (res == STTS751_12bit || res == STTS751_11bit))
cparata 0:128dae13c732 261 {
cparata 0:128dae13c732 262 /* We force resolution to the maximum allowed value */
cparata 0:128dae13c732 263 if (stts751_resolution_set(&_reg_ctx, STTS751_10bit) != 0)
cparata 0:128dae13c732 264 {
cparata 0:128dae13c732 265 return 1;
cparata 0:128dae13c732 266 }
cparata 0:128dae13c732 267 }
cparata 0:128dae13c732 268
cparata 0:128dae13c732 269 new_odr = (odr <= 0.0625f) ? STTS751_TEMP_ODR_62mHz5
cparata 0:128dae13c732 270 : (odr <= 0.125f ) ? STTS751_TEMP_ODR_125mHz
cparata 0:128dae13c732 271 : (odr <= 0.25f ) ? STTS751_TEMP_ODR_250mHz
cparata 0:128dae13c732 272 : (odr <= 0.5f ) ? STTS751_TEMP_ODR_500mHz
cparata 0:128dae13c732 273 : (odr <= 1.0f ) ? STTS751_TEMP_ODR_1Hz
cparata 0:128dae13c732 274 : (odr <= 2.0f ) ? STTS751_TEMP_ODR_2Hz
cparata 0:128dae13c732 275 : (odr <= 4.0f ) ? STTS751_TEMP_ODR_4Hz
cparata 0:128dae13c732 276 : (odr <= 8.0f ) ? STTS751_TEMP_ODR_8Hz
cparata 0:128dae13c732 277 : (odr <= 16.0f ) ? STTS751_TEMP_ODR_16Hz
cparata 0:128dae13c732 278 : STTS751_TEMP_ODR_32Hz;
cparata 0:128dae13c732 279
cparata 0:128dae13c732 280 if (stts751_temp_data_rate_set(&_reg_ctx, new_odr) != 0)
cparata 0:128dae13c732 281 {
cparata 0:128dae13c732 282 return 1;
cparata 0:128dae13c732 283 }
cparata 0:128dae13c732 284
cparata 0:128dae13c732 285 return 0;
cparata 0:128dae13c732 286 }
cparata 0:128dae13c732 287
cparata 0:128dae13c732 288 /**
cparata 0:128dae13c732 289 * @brief Get the STTS751 temperature value
cparata 0:128dae13c732 290 * @param value pointer where the temperature value is written
cparata 0:128dae13c732 291 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 292 */
cparata 0:128dae13c732 293 int STTS751Sensor::get_temperature(float *value)
cparata 0:128dae13c732 294 {
cparata 0:128dae13c732 295 int16_t raw_value = 0;
cparata 0:128dae13c732 296
cparata 0:128dae13c732 297 /* Get the temperature */
cparata 0:128dae13c732 298 if (stts751_temperature_raw_get(&_reg_ctx, &raw_value) != 0)
cparata 0:128dae13c732 299 {
cparata 0:128dae13c732 300 return 1;
cparata 0:128dae13c732 301 }
cparata 0:128dae13c732 302
cparata 0:128dae13c732 303 *value = stts751_from_lsb_to_celsius(raw_value);
cparata 0:128dae13c732 304
cparata 0:128dae13c732 305 return 0;
cparata 0:128dae13c732 306 }
cparata 0:128dae13c732 307
cparata 0:128dae13c732 308 /**
cparata 0:128dae13c732 309 * @brief Get the STTS751 temperature data ready bit value
cparata 0:128dae13c732 310 * @param status the status of data ready bit
cparata 0:128dae13c732 311 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 312 */
cparata 0:128dae13c732 313 int STTS751Sensor::get_temp_drdy_status(uint8_t *status)
cparata 0:128dae13c732 314 {
cparata 0:128dae13c732 315 uint8_t val;
cparata 0:128dae13c732 316
cparata 0:128dae13c732 317 if (stts751_flag_busy_get(&_reg_ctx, &val) != 0)
cparata 0:128dae13c732 318 {
cparata 0:128dae13c732 319 return 1;
cparata 0:128dae13c732 320 }
cparata 0:128dae13c732 321
cparata 0:128dae13c732 322 if(val)
cparata 0:128dae13c732 323 {
cparata 0:128dae13c732 324 *status = 0;
cparata 0:128dae13c732 325 } else
cparata 0:128dae13c732 326 {
cparata 0:128dae13c732 327 *status = 1;
cparata 0:128dae13c732 328 }
cparata 0:128dae13c732 329
cparata 0:128dae13c732 330 return 0;
cparata 0:128dae13c732 331 }
cparata 0:128dae13c732 332
cparata 0:128dae13c732 333 /**
cparata 0:128dae13c732 334 * @brief Set the STTS751 low temperature threshold value
cparata 0:128dae13c732 335 * @param value the low temperature threshold to be set
cparata 0:128dae13c732 336 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 337 */
cparata 0:128dae13c732 338 int STTS751Sensor::set_low_temp_thr(float value)
cparata 0:128dae13c732 339 {
cparata 0:128dae13c732 340 int16_t raw_value;
cparata 0:128dae13c732 341
cparata 0:128dae13c732 342 raw_value = stts751_from_celsius_to_lsb(value);
cparata 0:128dae13c732 343
cparata 0:128dae13c732 344 /* Set the temperature threshold */
cparata 0:128dae13c732 345 if (stts751_low_temperature_threshold_set(&_reg_ctx, raw_value) != 0)
cparata 0:128dae13c732 346 {
cparata 0:128dae13c732 347 return 1;
cparata 0:128dae13c732 348 }
cparata 0:128dae13c732 349
cparata 0:128dae13c732 350 return 0;
cparata 0:128dae13c732 351 }
cparata 0:128dae13c732 352
cparata 0:128dae13c732 353 /**
cparata 0:128dae13c732 354 * @brief Set the STTS751 high temperature threshold value
cparata 0:128dae13c732 355 * @param value the high temperature threshold to be set
cparata 0:128dae13c732 356 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 357 */
cparata 0:128dae13c732 358 int STTS751Sensor::set_high_temp_thr(float value)
cparata 0:128dae13c732 359 {
cparata 0:128dae13c732 360 int16_t raw_value;
cparata 0:128dae13c732 361
cparata 0:128dae13c732 362 raw_value = stts751_from_celsius_to_lsb(value);
cparata 0:128dae13c732 363
cparata 0:128dae13c732 364 /* Set the temperature threshold */
cparata 0:128dae13c732 365 if (stts751_high_temperature_threshold_set(&_reg_ctx, raw_value) != 0)
cparata 0:128dae13c732 366 {
cparata 0:128dae13c732 367 return 1;
cparata 0:128dae13c732 368 }
cparata 0:128dae13c732 369
cparata 0:128dae13c732 370 return 0;
cparata 0:128dae13c732 371 }
cparata 0:128dae13c732 372
cparata 0:128dae13c732 373 /**
cparata 0:128dae13c732 374 * @brief Get the STTS751 temperature limits status
cparata 0:128dae13c732 375 * @param high_limit indicates that high temperature limit has been exceeded
cparata 0:128dae13c732 376 * @param low_limit indicates that low temperature limit has been exceeded
cparata 0:128dae13c732 377 * @param therm_limit indicates that therm temperature limit has been exceeded
cparata 0:128dae13c732 378 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 379 */
cparata 0:128dae13c732 380 int STTS751Sensor::get_temp_limit_status(uint8_t *high_limit, uint8_t *low_limit, uint8_t *therm_limit)
cparata 0:128dae13c732 381 {
cparata 0:128dae13c732 382 stts751_status_t status;
cparata 0:128dae13c732 383
cparata 0:128dae13c732 384 /* Read status register */
cparata 0:128dae13c732 385 if (stts751_status_reg_get(&_reg_ctx, &status) != 0)
cparata 0:128dae13c732 386 {
cparata 0:128dae13c732 387 return 1;
cparata 0:128dae13c732 388 }
cparata 0:128dae13c732 389
cparata 0:128dae13c732 390 *high_limit = status.t_high;
cparata 0:128dae13c732 391 *low_limit = status.t_low;
cparata 0:128dae13c732 392 *therm_limit = status.thrm;
cparata 0:128dae13c732 393
cparata 0:128dae13c732 394 return 0;
cparata 0:128dae13c732 395 }
cparata 0:128dae13c732 396
cparata 0:128dae13c732 397 /**
cparata 0:128dae13c732 398 * @brief Enable or disable interrupt on EVENT pin
cparata 0:128dae13c732 399 * @param enable 0 disable the EVENT pin, 1 enable EVENT pin
cparata 0:128dae13c732 400 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 401 */
cparata 0:128dae13c732 402 int STTS751Sensor::set_event_pin(uint8_t enable)
cparata 0:128dae13c732 403 {
cparata 0:128dae13c732 404 uint8_t state;
cparata 0:128dae13c732 405
cparata 0:128dae13c732 406 /* The MASK1 bit in configuration register has inverted logic */
cparata 0:128dae13c732 407 if (enable == 0) state = PROPERTY_ENABLE; else state = PROPERTY_DISABLE;
cparata 0:128dae13c732 408
cparata 0:128dae13c732 409 if (stts751_pin_event_route_set(&_reg_ctx, state) != 0)
cparata 0:128dae13c732 410 {
cparata 0:128dae13c732 411 return 1;
cparata 0:128dae13c732 412 }
cparata 0:128dae13c732 413
cparata 0:128dae13c732 414 return 0;
cparata 0:128dae13c732 415 }
cparata 0:128dae13c732 416
cparata 0:128dae13c732 417 /**
cparata 0:128dae13c732 418 * @brief Get the STTS751 register value
cparata 0:128dae13c732 419 * @param reg address to be read
cparata 0:128dae13c732 420 * @param data pointer where the value is written
cparata 0:128dae13c732 421 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 422 */
cparata 0:128dae13c732 423 int STTS751Sensor::read_reg(uint8_t reg, uint8_t *data)
cparata 0:128dae13c732 424 {
cparata 0:128dae13c732 425 if (stts751_read_reg(&_reg_ctx, reg, data, 1) != 0)
cparata 0:128dae13c732 426 {
cparata 0:128dae13c732 427 return 1;
cparata 0:128dae13c732 428 }
cparata 0:128dae13c732 429
cparata 0:128dae13c732 430 return 0;
cparata 0:128dae13c732 431 }
cparata 0:128dae13c732 432
cparata 0:128dae13c732 433 /**
cparata 0:128dae13c732 434 * @brief Set the STTS751 register value
cparata 0:128dae13c732 435 * @param reg address to be written
cparata 0:128dae13c732 436 * @param data value to be written
cparata 0:128dae13c732 437 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 438 */
cparata 0:128dae13c732 439 int STTS751Sensor::write_reg(uint8_t reg, uint8_t data)
cparata 0:128dae13c732 440 {
cparata 0:128dae13c732 441 if (stts751_write_reg(&_reg_ctx, reg, &data, 1) != 0)
cparata 0:128dae13c732 442 {
cparata 0:128dae13c732 443 return 1;
cparata 0:128dae13c732 444 }
cparata 0:128dae13c732 445
cparata 0:128dae13c732 446 return 0;
cparata 0:128dae13c732 447 }
cparata 0:128dae13c732 448
cparata 0:128dae13c732 449 /**
cparata 0:128dae13c732 450 * @brief Set the STTS751 One Shot Mode
cparata 0:128dae13c732 451 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 452 */
cparata 0:128dae13c732 453 int STTS751Sensor::set_one_shot()
cparata 0:128dae13c732 454 {
cparata 0:128dae13c732 455 /* Start One Shot Measurement */
cparata 0:128dae13c732 456 if(stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_ONE_SHOT) != 0)
cparata 0:128dae13c732 457 {
cparata 0:128dae13c732 458 return 1;
cparata 0:128dae13c732 459 }
cparata 0:128dae13c732 460
cparata 0:128dae13c732 461 return 0;
cparata 0:128dae13c732 462 }
cparata 0:128dae13c732 463
cparata 0:128dae13c732 464 /**
cparata 0:128dae13c732 465 * @brief Get the STTS751 One Shot Status
cparata 0:128dae13c732 466 * @param status pointer to the one shot status (1 means measurements available, 0 means measurements not available yet)
cparata 0:128dae13c732 467 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 468 */
cparata 0:128dae13c732 469 int STTS751Sensor::get_one_shot_status(uint8_t *status)
cparata 0:128dae13c732 470 {
cparata 0:128dae13c732 471 uint8_t busy;
cparata 0:128dae13c732 472
cparata 0:128dae13c732 473 /* Get Busy flag */
cparata 0:128dae13c732 474 if(stts751_flag_busy_get(&_reg_ctx, &busy) != 0)
cparata 0:128dae13c732 475 {
cparata 0:128dae13c732 476 return 1;
cparata 0:128dae13c732 477 }
cparata 0:128dae13c732 478
cparata 0:128dae13c732 479 if(busy)
cparata 0:128dae13c732 480 {
cparata 0:128dae13c732 481 *status = 0;
cparata 0:128dae13c732 482 }
cparata 0:128dae13c732 483 else
cparata 0:128dae13c732 484 {
cparata 0:128dae13c732 485 *status = 1;
cparata 0:128dae13c732 486 }
cparata 0:128dae13c732 487
cparata 0:128dae13c732 488 return 0;
cparata 0:128dae13c732 489 }
cparata 0:128dae13c732 490
cparata 0:128dae13c732 491
cparata 0:128dae13c732 492 int32_t STTS751_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
cparata 0:128dae13c732 493 {
cparata 0:128dae13c732 494 return ((STTS751Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:128dae13c732 495 }
cparata 0:128dae13c732 496
cparata 0:128dae13c732 497 int32_t STTS751_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
cparata 0:128dae13c732 498 {
cparata 0:128dae13c732 499 return ((STTS751Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:128dae13c732 500 }