From cf97c18554b9aa88202b9d2463ee08cafd1252b0 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 14 Feb 2021 21:42:26 +0100 Subject: add initialization and readout to CCS811 driver --- include/driver/ccs811.h | 24 ++++++++++++---- src/app/i2cdetect/main.cc | 18 +++++++++++- src/driver/ccs811.cc | 72 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/include/driver/ccs811.h b/include/driver/ccs811.h index aab1e35..8be0e6e 100644 --- a/include/driver/ccs811.h +++ b/include/driver/ccs811.h @@ -9,15 +9,29 @@ class CCS811 { private: CCS811(const CCS811 ©); - unsigned char const address; - unsigned char txbuf[3]; - unsigned char rxbuf[2]; + unsigned char const address = 0x5a; + unsigned char txbuf[5]; + unsigned char rxbuf[8]; public: - CCS811(unsigned char const addr) : address(addr) {} + CCS811() {} + + unsigned short eco2; + unsigned short tvoc; + unsigned char status; + unsigned char error_id; + unsigned char raw_current; + unsigned short raw_voltage; void init(); - short check(); + unsigned char getStatus(); + unsigned char getError(); + unsigned char getManufacturerID(); + void startFirmware(); + void setMode(unsigned char mode); + void read(); + void setEnv(unsigned char humi, unsigned char humi_fraction, unsigned char temp, unsigned char temp_fraction); + void reset(); }; extern CCS811 ccs811; diff --git a/src/app/i2cdetect/main.cc b/src/app/i2cdetect/main.cc index 4d6ebe9..92c812c 100644 --- a/src/app/i2cdetect/main.cc +++ b/src/app/i2cdetect/main.cc @@ -125,7 +125,13 @@ void loop(void) kout << "BME680 gas resistance " << data.gas_resistance << endl; #endif #ifdef DRIVER_CCS811 - kout << "CCS811 status is " << ccs811.check() << endl; + ccs811.read(); + kout << "CCS811 eCO₂ : " << ccs811.eco2 << " ppm" << endl; + kout << "CCS811 tVOC : " << ccs811.tvoc << " ppb" << endl; + kout << bin; + kout << "CCS811 status: " << ccs811.status << endl; + kout << "CCS811 error: " << ccs811.error_id << endl; + kout << dec; #endif #ifdef DRIVER_HDC1080 /* @@ -186,7 +192,17 @@ int main(void) kout << "I2C setup OK" << endl; #ifdef DRIVER_CCS811 + kout << hex; + kout << "CCS811 HWID: " << ccs811.getManufacturerID() << endl; + arch.delay_ms(65); + kout << "CCS811 status: " << ccs811.getStatus() << endl; + arch.delay_ms(65); ccs811.init(); + arch.delay_ms(65); + kout << "CCS811 status: " << ccs811.getStatus() << endl; + kout << dec; + ccs811.setMode(1); + //arch.delay_ms(50); #endif #ifdef DRIVER_HDC1080 hdc1080.init(); diff --git a/src/driver/ccs811.cc b/src/driver/ccs811.cc index ad0eabc..8c16b8a 100644 --- a/src/driver/ccs811.cc +++ b/src/driver/ccs811.cc @@ -17,18 +17,76 @@ void CCS811::init() { - gpio.output(nWAKE); - gpio.write(nWAKE, 1); + startFirmware(); + //setMode(1); + //setEnv(50, 0, 0, 0); } -short CCS811::check() +// switch to Application mode +void CCS811::startFirmware() +{ + if (getStatus() & 0x10) { + txbuf[0] = 0xf4; + i2c.xmit(address, 1, txbuf, 0, rxbuf); + } +} + +unsigned char CCS811::getManufacturerID() { - gpio.write(nWAKE, 0); txbuf[0] = 0x20; - rxbuf[0] = 0; i2c.xmit(address, 1, txbuf, 1, rxbuf); - gpio.write(nWAKE, 1); return rxbuf[0]; } -CCS811 ccs811(0x5a); +void CCS811::setMode(unsigned char mode) +{ + txbuf[0] = 0x01; + txbuf[1] = (mode & 0x07) << 4; + i2c.xmit(address, 2, txbuf, 0, rxbuf); +} + +void CCS811::read() +{ + txbuf[0] = 0x02; + i2c.xmit(address, 1, txbuf, 8, rxbuf); + eco2 = (rxbuf[0] << 8) + rxbuf[1]; + tvoc = (rxbuf[2] << 8) + rxbuf[3]; + status = rxbuf[4]; + error_id = rxbuf[5]; +} + +void CCS811::setEnv(unsigned char humi, unsigned char humi_fraction, unsigned char temp, unsigned char temp_fraction) +{ + txbuf[0] = 0x05; + txbuf[1] = humi; + txbuf[2] = humi_fraction; + txbuf[3] = temp; //((temp - 25) << 1) + (((temp - 25) & 0x80) >> 7); + txbuf[4] = temp_fraction; //(temp << 1) + ((temp & 0x80) >> 7); + i2c.xmit(address, 5, txbuf, 0, rxbuf); +} + +unsigned char CCS811::getStatus() +{ + txbuf[0] = 0x00; + i2c.xmit(address, 1, txbuf, 1, rxbuf); + return rxbuf[0]; +} + +unsigned char CCS811::getError() +{ + txbuf[0] = 0xe0; + i2c.xmit(address, 1, txbuf, 1, rxbuf); + return rxbuf[0]; +} + +// switch to Bootloader mode +void CCS811::reset() +{ + txbuf[0] = 0xff; + txbuf[1] = 0x11; + txbuf[2] = 0xe5; + txbuf[3] = 0x72; + txbuf[4] = 0x8a; +} + +CCS811 ccs811; -- cgit v1.2.3