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 Jul 24 14:20:10 2019 +0000
Revision:
1:5859badee052
Parent:
0:128dae13c732
Child:
2:2ea2e88ce467
Format with Astyle

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 1:5859badee052 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 1:5859badee052 53 assert(i2c);
cparata 1:5859badee052 54 _reg_ctx.write_reg = STTS751_io_write;
cparata 1:5859badee052 55 _reg_ctx.read_reg = STTS751_io_read;
cparata 1:5859badee052 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 1:5859badee052 66 /* Disable EVENT pin of SMBus. */
cparata 1:5859badee052 67 if (stts751_pin_event_route_set(&_reg_ctx, PROPERTY_ENABLE) != 0) {
cparata 1:5859badee052 68 return 1;
cparata 1:5859badee052 69 }
cparata 0:128dae13c732 70
cparata 1:5859badee052 71 /* Set default ODR */
cparata 1:5859badee052 72 _last_odr = 1.0f;
cparata 0:128dae13c732 73
cparata 1:5859badee052 74 /* Set the resolution to the maximum allowed value */
cparata 1:5859badee052 75 if (stts751_resolution_set(&_reg_ctx, STTS751_12bit) != 0) {
cparata 1:5859badee052 76 return 1;
cparata 1:5859badee052 77 }
cparata 0:128dae13c732 78
cparata 1:5859badee052 79 /* Put the component in standby mode. */
cparata 1:5859badee052 80 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0) {
cparata 1:5859badee052 81 return 1;
cparata 1:5859badee052 82 }
cparata 1:5859badee052 83
cparata 1:5859badee052 84 _is_enabled = 0;
cparata 1:5859badee052 85
cparata 1:5859badee052 86 return 0;
cparata 0:128dae13c732 87 }
cparata 0:128dae13c732 88
cparata 0:128dae13c732 89 /**
cparata 0:128dae13c732 90 * @brief Get WHO_AM_I value
cparata 0:128dae13c732 91 * @param id the WHO_AM_I value
cparata 0:128dae13c732 92 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 93 */
cparata 0:128dae13c732 94 int STTS751Sensor::read_id(uint8_t *id)
cparata 0:128dae13c732 95 {
cparata 1:5859badee052 96 stts751_id_t buf;
cparata 0:128dae13c732 97
cparata 1:5859badee052 98 if (stts751_device_id_get(&_reg_ctx, &buf) != 0) {
cparata 1:5859badee052 99 return 1;
cparata 1:5859badee052 100 }
cparata 0:128dae13c732 101
cparata 1:5859badee052 102 *id = buf.manufacturer_id;
cparata 0:128dae13c732 103
cparata 1:5859badee052 104 return 0;
cparata 0:128dae13c732 105 }
cparata 0:128dae13c732 106
cparata 0:128dae13c732 107 /**
cparata 0:128dae13c732 108 * @brief Enable the STTS751 temperature sensor
cparata 0:128dae13c732 109 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 110 */
cparata 0:128dae13c732 111 int STTS751Sensor::enable()
cparata 0:128dae13c732 112 {
cparata 1:5859badee052 113 /* Check if the component is already enabled */
cparata 1:5859badee052 114 if (_is_enabled == 1U) {
cparata 1:5859badee052 115 return 0;
cparata 1:5859badee052 116 }
cparata 0:128dae13c732 117
cparata 1:5859badee052 118 /* Power on the component and set the odr. */
cparata 1:5859badee052 119 if (set_odr(_last_odr) != 0) {
cparata 1:5859badee052 120 return 1;
cparata 1:5859badee052 121 }
cparata 0:128dae13c732 122
cparata 1:5859badee052 123 _is_enabled = 1;
cparata 0:128dae13c732 124
cparata 1:5859badee052 125 return 0;
cparata 0:128dae13c732 126 }
cparata 0:128dae13c732 127
cparata 0:128dae13c732 128 /**
cparata 0:128dae13c732 129 * @brief Disable the STTS751 temperature sensor
cparata 0:128dae13c732 130 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 131 */
cparata 0:128dae13c732 132 int STTS751Sensor::disable()
cparata 0:128dae13c732 133 {
cparata 1:5859badee052 134 /* Check if the component is already disabled */
cparata 1:5859badee052 135 if (_is_enabled == 0U) {
cparata 1:5859badee052 136 return 0;
cparata 1:5859badee052 137 }
cparata 0:128dae13c732 138
cparata 1:5859badee052 139 /* Save the current odr. */
cparata 1:5859badee052 140 if (get_odr(&_last_odr) != 0) {
cparata 1:5859badee052 141 return 1;
cparata 1:5859badee052 142 }
cparata 0:128dae13c732 143
cparata 1:5859badee052 144 /* Put the component in standby mode. */
cparata 1:5859badee052 145 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_OFF) != 0) {
cparata 1:5859badee052 146 return 1;
cparata 1:5859badee052 147 }
cparata 0:128dae13c732 148
cparata 1:5859badee052 149 _is_enabled = 0;
cparata 1:5859badee052 150
cparata 1:5859badee052 151 return 0;
cparata 0:128dae13c732 152 }
cparata 0:128dae13c732 153
cparata 0:128dae13c732 154 /**
cparata 0:128dae13c732 155 * @brief Get the STTS751 temperature sensor output data rate
cparata 0:128dae13c732 156 * @param odr pointer where the output data rate is written
cparata 0:128dae13c732 157 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 158 */
cparata 0:128dae13c732 159 int STTS751Sensor::get_odr(float *odr)
cparata 0:128dae13c732 160 {
cparata 1:5859badee052 161 int ret = 0;
cparata 1:5859badee052 162 stts751_odr_t odr_low_level;
cparata 0:128dae13c732 163
cparata 1:5859badee052 164 if (stts751_temp_data_rate_get(&_reg_ctx, &odr_low_level) != 0) {
cparata 1:5859badee052 165 return 1;
cparata 1:5859badee052 166 }
cparata 0:128dae13c732 167
cparata 1:5859badee052 168 switch (odr_low_level) {
cparata 1:5859badee052 169 case STTS751_TEMP_ODR_OFF:
cparata 1:5859badee052 170 case STTS751_TEMP_ODR_ONE_SHOT:
cparata 1:5859badee052 171 *odr = 0.0f;
cparata 1:5859badee052 172 break;
cparata 0:128dae13c732 173
cparata 1:5859badee052 174 case STTS751_TEMP_ODR_62mHz5:
cparata 1:5859badee052 175 *odr = 0.0625f;
cparata 1:5859badee052 176 break;
cparata 0:128dae13c732 177
cparata 1:5859badee052 178 case STTS751_TEMP_ODR_125mHz:
cparata 1:5859badee052 179 *odr = 0.125f;
cparata 1:5859badee052 180 break;
cparata 0:128dae13c732 181
cparata 1:5859badee052 182 case STTS751_TEMP_ODR_250mHz:
cparata 1:5859badee052 183 *odr = 0.250f;
cparata 1:5859badee052 184 break;
cparata 0:128dae13c732 185
cparata 1:5859badee052 186 case STTS751_TEMP_ODR_500mHz:
cparata 1:5859badee052 187 *odr = 0.500f;
cparata 1:5859badee052 188 break;
cparata 0:128dae13c732 189
cparata 1:5859badee052 190 case STTS751_TEMP_ODR_1Hz:
cparata 1:5859badee052 191 *odr = 1.0f;
cparata 1:5859badee052 192 break;
cparata 0:128dae13c732 193
cparata 1:5859badee052 194 case STTS751_TEMP_ODR_2Hz:
cparata 1:5859badee052 195 *odr = 2.0f;
cparata 1:5859badee052 196 break;
cparata 0:128dae13c732 197
cparata 1:5859badee052 198 case STTS751_TEMP_ODR_4Hz:
cparata 1:5859badee052 199 *odr = 4.0f;
cparata 1:5859badee052 200 break;
cparata 0:128dae13c732 201
cparata 1:5859badee052 202 case STTS751_TEMP_ODR_8Hz:
cparata 1:5859badee052 203 *odr = 8.0f;
cparata 1:5859badee052 204 break;
cparata 0:128dae13c732 205
cparata 1:5859badee052 206 case STTS751_TEMP_ODR_16Hz:
cparata 1:5859badee052 207 *odr = 16.0f;
cparata 1:5859badee052 208 break;
cparata 0:128dae13c732 209
cparata 1:5859badee052 210 case STTS751_TEMP_ODR_32Hz:
cparata 1:5859badee052 211 *odr = 32.0f;
cparata 1:5859badee052 212 break;
cparata 0:128dae13c732 213
cparata 1:5859badee052 214 default:
cparata 1:5859badee052 215 ret = 1;
cparata 1:5859badee052 216 break;
cparata 1:5859badee052 217 }
cparata 0:128dae13c732 218
cparata 1:5859badee052 219 return ret;
cparata 0:128dae13c732 220 }
cparata 0:128dae13c732 221
cparata 0:128dae13c732 222 /**
cparata 0:128dae13c732 223 * @brief Set the STTS751 temperature sensor output data rate
cparata 0:128dae13c732 224 * @param odr the output data rate value to be set
cparata 0:128dae13c732 225 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 226 */
cparata 0:128dae13c732 227 int STTS751Sensor::set_odr(float odr)
cparata 0:128dae13c732 228 {
cparata 1:5859badee052 229 stts751_odr_t new_odr;
cparata 1:5859badee052 230 stts751_tres_t res;
cparata 0:128dae13c732 231
cparata 1:5859badee052 232 /* Get the current resolution */
cparata 1:5859badee052 233 if (stts751_resolution_get(&_reg_ctx, &res) != 0) {
cparata 1:5859badee052 234 return 1;
cparata 1:5859badee052 235 }
cparata 0:128dae13c732 236
cparata 1:5859badee052 237 /* If the requested odr is 16Hz we cannot use the 12 bits resolution */
cparata 1:5859badee052 238 if (odr == 16.0f && res == STTS751_12bit) {
cparata 1:5859badee052 239 /* We force resolution to the maximum allowed value */
cparata 1:5859badee052 240 if (stts751_resolution_set(&_reg_ctx, STTS751_11bit) != 0) {
cparata 1:5859badee052 241 return 1;
cparata 1:5859badee052 242 }
cparata 0:128dae13c732 243 }
cparata 0:128dae13c732 244
cparata 1:5859badee052 245 /* If the requested odr is 32Hz we cannot use the 12 bits and 11 bits resolutions */
cparata 1:5859badee052 246 if (odr == 32.0f && (res == STTS751_12bit || res == STTS751_11bit)) {
cparata 1:5859badee052 247 /* We force resolution to the maximum allowed value */
cparata 1:5859badee052 248 if (stts751_resolution_set(&_reg_ctx, STTS751_10bit) != 0) {
cparata 1:5859badee052 249 return 1;
cparata 1:5859badee052 250 }
cparata 0:128dae13c732 251 }
cparata 0:128dae13c732 252
cparata 1:5859badee052 253 new_odr = (odr <= 0.0625f) ? STTS751_TEMP_ODR_62mHz5
cparata 1:5859badee052 254 : (odr <= 0.125f) ? STTS751_TEMP_ODR_125mHz
cparata 1:5859badee052 255 : (odr <= 0.25f) ? STTS751_TEMP_ODR_250mHz
cparata 1:5859badee052 256 : (odr <= 0.5f) ? STTS751_TEMP_ODR_500mHz
cparata 1:5859badee052 257 : (odr <= 1.0f) ? STTS751_TEMP_ODR_1Hz
cparata 1:5859badee052 258 : (odr <= 2.0f) ? STTS751_TEMP_ODR_2Hz
cparata 1:5859badee052 259 : (odr <= 4.0f) ? STTS751_TEMP_ODR_4Hz
cparata 1:5859badee052 260 : (odr <= 8.0f) ? STTS751_TEMP_ODR_8Hz
cparata 1:5859badee052 261 : (odr <= 16.0f) ? STTS751_TEMP_ODR_16Hz
cparata 1:5859badee052 262 : STTS751_TEMP_ODR_32Hz;
cparata 0:128dae13c732 263
cparata 1:5859badee052 264 if (stts751_temp_data_rate_set(&_reg_ctx, new_odr) != 0) {
cparata 1:5859badee052 265 return 1;
cparata 1:5859badee052 266 }
cparata 0:128dae13c732 267
cparata 1:5859badee052 268 return 0;
cparata 0:128dae13c732 269 }
cparata 0:128dae13c732 270
cparata 0:128dae13c732 271 /**
cparata 0:128dae13c732 272 * @brief Get the STTS751 temperature value
cparata 0:128dae13c732 273 * @param value pointer where the temperature value is written
cparata 0:128dae13c732 274 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 275 */
cparata 0:128dae13c732 276 int STTS751Sensor::get_temperature(float *value)
cparata 0:128dae13c732 277 {
cparata 1:5859badee052 278 int16_t raw_value = 0;
cparata 0:128dae13c732 279
cparata 1:5859badee052 280 /* Get the temperature */
cparata 1:5859badee052 281 if (stts751_temperature_raw_get(&_reg_ctx, &raw_value) != 0) {
cparata 1:5859badee052 282 return 1;
cparata 1:5859badee052 283 }
cparata 0:128dae13c732 284
cparata 1:5859badee052 285 *value = stts751_from_lsb_to_celsius(raw_value);
cparata 0:128dae13c732 286
cparata 1:5859badee052 287 return 0;
cparata 0:128dae13c732 288 }
cparata 0:128dae13c732 289
cparata 0:128dae13c732 290 /**
cparata 0:128dae13c732 291 * @brief Get the STTS751 temperature data ready bit value
cparata 0:128dae13c732 292 * @param status the status of data ready bit
cparata 0:128dae13c732 293 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 294 */
cparata 0:128dae13c732 295 int STTS751Sensor::get_temp_drdy_status(uint8_t *status)
cparata 0:128dae13c732 296 {
cparata 1:5859badee052 297 uint8_t val;
cparata 0:128dae13c732 298
cparata 1:5859badee052 299 if (stts751_flag_busy_get(&_reg_ctx, &val) != 0) {
cparata 1:5859badee052 300 return 1;
cparata 1:5859badee052 301 }
cparata 0:128dae13c732 302
cparata 1:5859badee052 303 if (val) {
cparata 1:5859badee052 304 *status = 0;
cparata 1:5859badee052 305 } else {
cparata 1:5859badee052 306 *status = 1;
cparata 1:5859badee052 307 }
cparata 0:128dae13c732 308
cparata 1:5859badee052 309 return 0;
cparata 0:128dae13c732 310 }
cparata 0:128dae13c732 311
cparata 0:128dae13c732 312 /**
cparata 0:128dae13c732 313 * @brief Set the STTS751 low temperature threshold value
cparata 0:128dae13c732 314 * @param value the low temperature threshold to be set
cparata 0:128dae13c732 315 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 316 */
cparata 0:128dae13c732 317 int STTS751Sensor::set_low_temp_thr(float value)
cparata 0:128dae13c732 318 {
cparata 1:5859badee052 319 int16_t raw_value;
cparata 0:128dae13c732 320
cparata 1:5859badee052 321 raw_value = stts751_from_celsius_to_lsb(value);
cparata 0:128dae13c732 322
cparata 1:5859badee052 323 /* Set the temperature threshold */
cparata 1:5859badee052 324 if (stts751_low_temperature_threshold_set(&_reg_ctx, raw_value) != 0) {
cparata 1:5859badee052 325 return 1;
cparata 1:5859badee052 326 }
cparata 0:128dae13c732 327
cparata 1:5859badee052 328 return 0;
cparata 0:128dae13c732 329 }
cparata 0:128dae13c732 330
cparata 0:128dae13c732 331 /**
cparata 0:128dae13c732 332 * @brief Set the STTS751 high temperature threshold value
cparata 0:128dae13c732 333 * @param value the high temperature threshold to be set
cparata 0:128dae13c732 334 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 335 */
cparata 0:128dae13c732 336 int STTS751Sensor::set_high_temp_thr(float value)
cparata 0:128dae13c732 337 {
cparata 1:5859badee052 338 int16_t raw_value;
cparata 0:128dae13c732 339
cparata 1:5859badee052 340 raw_value = stts751_from_celsius_to_lsb(value);
cparata 0:128dae13c732 341
cparata 1:5859badee052 342 /* Set the temperature threshold */
cparata 1:5859badee052 343 if (stts751_high_temperature_threshold_set(&_reg_ctx, raw_value) != 0) {
cparata 1:5859badee052 344 return 1;
cparata 1:5859badee052 345 }
cparata 0:128dae13c732 346
cparata 1:5859badee052 347 return 0;
cparata 0:128dae13c732 348 }
cparata 0:128dae13c732 349
cparata 0:128dae13c732 350 /**
cparata 0:128dae13c732 351 * @brief Get the STTS751 temperature limits status
cparata 0:128dae13c732 352 * @param high_limit indicates that high temperature limit has been exceeded
cparata 0:128dae13c732 353 * @param low_limit indicates that low temperature limit has been exceeded
cparata 0:128dae13c732 354 * @param therm_limit indicates that therm temperature limit has been exceeded
cparata 0:128dae13c732 355 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 356 */
cparata 0:128dae13c732 357 int STTS751Sensor::get_temp_limit_status(uint8_t *high_limit, uint8_t *low_limit, uint8_t *therm_limit)
cparata 0:128dae13c732 358 {
cparata 1:5859badee052 359 stts751_status_t status;
cparata 0:128dae13c732 360
cparata 1:5859badee052 361 /* Read status register */
cparata 1:5859badee052 362 if (stts751_status_reg_get(&_reg_ctx, &status) != 0) {
cparata 1:5859badee052 363 return 1;
cparata 1:5859badee052 364 }
cparata 0:128dae13c732 365
cparata 1:5859badee052 366 *high_limit = status.t_high;
cparata 1:5859badee052 367 *low_limit = status.t_low;
cparata 1:5859badee052 368 *therm_limit = status.thrm;
cparata 0:128dae13c732 369
cparata 1:5859badee052 370 return 0;
cparata 0:128dae13c732 371 }
cparata 0:128dae13c732 372
cparata 0:128dae13c732 373 /**
cparata 0:128dae13c732 374 * @brief Enable or disable interrupt on EVENT pin
cparata 0:128dae13c732 375 * @param enable 0 disable the EVENT pin, 1 enable EVENT pin
cparata 0:128dae13c732 376 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 377 */
cparata 0:128dae13c732 378 int STTS751Sensor::set_event_pin(uint8_t enable)
cparata 0:128dae13c732 379 {
cparata 1:5859badee052 380 uint8_t state;
cparata 0:128dae13c732 381
cparata 1:5859badee052 382 /* The MASK1 bit in configuration register has inverted logic */
cparata 1:5859badee052 383 if (enable == 0) {
cparata 1:5859badee052 384 state = PROPERTY_ENABLE;
cparata 1:5859badee052 385 } else {
cparata 1:5859badee052 386 state = PROPERTY_DISABLE;
cparata 1:5859badee052 387 }
cparata 0:128dae13c732 388
cparata 1:5859badee052 389 if (stts751_pin_event_route_set(&_reg_ctx, state) != 0) {
cparata 1:5859badee052 390 return 1;
cparata 1:5859badee052 391 }
cparata 1:5859badee052 392
cparata 1:5859badee052 393 return 0;
cparata 0:128dae13c732 394 }
cparata 0:128dae13c732 395
cparata 0:128dae13c732 396 /**
cparata 0:128dae13c732 397 * @brief Get the STTS751 register value
cparata 0:128dae13c732 398 * @param reg address to be read
cparata 0:128dae13c732 399 * @param data pointer where the value is written
cparata 0:128dae13c732 400 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 401 */
cparata 0:128dae13c732 402 int STTS751Sensor::read_reg(uint8_t reg, uint8_t *data)
cparata 0:128dae13c732 403 {
cparata 1:5859badee052 404 if (stts751_read_reg(&_reg_ctx, reg, data, 1) != 0) {
cparata 1:5859badee052 405 return 1;
cparata 1:5859badee052 406 }
cparata 0:128dae13c732 407
cparata 1:5859badee052 408 return 0;
cparata 0:128dae13c732 409 }
cparata 0:128dae13c732 410
cparata 0:128dae13c732 411 /**
cparata 0:128dae13c732 412 * @brief Set the STTS751 register value
cparata 0:128dae13c732 413 * @param reg address to be written
cparata 0:128dae13c732 414 * @param data value to be written
cparata 0:128dae13c732 415 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 416 */
cparata 0:128dae13c732 417 int STTS751Sensor::write_reg(uint8_t reg, uint8_t data)
cparata 0:128dae13c732 418 {
cparata 1:5859badee052 419 if (stts751_write_reg(&_reg_ctx, reg, &data, 1) != 0) {
cparata 1:5859badee052 420 return 1;
cparata 1:5859badee052 421 }
cparata 0:128dae13c732 422
cparata 1:5859badee052 423 return 0;
cparata 0:128dae13c732 424 }
cparata 0:128dae13c732 425
cparata 0:128dae13c732 426 /**
cparata 0:128dae13c732 427 * @brief Set the STTS751 One Shot Mode
cparata 0:128dae13c732 428 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 429 */
cparata 0:128dae13c732 430 int STTS751Sensor::set_one_shot()
cparata 0:128dae13c732 431 {
cparata 1:5859badee052 432 /* Start One Shot Measurement */
cparata 1:5859badee052 433 if (stts751_temp_data_rate_set(&_reg_ctx, STTS751_TEMP_ODR_ONE_SHOT) != 0) {
cparata 1:5859badee052 434 return 1;
cparata 1:5859badee052 435 }
cparata 0:128dae13c732 436
cparata 1:5859badee052 437 return 0;
cparata 0:128dae13c732 438 }
cparata 0:128dae13c732 439
cparata 0:128dae13c732 440 /**
cparata 0:128dae13c732 441 * @brief Get the STTS751 One Shot Status
cparata 0:128dae13c732 442 * @param status pointer to the one shot status (1 means measurements available, 0 means measurements not available yet)
cparata 0:128dae13c732 443 * @retval 0 in case of success, an error code otherwise
cparata 0:128dae13c732 444 */
cparata 0:128dae13c732 445 int STTS751Sensor::get_one_shot_status(uint8_t *status)
cparata 0:128dae13c732 446 {
cparata 1:5859badee052 447 uint8_t busy;
cparata 0:128dae13c732 448
cparata 1:5859badee052 449 /* Get Busy flag */
cparata 1:5859badee052 450 if (stts751_flag_busy_get(&_reg_ctx, &busy) != 0) {
cparata 1:5859badee052 451 return 1;
cparata 1:5859badee052 452 }
cparata 0:128dae13c732 453
cparata 1:5859badee052 454 if (busy) {
cparata 1:5859badee052 455 *status = 0;
cparata 1:5859badee052 456 } else {
cparata 1:5859badee052 457 *status = 1;
cparata 1:5859badee052 458 }
cparata 0:128dae13c732 459
cparata 1:5859badee052 460 return 0;
cparata 0:128dae13c732 461 }
cparata 0:128dae13c732 462
cparata 0:128dae13c732 463
cparata 0:128dae13c732 464 int32_t STTS751_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
cparata 0:128dae13c732 465 {
cparata 1:5859badee052 466 return ((STTS751Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:128dae13c732 467 }
cparata 0:128dae13c732 468
cparata 0:128dae13c732 469 int32_t STTS751_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
cparata 0:128dae13c732 470 {
cparata 1:5859badee052 471 return ((STTS751Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:128dae13c732 472 }