Contiki-NG
sht25.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Zolertia <http://www.zolertia.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 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  * \file
34  * SHT25 temperature and humidity sensor driver
35  * \author
36  * Antonio Lignan <alinan@zolertia.com>
37  */
38 #include <stdio.h>
39 #include "contiki.h"
40 #include "i2cmaster.h"
41 #include "dev/sht25.h"
42 #include "lib/sensors.h"
43 /*---------------------------------------------------------------------------*/
44 static uint8_t enabled;
45 /*---------------------------------------------------------------------------*/
46 static int
47 configure(int type, int value)
48 {
49  if(type != SENSORS_ACTIVE) {
50  return SHT25_ERROR;
51  }
52  if(value) {
53  i2c_enable();
54  } else {
55  i2c_disable();
56  }
57  enabled = value;
58  return 0;
59 }
60 /*---------------------------------------------------------------------------*/
61 static int
62 status(int type)
63 {
64  switch(type) {
65  case SENSORS_ACTIVE:
66  case SENSORS_READY:
67  return enabled;
68  }
69  return 0;
70 }
71 /*---------------------------------------------------------------------------*/
72 static uint16_t
73 sht25_read_reg(uint8_t reg)
74 {
75  uint8_t buf[] = { 0x00, 0x00 };
76  uint16_t retval;
77  uint8_t rtx = reg;
78 
79  /* transmit the register to read */
80  i2c_transmitinit(SHT25_ADDR);
81  while(i2c_busy());
82  i2c_transmit_n(1, &rtx);
83  while(i2c_busy());
84  /* receive the data */
85  i2c_receiveinit(SHT25_ADDR);
86  while(i2c_busy());
87  i2c_receive_n(2, &buf[0]);
88  while(i2c_busy());
89 
90  retval = (uint16_t)(buf[0] << 8 | (buf[1]));
91  return retval;
92 }
93 /*---------------------------------------------------------------------------*/
94 static int16_t
95 sht25_convert(uint8_t variable, uint16_t value)
96 {
97  int16_t rd;
98  uint32_t buff;
99  buff = (uint32_t)value;
100  if(variable == SHT25_VAL_TEMP) {
101  buff *= 17572;
102  buff = buff >> 16;
103  rd = (int16_t)buff - 4685;
104  } else {
105  buff *= 12500;
106  buff = buff >> 16;
107  rd = (int16_t)buff - 600;
108  rd = (rd > 10000) ? 10000 : rd;
109  }
110  return rd;
111 }
112 /*---------------------------------------------------------------------------*/
113 static int16_t
114 sht25_read(uint8_t variable)
115 {
116  int16_t rd;
117  uint16_t raw;
118 
119  if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) {
120  return SHT25_ERROR;
121  }
122  raw = sht25_read_reg(variable);
123  rd = sht25_convert(variable, raw);
124  return rd;
125 }
126 /*---------------------------------------------------------------------------*/
127 static int
128 value(int type)
129 {
130  return sht25_read(type);
131 }
132 /*---------------------------------------------------------------------------*/
133 SENSORS_SENSOR(sht25, SHT25_SENSOR, value, configure, status);
134 /*---------------------------------------------------------------------------*/
I2C communication device driver header file for Zolertia Z1 sensor node.