Contiki-NG
hdc-1000-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup sensortag-cc26xx-hdc-sensor
33  * @{
34  *
35  * \file
36  * Driver for the Sensortag HDC sensor
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "sys/ctimer.h"
41 #include "lib/sensors.h"
42 #include "hdc-1000-sensor.h"
43 #include "sensor-common.h"
44 #include "board-i2c.h"
45 
46 #include "ti-lib.h"
47 
48 #include <stdint.h>
49 #include <string.h>
50 #include <stdbool.h>
51 #include <stdio.h>
52 /*---------------------------------------------------------------------------*/
53 #define DEBUG 0
54 #if DEBUG
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 /*---------------------------------------------------------------------------*/
60 /* Sensor I2C address */
61 #define SENSOR_I2C_ADDRESS 0x43
62 
63 /* Registers */
64 #define HDC1000_REG_TEMP 0x00 /* Temperature */
65 #define HDC1000_REG_HUM 0x01 /* Humidity */
66 #define HDC1000_REG_CONFIG 0x02 /* Configuration */
67 #define HDC1000_REG_SERID_H 0xFB /* Serial ID high */
68 #define HDC1000_REG_SERID_M 0xFC /* Serial ID middle */
69 #define HDC1000_REG_SERID_L 0xFD /* Serial ID low */
70 #define HDC1000_REG_MANF_ID 0xFE /* Manufacturer ID */
71 #define HDC1000_REG_DEV_ID 0xFF /* Device ID */
72 
73 /* Fixed values */
74 #define HDC1000_VAL_MANF_ID 0x5449
75 #define HDC1000_VAL_DEV_ID 0x1000
76 #define HDC1000_VAL_CONFIG 0x1000 /* 14 bit, acquired in sequence */
77 
78 /* Sensor selection/deselection */
79 #define SENSOR_SELECT() board_i2c_select(BOARD_I2C_INTERFACE_0, SENSOR_I2C_ADDRESS)
80 #define SENSOR_DESELECT() board_i2c_deselect()
81 /*---------------------------------------------------------------------------*/
82 /* Byte swap of 16-bit register value */
83 #define HI_UINT16(a) (((a) >> 8) & 0xFF)
84 #define LO_UINT16(a) ((a) & 0xFF)
85 
86 #define SWAP(v) ((LO_UINT16(v) << 8) | HI_UINT16(v))
87 /*---------------------------------------------------------------------------*/
88 /* Raw data as returned from the sensor (Big Endian) */
89 typedef struct sensor_data {
90  uint16_t temp;
91  uint16_t hum;
92 } sensor_data_t;
93 
94 /* Raw data, little endian */
95 static uint16_t raw_temp;
96 static uint16_t raw_hum;
97 /*---------------------------------------------------------------------------*/
98 static bool success;
99 static sensor_data_t data;
100 /*---------------------------------------------------------------------------*/
101 static int enabled = HDC_1000_SENSOR_STATUS_DISABLED;
102 /*---------------------------------------------------------------------------*/
103 /*
104  * Maximum measurement durations in clock ticks. We use 14bit resolution, thus:
105  * - Tmp: 6.35ms
106  * - RH: 6.5ms
107  */
108 #define MEASUREMENT_DURATION 2
109 
110 /*
111  * Wait SENSOR_STARTUP_DELAY clock ticks between activation and triggering a
112  * reading (max 15ms)
113  */
114 #define SENSOR_STARTUP_DELAY 3
115 
116 static struct ctimer startup_timer;
117 /*---------------------------------------------------------------------------*/
118 /**
119  * \brief Initialise the humidity sensor driver
120  * \return True if I2C operation successful
121  */
122 static bool
124 {
125  uint16_t val;
126 
127  SENSOR_SELECT();
128 
129  /* Enable reading data in one operation */
130  val = SWAP(HDC1000_VAL_CONFIG);
131  success = sensor_common_write_reg(HDC1000_REG_CONFIG, (uint8_t *)&val, 2);
132 
133  SENSOR_DESELECT();
134 
135  return success;
136 }
137 /*---------------------------------------------------------------------------*/
138 /**
139  * \brief Start measurement
140  */
141 static void
142 start(void)
143 {
144  if(success) {
145  SENSOR_SELECT();
146 
147  success = board_i2c_write_single(HDC1000_REG_TEMP);
148  SENSOR_DESELECT();
149  }
150 }
151 /*---------------------------------------------------------------------------*/
152 /**
153  * \brief Take readings from the sensor
154  * \return true of I2C operations successful
155  */
156 static bool
158 {
159  bool valid;
160 
161  if(success) {
162  SENSOR_SELECT();
163 
164  success = board_i2c_read((uint8_t *)&data, sizeof(data));
165  SENSOR_DESELECT();
166 
167  /* Store temperature */
168  raw_temp = SWAP(data.temp);
169 
170  /* Store humidity */
171  raw_hum = SWAP(data.hum);
172  }
173 
174  valid = success;
175  success = true;
176 
177  return valid;
178 }
179 /*---------------------------------------------------------------------------*/
180 /**
181  * \brief Convert raw data to temperature and humidity
182  * \param temp - converted temperature
183  * \param hum - converted humidity
184  */
185 static void
186 convert(float *temp, float *hum)
187 {
188  /* Convert temperature to degrees C */
189  *temp = ((double)raw_temp / 65536) * 165 - 40;
190 
191  /* Convert relative humidity to a %RH value */
192  *hum = ((double)raw_hum / 65536) * 100;
193 }
194 /*---------------------------------------------------------------------------*/
195 static void
196 notify_ready(void *not_used)
197 {
199 
200  /* Latch readings */
201  read_data();
202 
203  sensors_changed(&hdc_1000_sensor);
204 }
205 /*---------------------------------------------------------------------------*/
206 /**
207  * \brief Returns a reading from the sensor
208  * \param type HDC_1000_SENSOR_TYPE_TEMP or HDC_1000_SENSOR_TYPE_HUMID
209  * \return Temperature (centi degrees C) or Humidity (centi %RH)
210  */
211 static int
212 value(int type)
213 {
214  int rv;
215  float temp;
216  float hum;
217 
219  PRINTF("Sensor disabled or starting up (%d)\n", enabled);
220  return CC26XX_SENSOR_READING_ERROR;
221  }
222 
223  if((type != HDC_1000_SENSOR_TYPE_TEMP) &&
224  type != HDC_1000_SENSOR_TYPE_HUMID) {
225  PRINTF("Invalid type\n");
226  return CC26XX_SENSOR_READING_ERROR;
227  } else {
228  convert(&temp, &hum);
229  PRINTF("HDC: %04X %04X t=%d h=%d\n", raw_temp, raw_hum,
230  (int)(temp * 100), (int)(hum * 100));
231 
232  if(type == HDC_1000_SENSOR_TYPE_TEMP) {
233  rv = (int)(temp * 100);
234  } else if(type == HDC_1000_SENSOR_TYPE_HUMID) {
235  rv = (int)(hum * 100);
236  }
237  }
238  return rv;
239 }
240 /*---------------------------------------------------------------------------*/
241 /**
242  * \brief Configuration function for the HDC1000 sensor.
243  *
244  * \param type Activate, enable or disable the sensor. See below
245  * \param enable
246  *
247  * When type == SENSORS_HW_INIT we turn on the hardware
248  * When type == SENSORS_ACTIVE and enable==1 we enable the sensor
249  * When type == SENSORS_ACTIVE and enable==0 we disable the sensor
250  */
251 static int
252 configure(int type, int enable)
253 {
254  switch(type) {
255  case SENSORS_HW_INIT:
256  raw_temp = 0;
257  raw_hum = 0;
258  memset(&data, 0, sizeof(data));
259 
260  sensor_init();
262  break;
263  case SENSORS_ACTIVE:
264  /* Must be initialised first */
265  if(enabled == HDC_1000_SENSOR_STATUS_DISABLED) {
267  }
268  if(enable) {
269  start();
270  ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
272  } else {
273  ctimer_stop(&startup_timer);
275  }
276  break;
277  default:
278  break;
279  }
280  return enabled;
281 }
282 /*---------------------------------------------------------------------------*/
283 /**
284  * \brief Returns the status of the sensor
285  * \param type SENSORS_ACTIVE or SENSORS_READY
286  * \return One of the SENSOR_STATUS_xyz defines
287  */
288 static int
289 status(int type)
290 {
291  switch(type) {
292  case SENSORS_ACTIVE:
293  case SENSORS_READY:
294  return enabled;
295  break;
296  default:
297  break;
298  }
300 }
301 /*---------------------------------------------------------------------------*/
302 SENSORS_SENSOR(hdc_1000_sensor, "HDC1000", value, configure, status);
303 /*---------------------------------------------------------------------------*/
304 /** @} */
bool board_i2c_read(uint8_t *data, uint8_t len)
Burst read from an I2C device.
Definition: board-i2c.c:207
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
Header file with macros which rename TI CC26xxware functions.
Header file for the Sensortag I2C Driver.
static int status(int type)
Returns the status of the sensor.
bool board_i2c_write_single(uint8_t data)
Single write to an I2C device.
Definition: board-i2c.c:188
static void start(void)
Start measurement.
Header file for the callback timer
Header file for the Sensortag Common sensor utilities.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Header file for the Sensortag TI HDC1000 sensor.
bool sensor_common_write_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Write to a sensor&#39;s register over I2C.
Definition: sensor-common.c:54
static bool read_data()
Take readings from the sensor.
static void convert(float *temp, float *hum)
Convert raw data to temperature and humidity.
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
static int value(int type)
Returns a reading from the sensor.
static int configure(int type, int enable)
Configuration function for the HDC1000 sensor.
static bool sensor_init(void)
Initialise the humidity sensor driver.