Contiki 3.x
tmp102.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Device drivers for tmp102 temperature sensor in Zolertia Z1.
36  * \author
37  * Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
38  * Marcus Lundén, SICS <mlunden@sics.se>
39  */
40 
41 #include <stdio.h>
42 #include "contiki.h"
43 #include "i2cmaster.h"
44 #include "tmp102.h"
45 
46 /* Bitmasks and bit flag variable for keeping track of tmp102 status. */
47 enum TMP102_STATUSTYPES {
48  /* must be a bit and not more, not using 0x00. */
49  INITED = 0x01,
50  RUNNING = 0x02,
51  STOPPED = 0x04,
52  LOW_POWER = 0x08,
53  AAA = 0x10, /* available to extend this... */
54  BBB = 0x20, /* available to extend this... */
55  CCC = 0x40, /* available to extend this... */
56  DDD = 0x80 /* available to extend this... */
57 };
58 static enum TMP102_STATUSTYPES _TMP102_STATUS = 0x00;
59 
60 /*---------------------------------------------------------------------------*/
61 /* PROCESS(tmp102_process, "Temperature Sensor process"); */
62 
63 /*---------------------------------------------------------------------------*/
64 /* Init the temperature sensor: ports, pins, registers, interrupts (none enabled), I2C,
65  default threshold values etc. */
66 
67 void
68 tmp102_init(void)
69 {
70  if(!(_TMP102_STATUS & INITED)) {
71  PRINTFDEBUG("TMP102 init\n");
72  _TMP102_STATUS |= INITED;
73  /* Power Up TMP102 via pin */
74  TMP102_PWR_DIR |= TMP102_PWR_PIN;
75  TMP102_PWR_SEL &= ~TMP102_PWR_SEL;
76  TMP102_PWR_SEL2 &= ~TMP102_PWR_SEL;
77  TMP102_PWR_REN &= ~TMP102_PWR_SEL;
78  TMP102_PWR_OUT |= TMP102_PWR_PIN;
79 
80  /* Set up ports and pins for I2C communication */
81  i2c_enable();
82  }
83 }
84 /*---------------------------------------------------------------------------*/
85 /* Write to a 16-bit register.
86  args:
87  reg register to write to
88  val value to write
89  */
90 
91 void
92 tmp102_write_reg(uint8_t reg, uint16_t val)
93 {
94  uint8_t tx_buf[] = { reg, 0x00, 0x00 };
95 
96  tx_buf[1] = (uint8_t)(val >> 8);
97  tx_buf[2] = (uint8_t)(val & 0x00FF);
98 
99  i2c_transmitinit(TMP102_ADDR);
100  while(i2c_busy());
101  PRINTFDEBUG("I2C Ready to TX\n");
102 
103  i2c_transmit_n(3, tx_buf);
104  while(i2c_busy());
105  PRINTFDEBUG("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg);
106 }
107 /*---------------------------------------------------------------------------*/
108 /* Read register.
109  args:
110  reg what register to read
111  returns the value of the read register type uint16_t
112  */
113 
114 uint16_t
115 tmp102_read_reg(uint8_t reg)
116 {
117  uint8_t buf[] = { 0x00, 0x00 };
118  uint16_t retVal = 0;
119  uint8_t rtx = reg;
120  PRINTFDEBUG("READ_REG 0x%02X\n", reg);
121 
122  /* transmit the register to read */
123  i2c_transmitinit(TMP102_ADDR);
124  while(i2c_busy());
125  i2c_transmit_n(1, &rtx);
126  while(i2c_busy());
127 
128  /* receive the data */
129  i2c_receiveinit(TMP102_ADDR);
130  while(i2c_busy());
131  i2c_receive_n(2, &buf[0]);
132  while(i2c_busy());
133 
134  retVal = (uint16_t)(buf[0] << 8 | (buf[1]));
135 
136  return retVal;
137 }
138 /*---------------------------------------------------------------------------*/
139 /* Read temperature in a raw format. Further processing will be needed
140  to make an interpretation of these 12 or 13-bit data, depending on configuration
141  */
142 
143 uint16_t
144 tmp102_read_temp_raw(void)
145 {
146  uint16_t rd = 0;
147 
148  rd = tmp102_read_reg(TMP102_TEMP);
149 
150  return rd;
151 }
152 int16_t
153 tmp102_read_temp_x100(void)
154 {
155  int16_t raw = 0;
156  int16_t sign = 1;
157  int16_t abstemp, temp_int;
158 
159  raw = (int16_t)tmp102_read_reg(TMP102_TEMP);
160  if(raw < 0) {
161  abstemp = (raw ^ 0xFFFF) + 1;
162  sign = -1;
163  } else {
164  abstemp = raw;
165  }
166 
167  /* Integer part of the temperature value and percents*/
168  temp_int = (abstemp >> 8) * sign * 100;
169  temp_int += ((abstemp & 0xff) * 100) / 0x100;
170 
171  /* See test-tmp102.c on how to print values of temperature with decimals
172  fractional part in 1/10000 of degree
173  temp_frac = ((abstemp >>4) % 16) * 625;
174  Data could be multiplied by 63 to have less bit-growth and 1/1000 precision
175  Data could be multiplied by 64 (<< 6) to trade-off precision for speed
176  */
177 
178  return temp_int;
179 }
180 /*---------------------------------------------------------------------------*/
181 /* Simple Read temperature. Return is an integer with temperature in 1deg. precision
182  Return value is a signed 8 bit integer.
183  */
184 
185 int8_t
186 tmp102_read_temp_simple(void)
187 {
188  return (int8_t)tmp102_read_temp_x100() / 100;
189 }
Device drivers header file for tmp102 temperature sensor in Zolertia Z1 WSN Platform.
void i2c_enable(void)
Configure serial controller in I2C mode and set I2C speed.
Definition: i2c.c:52
I2C communication device driver header file for Zolertia Z1 sensor node.