Contiki-NG
tmp102.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * Copyright (c) 2016, Zolertia <http://www.zolertia.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of the Contiki operating system.
31  *
32  */
33 /*---------------------------------------------------------------------------*/
34 /**
35  * \file
36  * Device drivers for tmp102 temperature sensor in Zolertia Z1.
37  * \author
38  * Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
39  * Marcus Lundén, SICS <mlunden@sics.se>
40  * Antonio Lignan, Zolertia <alinan@zolertia.com>
41  */
42 /*---------------------------------------------------------------------------*/
43 #include <stdio.h>
44 #include "contiki.h"
45 #include "i2cmaster.h"
46 #include "tmp102.h"
47 #include "lib/sensors.h"
48 /*---------------------------------------------------------------------------*/
49 #define DEBUG 0
50 #if DEBUG
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #else
53 #define PRINTF(...)
54 #endif
55 /*---------------------------------------------------------------------------*/
56 static uint8_t enabled;
57 /*---------------------------------------------------------------------------*/
58 void
60 {
61  /* Power Up TMP102 via pin */
62  TMP102_PWR_DIR |= TMP102_PWR_PIN;
63  TMP102_PWR_SEL &= ~TMP102_PWR_SEL;
64  TMP102_PWR_SEL2 &= ~TMP102_PWR_SEL;
65  TMP102_PWR_REN &= ~TMP102_PWR_SEL;
66  TMP102_PWR_OUT |= TMP102_PWR_PIN;
67 
68  /* Set up ports and pins for I2C communication */
69  i2c_enable();
70 
71  enabled = 1;
72 }
73 /*---------------------------------------------------------------------------*/
74 void
75 tmp102_stop(void)
76 {
77  /* Power off */
78  TMP102_PWR_OUT &= ~TMP102_PWR_PIN;
79  enabled = 0;
80 }
81 /*---------------------------------------------------------------------------*/
82 void
83 tmp102_write_reg(uint8_t reg, uint16_t val)
84 {
85  uint8_t tx_buf[] = { reg, 0x00, 0x00 };
86 
87  tx_buf[1] = (uint8_t)(val >> 8);
88  tx_buf[2] = (uint8_t)(val & 0x00FF);
89 
90  i2c_transmitinit(TMP102_ADDR);
91  while(i2c_busy());
92  PRINTF("I2C Ready to TX\n");
93 
94  i2c_transmit_n(3, tx_buf);
95  while(i2c_busy());
96  PRINTF("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg);
97 }
98 /*---------------------------------------------------------------------------*/
99 uint16_t
100 tmp102_read_reg(uint8_t reg)
101 {
102  uint8_t buf[] = { 0x00, 0x00 };
103  uint16_t retVal = 0;
104  uint8_t rtx = reg;
105  PRINTF("READ_REG 0x%02X\n", reg);
106 
107  /* transmit the register to read */
108  i2c_transmitinit(TMP102_ADDR);
109  while(i2c_busy());
110  i2c_transmit_n(1, &rtx);
111  while(i2c_busy());
112 
113  /* receive the data */
114  i2c_receiveinit(TMP102_ADDR);
115  while(i2c_busy());
116  i2c_receive_n(2, &buf[0]);
117  while(i2c_busy());
118 
119  retVal = (uint16_t)(buf[0] << 8 | (buf[1]));
120 
121  return retVal;
122 }
123 /*---------------------------------------------------------------------------*/
124 uint16_t
125 tmp102_read_temp_raw(void)
126 {
127  uint16_t rd = 0;
128  rd = tmp102_read_reg(TMP102_TEMP);
129  return rd;
130 }
131 /*---------------------------------------------------------------------------*/
132 int16_t
133 tmp102_read_temp_x100(void)
134 {
135  int16_t raw = 0;
136  int16_t sign = 1;
137  int16_t abstemp, temp_int;
138 
139  raw = (int16_t)tmp102_read_reg(TMP102_TEMP);
140  if(raw < 0) {
141  abstemp = (raw ^ 0xFFFF) + 1;
142  sign = -1;
143  } else {
144  abstemp = raw;
145  }
146 
147  /* Integer part of the temperature value and percents*/
148  temp_int = (abstemp >> 8) * sign * 100;
149  temp_int += ((abstemp & 0xff) * 100) / 0x100;
150  return temp_int;
151 }
152 /*---------------------------------------------------------------------------*/
153 int8_t
154 tmp102_read_temp_simple(void)
155 {
156  /* Casted to int8_t: We don't expect temperatures outside -128 to 127 C */
157  return tmp102_read_temp_x100() / 100;
158 }
159 /*---------------------------------------------------------------------------*/
160 static int
161 configure(int type, int value)
162 {
163  if(type != SENSORS_ACTIVE) {
164  return TMP102_ERROR;
165  }
166  if(value) {
167  tmp102_init();
168  } else {
169  tmp102_stop();
170  }
171  enabled = value;
172  return TMP102_SUCCESS;
173 }
174 /*---------------------------------------------------------------------------*/
175 static int
176 status(int type)
177 {
178  switch(type) {
179  case SENSORS_ACTIVE:
180  case SENSORS_READY:
181  return enabled;
182  }
183  return TMP102_SUCCESS;
184 }
185 /*---------------------------------------------------------------------------*/
186 static int
187 value(int type)
188 {
189  return (int)tmp102_read_temp_x100();
190 }
191 /*---------------------------------------------------------------------------*/
192 SENSORS_SENSOR(tmp102, TMP102_SENSOR, value, configure, status);
193 /*---------------------------------------------------------------------------*/
I2C communication device driver header file for Zolertia Z1 sensor node.
#define TMP102_ADDR
TMP102 slave address.
Definition: tmp102.h:59
void tmp102_init(void)
Initialiser for the TMP102 sensor driver.
Definition: tmp102.c:59
Device drivers header file for tmp102 temperature sensor in Zolertia Z1 WSN Platform...
#define TMP102_TEMP
TMP102 temperature data register.
Definition: tmp102.h:60