|
@@ -0,0 +1,243 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: William Penner <william.penner@intel.com>
|
|
3
|
+ * Copyright (c) 2014 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * This driver supports the HTU21D digital humidity and temperature
|
|
6
|
+ * sensor. The datasheet is available from:
|
|
7
|
+ * http://www.meas-spec.com/downloads/HTU21D.pdf
|
|
8
|
+ *
|
|
9
|
+ * This program is free software; you can redistribute it and/or modify
|
|
10
|
+ * it under the terms of the GNU General Public License as published by
|
|
11
|
+ * the Free Software Foundation; either version 2 of the License, or
|
|
12
|
+ * (at your option) any later version.
|
|
13
|
+ *
|
|
14
|
+ * This program is distributed in the hope that it will be useful,
|
|
15
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17
|
+ * GNU General Public License for more details.
|
|
18
|
+ *
|
|
19
|
+ * You should have received a copy of the GNU General Public License
|
|
20
|
+ * along with this program; if not, write to the Free Software
|
|
21
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
+ */
|
|
23
|
+
|
|
24
|
+#include <iostream>
|
|
25
|
+#include <unistd.h>
|
|
26
|
+#include <stdlib.h>
|
|
27
|
+
|
|
28
|
+#include "htu21d.h"
|
|
29
|
+
|
|
30
|
+using namespace upm;
|
|
31
|
+
|
|
32
|
+HTU21D::HTU21D(int bus, int devAddr) {
|
|
33
|
+ m_temperature = 0;
|
|
34
|
+ m_humidity = 0;
|
|
35
|
+
|
|
36
|
+ m_name = HTU21D_NAME;
|
|
37
|
+
|
|
38
|
+ m_controlAddr = devAddr;
|
|
39
|
+ m_bus = bus;
|
|
40
|
+
|
|
41
|
+ m_i2ControlCtx = mraa_i2c_init(m_bus);
|
|
42
|
+
|
|
43
|
+ mraa_result_t ret = mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|
|
44
|
+ if (ret != MRAA_SUCCESS) {
|
|
45
|
+ fprintf(stderr, "Error accessing i2c bus\n");
|
|
46
|
+ }
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+HTU21D::~HTU21D() {
|
|
50
|
+ mraa_i2c_stop(m_i2ControlCtx);
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+int32_t
|
|
54
|
+HTU21D::htu21_temp_ticks_to_millicelsius(int ticks)
|
|
55
|
+{
|
|
56
|
+ ticks &= ~0x0003; /* clear status bits */
|
|
57
|
+ /*
|
|
58
|
+ * Formula T = -46.85 + 175.72 * ST / 2^16 from datasheet p14,
|
|
59
|
+ * optimized for integer fixed point (3 digits) arithmetic
|
|
60
|
+ */
|
|
61
|
+ return ((21965 * (int32_t)ticks) >> 13) - 46850;
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+int32_t
|
|
65
|
+HTU21D::htu21_rh_ticks_to_per_cent_mille(int ticks)
|
|
66
|
+{
|
|
67
|
+ ticks &= ~0x0003; /* clear status bits */
|
|
68
|
+ /*
|
|
69
|
+ * Formula RH = -6 + 125 * SRH / 2^16 from datasheet p14,
|
|
70
|
+ * optimized for integer fixed point (3 digits) arithmetic
|
|
71
|
+ */
|
|
72
|
+ return ((15625 * (int32_t)ticks) >> 13) - 6000;
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+int
|
|
76
|
+HTU21D::sampleData(void)
|
|
77
|
+{
|
|
78
|
+ uint32_t itemp;
|
|
79
|
+
|
|
80
|
+ itemp = i2cReadReg_16(HTU21D_T_MEASUREMENT_HM);
|
|
81
|
+ m_temperature = htu21_temp_ticks_to_millicelsius(itemp);
|
|
82
|
+
|
|
83
|
+ itemp = i2cReadReg_16(HTU21D_RH_MEASUREMENT_HM);
|
|
84
|
+ m_humidity = htu21_rh_ticks_to_per_cent_mille(itemp);
|
|
85
|
+
|
|
86
|
+ return 0;
|
|
87
|
+}
|
|
88
|
+
|
|
89
|
+int32_t
|
|
90
|
+HTU21D::getTemperature(void)
|
|
91
|
+{
|
|
92
|
+ return m_temperature;
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+int32_t
|
|
96
|
+HTU21D::getHumidity(void)
|
|
97
|
+{
|
|
98
|
+ return m_humidity;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+/*
|
|
102
|
+ * This is the primary function to read the data. It will initiate
|
|
103
|
+ * a measurement cycle and will then return both the humidity and
|
|
104
|
+ * temperature values. piTemperature can be NULL.
|
|
105
|
+ */
|
|
106
|
+
|
|
107
|
+int32_t
|
|
108
|
+HTU21D::getRHumidity(int32_t* piTemperature)
|
|
109
|
+{
|
|
110
|
+ sampleData();
|
|
111
|
+ if (NULL != piTemperature)
|
|
112
|
+ *piTemperature = m_temperature;
|
|
113
|
+ return m_humidity;
|
|
114
|
+}
|
|
115
|
+
|
|
116
|
+/*
|
|
117
|
+ * Use the compensation equation from the datasheet to correct the
|
|
118
|
+ * current reading
|
|
119
|
+ * RHcomp = RHactualT + (25 - Tactual) * CoeffTemp
|
|
120
|
+ * RHcomp is in units of %RH * 1000
|
|
121
|
+ */
|
|
122
|
+int32_t
|
|
123
|
+HTU21D::getCompRH(void)
|
|
124
|
+{
|
|
125
|
+ return m_humidity + (25000 - m_temperature) * 3 / 20;
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+/*
|
|
130
|
+ * Test function: when reading the HTU21D many times rapidly should
|
|
131
|
+ * result in a temperature increase. This test will verify that the
|
|
132
|
+ * value is changing from read to read
|
|
133
|
+ */
|
|
134
|
+
|
|
135
|
+int
|
|
136
|
+HTU21D::testSensor(void)
|
|
137
|
+{
|
|
138
|
+ int iError = 0;
|
|
139
|
+ int i, j;
|
|
140
|
+ int32_t iTemp, iHum;
|
|
141
|
+ int32_t iTempMax, iTempMin;
|
|
142
|
+ int32_t iHumMax, iHumMin;
|
|
143
|
+ int32_t iHumFirst, iTempFirst;
|
|
144
|
+
|
|
145
|
+ fprintf(stdout, "Executing Sensor Test.\n Reading registers 100 times to look for operation\n" );
|
|
146
|
+
|
|
147
|
+ iHum = getRHumidity(&iTemp);
|
|
148
|
+ iTempFirst = iTempMax = iTempMin = iTemp;
|
|
149
|
+ iHumFirst = iHumMax = iHumMin = iHum;
|
|
150
|
+
|
|
151
|
+ for (i=0; i < 100; i++) {
|
|
152
|
+ iHum = getRHumidity(&iTemp);
|
|
153
|
+ if (iHum < iHumMin) iHumMin = iHum;
|
|
154
|
+ if (iHum > iHumMax) iHumMax = iHum;
|
|
155
|
+ if (iTemp < iTempMin) iTempMin = iTemp;
|
|
156
|
+ if (iTemp > iTempMax) iTempMax = iTemp;
|
|
157
|
+// fprintf(stdout, "Temp: %d Humidity: %d\n", iTemp, iHum);
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ if ((iTemp - iTempFirst) <= 0) {
|
|
161
|
+ fprintf(stdout, "! Temperature should have increased, but didn't\n" );
|
|
162
|
+ iError++;
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ if (iHumMin == iHumMax) {
|
|
166
|
+ fprintf(stdout, "! Humidity was unchanged - not working?\n" );
|
|
167
|
+ iError++;
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ if (iTempMin == iTempMax) {
|
|
171
|
+ fprintf(stdout, "! Temperature was unchanged - not working?\n" );
|
|
172
|
+ iError++;
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ if (iError == 0) {
|
|
176
|
+ fprintf(stdout, " Device appears functional\n" );
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ fprintf(stdout, " Test complete\n" );
|
|
180
|
+
|
|
181
|
+ return iError;
|
|
182
|
+}
|
|
183
|
+
|
|
184
|
+/*
|
|
185
|
+ * Functions to read and write data to the i2c device
|
|
186
|
+ */
|
|
187
|
+
|
|
188
|
+mraa_result_t
|
|
189
|
+HTU21D::i2cReadRegValue(int reg, uint32_t* puint32, int ibytes) {
|
|
190
|
+ uint32_t data = 0;
|
|
191
|
+
|
|
192
|
+ if (ibytes > 4)
|
|
193
|
+ ibytes = 4;
|
|
194
|
+
|
|
195
|
+ mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|
|
196
|
+ mraa_i2c_write_byte(m_i2ControlCtx, reg);
|
|
197
|
+
|
|
198
|
+ mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|
|
199
|
+ mraa_i2c_read(m_i2ControlCtx, (uint8_t *)&data, ibytes);
|
|
200
|
+
|
|
201
|
+ fprintf(stdout, "reg data = %08x\n", data);
|
|
202
|
+ *puint32 = be32toh(data) >> ((4-ibytes) * 8);
|
|
203
|
+ fprintf(stdout, "reg return = %08x\n", *puint32);
|
|
204
|
+
|
|
205
|
+ return MRAA_SUCCESS;
|
|
206
|
+}
|
|
207
|
+
|
|
208
|
+mraa_result_t
|
|
209
|
+HTU21D::i2cWriteReg (uint8_t reg, uint8_t value) {
|
|
210
|
+ mraa_result_t error = MRAA_SUCCESS;
|
|
211
|
+
|
|
212
|
+ uint8_t data[2] = { reg, value };
|
|
213
|
+ error = mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
|
|
214
|
+ error = mraa_i2c_write (m_i2ControlCtx, data, 2);
|
|
215
|
+
|
|
216
|
+ return error;
|
|
217
|
+}
|
|
218
|
+
|
|
219
|
+/*
|
|
220
|
+ * Function to read 16 bits starting at reg. This function
|
|
221
|
+ * was replaced due to functionality of using read() to
|
|
222
|
+ * access i2c data.
|
|
223
|
+ */
|
|
224
|
+uint16_t
|
|
225
|
+HTU21D::i2cReadReg_16 (int reg) {
|
|
226
|
+ uint16_t data;
|
|
227
|
+ mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|
|
228
|
+ data = (uint16_t)mraa_i2c_read_byte_data(m_i2ControlCtx, reg) << 8;
|
|
229
|
+ data |= (uint16_t)mraa_i2c_read_byte_data(m_i2ControlCtx, reg+1);
|
|
230
|
+ return data;
|
|
231
|
+}
|
|
232
|
+
|
|
233
|
+/*
|
|
234
|
+ * Function to read 8 bits starting at reg. This function
|
|
235
|
+ * was replaced due to functionality of using read() to
|
|
236
|
+ * access i2c data.
|
|
237
|
+ */
|
|
238
|
+uint8_t
|
|
239
|
+HTU21D::i2cReadReg_8 (int reg) {
|
|
240
|
+ mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|
|
241
|
+ return mraa_i2c_read_byte_data(m_i2ControlCtx, reg);
|
|
242
|
+}
|
|
243
|
+
|