41 #include "lib/sensors.h" 49 #include <ti/drivers/I2C.h> 50 #include <ti/drivers/PIN.h> 58 #define PRINTF(...) printf(__VA_ARGS__) 68 #if BOARD_SENSORS_ENABLE 71 #ifndef Board_TMP_ADDR 72 #error "Board file doesn't define I2C address Board_TMP_ADDR" 74 #define TMP_007_I2C_ADDRESS Board_TMP_ADDR 78 #error "Board file doesn't define interrupt pin Board_TMP_RDY" 80 #define TMP_007_TMP_RDY Board_TMP_RDY 83 #define REG_VOLTAGE 0x00 84 #define REG_LOCAL_TEMP 0x01 85 #define REG_CONFIG 0x02 86 #define REG_OBJ_TEMP 0x03 87 #define REG_STATUS 0x04 88 #define REG_PROD_ID 0x1F 91 #define VAL_CONFIG_ON 0x1000 92 #define VAL_CONFIG_OFF 0x0000 93 #define VAL_CONFIG_RESET 0x8000 94 #define VAL_PROD_ID 0x0078 97 #define CONV_RDY_BIT 0x4000 100 #define REGISTER_LENGTH 2 106 #define HI_UINT16(a) (((a) >> 8) & 0xFF) 107 #define LO_UINT16(a) (((a) >> 0) & 0xFF) 109 #define SWAP16(v) ((LO_UINT16(v) << 8) | (HI_UINT16(v) << 0)) 111 #define LSB16(v) (LO_UINT16(v)), (HI_UINT16(v)) 112 #define MSB16(v) (HI_UINT16(v)), (LO_UINT16(v)) 114 static const PIN_Config pin_table[] = {
115 TMP_007_TMP_RDY | PIN_INPUT_EN | PIN_PULLUP | PIN_HYSTERESIS | PIN_IRQ_NEGEDGE,
119 static PIN_State pin_state;
120 static PIN_Handle pin_handle;
122 static I2C_Handle i2c_handle;
126 volatile TMP_007_STATUS
status;
127 uint16_t local_tmp_latched;
128 uint16_t obj_tmp_latched;
131 static TMP_007_Object tmp_007;
134 #define SENSOR_STARTUP_DELAY 36 136 static struct ctimer startup_timer;
150 I2C_Transaction i2c_transaction = {
152 .writeCount = wcount,
155 .slaveAddress = TMP_007_I2C_ADDRESS,
158 return I2C_transfer(i2c_handle, &i2c_transaction);
192 if(pin_handle && i2c_handle) {
196 pin_handle = PIN_open(&pin_state, pin_table);
201 I2C_Params i2c_params;
202 I2C_Params_init(&i2c_params);
204 i2c_params.transferMode = I2C_MODE_BLOCKING;
205 i2c_params.bitRate = I2C_400kHz;
207 i2c_handle = I2C_open(Board_I2C0, &i2c_params);
208 if(i2c_handle == NULL) {
209 PIN_close(pin_handle);
213 tmp_007.status = TMP_007_STATUS_DISABLED;
224 tmp_007.status = TMP_007_STATUS_READY;
225 sensors_changed(&tmp_007_sensor);
234 uint16_t cfg_value = (enable)
238 uint8_t cfg_data[] = { REG_CONFIG, LSB16(cfg_value) };
240 return i2c_write(cfg_data,
sizeof(cfg_data));
256 uint8_t status_data[] = { REG_STATUS };
257 uint16_t status_value = 0;
260 &status_value,
sizeof(status_value));
264 status_value = SWAP16(status_value);
266 if((status_value & CONV_RDY_BIT) == 0) {
270 uint8_t local_temp_data[] = { REG_LOCAL_TEMP };
271 uint16_t local_temp_value = 0;
274 &local_temp_value,
sizeof(local_temp_value));
279 uint8_t obj_temp_data[] = { REG_OBJ_TEMP };
280 uint16_t obj_temp_value = 0;
283 &obj_temp_value,
sizeof(obj_temp_value));
288 *local_tmp = SWAP16(local_temp_value);
289 *obj_tmp = SWAP16(obj_temp_value);
302 convert(uint16_t *local_tmp, uint16_t *obj_tmp)
304 uint32_t local = (uint32_t)*local_tmp;
305 uint32_t obj = (uint32_t)*obj_tmp;
307 local = (local >> 2) * 3125 / 100;
308 obj = (obj >> 2) * 3125 / 100;
310 *local_tmp = (uint16_t)local;
311 *obj_tmp = (uint16_t)obj;
322 uint16_t raw_local_tmp = 0, local_tmp = 0;
323 uint16_t raw_obj_tmp = 0, obj_tmp = 0;
325 if(tmp_007.status != TMP_007_STATUS_READY) {
326 PRINTF(
"Sensor disabled or starting up (%d)\n", tmp_007.status);
327 return TMP_007_READING_ERROR;
331 case TMP_007_TYPE_OBJECT:
return tmp_007.obj_tmp_latched;
332 case TMP_007_TYPE_AMBIENT:
return tmp_007.local_tmp_latched;
334 case TMP_007_TYPE_ALL:
335 if(!
read_data(&raw_local_tmp, &raw_obj_tmp)) {
336 return TMP_007_READING_ERROR;
339 local_tmp = raw_local_tmp;
340 obj_tmp = raw_obj_tmp;
343 PRINTF(
"TMP: %04X %04X o=%d a=%d\n", raw_local_tmp, raw_obj_tmp,
344 (
int)(local_tmp), (
int)(obj_tmp));
346 tmp_007.local_tmp_latched = (int)(local_tmp);
347 tmp_007.obj_tmp_latched = (int)(obj_tmp);
352 PRINTF(
"Invalid type (%d)\n", type);
353 return TMP_007_READING_ERROR;
370 case SENSORS_HW_INIT:
372 return TMP_007_STATUS_DISABLED;
377 tmp_007.status = TMP_007_STATUS_INITIALIZED;
382 if(tmp_007.status == TMP_007_STATUS_DISABLED) {
383 return TMP_007_STATUS_DISABLED;
388 tmp_007.status = TMP_007_STATUS_NOT_READY;
392 tmp_007.status = TMP_007_STATUS_INITIALIZED;
400 return tmp_007.status;
413 return tmp_007.status;
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static bool i2c_write_read(void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
static bool i2c_write(void *wbuf, size_t wcount)
Peform a write only I2C transaction.
static bool enable_sensor(bool enable)
Turn the sensor on or off.
static int value(int type)
Returns a reading from the sensor.
static bool read_data(uint16_t *local_tmp, uint16_t *obj_tmp)
Read the sensor value registers.
Header file for the callback timer
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Header file for the Sensortag TMP-007 IR Thermophile sensor.
static bool i2c_read(void *rbuf, size_t rcount)
Peform a read only I2C transaction.
#define VAL_CONFIG_OFF
Sensor off state.
static void notify_ready_cb(void *not_used)
Callback when sensor is ready to read data from.
static int configure(int type, int enable)
Configuration function for the TMP-007 sensor.
#define VAL_CONFIG_ON
Sensor on state.
Header file with definitions related to the sensors on the Sensortags.
static bool sensor_init(void)
Initialize the TMP-007 sensor driver.
static int status(int type)
Returns the status of the sensor.
static void convert(uint16_t *local_tmp, uint16_t *obj_tmp)
Convert raw data to values in degrees Celsius.