1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers: rework tmp006

This commit is contained in:
smlng 2017-06-09 22:17:02 +02:00
parent 3af1fe5675
commit 42a09128aa
5 changed files with 342 additions and 253 deletions

View File

@ -76,6 +76,9 @@ endif
ifneq (,$(filter bm%280,$(USEMODULE))) ifneq (,$(filter bm%280,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/bmx280/include USEMODULE_INCLUDES += $(RIOTBASE)/drivers/bmx280/include
endif endif
ifneq (,$(filter tmp006,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/tmp006/include
endif
ifneq (,$(filter tsl2561,$(USEMODULE))) ifneq (,$(filter tsl2561,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/tsl2561/include USEMODULE_INCLUDES += $(RIOTBASE)/drivers/tsl2561/include
endif endif

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2014 PHYTEC Messtechnik GmbH * Copyright (C) 2014 PHYTEC Messtechnik GmbH
* 2017 HAW Hamburg
* *
* This file is subject to the terms and conditions of the GNU Lesser * This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level * General Public License v2.1. See the file LICENSE in the top level
@ -7,17 +8,16 @@
*/ */
/** /**
* @defgroup drivers_tmp006 TMP006 Infrared Thermopile Sensor * @defgroup drivers_tmp006 TI TMP006 Infrared Thermopile Sensor
* @ingroup drivers_sensors * @ingroup drivers_sensors
* @brief Driver for the Texas Instruments TMP006 sensor. * @brief Driver for the Texas Instruments TMP006 sensor.
* The sensor measures the temperature of an object *
* without the need of direct contact with the object. * The TI TMP006 (Infrared Thermopile Contactless Temperature Sensor) measures
* After initialization and set active the sensor * the temperature of an object without need of direct contact with the object.
* will make measurements at periodic times. * After initialization the sensor can be set active for periodic measurements.
* The conversion duration depends on oversample ratio.<br> * <br> The conversion duration depends on oversample ratio. The oversample
* The oversample ratio can be determined * ratio can be determined by sensor initialization. The target object
* by sensor initialization.<br> * temperature can be calculated as follows:<br><br>
* The target object temperature can be calculated as follows:<br><br>
* Temperature of sensor's die (2 times right shifting and devide-by 32): * Temperature of sensor's die (2 times right shifting and devide-by 32):
* \f{eqnarray*}{ * \f{eqnarray*}{
* T_{\mathrm{DIE}} &=& \frac{T_{\mathrm{RAW}}}{128} * T_{\mathrm{DIE}} &=& \frac{T_{\mathrm{RAW}}}{128}
@ -26,8 +26,8 @@
* \f{eqnarray*}{ * \f{eqnarray*}{
* T_{\mathrm{DIFF}} &=& T_{\mathrm{DIE}} - T_{\mathrm{REF}} * T_{\mathrm{DIFF}} &=& T_{\mathrm{DIE}} - T_{\mathrm{REF}}
* \f} * \f}
* Sensitivity of the thermopile sensor, * Sensitivity of the thermopile sensor, with \f$S_{\mathrm{0}}\f$ as
* with \f$S_{\mathrm{0}}\f$ as calibration factor: * calibration factor:
* \f{eqnarray*}{ * \f{eqnarray*}{
* S &=& S_{\mathrm{0}} \cdot ( 1 + a_1 \cdot T_{\mathrm{DIFF}} * S &=& S_{\mathrm{0}} \cdot ( 1 + a_1 \cdot T_{\mathrm{DIFF}}
* + a_2 \cdot T_{\mathrm{DIFF}}^2 ) \\ * + a_2 \cdot T_{\mathrm{DIFF}}^2 ) \\
@ -44,7 +44,8 @@
* Seebeck coefficients of the thermopile: * Seebeck coefficients of the thermopile:
* \f{eqnarray*}{ * \f{eqnarray*}{
* f(V_{\mathrm{OBJ}}) &=& (V_{\mathrm{OBJ}} - V_{O\mathrm{S}}) * f(V_{\mathrm{OBJ}}) &=& (V_{\mathrm{OBJ}} - V_{O\mathrm{S}})
* + c_2 \cdot (V_{\mathrm{OBJ}} - V_{\mathrm{OS}})^2 \\ * + c_2 \cdot (V_{\mathrm{OBJ}}
* - V_{\mathrm{OS}})^2 \\
* \f} * \f}
* Temperature of the target object: * Temperature of the target object:
* \f{eqnarray*}{ * \f{eqnarray*}{
@ -70,6 +71,7 @@
* @brief Interface definition for the TMP006 sensor driver. * @brief Interface definition for the TMP006 sensor driver.
* *
* @author Johann Fischer <j.fischer@phytec.de> * @author Johann Fischer <j.fischer@phytec.de>
* @author Sebastian Meiling <s@mlng.net>
*/ */
#ifndef TMP006_H #ifndef TMP006_H
@ -84,72 +86,91 @@ extern "C"
{ {
#endif #endif
/**
* @brief TMP006 Default Address
*/
#ifndef TMP006_I2C_ADDRESS #ifndef TMP006_I2C_ADDRESS
#define TMP006_I2C_ADDRESS 0x41 /**< TMP006 Sensor Default Address */ #define TMP006_I2C_ADDRESS (0x41)
#endif #endif
/**
* @brief Default Conversion Time in us
*/
#ifndef TMP006_CONVERSION_TIME #ifndef TMP006_CONVERSION_TIME
#define TMP006_CONVERSION_TIME 1E6 /**< Default Conversion Time in us */ #define TMP006_CONVERSION_TIME (1E6)
#endif #endif
#define TMP006_CONFIG_CR_AS1 0x00 /**< Conversion Time 0.25 s, AVG Samples: 1 */ /**
#define TMP006_CONFIG_CR_AS2 0x01 /**< Conversion Time 0.5 s, AVG Samples: 2 */ * @name Conversion rate and AVG sampling configuration
#define TMP006_CONFIG_CR_AS4 0x02 /**< Conversion Time 1 s, AVG Samples: 4 */ * @{
#define TMP006_CONFIG_CR_AS8 0x03 /**< Conversion Time 2 s, AVG Samples: 8 */ */
#define TMP006_CONFIG_CR_AS16 0x04 /**< Conversion Time 4 s, AVG Samples: 16 */ #define TMP006_CONFIG_CR_AS1 (0x00) /**< Conversion Time 0.25s, AVG Samples: 1 */
#define TMP006_CONFIG_CR_AS2 (0x01) /**< Conversion Time 0.5s, AVG Samples: 2 */
#define TMP006_CONFIG_CR_AS4 (0x02) /**< Conversion Time 1s, AVG Samples: 4 */
#define TMP006_CONFIG_CR_AS8 (0x03) /**< Conversion Time 2s, AVG Samples: 8 */
#define TMP006_CONFIG_CR_AS16 (0x04) /**< Conversion Time 4s, AVG Samples: 16 */
#define TMP006_CONFIG_CR_DEF TMP006_CONFIG_CR_AS4 /**< Default for Testing */ #define TMP006_CONFIG_CR_DEF TMP006_CONFIG_CR_AS4 /**< Default for Testing */
/** @} */
/**
* @name Constants for TMP006 calibration
* @{
*/
#ifndef TMP006_CCONST_S0 #ifndef TMP006_CCONST_S0
#define TMP006_CCONST_S0 6.4E-14 /**< Calibration Factor */ #define TMP006_CCONST_S0 (6.4E-14) /**< Calibration Factor */
#endif #endif
#define TMP006_CCONST_A1 (1.75E-3) /**< Constant \f$a_{\mathrm{1}}\f$ */
#define TMP006_CCONST_A2 (-1.678E-5) /**< Constant \f$a_{\mathrm{2}}\f$ */
#define TMP006_CCONST_TREF (298.15) /**< Constant \f$T_{\mathrm{REF}}\f$ */
#define TMP006_CCONST_B0 (-2.94E-5) /**< Constant \f$b_{\mathrm{0}}\f$ */
#define TMP006_CCONST_B1 (-5.7E-7) /**< Constant \f$b_{\mathrm{1}}\f$ */
#define TMP006_CCONST_B2 (4.63E-9) /**< Constant \f$b_{\mathrm{2}}\f$ */
#define TMP006_CCONST_C2 (13.4) /**< Constant \f$c_{\mathrm{2}}\f$ */
#define TMP006_CCONST_LSB_SIZE (156.25E-9) /**< Sensor Voltage Register LSB Size */
/** @} */
#define TMP006_CCONST_A1 1.75E-3 /**< Constant \f$a_{\mathrm{1}}\f$ */ /**
#define TMP006_CCONST_A2 -1.678E-5 /**< Constant \f$a_{\mathrm{2}}\f$ */ * @brief Parameters needed for device initialization
#define TMP006_CCONST_TREF 298.15 /**< Constant \f$T_{\mathrm{REF}}\f$ */ */
#define TMP006_CCONST_B0 -2.94E-5 /**< Constant \f$b_{\mathrm{0}}\f$ */ typedef struct {
#define TMP006_CCONST_B1 -5.7E-7 /**< Constant \f$b_{\mathrm{1}}\f$ */ i2c_t i2c; /**< I2C device, the sensor is connected to */
#define TMP006_CCONST_B2 4.63E-9 /**< Constant \f$b_{\mathrm{2}}\f$ */ uint8_t addr; /**< the sensor's slave address on the I2C bus */
#define TMP006_CCONST_C2 13.4 /**< Constant \f$c_{\mathrm{2}}\f$ */ uint8_t rate; /**< number of averaged samples */
#define TMP006_CCONST_LSB_SIZE 156.25E-9 /**< Sensor Voltage Register LSB Size */ } tmp006_params_t;
/** /**
* @brief Device descriptor for TMP006 sensors. * @brief Device descriptor for TMP006 sensors.
*/ */
typedef struct { typedef struct {
i2c_t i2c; /**< I2C device, the sensor is connected to */ tmp006_params_t p; /**< Configuration parameters */
uint8_t addr; /**< the sensor's slave address on the I2C bus */
bool initialized; /**< sensor status, true if sensor is initialized */
} tmp006_t; } tmp006_t;
/** /**
* @brief TMP006 sensor test. * @brief TMP006 specific return values
* This function looks for Device ID of the TMP006 sensor.
*
* @param[in] dev device descriptor of sensor
*
* @return 0 on success
* @return -1 on error
*/ */
int tmp006_test(tmp006_t *dev); enum {
TMP006_OK, /**< Success, no error */
TMP006_ERROR_BUS, /**< I2C bus error */
TMP006_ERROR_DEV, /**< internal device error */
TMP006_ERROR_CONF, /**< invalid device configuration */
TMP006_ERROR, /**< general error */
};
/** /**
* @brief Initialize the TMP006 sensor driver. * @brief Initialize the TMP006 sensor driver.
* *
* @param[out] dev device descriptor of sensor to initialize * @param[out] dev device descriptor of sensor to initialize
* @param[in] i2c I2C bus the sensor is connected to * @param[in] params configuration parameters
* @param[in] address sensor's I2C slave address
* @param[in] conv_rate number of averaged samples
* *
* @return 0 on success * @return 0 on success
* @return -1 if conv_rate is wrong * @return -TMP006_ERROR_BUS on I2C bus error
* @return -2 if initialization of I2C bus failed * @return -TMP006_ERROR_DEV if sensor test failed
* @return -3 if sensor test failed * @return -TMP006_ERROR_CONF if sensor configuration failed
* @return -4 if sensor configuration failed
*/ */
int tmp006_init(tmp006_t *dev, i2c_t i2c, uint8_t address, uint8_t conv_rate); int tmp006_init(tmp006_t *dev, const tmp006_params_t *params);
/** /**
* @brief Reset the TMP006 sensor. After that, the sensor should be reinitialized. * @brief Reset the TMP006 sensor, afterwards it should be reinitialized.
* *
* @param[out] dev device descriptor of sensor * @param[out] dev device descriptor of sensor
* *

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2017 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_tmp006
*
* @{
* @file
* @brief Default configuration for TMP006 devices
*
* @author Sebastian Meiling <s@mlng.net>
*/
#ifndef TMP006_PARAMS_H
#define TMP006_PARAMS_H
#include "board.h"
#include "tmp006.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Set default configuration parameters for the TMP006 driver
* @{
*/
#ifndef TMP006_PARAM_I2C
#define TMP006_PARAM_I2C I2C_DEV(0)
#endif
#ifndef TMP006_PARAM_ADDR
#define TMP006_PARAM_ADDR (TMP006_I2C_ADDRESS)
#endif
#ifndef TMP006_PARAM_RATE
#define TMP006_PARAM_RATE TMP006_CONFIG_CR_DEF
#endif
#define TMP006_PARAMS_DEFAULT { .i2c = TMP006_PARAM_I2C, \
.addr = TMP006_PARAM_ADDR, \
.rate = TMP006_PARAM_RATE }
/**@}*/
/**
* @brief HDC1000 configuration
*/
static const tmp006_params_t tmp006_params[] =
{
#ifdef TMP006_PARAMS_BOARD
TMP006_PARAMS_BOARD,
#else
TMP006_PARAMS_DEFAULT,
#endif
};
#ifdef __cplusplus
}
#endif
#endif /* TMP006_PARAMS_H */
/** @} */

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2017 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_tmp006
*
* @{
* @file
* @brief Register definitions for TMP006 devices
*
* @author Sebastian Meiling <s@mlng.net>
*/
#ifndef TMP006_REGS_H
#define TMP006_REGS_H
#include "board.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Register Map
* @{
*/
#define TMP006_REGS_V_OBJECT 0x00 /**< Sensor Voltage Register */
#define TMP006_REGS_T_AMBIENT 0x01 /**< Ambient Temperature Register */
#define TMP006_REGS_CONFIG 0x02 /**< Configuration Register */
#define TMP006_REGS_MANUFACTURER_ID 0xFE /**< Manufacturer ID Register */
#define TMP006_REGS_DEVICE_ID 0xFF /**< Device ID Register */
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* TMP006_REGS_H */
/** @} */

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2014 PHYTEC Messtechnik GmbH * Copyright (C) 2014 PHYTEC Messtechnik GmbH
* 2017 HAW Hamburg
* *
* This file is subject to the terms and conditions of the GNU Lesser * This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level * General Public License v2.1. See the file LICENSE in the top level
@ -16,230 +17,185 @@
* *
* @author Johann Fischer <j.fischer@phytec.de> * @author Johann Fischer <j.fischer@phytec.de>
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de> * @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
* @author Sebastian Meiling <s@mlng.net>
* *
* @} * @}
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include <math.h> #include <math.h>
#include "log.h"
#include "periph/i2c.h" #include "periph/i2c.h"
#include "tmp006.h" #include "tmp006.h"
#include "tmp006_regs.h"
#define ENABLE_DEBUG (0) #define ENABLE_DEBUG (0)
#include "debug.h" #include "debug.h"
#define TMP006_V_OBJECT 0x00 /**< Sensor Voltage Register */
#define TMP006_T_AMBIENT 0x01 /**< Ambient Temperature Register */
#define TMP006_CONFIG 0x02 /**< Configuration Register */
#define TMP006_MANUFACTURER_ID 0xFE /**< Manufacturer ID Register */
#define TMP006_DEVICE_ID 0xFF /**< Device ID Register */
#define TMP006_CONFIG_RST (1 << 15) #define TMP006_CONFIG_RST (1 << 15)
#define TMP006_CONFIG_MOD_SHIFT 12 #define TMP006_CONFIG_MOD_SHIFT (12U)
#define TMP006_CONFIG_MOD_MASK 0x7000 #define TMP006_CONFIG_MOD_MASK (0x7000)
#define TMP006_CONFIG_MOD(x) (((uint16_t)(((uint16_t)(x))<<TMP006_CONFIG_MOD_SHIFT))\ #define TMP006_CONFIG_MOD(x) (((uint16_t)(((uint16_t)(x)) << TMP006_CONFIG_MOD_SHIFT))\
&TMP006_CONFIG_MOD_MASK) & TMP006_CONFIG_MOD_MASK)
#define TMP006_CONFIG_MOD_CC 0x07 #define TMP006_CONFIG_MOD_CC (0x07)
#define TMP006_CONFIG_MOD_OFF 0x00 #define TMP006_CONFIG_MOD_OFF (0x00)
#define TMP006_CONFIG_CR_SHIFT 9 #define TMP006_CONFIG_CR_SHIFT (9U)
#define TMP006_CONFIG_CR_MASK 0x0E00 #define TMP006_CONFIG_CR_MASK (0x0E00)
#define TMP006_CONFIG_CR(x) (((uint16_t)(((uint16_t)(x))<<TMP006_CONFIG_CR_SHIFT))\ #define TMP006_CONFIG_CR(x) (((uint16_t)(((uint16_t)(x)) << TMP006_CONFIG_CR_SHIFT))\
&TMP006_CONFIG_CR_MASK) & TMP006_CONFIG_CR_MASK)
#define TMP006_CONFIG_DRDY_PIN_EN (1 << 8) #define TMP006_CONFIG_DRDY_PIN_EN (1 << 8)
#define TMP006_CONFIG_DRDY (1 << 7) #define TMP006_CONFIG_DRDY (1 << 7)
#define TMP006_MID_VALUE 0x5449 /**< Manufacturer ID */ #define TMP006_MID_VALUE (0x5449) /**< Manufacturer ID */
#define TMP006_DID_VALUE 0x0067 /**< Device ID */ #define TMP006_DID_VALUE (0x0067) /**< Device ID */
#define I2C_SPEED I2C_SPEED_FAST #define I2C_SPEED I2C_SPEED_FAST
#define BUS (dev->p.i2c)
#define ADDR (dev->p.addr)
int tmp006_test(tmp006_t *dev) int tmp006_init(tmp006_t *dev, const tmp006_params_t *params)
{ {
int status; /* check parameters */
assert(dev && params);
uint8_t reg[2]; uint8_t reg[2];
uint16_t tmp; uint16_t tmp;
/* Acquire exclusive access to the bus. */ /* initialize the device descriptor */
i2c_acquire(dev->i2c); memcpy(&dev->p, params, sizeof(tmp006_params_t));
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_DEVICE_ID, reg, 2);
if (status != 2) {
/* Release the bus for other threads. */
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
if (dev->p.rate > TMP006_CONFIG_CR_AS16) {
LOG_ERROR("tmp006_init: invalid conversion rate!\n");
return -TMP006_ERROR_CONF;
}
/* setup the I2C bus */
i2c_acquire(BUS);
if (i2c_init_master(BUS, I2C_SPEED) < 0) {
i2c_release(BUS);
LOG_ERROR("tmp006_init: error initializing I2C bus\n");
return -TMP006_ERROR_BUS;
}
/* test device id */
if (i2c_read_regs(BUS, ADDR, TMP006_REGS_DEVICE_ID, reg, 2) != 2) {
i2c_release(BUS);
LOG_ERROR("tmp006_init: error reading device ID!\n");
return -TMP006_ERROR_BUS;
}
tmp = ((uint16_t)reg[0] << 8) | reg[1]; tmp = ((uint16_t)reg[0] << 8) | reg[1];
if (tmp != TMP006_DID_VALUE) { if (tmp != TMP006_DID_VALUE) {
return -1; return -TMP006_ERROR_DEV;
} }
return 0; /* set conversion rate */
} tmp = TMP006_CONFIG_CR(dev->p.rate);
int tmp006_init(tmp006_t *dev, i2c_t i2c, uint8_t address, uint8_t conv_rate)
{
int status;
uint8_t reg[2];
/* write device descriptor */
dev->i2c = i2c;
dev->addr = address;
dev->initialized = false;
if (conv_rate > TMP006_CONFIG_CR_AS16) {
return -1;
}
i2c_acquire(dev->i2c);
/* initialize the I2C bus */
status = i2c_init_master(i2c, I2C_SPEED);
if (status < 0) {
i2c_release(dev->i2c);
return -2;
}
i2c_release(dev->i2c);
if (tmp006_test(dev)) {
return -3;
}
uint16_t tmp = TMP006_CONFIG_CR(conv_rate);
reg[0] = (tmp >> 8); reg[0] = (tmp >> 8);
reg[1] = tmp; reg[1] = tmp;
if (i2c_write_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
/* Acquire exclusive access to the bus. */ i2c_release(BUS);
i2c_acquire(dev->i2c); LOG_ERROR("tmp006_init: error setting conversion rate!\n");
status = i2c_write_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2); return -TMP006_ERROR_BUS;
if (status != 2) {
i2c_release(dev->i2c);
return -4;
} }
i2c_release(dev->i2c); i2c_release(BUS);
dev->initialized = true;
return 0; return TMP006_OK;
} }
int tmp006_reset(tmp006_t *dev) int tmp006_reset(tmp006_t *dev)
{ {
int status;
uint8_t reg[2]; uint8_t reg[2];
uint16_t tmp = TMP006_CONFIG_RST; uint16_t tmp = TMP006_CONFIG_RST;
reg[0] = (tmp >> 8); reg[0] = (tmp >> 8);
reg[1] = tmp; reg[1] = tmp;
dev->initialized = false;
/* Acquire exclusive access to the bus. */ /* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c); i2c_acquire(BUS);
status = i2c_write_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2); if (i2c_write_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
if (status != 2) { i2c_release(BUS);
i2c_release(dev->i2c); return -TMP006_ERROR_BUS;
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
return 0; return TMP006_OK;
} }
int tmp006_set_active(tmp006_t *dev) int tmp006_set_active(tmp006_t *dev)
{ {
int status;
uint8_t reg[2]; uint8_t reg[2];
if (dev->initialized == false) { i2c_acquire(BUS);
return -1; if (i2c_read_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
} i2c_release(BUS);
return -TMP006_ERROR_BUS;
i2c_acquire(dev->i2c);
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2);
if (status != 2) {
i2c_release(dev->i2c);
return -1;
} }
reg[0] |= (TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8); reg[0] |= (TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8);
if (i2c_write_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
status = i2c_write_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2); i2c_release(BUS);
if (status != 2) { return -TMP006_ERROR_BUS;
i2c_release(dev->i2c);
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
return 0; return TMP006_OK;
} }
int tmp006_set_standby(tmp006_t *dev) int tmp006_set_standby(tmp006_t *dev)
{ {
int status;
uint8_t reg[2]; uint8_t reg[2];
i2c_acquire(dev->i2c); i2c_acquire(BUS);
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2); if (i2c_read_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
if (status != 2) { i2c_release(BUS);
i2c_release(dev->i2c); return -TMP006_ERROR_BUS;
return -1;
} }
i2c_release(dev->i2c);
reg[0] &= ~(TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8); reg[0] &= ~(TMP006_CONFIG_MOD(TMP006_CONFIG_MOD_CC) >> 8);
if (i2c_write_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
i2c_acquire(dev->i2c); i2c_release(BUS);
status = i2c_write_regs(dev->i2c, dev->addr, TMP006_CONFIG, reg, 2); return -TMP006_ERROR_BUS;
if (status != 2) {
i2c_release(dev->i2c);
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
return 0; return TMP006_OK;
} }
int tmp006_read(tmp006_t *dev, int16_t *rawv, int16_t *rawt, uint8_t *drdy) int tmp006_read(tmp006_t *dev, int16_t *rawv, int16_t *rawt, uint8_t *drdy)
{ {
int status; uint8_t reg[2];
uint8_t buf[2];
if (dev->initialized == false) { i2c_acquire(BUS);
return -1;
}
i2c_acquire(dev->i2c);
/* Register bytes are sent MSB first. */ /* Register bytes are sent MSB first. */
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_CONFIG, buf, 2); if (i2c_read_regs(BUS, ADDR, TMP006_REGS_CONFIG, reg, 2) != 2) {
if (status != 2) { i2c_release(BUS);
i2c_release(dev->i2c); return -TMP006_ERROR_BUS;
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
*drdy = buf[1] & (TMP006_CONFIG_DRDY);
*drdy = reg[1] & (TMP006_CONFIG_DRDY);
if (!(*drdy)) { if (!(*drdy)) {
/* conversion in progress */ LOG_DEBUG("tmp006_read: conversion in progress!\n");
return -1; return -TMP006_ERROR;
} }
i2c_acquire(dev->i2c); i2c_acquire(BUS);
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_V_OBJECT, buf, 2); if (i2c_read_regs(BUS, ADDR, TMP006_REGS_V_OBJECT, reg, 2) != 2) {
if (status != 2) { i2c_release(BUS);
i2c_release(dev->i2c); return -TMP006_ERROR_BUS;
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
*rawv = ((uint16_t)buf[0] << 8) | buf[1]; *rawv = ((uint16_t)reg[0] << 8) | reg[1];
i2c_acquire(dev->i2c); i2c_acquire(BUS);
status = i2c_read_regs(dev->i2c, dev->addr, TMP006_T_AMBIENT, buf, 2); if (i2c_read_regs(BUS, ADDR, TMP006_REGS_T_AMBIENT, reg, 2) != 2) {
if (status != 2) { i2c_release(BUS);
i2c_release(dev->i2c); return -TMP006_ERROR_BUS;
return -1;
} }
i2c_release(dev->i2c); i2c_release(BUS);
*rawt = ((uint16_t)buf[0] << 8) | buf[1]; *rawt = ((uint16_t)reg[0] << 8) | reg[1];
return 0; return TMP006_OK;
} }
void tmp006_convert(int16_t rawv, int16_t rawt, float *tamb, float *tobj) void tmp006_convert(int16_t rawv, int16_t rawt, float *tamb, float *tobj)