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) */
89typedef struct sensor_data {
90 uint16_t temp;
91 uint16_t hum;
92} sensor_data_t;
93
94/* Raw data, little endian */
95static uint16_t raw_temp;
96static uint16_t raw_hum;
97/*---------------------------------------------------------------------------*/
98static bool success;
99static sensor_data_t data;
100/*---------------------------------------------------------------------------*/
101static 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
116static struct ctimer startup_timer;
117/*---------------------------------------------------------------------------*/
118/**
119 * \brief Initialise the humidity sensor driver
120 * \return True if I2C operation successful
121 */
122static 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 */
141static void
142start(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 */
156static 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 */
185static void
186convert(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/*---------------------------------------------------------------------------*/
195static void
196notify_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 */
211static int
212value(int type)
213{
215 PRINTF("Sensor disabled or starting up (%d)\n", enabled);
216 return CC26XX_SENSOR_READING_ERROR;
217 }
218
219 if((type != HDC_1000_SENSOR_TYPE_TEMP) &&
220 type != HDC_1000_SENSOR_TYPE_HUMID) {
221 PRINTF("Invalid type\n");
222 return CC26XX_SENSOR_READING_ERROR;
223 }
224
225 float temp;
226 float hum;
227 convert(&temp, &hum);
228 PRINTF("HDC: %04X %04X t=%d h=%d\n", raw_temp, raw_hum,
229 (int)(temp * 100), (int)(hum * 100));
230
231 int rv = 0;
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 return rv;
238}
239/*---------------------------------------------------------------------------*/
240/**
241 * \brief Configuration function for the HDC1000 sensor.
242 *
243 * \param type Activate, enable or disable the sensor. See below
244 * \param enable
245 *
246 * When type == SENSORS_HW_INIT we turn on the hardware
247 * When type == SENSORS_ACTIVE and enable==1 we enable the sensor
248 * When type == SENSORS_ACTIVE and enable==0 we disable the sensor
249 */
250static int
251configure(int type, int enable)
252{
253 switch(type) {
254 case SENSORS_HW_INIT:
255 raw_temp = 0;
256 raw_hum = 0;
257 memset(&data, 0, sizeof(data));
258
259 sensor_init();
261 break;
262 case SENSORS_ACTIVE:
263 /* Must be initialised first */
264 if(enabled == HDC_1000_SENSOR_STATUS_DISABLED) {
266 }
267 if(enable) {
268 start();
269 ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
271 } else {
272 ctimer_stop(&startup_timer);
274 }
275 break;
276 default:
277 break;
278 }
279 return enabled;
280}
281/*---------------------------------------------------------------------------*/
282/**
283 * \brief Returns the status of the sensor
284 * \param type SENSORS_ACTIVE or SENSORS_READY
285 * \return One of the SENSOR_STATUS_xyz defines
286 */
287static int
288status(int type)
289{
290 switch(type) {
291 case SENSORS_ACTIVE:
292 case SENSORS_READY:
293 return enabled;
294 break;
295 default:
296 break;
297 }
299}
300/*---------------------------------------------------------------------------*/
301SENSORS_SENSOR(hdc_1000_sensor, "HDC1000", value, configure, status);
302/*---------------------------------------------------------------------------*/
303/** @} */
Header file for the Sensortag I2C Driver.
Header file for the callback timer.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
static bool read_data()
Take readings from the sensor.
static int value(int type)
Returns a reading from the sensor.
static int status(int type)
Returns the status of the sensor.
static void start(void)
Start measurement.
static void convert(float *temp, float *hum)
Convert raw data to temperature and humidity.
static bool sensor_init(void)
Initialise the humidity sensor driver.
static int configure(int type, int enable)
Configuration function for the HDC1000 sensor.
bool board_i2c_write_single(uint8_t data)
Single write to an I2C device.
Definition: board-i2c.c:188
bool board_i2c_read(uint8_t *data, uint8_t len)
Burst read from an I2C device.
Definition: board-i2c.c:207
bool sensor_common_write_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Write to a sensor's register over I2C.
Definition: sensor-common.c:54
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
@ HDC_1000_SENSOR_STATUS_INITIALISED
Initialised but idle.
@ HDC_1000_SENSOR_STATUS_TAKING_READINGS
Readings in progress.
@ HDC_1000_SENSOR_STATUS_READINGS_READY
Both readings ready.
@ HDC_1000_SENSOR_STATUS_DISABLED
Not initialised.
Header file for the Sensortag Common sensor utilities.
Header file with macros which rename TI CC26xxware functions.