41#include "lib/sensors.h"
50#include <ti/drivers/I2C.h>
51#include <ti/drivers/PIN.h>
59#define PRINTF(...) printf(__VA_ARGS__)
69#if BOARD_SENSORS_ENABLE
73#error "Board file doesn't define I2C address Board_TMP_ADDR"
75#define TMP_007_I2C_ADDRESS Board_TMP_ADDR
79#error "Board file doesn't define interrupt pin Board_TMP_RDY"
81#define TMP_007_TMP_RDY Board_TMP_RDY
84#define REG_VOLTAGE 0x00
85#define REG_LOCAL_TEMP 0x01
86#define REG_CONFIG 0x02
87#define REG_OBJ_TEMP 0x03
88#define REG_STATUS 0x04
89#define REG_PROD_ID 0x1F
92#define VAL_CONFIG_ON 0x1000
93#define VAL_CONFIG_OFF 0x0000
94#define VAL_CONFIG_RESET 0x8000
95#define VAL_PROD_ID 0x0078
98#define CONV_RDY_BIT 0x4000
101#define REGISTER_LENGTH 2
107#define HI_UINT16(a) (((a) >> 8) & 0xFF)
108#define LO_UINT16(a) (((a) >> 0) & 0xFF)
110#define SWAP16(v) ((LO_UINT16(v) << 8) | (HI_UINT16(v) << 0))
112#define LSB16(v) (HI_UINT16(v)), (LO_UINT16(v))
113#define MSB16(v) (LO_UINT16(v)), (HI_UINT16(v))
115static const PIN_Config pin_table[] = {
116 TMP_007_TMP_RDY | PIN_INPUT_EN | PIN_PULLUP | PIN_HYSTERESIS | PIN_IRQ_NEGEDGE,
120static PIN_State pin_state;
121static PIN_Handle pin_handle;
123static I2C_Handle i2c_handle;
127 volatile TMP_007_STATUS
status;
128 uint16_t local_tmp_latched;
129 uint16_t obj_tmp_latched;
132static TMP_007_Object tmp_007;
135#define SENSOR_STARTUP_DELAY 36
137static struct ctimer startup_timer;
146 if(pin_handle && i2c_handle) {
150 pin_handle = PIN_open(&pin_state, pin_table);
155 tmp_007.status = TMP_007_STATUS_DISABLED;
166 tmp_007.status = TMP_007_STATUS_READY;
167 sensors_changed(&tmp_007_sensor);
178 uint16_t cfg_value = (enable)
182 uint8_t cfg_data[] = { REG_CONFIG, LSB16(cfg_value) };
211 uint8_t status_data[] = { REG_STATUS };
212 uint16_t status_value = 0;
221 sizeof(status_data), &status_value,
222 sizeof(status_value));
227 status_value = SWAP16(status_value);
229 if((status_value & CONV_RDY_BIT) == 0) {
234 uint8_t local_temp_data[] = { REG_LOCAL_TEMP };
235 uint16_t local_temp_value = 0;
238 sizeof(local_temp_data), &local_temp_value,
239 sizeof(local_temp_value));
245 uint8_t obj_temp_data[] = { REG_OBJ_TEMP };
246 uint16_t obj_temp_value = 0;
249 sizeof(obj_temp_data), &obj_temp_value,
250 sizeof(obj_temp_value));
256 *local_tmp = SWAP16(local_temp_value);
257 *obj_tmp = SWAP16(obj_temp_value);
270convert(uint16_t *local_tmp, uint16_t *obj_tmp)
272 uint32_t local = (uint32_t)*local_tmp;
273 uint32_t obj = (uint32_t)*obj_tmp;
275 local = (local >> 2) * 3125 / 100;
276 obj = (obj >> 2) * 3125 / 100;
278 *local_tmp = (uint16_t)local;
279 *obj_tmp = (uint16_t)obj;
290 uint16_t raw_local_tmp = 0, local_tmp = 0;
291 uint16_t raw_obj_tmp = 0, obj_tmp = 0;
293 if(tmp_007.status != TMP_007_STATUS_READY) {
294 PRINTF(
"Sensor disabled or starting up (%d)\n", tmp_007.status);
295 return TMP_007_READING_ERROR;
299 case TMP_007_TYPE_OBJECT:
return tmp_007.obj_tmp_latched;
300 case TMP_007_TYPE_AMBIENT:
return tmp_007.local_tmp_latched;
302 case TMP_007_TYPE_ALL:
303 if(!
read_data(&raw_local_tmp, &raw_obj_tmp)) {
304 PRINTF(
"TMP: Read failed\n");
305 return TMP_007_READING_ERROR;
308 local_tmp = raw_local_tmp;
309 obj_tmp = raw_obj_tmp;
312 PRINTF(
"TMP: %04X %04X o=%d a=%d\n", raw_local_tmp, raw_obj_tmp,
313 (
int)(local_tmp), (
int)(obj_tmp));
315 tmp_007.local_tmp_latched = (int)(local_tmp);
316 tmp_007.obj_tmp_latched = (int)(obj_tmp);
321 PRINTF(
"Invalid type (%d)\n", type);
322 return TMP_007_READING_ERROR;
339 case SENSORS_HW_INIT:
341 return TMP_007_STATUS_DISABLED;
346 tmp_007.status = TMP_007_STATUS_INITIALIZED;
351 if(tmp_007.status == TMP_007_STATUS_DISABLED) {
352 return TMP_007_STATUS_DISABLED;
357 tmp_007.status = TMP_007_STATUS_NOT_READY;
361 tmp_007.status = TMP_007_STATUS_INITIALIZED;
369 return tmp_007.status;
382 return tmp_007.status;
Header file for the callback timer.
bool i2c_arch_write_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
static bool i2c_arch_write(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount)
Perform a write-only I2C transaction.
I2C_Handle i2c_arch_acquire(uint_least8_t index)
Open and lock the I2C Peripheral for use.
void i2c_arch_release(I2C_Handle i2c_handle)
Release the I2C Peripheral for other modules to use.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static bool read_data(uint16_t *local_tmp, uint16_t *obj_tmp)
Read the sensor value registers.
static int value(int type)
Returns a reading from the sensor.
#define VAL_CONFIG_OFF
Sensor off state.
static void convert(uint16_t *local_tmp, uint16_t *obj_tmp)
Convert raw data to values in degrees Celsius.
static int status(int type)
Returns the status of the sensor.
static bool enable_sensor(bool enable)
Turn the sensor on or off.
static void notify_ready_cb(void *not_used)
Callback when sensor is ready to read data from.
static bool sensor_init(void)
Initialize the TMP-007 sensor driver.
static int configure(int type, int enable)
Configuration function for the TMP-007 sensor.
#define VAL_CONFIG_ON
Sensor on state.
Implementation of the I2C HAL driver for CC13xx/CC26xx.
Header file for the Sensortag TMP-007 IR Thermophile sensor.
Header file with definitions related to SmartRF06 EB boards.