-
Notifications
You must be signed in to change notification settings - Fork 0
/
HELIOS_Si7021.cpp
411 lines (341 loc) · 7.51 KB
/
HELIOS_Si7021.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (c) 2020 Helmut Tschemernjak
* 30826 Garbsen (Hannover) Germany
* Licensed under the Apache License, Version 2.0);
*/
#ifdef __MBED__
#include "mbed.h"
#include "xPinMap.h"
#elif ARDUINO
#define FEATURE_SI7021
#include <Wire.h>
#else
#error "unkown platform"
typedef PinName int;
#endif
#ifdef FEATURE_SI7021
#include <HELIOS_Si7021.h>
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
#define SI7021_MEASRH_HOLD_CMD 0xE5 // Measure Relative Humidity, Hold Master Mode
#define SI7021_MEASRH_NOHOLD_CMD 0xF5 // Measure Relative Humidity, No Hold Master Mode
#define SI7021_MEASTEMP_HOLD_CMD 0xE3 // Measure Temperature, Hold Master Mode
#define SI7021_MEASTEMP_NOHOLD_CMD 0xF3 // Measure Temperature, No Hold Master Mode
#define SI7021_READPREVTEMP_CMD 0xE0 // Read Temperature Value from Previous RH Measurement
#define SI7021_RESET_CMD 0xFE
#define SI7021_WRITERHT_REG_CMD 0xE6 // Write RH/T User Register 1
#define SI7021_READRHT_REG_CMD 0xE7 // Read RH/T User Register 1
#define SI7021_WRITEHEATER_REG_CMD 0x51 // Write Heater Control Register
#define SI7021_READHEATER_REG_CMD 0x11 // Read Heater Control Register
#define SI7021_ID1_CMD 0xFA0F // Read Electronic ID 1st Byte
#define SI7021_ID2_CMD 0xFCC9 // Read Electronic ID 2nd Byte
#define SI7021_FIRMVERS_CMD 0x84B8 // Read Firmware Revision
#define SI7021_REV_1 0xff
#define SI7021_REV_2 0x20
/**************************************************************************/
HELIOS_Si7021::HELIOS_Si7021(PinName sda, PinName scl)
{
_foundDevice = false;
_initDone = false;
_skipDeviceInit = false;
_sda = sda;
_scl = scl;
#ifdef __MBED__
_i2c = NULL;
#endif
}
bool
HELIOS_Si7021::_init(void)
{
if (_initDone)
return true;
#ifdef __MBED__
if (!_i2c)
_i2c = new I2C(_sda, _scl);
#elif ARDUINO
#ifdef ARDUINO_ARCH_ESP32
Wire.begin(_sda, _scl);
#else
Wire.begin();
#endif
#else
#error "Unkown OS"
#endif
if (!_skipDeviceInit) {
reset();
if (_readRegister8(SI7021_READRHT_REG_CMD) != 0x3A)
return false;
}
_foundDevice = true;
_initDone = true;
return true;
}
HELIOS_Si7021::~HELIOS_Si7021(void)
{
#ifdef __MBED__
if (_i2c)
delete _i2c;
#endif
}
void HELIOS_Si7021::reset(void)
{
_writeRegister8(SI7021_RESET_CMD);
_waitMillis(50);
}
bool
HELIOS_Si7021::hasSensor(void)
{
if (!_initDone)
_init();
return _foundDevice;
}
uint64_t
HELIOS_Si7021::getSerialNumber(void)
{
if (!_initDone)
_init();
if (!_foundDevice)
return -1;
uint32_t sernum_a, sernum_b;
_writeRegister8x2(SI7021_ID1_CMD >> 8, SI7021_ID1_CMD & 0xFF);
_readBytes(_data, 8);
sernum_a = _data[0];
sernum_a <<= 8;
sernum_a |= _data[2];
sernum_a <<= 8;
sernum_a |= _data[4];
sernum_a <<= 8;
sernum_a |= _data[6];
_writeRegister8x2(SI7021_ID2_CMD >> 8, SI7021_ID2_CMD & 0xFF);
_readBytes(_data, 8);
sernum_b = _data[0];
sernum_b <<= 8;
sernum_b |= _data[2];
sernum_b <<= 8;
sernum_b |= _data[4];
sernum_b <<= 8;
sernum_b |= _data[6];
return (uint64_t)sernum_a << 32 | (uint64_t)sernum_b;
}
int
HELIOS_Si7021::getRevision(void)
{
if (!_initDone)
_init();
if (!_foundDevice)
return -1;
_writeRegister8x2(SI7021_FIRMVERS_CMD >> 8, SI7021_FIRMVERS_CMD & 0xFF);
_readBytes(_data, 2);
if (_data[0] == SI7021_REV_1) {
return 1;
} else if (_data[0] == SI7021_REV_2) {
return 2;
} else {
return -2; // unkown
}
}
float HELIOS_Si7021::readTemperature(void) {
if (!_initDone)
_init();
if (!_foundDevice)
return NAN;
if (_readCmdBytesTimeout(SI7021_MEASTEMP_NOHOLD_CMD, _data, 3, 6) != 3)
return NAN;
float temperature = _data[0] << 8 | _data[1];
temperature *= 175.72;
temperature /= 65536;
temperature -= 46.85;
return temperature;
}
float HELIOS_Si7021::readHumidity(void)
{
if (!_initDone)
_init();
if (!_foundDevice)
return NAN;
if (_readCmdBytesTimeout(SI7021_MEASRH_NOHOLD_CMD, _data, 3, 6) != 3)
return NAN;
float humidity = (_data[0] << 8 | _data[1]) * 125;
humidity /= 65536;
humidity -= 6;
return humidity;
}
const char *HELIOS_Si7021::getModelName(void)
{
if (!_initDone)
_init();
if (!_foundDevice)
return "no device found";
switch(getModel()) {
case SI_Engineering_Samples:
return "SI engineering samples";
case SI_7013:
return "Si7013";
case SI_7020:
return "Si7020";
case SI_7021:
return "Si7021";
case SI_unkown:
default:
return "unknown";
}
}
HELIOS_Si7021::sensorType HELIOS_Si7021::getModel(void)
{
if (!_initDone)
_init();
if (!_foundDevice)
return SI_unkown;
_writeRegister8x2(SI7021_ID2_CMD >> 8, SI7021_ID2_CMD & 0xFF);
_readBytes(_data, 8);
uint32_t sernum_b;
sernum_b = _data[0];
sernum_b <<= 8;
sernum_b |= _data[2];
sernum_b <<= 8;
sernum_b |= _data[4];
sernum_b <<= 8;
sernum_b |= _data[6];
switch(sernum_b >> 24) {
case 0:
case 0xff:
return SI_Engineering_Samples;
break;
case 0x0D:
return SI_7013;
break;
case 0x14:
return SI_7020;
break;
case 0x15:
return SI_7021;
break;
default:
return SI_unkown;
}
}
uint8_t
HELIOS_Si7021::_readRegister8(uint8_t reg) {
#ifdef __MBED__
_data[0] = reg;
_i2c->write(_i2caddr, _data, 1);
_i2c->read(_i2caddr, _data, 1);
return _data[0];
#elif ARDUINO
uint8_t value;
Wire.beginTransmission(_i2caddr);
Wire.write(reg);
Wire.endTransmission(false);
uint32_t start = millis(); // start timeout
while(millis()-start < _TRANSACTION_TIMEOUT) {
if (Wire.requestFrom(_i2caddr, 1) == 1) {
value = Wire.read();
return value;
}
_waitMillis(2);
}
return 0; // Error timeout
#else
#error "Unknown OS"
#endif
}
uint8_t
HELIOS_Si7021::_readBytes(char *buffer, int len)
{
#ifdef __MBED__
_i2c->read(_i2caddr, buffer, len);
#elif ARDUINO
bool gotData = false;
uint32_t start = millis(); // start timeout
while(millis()-start < _TRANSACTION_TIMEOUT) {
if (Wire.requestFrom(_i2caddr, len) == len) {
gotData = true;
break;
}
delay(2);
}
if (!gotData)
return 0; // error timeout
for (int i = 0; i < len; i++)
*buffer++ = Wire.read();
#else
#error "Unkown OS"
#endif
return len;
}
uint8_t
HELIOS_Si7021::_readCmdBytesTimeout(uint8_t reg, char *buffer, int len, int timeout_ms)
{
_data[0] = reg;
#ifdef __MBED__
_i2c->write(_i2caddr, _data, 1);
Timer t;
t.start();
while(_i2c->read(_i2caddr, buffer, len) != 0) {
if (t.read_ms() > _TRANSACTION_TIMEOUT)
return 0;
_waitMillis(timeout_ms); // 1/2 typical sample processing time
}
return len;
#elif ARDUINO
Wire.beginTransmission(_i2caddr);
Wire.write(*buffer);
uint8_t err = Wire.endTransmission();
if (err != 0)
return 0; //error
delay(20);
uint32_t start = millis(); // start timeout
while(millis()-start < _TRANSACTION_TIMEOUT) {
if (Wire.requestFrom(_i2caddr, 3) == 3) {
for (int i = 0; i < 3; i++)
buffer[i] = Wire.read();
return len;
}
delay(timeout_ms); // 1/2 typical sample processing time
}
return 0; // Error timeout
#else
#error "Unkown OS"
#endif
}
void
HELIOS_Si7021::_writeRegister8(uint8_t reg)
{
#ifdef __MBED__
_data[0] = reg;
_i2c->write(_i2caddr, _data, 1);
#elif ARDUINO
Wire.beginTransmission(_i2caddr);
Wire.write(reg);
Wire.endTransmission();
#else
#error "Unkown OS"
#endif
}
void
HELIOS_Si7021::_writeRegister8x2(uint8_t reg, uint8_t reg2)
{
#ifdef __MBED__
_data[0] = reg;
_data[1] = reg2;
_i2c->write(_i2caddr, _data, 2);
#elif ARDUINO
Wire.beginTransmission(_i2caddr);
Wire.write(reg);
Wire.write(reg2);
Wire.endTransmission();
#else
#error "Unkown OS"
#endif
}
void
HELIOS_Si7021::_waitMillis(int millis)
{
#ifdef __MBED__
wait_us(millis * 1000);
#else
delay(millis);
#endif
}
#endif // FEATURE_SI7021