forked from LucAce/CCS811
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCS811.cpp
683 lines (573 loc) · 22.9 KB
/
CCS811.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//*****************************************************************************
// Copyright (c) 2017-2018 LucAce
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//*****************************************************************************
//
// Arduino Library for AMS CCS811 Ultra-low power Digital VOC Sensor
//
// Library Dependencies:
// - Wire
//
// Notes:
// - ESP8266 Arduino Library v2.3.0 uses a default I2C timeout value that is
// too short for the CCS811. A timeout value of 500us is sufficient in most
// use cases. Increase the timeout value by calling:
// Wire.setClockStretchLimit(500);
// - ESP8266 Arduino Library v2.3.0 Wire library resets the I2C timeout every
// time a call to .begin is made. setClockStretchLimit(TIMEOUT) must be
// called following every subsequent call to .begin as a workaround. Later
// versions of the ESP8266 for Arduino libraries are expected to correct
// this. See:
// https://github.com/esp8266/Arduino/issues/2162
// - Source code uses Natural Docs for Document Generation.
//
//*****************************************************************************
#include "CCS811.h"
// Group: Public Functions
//*****************************************************************************
// Function: begin
// Initialize the CCS811 Sensor.
//
// Parameters:
// addr - I2C address of the sensor
//
// Returns:
// bool - true if successful; false otherwise
//*****************************************************************************
bool CCS811::begin(uint8_t addr) {
// Set the I2C Address used by this library
_i2c_addr = addr;
// Initialize I2C interface
this->_i2c_init();
// Issue reset to sensor
this->issueSwReset();
// Verify the Hardware ID field matches the expected value
// if not do not continue intializing the sensor
this->readHWIDRegister();
this->readHWVersionRegister();
this->readFWBootVersionRegister();
this->readFWAppVersionRegister();
if (_HW_ID != CCS811_HW_ID_CODE)
return false;
// Issue App Start command to sensor
this->startApplication();
// Read the state of the sensor
this->readStatusRegister();
// Return if an error is reported
if (_status.ERROR)
return false;
// Return if not in application mode
if (!_status.FW_MODE)
return false;
// Set mode
// - 1 sample/sec
// - Disable interrupt
// - Normal interrupt mode
this->writeMeasModeRegister(
CCS811_DRIVE_MODE_1SEC,
0, 0
);
return true;
}
//*****************************************************************************
// Function: reset
// Re-initialize the CCS811 Sensor using the same I2C address already
// configured.
//
// Returns:
// bool - true if successful; false otherwise
//*****************************************************************************
bool CCS811::reset() {
return this->begin(_i2c_addr);
}
//*****************************************************************************
// Function: readStatusRegister
// Read the Status Register (0x00, 1 byte)
//*****************************************************************************
void CCS811::readStatusRegister() {
_status.set(read8(CCS811_STATUS));
}
//*****************************************************************************
// Function: readMeasModeRegister
// Read the Measurement and Conditions Register (0x01, 1 byte)
//*****************************************************************************
void CCS811::readMeasModeRegister() {
_meas_mode.set(read8(CCS811_MEAS_MODE));
}
//*****************************************************************************
// Function: writeMeasModeRegister
// Write the Measurement and Conditions Register (0x01, 1 byte)
//
// Notes:
// - Invalid mode selection will result in a reported error
//
// Parameters:
// drive_mode - Measurement mode
// int_datardy - Interrupt generation
// int_thresh - Interrupt threshold mode
//*****************************************************************************
void CCS811::writeMeasModeRegister(
uint8_t drive_mode,
uint8_t int_datardy,
uint8_t int_thresh
) {
_meas_mode.DRIVE_MODE = drive_mode & 0x07;
_meas_mode.INT_DATARDY = int_datardy & 0x01;
_meas_mode.INT_THRESH = int_thresh & 0x01;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: readAlgResultDataRegister
// Read the Algorithm Results Data Register (0x02, 8 bytes)
//
// Notes:
// - All 8 bytes of this register, including the raw data, are read
//*****************************************************************************
void CCS811::readAlgResultDataRegister() {
uint8_t buf[8];
this->read(CCS811_ALG_RESULT_DATA, buf, 8);
_eCO2 = ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
_TVOC = ((uint16_t)buf[2] << 8) | ((uint16_t)buf[3]);
_status.set(buf[4]);
_error_id.set(buf[5]);
_raw_data = ((uint16_t)buf[6] << 8) | ((uint16_t)buf[7]);
}
//*****************************************************************************
// Function: readRawDataRegister
// Read the Algorithm Results Data Register (0x03, 2 bytes)
//*****************************************************************************
void CCS811::readRawDataRegister() {
uint8_t buf[2];
this->read(CCS811_RAW_DATA, buf, 2);
_raw_data = ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
}
//*****************************************************************************
// Function: writeEnvDataRegister
// Write the Environment Data Register (0x05, 2 bytes)
//
// Notes:
// - Humidity is stored as an unsigned 16 bits in 1/512%RH. The default
// value is 50% = 0x64, 0x00. As an example 48.5% humidity would be
// 0x61, 0x00.
// - Temperature is stored as an unsigned 16 bits integer in 1/512 degrees;
// there is an offset: 0 maps to -25C. The default value is
// 25C = 0x64, 0x00. As an example 23.5% temperature would be 0x61, 0x00.
// The internal algorithm uses these values (or default values if not set
// by the application) to compensate for changes in relative humidity and
// ambient temperature.
//
// Parameters:
// humidity - Sensor representation of the humidity value
// temperature - Sensor representation of the temperature value
//*****************************************************************************
void CCS811::writeEnvDataRegister(
uint16_t humdity,
uint16_t temperature
) {
uint8_t buf[] = {
(uint8_t)((humdity >> 8) & 0xFF),
(uint8_t)((humdity >> 0) & 0xFF),
(uint8_t)((temperature >> 8) & 0xFF),
(uint8_t)((temperature >> 0) & 0xFF)
};
this->write(CCS811_ENV_DATA, buf, 4);
}
//*****************************************************************************
// Function: readNTCRegister
// Read the NTC Register (0x06, 4 bytes)
//*****************************************************************************
void CCS811::readNTCRegister() {
uint8_t buf[4];
this->read(CCS811_NTC, buf, 4);
_vref = ((uint32_t)buf[0] << 8) | (uint32_t)buf[1];
_vntc = ((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
}
//*****************************************************************************
// Function: writeThresholdsRegister
// Write the Thresholds Register (0x10, 5 bytes)
//
// Notes:
// - An interrupt is asserted (if enabled) if the eCO2 value moved from the
// current range (Low, Medium, or High) into another range by more than the
// Hysteresis value (used to prevent multiple interrupts close to a
// threshold).
//
// Parameters:
// low_med - Low to Medium Threshold (default = 1500ppm = 0x05D)
// med_high - Medium to High Threshold (default = 2500ppm = 0x09C4)
// hysteresis - Hysteresis value (default = 50 = 0x32)
//*****************************************************************************
void CCS811::writeThresholdsRegister(
uint16_t low_med,
uint16_t med_high,
uint8_t hysteresis
) {
uint8_t buf[] = {
(uint8_t)((low_med >> 8) & 0xFF),
(uint8_t)((low_med >> 0) & 0xFF),
(uint8_t)((med_high >> 8) & 0xFF),
(uint8_t)((med_high >> 0) & 0xFF),
hysteresis
};
this->write(CCS811_THRESHOLDS, buf, 5);
}
//*****************************************************************************
// Function: readBaselineRegister
// Read the Baseline Register (0x11, 2 bytes)
//
// Notes:
// - The format of the baseline value is not specified.
//*****************************************************************************
void CCS811::readBaselineRegister() {
uint8_t buf[2];
this->read(CCS811_BASELINE, buf, 2);
_BASELINE = ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
}
//*****************************************************************************
// Function: writeBaselineRegister
// Write the Baseline Register (0x11, 2 bytes)
//
// Notes:
// - The format of the baseline value is not specified.
//
// Parameters:
// baseline - Sensor baseline value
//*****************************************************************************
void CCS811::writeBaselineRegister(uint16_t baseline) {
uint8_t buf[] = {
(uint8_t)((baseline >> 8) & 0xFF),
(uint8_t)((baseline >> 0) & 0xFF)
};
_BASELINE = baseline;
this->write(CCS811_BASELINE, buf, 2);
}
//*****************************************************************************
// Function: readHWIDRegister
// Read the Hardware ID Register (0x20, 1 byte)
//*****************************************************************************
void CCS811::readHWIDRegister() {
_HW_ID = this->read8(CCS811_HW_ID);
}
//*****************************************************************************
// Function: readHWVersionRegister
// Read the Hardware Version Register (0x21, 1 byte)
//*****************************************************************************
void CCS811::readHWVersionRegister() {
_HW_Version = this->read8(CCS811_HW_VERSION);
}
//*****************************************************************************
// Function: readFWBootVersionRegister
// Read the Firmware Bootloader Version Register (0x23, 2 bytes)
//*****************************************************************************
void CCS811::readFWBootVersionRegister() {
uint8_t buf[2];
this->read(CCS811_FW_BOOT_VERSION, buf, 2);
_FW_Boot_Version = ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
}
//*****************************************************************************
// Function: readFWAppVersionRegister
// Read the Firmware Application Version Register (0x24, 2 bytes)
//*****************************************************************************
void CCS811::readFWAppVersionRegister() {
uint8_t buf[2];
this->read(CCS811_FW_APP_VERSION, buf, 2);
_FW_App_Version = ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
}
//*****************************************************************************
// Function: readErrorIDRegister
// Read the Error ID Register (0xE0, 2 bytes)
//*****************************************************************************
void CCS811::readErrorIDRegister() {
_error_id.set(read8(CCS811_ERROR_ID));
}
//*****************************************************************************
// Function: writeAppEraseRegister
// Write the Application Erase Register (0xF1, 4 bytes)
//
// Notes:
// - Application is erased only when the proper sequence is provided
// 0xE7, 0xA7, 0xE6, 0x09
// - To protect the application code from accidental erases the sequence must
// be provided with the function call
//
// Parameters:
// buf - Erase sequence
//*****************************************************************************
void CCS811::writeAppEraseRegister(uint8_t buf[4]) {
this->write(CCS811_BOOTLOADER_APP_ERASE, buf, 4);
}
//*****************************************************************************
// Function: writeAppDataRegister
// Write the Application Data Register (0xF2, 9 bytes)
//
// Parameters:
// buf - 9 bytes of application data
//*****************************************************************************
void CCS811::writeAppDataRegister(uint8_t buf[9]) {
this->write(CCS811_BOOTLOADER_APP_DATA, buf, 9);
}
//*****************************************************************************
// Function: writeAppVerifyRegister
// Write the Application Verify Register (0xF3, 0 bytes)
//
// Notes:
// - No data is sent with this write
//*****************************************************************************
void CCS811::writeAppVerifyRegister() {
this->write(CCS811_BOOTLOADER_APP_VERIFY, NULL, 0);
}
//*****************************************************************************
// Function: writeAppStartRegister
// Write the Application Start Register (0xF4, 0 bytes)
//
// Notes:
// - No data is sent with this write
//*****************************************************************************
void CCS811::writeAppStartRegister() {
this->write(CCS811_BOOTLOADER_APP_START, NULL, 0);
}
//*****************************************************************************
// Function: writeSwResetRegister()
// Write the Software Reset Register (0xFF, 4 bytes)
//
// Notes:
// - Application is reset only when the proper sequence is provided
// 0x11, 0xE5, 0x72, 0x8A
// - To protect the application code from accidental resets the sequence must
// be provided with the function call
//
// Parameters:
// buf - 4 bytes reset sequence
//*****************************************************************************
void CCS811::writeSwResetRegister(uint8_t buf[4]) {
this->write(CCS811_SW_RESET, buf, 4);
}
//*****************************************************************************
// Function: setDriveMode
// Set the sensor sample/drive mode.
//
// Notes:
// Invalid mode selection will result in a reported error
//
// Parameters:
// drive_mode - Drive mode to configure
//*****************************************************************************
void CCS811::setDriveMode(uint8_t drive_mode) {
_meas_mode.DRIVE_MODE = drive_mode;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: enableInterrupt
// Enable the interrupt output signal.
//*****************************************************************************
void CCS811::enableInterrupt() {
_meas_mode.INT_DATARDY = 1;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: disableInterrupt
// Disable the interrupt output signal.
//*****************************************************************************
void CCS811::disableInterrupt() {
_meas_mode.INT_DATARDY = 0;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: enableInterruptThreshold
// Enable the interrupt threshold mode.
//*****************************************************************************
void CCS811::enableInterruptThreshold() {
_meas_mode.INT_THRESH = 1;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: disableInterruptThreshold
// Disable the interrupt threshold mode.
//*****************************************************************************
void CCS811::disableInterruptThreshold() {
_meas_mode.INT_THRESH = 0;
this->write8(CCS811_MEAS_MODE, _meas_mode.get());
}
//*****************************************************************************
// Function: calculateTemperature
// Calculate the Temperature using the onboard NTC register.
//
// Notes:
// - Calculation based on vendor App note
//
// Returns:
// double - Calculated temperature value
//*****************************************************************************
double CCS811::calculateTemperature() {
double ntc_temp;
uint32_t rntc;
this->readNTCRegister();
rntc = _vntc * CCS811_REF_RESISTOR / _vref;
ntc_temp = log((double)rntc / CCS811_REF_RESISTOR);
ntc_temp /= 3380;
ntc_temp += 1.0 / (25 + 273.15);
ntc_temp = 1.0 / ntc_temp;
ntc_temp -= 273.15;
return ntc_temp - _temp_offset;
}
//*****************************************************************************
// Function: setTempOffset
// Set the temperature office attribute.
//
// Parameters:
// offset - Temperature offset to set
//*****************************************************************************
void CCS811::setTempOffset(float offset) {
_temp_offset = offset;
}
//*****************************************************************************
// Function: setEnvData
// Set the Environment Data
//
// Parameters:
// humidity - Relative Humidity
// temperature - Temperature
//*****************************************************************************
void CCS811::setEnvData(float humidity, float temperature) {
uint16_t humd_buf;
uint16_t temp_buf;
uint8_t envData[4];
// Check for invalid temperature
if ((temperature < -25.0F) || (temperature > 50.0F))
return;
// Check for invalid humidity
if ((humidity < 0.0F) || (humidity > 100.0F))
return;
uint32_t rH = humidity * 1000;
uint32_t temp = temperature * 1000;
// Split value into 7-bit integer and 9-bit fractional
envData[0] = ((rH % 1000) / 100) > 7 ? (rH / 1000 + 1) << 1 : (rH / 1000) << 1;
// CCS811 only supports increments of 0.5 so bits 7-0 will always be zero
envData[1] = 0;
if (((rH % 1000) / 100) > 2 && (((rH % 1000) / 100) < 8)) {
// Set 9th bit of fractional to indicate 0.5%
envData[0] |= 1;
}
temp += 25000; // Add the 25C offset
// Split value into 7-bit integer and 9-bit fractional
envData[2] = ((temp % 1000) / 100) > 7 ? (temp / 1000 + 1) << 1 : (temp / 1000) << 1;
envData[3] = 0;
if (((temp % 1000) / 100) > 2 && (((temp % 1000) / 100) < 8)) {
// Set 9th bit of fractional to indicate 0.5C
envData[2] |= 1;
}
humd_buf = ((uint16_t)envData[0] << 8) | ((uint16_t)envData[1] << 0);
temp_buf = ((uint16_t)envData[2] << 8) | ((uint16_t)envData[3] << 0);
this->writeEnvDataRegister(humd_buf, temp_buf);
}
//*****************************************************************************
// Function: issueSwReset()
// Issue software reset.
//*****************************************************************************
void CCS811::issueSwReset() {
uint8_t buf[4] = {0x11, 0xE5, 0x72, 0x8A};
this->writeSwResetRegister(buf);
delay(100);
}
//*****************************************************************************
// Function: startApplication()
// Issue application start.
//*****************************************************************************
void CCS811::startApplication() {
this->writeAppStartRegister();
delay(100);
}
// Group: Private Functions
//*****************************************************************************
// Function: _i2c_init
// Initialize the I2C interface.
//*****************************************************************************
void CCS811::_i2c_init() {
Wire.begin();
#ifdef ESP8266
Wire.setClockStretchLimit(500);
#endif
}
//*****************************************************************************
// Function: read8
// Read one byte of data from the I2C interface.
//
// Parameters:
// reg - Register/Mailbox to read
//
// Returns:
// uint8_t - Value read
//*****************************************************************************
uint8_t CCS811::read8(byte reg) {
uint8_t ret;
this->read(reg, &ret, 1);
return ret;
}
//*****************************************************************************
// Function: write8
// Write one byte of data over the I2C interface.
//
// Parameters:
// reg - Register/Mailbox to write
// value - Value to write
//*****************************************************************************
void CCS811::write8(byte reg, byte value) {
this->write(reg, &value, 1);
}
//*****************************************************************************
// Function: read
// Read multi-byte data from the I2C interface.
//
// Parameters:
// reg - Register/Mailbox to read
// *buf - Pointer to data read on interface
// num - Number of bytes to read
//*****************************************************************************
void CCS811::read(uint8_t reg, uint8_t *buf, uint8_t num) {
uint8_t value;
uint8_t pos = 0;
// On arduino we need to read in 32 byte chunks
while (pos < num){
uint8_t read_now = min(32, num - pos);
Wire.beginTransmission((uint8_t)_i2c_addr);
Wire.write((uint8_t)reg + pos);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2c_addr, read_now);
for (int i=0; i<read_now; i++){
buf[pos] = Wire.read();
pos++;
}
}
}
//*****************************************************************************
// Function: write
// Write multi-byte data to the I2C interface.
//
// Parameters:
// reg - Register/Mailbox to write
// *buf - Pointer to data to write on interface
// num - Number of bytes to write
//*****************************************************************************
void CCS811::write(uint8_t reg, uint8_t *buf, uint8_t num) {
Wire.beginTransmission((uint8_t)_i2c_addr);
Wire.write((uint8_t)reg);
Wire.write((uint8_t *)buf, num);
Wire.endTransmission();
}