Contiki 2.6

tmp102-sensor.c

00001 /*
00002  * An interface to the TI TMP102 temperature sensor
00003  * 12 bit temperature reading, 0.5 deg. Celsius accuracy
00004  * -----------------------------------------------------------------
00005  *
00006  * Author  : Hedde Bosman (heddebosman@incas3.eu)
00007  */
00008 
00009 #include "contiki.h"
00010 #include "lib/sensors.h"
00011 #include "dev/tmp102-sensor.h"
00012 
00013 #ifndef bool
00014 #define bool uint8_t
00015 #endif
00016 
00017 #ifndef false
00018 #define false 0
00019 #endif
00020 
00021 #ifndef true
00022 #define true 1
00023 #endif
00024 
00025 
00026 static void set_configuration(uint8_t rate, bool precision) {
00027         uint8_t tx_buf[] = {TMP102_REGISTER_CONFIGURATION, 
00028                                                 0,
00029                                                 (precision ? TMP102_CONF_EXTENDED_MODE : 0) | ((rate << 6) & TMP102_CONF_CONVERSION_RATE)
00030                                                 };
00031 
00032         i2c_transmitinit(TMP102_ADDR, 3, tx_buf);
00033 }
00034 
00035 /*---------------------------------------------------------------------------*/
00036 static int value(int type) {
00037         uint8_t reg = TMP102_REGISTER_TEMPERATURE;
00038         uint8_t temp[2];
00039         int16_t temperature = 0;
00040         
00041         /* transmit the register to start reading from */
00042         i2c_transmitinit(TMP102_ADDR, 1, &reg);
00043         while (!i2c_transferred()); // wait for data to arrive
00044 
00045         /* receive the data */
00046         i2c_receiveinit(TMP102_ADDR, 2, temp);
00047         while (!i2c_transferred()); // wait for data to arrive
00048         
00049         // 12 bit normal mode
00050         temperature = ((temp[0] <<8) | (temp[1])) >> 4; // lsb
00051 
00052         // 13 bit extended mode
00053         //temperature = ((temp[0] <<8) | (temp[1])) >> 3; // lsb
00054 
00055         temperature = (100*temperature)/16; // in 100th of degrees
00056         
00057         return temperature;
00058 }
00059 /*---------------------------------------------------------------------------*/
00060 static int status(int type) {
00061         switch (type) {
00062                 case SENSORS_ACTIVE:
00063                 case SENSORS_READY:
00064                         return 1; // fix?
00065                         break;
00066         }
00067         return 0;
00068 }
00069 /*---------------------------------------------------------------------------*/
00070 static int configure(int type, int c) {
00071         switch (type) {
00072                 case SENSORS_ACTIVE:
00073                         if (c) {
00074                                 // set active
00075                                 set_configuration(1, false); // every 1 second, 12bit precision
00076                         } else {
00077                                 // set inactive
00078                         }
00079                         return 1;
00080         }
00081         return 0;
00082 }
00083 
00084 
00085 /*---------------------------------------------------------------------------*/
00086 SENSORS_SENSOR(tmp102_sensor, "Temperature", value, configure, status); // register the functions
00087