Contiki 2.5
tmp102-sensor.c
1 /*
2  * An interface to the TI TMP102 temperature sensor
3  * 12 bit temperature reading, 0.5 deg. Celsius accuracy
4  * -----------------------------------------------------------------
5  *
6  * Author : Hedde Bosman (heddebosman@incas3.eu)
7  */
8 
9 #include "contiki.h"
10 #include "lib/sensors.h"
11 #include "dev/tmp102-sensor.h"
12 
13 #ifndef bool
14 #define bool uint8_t
15 #endif
16 
17 #ifndef false
18 #define false 0
19 #endif
20 
21 #ifndef true
22 #define true 1
23 #endif
24 
25 
26 static void set_configuration(uint8_t rate, bool precision) {
27  uint8_t tx_buf[] = {TMP102_REGISTER_CONFIGURATION,
28  0,
29  (precision ? TMP102_CONF_EXTENDED_MODE : 0) | ((rate << 6) & TMP102_CONF_CONVERSION_RATE)
30  };
31 
32  i2c_transmitinit(TMP102_ADDR, 3, tx_buf);
33 }
34 
35 /*---------------------------------------------------------------------------*/
36 static int value(int type) {
37  uint8_t reg = TMP102_REGISTER_TEMPERATURE;
38  uint8_t temp[2];
39  int16_t temperature = 0;
40 
41  /* transmit the register to start reading from */
42  i2c_transmitinit(TMP102_ADDR, 1, &reg);
43  while (!i2c_transferred()); // wait for data to arrive
44 
45  /* receive the data */
46  i2c_receiveinit(TMP102_ADDR, 2, temp);
47  while (!i2c_transferred()); // wait for data to arrive
48 
49  // 12 bit normal mode
50  temperature = ((temp[0] <<8) | (temp[1])) >> 4; // lsb
51 
52  // 13 bit extended mode
53  //temperature = ((temp[0] <<8) | (temp[1])) >> 3; // lsb
54 
55  temperature = (100*temperature)/16; // in 100th of degrees
56 
57  return temperature;
58 }
59 /*---------------------------------------------------------------------------*/
60 static int status(int type) {
61  switch (type) {
62  case SENSORS_ACTIVE:
63  case SENSORS_READY:
64  return 1; // fix?
65  break;
66  }
67  return 0;
68 }
69 /*---------------------------------------------------------------------------*/
70 static int configure(int type, int c) {
71  switch (type) {
72  case SENSORS_ACTIVE:
73  if (c) {
74  // set active
75  set_configuration(1, false); // every 1 second, 12bit precision
76  } else {
77  // set inactive
78  }
79  return 1;
80  }
81  return 0;
82 }
83 
84 
85 /*---------------------------------------------------------------------------*/
86 SENSORS_SENSOR(tmp102_sensor, "Temperature", value, configure, status); // register the functions
87