|
@@ -2,23 +2,23 @@
|
2
|
2
|
* Author: William Penner <william.penner@intel.com>
|
3
|
3
|
* Copyright (c) 2014 Intel Corporation.
|
4
|
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
|
|
5
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+ * in the Software without restriction, including without limitation the rights
|
|
8
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+ * furnished to do so, subject to the following conditions:
|
8
|
11
|
*
|
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.
|
|
12
|
+ * The above copyright notice and this permission notice shall be included in
|
|
13
|
+ * all copies or substantial portions of the Software.
|
13
|
14
|
*
|
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.
|
|
15
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+ * THE SOFTWARE.
|
22
|
22
|
*/
|
23
|
23
|
|
24
|
24
|
#include <iostream>
|
|
@@ -44,32 +44,38 @@ HTU21D::HTU21D(int bus, int devAddr) {
|
44
|
44
|
if (ret != MRAA_SUCCESS) {
|
45
|
45
|
fprintf(stderr, "Error accessing i2c bus\n");
|
46
|
46
|
}
|
|
47
|
+ resetSensor();
|
47
|
48
|
}
|
48
|
49
|
|
49
|
50
|
HTU21D::~HTU21D() {
|
50
|
51
|
mraa_i2c_stop(m_i2ControlCtx);
|
51
|
52
|
}
|
52
|
53
|
|
|
54
|
+int
|
|
55
|
+HTU21D::resetSensor(void)
|
|
56
|
+{
|
|
57
|
+ uint8_t data;
|
|
58
|
+ mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
|
|
59
|
+ mraa_i2c_write (m_i2ControlCtx, &data, 1);
|
|
60
|
+ usleep(20000);
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+/*
|
|
64
|
+ * Convert register value to degC * 1000
|
|
65
|
+ */
|
53
|
66
|
int32_t
|
54
|
|
-HTU21D::htu21_temp_ticks_to_millicelsius(int ticks)
|
|
67
|
+HTU21D::convertTemp(int32_t regval)
|
55
|
68
|
{
|
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;
|
|
69
|
+ return ((21965 * (regval & 0xFFFC)) >> 13) - 46850;
|
62
|
70
|
}
|
63
|
71
|
|
|
72
|
+/*
|
|
73
|
+ * Convert register value to %RH * 1000
|
|
74
|
+ */
|
64
|
75
|
int32_t
|
65
|
|
-HTU21D::htu21_rh_ticks_to_per_cent_mille(int ticks)
|
|
76
|
+HTU21D::convertRH(int regval)
|
66
|
77
|
{
|
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;
|
|
78
|
+ return ((15625 * (regval & 0xFFFC)) >> 13) - 6000;
|
73
|
79
|
}
|
74
|
80
|
|
75
|
81
|
int
|
|
@@ -77,40 +83,31 @@ HTU21D::sampleData(void)
|
77
|
83
|
{
|
78
|
84
|
uint32_t itemp;
|
79
|
85
|
|
80
|
|
- itemp = i2cReadReg_16(HTU21D_T_MEASUREMENT_HM);
|
81
|
|
- m_temperature = htu21_temp_ticks_to_millicelsius(itemp);
|
|
86
|
+ itemp = i2cReadReg_16(HTU21D_READ_TEMP_HOLD);
|
|
87
|
+ m_temperature = convertTemp(itemp);
|
82
|
88
|
|
83
|
|
- itemp = i2cReadReg_16(HTU21D_RH_MEASUREMENT_HM);
|
84
|
|
- m_humidity = htu21_rh_ticks_to_per_cent_mille(itemp);
|
|
89
|
+ itemp = i2cReadReg_16(HTU21D_READ_HUMIDITY_HOLD);
|
|
90
|
+ m_humidity = convertRH(itemp);
|
85
|
91
|
|
86
|
92
|
return 0;
|
87
|
93
|
}
|
88
|
94
|
|
89
|
|
-int32_t
|
90
|
|
-HTU21D::getTemperature(void)
|
91
|
|
-{
|
92
|
|
- return m_temperature;
|
93
|
|
-}
|
94
|
|
-
|
95
|
|
-int32_t
|
96
|
|
-HTU21D::getHumidity(void)
|
|
95
|
+float
|
|
96
|
+HTU21D::getTemperature(int bSampleData)
|
97
|
97
|
{
|
98
|
|
- return m_humidity;
|
|
98
|
+ if (bSampleData) {
|
|
99
|
+ sampleData();
|
|
100
|
+ }
|
|
101
|
+ return (float)m_temperature / 1000;
|
99
|
102
|
}
|
100
|
103
|
|
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)
|
|
104
|
+float
|
|
105
|
+HTU21D::getHumidity(int bSampleData)
|
109
|
106
|
{
|
110
|
|
- sampleData();
|
111
|
|
- if (NULL != piTemperature)
|
112
|
|
- *piTemperature = m_temperature;
|
113
|
|
- return m_humidity;
|
|
107
|
+ if (bSampleData) {
|
|
108
|
+ sampleData();
|
|
109
|
+ }
|
|
110
|
+ return (float)m_humidity / 1000;
|
114
|
111
|
}
|
115
|
112
|
|
116
|
113
|
/*
|
|
@@ -119,12 +116,30 @@ HTU21D::getRHumidity(int32_t* piTemperature)
|
119
|
116
|
* RHcomp = RHactualT + (25 - Tactual) * CoeffTemp
|
120
|
117
|
* RHcomp is in units of %RH * 1000
|
121
|
118
|
*/
|
122
|
|
-int32_t
|
123
|
|
-HTU21D::getCompRH(void)
|
|
119
|
+float
|
|
120
|
+HTU21D::getCompRH(int bSampleData)
|
124
|
121
|
{
|
125
|
|
- return m_humidity + (25000 - m_temperature) * 3 / 20;
|
|
122
|
+ if (bSampleData) {
|
|
123
|
+ sampleData();
|
|
124
|
+ }
|
|
125
|
+ return (float)(m_humidity + (25000 - m_temperature) * 3 / 20) / 1000;
|
126
|
126
|
}
|
127
|
127
|
|
|
128
|
+int
|
|
129
|
+HTU21D::setHeater(int bEnable)
|
|
130
|
+{
|
|
131
|
+ uint8_t userreg;
|
|
132
|
+
|
|
133
|
+ userreg = i2cReadReg_8(HTU21D_READ_USER_REG);
|
|
134
|
+ if (bEnable)
|
|
135
|
+ userreg |= HTU21D_HEATER_ENABLE;
|
|
136
|
+ else
|
|
137
|
+ userreg &= ~HTU21D_HEATER_ENABLE;
|
|
138
|
+ if (i2cWriteReg(HTU21D_WRITE_USER_REG, userreg) < 0)
|
|
139
|
+ return -1;
|
|
140
|
+
|
|
141
|
+ return 0;
|
|
142
|
+}
|
128
|
143
|
|
129
|
144
|
/*
|
130
|
145
|
* Test function: when reading the HTU21D many times rapidly should
|
|
@@ -135,43 +150,50 @@ HTU21D::getCompRH(void)
|
135
|
150
|
int
|
136
|
151
|
HTU21D::testSensor(void)
|
137
|
152
|
{
|
|
153
|
+ int i;
|
138
|
154
|
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);
|
|
155
|
+ float fTemp, fHum;
|
|
156
|
+ float fTempMax, fTempMin;
|
|
157
|
+ float fHumMax, fHumMin;
|
|
158
|
+ float fHumFirst, fTempFirst;
|
|
159
|
+
|
|
160
|
+ fprintf(stdout, "Executing Sensor Test\n" );
|
|
161
|
+
|
|
162
|
+ fHum = getHumidity(true);
|
|
163
|
+ fTemp = getTemperature(false);
|
|
164
|
+ fTempFirst = fTempMax = fTempMin = fTemp;
|
|
165
|
+ fHumFirst = fHumMax = fHumMin = fHum;
|
|
166
|
+
|
|
167
|
+ // Turn on the heater to make a sensor change
|
|
168
|
+ setHeater(true);
|
|
169
|
+
|
|
170
|
+ // Then sample the sensor a few times
|
|
171
|
+ for (i=0; i < 10; i++) {
|
|
172
|
+ fHum = getHumidity(true);
|
|
173
|
+ fTemp = getTemperature(false);
|
|
174
|
+ if (fHum < fHumMin) fHumMin = fHum;
|
|
175
|
+ if (fHum > fHumMax) fHumMax = fHum;
|
|
176
|
+ if (fTemp < fTempMin) fTempMin = fTemp;
|
|
177
|
+ if (fTemp > fTempMax) fTempMax = fTemp;
|
|
178
|
+ usleep(50000);
|
158
|
179
|
}
|
159
|
180
|
|
160
|
|
- if ((iTemp - iTempFirst) <= 0) {
|
161
|
|
- fprintf(stdout, "! Temperature should have increased, but didn't\n" );
|
|
181
|
+ // Turn off the heater
|
|
182
|
+ setHeater(false);
|
|
183
|
+
|
|
184
|
+ // Now check the results
|
|
185
|
+ if ((fTemp - fTempFirst) <= 0) {
|
|
186
|
+ fprintf(stdout, " Temperature should have increased, but didn't\n" );
|
162
|
187
|
iError++;
|
163
|
188
|
}
|
164
|
|
-
|
165
|
|
- if (iHumMin == iHumMax) {
|
166
|
|
- fprintf(stdout, "! Humidity was unchanged - not working?\n" );
|
|
189
|
+ if (fHumMin == fHumMax) {
|
|
190
|
+ fprintf(stdout, " Humidity reading was unchanged - warning\n" );
|
167
|
191
|
iError++;
|
168
|
192
|
}
|
169
|
|
-
|
170
|
|
- if (iTempMin == iTempMax) {
|
171
|
|
- fprintf(stdout, "! Temperature was unchanged - not working?\n" );
|
|
193
|
+ if (fTempMin == fTempMax) {
|
|
194
|
+ fprintf(stdout, " Temperature reading was unchanged - warning\n" );
|
172
|
195
|
iError++;
|
173
|
196
|
}
|
174
|
|
-
|
175
|
197
|
if (iError == 0) {
|
176
|
198
|
fprintf(stdout, " Device appears functional\n" );
|
177
|
199
|
}
|
|
@@ -185,42 +207,17 @@ HTU21D::testSensor(void)
|
185
|
207
|
* Functions to read and write data to the i2c device
|
186
|
208
|
*/
|
187
|
209
|
|
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
|
210
|
mraa_result_t
|
209
|
211
|
HTU21D::i2cWriteReg (uint8_t reg, uint8_t value) {
|
210
|
212
|
mraa_result_t error = MRAA_SUCCESS;
|
211
|
213
|
|
212
|
214
|
uint8_t data[2] = { reg, value };
|
213
|
|
- error = mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
|
|
215
|
+ mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
|
214
|
216
|
error = mraa_i2c_write (m_i2ControlCtx, data, 2);
|
215
|
217
|
|
216
|
218
|
return error;
|
217
|
219
|
}
|
218
|
220
|
|
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
|
221
|
uint16_t
|
225
|
222
|
HTU21D::i2cReadReg_16 (int reg) {
|
226
|
223
|
uint16_t data;
|
|
@@ -230,11 +227,6 @@ HTU21D::i2cReadReg_16 (int reg) {
|
230
|
227
|
return data;
|
231
|
228
|
}
|
232
|
229
|
|
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
|
230
|
uint8_t
|
239
|
231
|
HTU21D::i2cReadReg_8 (int reg) {
|
240
|
232
|
mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
|