Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: CCS811.cpp
- Revision:
- 2:ec392b3a49f2
- Parent:
- 1:57eb62ded32d
- Child:
- 3:423f2830684e
--- a/CCS811.cpp Fri Apr 27 09:51:20 2018 +0000
+++ b/CCS811.cpp Thu Mar 05 08:27:24 2020 +0000
@@ -7,25 +7,62 @@
/**
** Initial CCS811 need write MODE register and should Write APP START register to begin measurement.
**/
-void CCS811::init() {
+int CCS811::init() {
char send[2];
if (!checkHW()) {
- return;
+ m_pc.printf("ERROR: CCS811 does not have a correct hardware id.\r\n");
+ return CCS811_ERR_HWID;
}else {
m_pc.printf("CCS811 is confirm!\r\n");
}
+ // check valid app
+ send[0] = CCS811_REG_STATUS;
+ m_i2c.write(CCS811_I2C_ADDR, send, 1);
+ m_i2c.read(CCS811_I2C_ADDR, send, 1);
+ if (!(send[0] & 0x10)){
+ return CCS811_ERR_INVALID_APP;
+ }
+
+ // send app start
send[0] = CCS811_REG_APP_START;
send[1] = 0x00;
+ m_i2c.write(CCS811_I2C_ADDR, send, 1);
- m_i2c.write(CCS811_I2C_ADDR, send, 2);
+ m_pc.printf("done setting app start\r\n");
+ {
+ char status = readStatus();
+ if (status & 0x01){
+ char errid = readErr();
+ printerr_reg1(m_pc, errid);
+ }
+ }
+
+ // check fw mode
+ send[0] = CCS811_REG_STATUS;
+ m_i2c.write(CCS811_I2C_ADDR, send, 1);
+ m_i2c.read(CCS811_I2C_ADDR, send, 1);
+ if (!(send[0] & 0x90)){
+ return CCS811_ERR_INVALID_APPMODE;
+ }
+
+
send[0] = CCS811_REG_MEAS_MODE;
send[1] = CCS811_MEASUREMENT_MODE1;
m_i2c.write(CCS811_I2C_ADDR, send, 2);
-
+ m_pc.printf("setting meas mode\r\n");
+ {
+ char status = readStatus();
+ if (status & 0x01){
+ char errid = readErr();
+ printerr_reg1(m_pc, errid);
+ }
+ }
+ m_pc.printf("done set meas mode\r\n");
+ return 0;
}
int CCS811::setMeasureMode(char mode) {
@@ -66,6 +103,11 @@
*ECO2 = (uint16_t) (recv[0] <<8) + recv[1];
*TVOC = (uint16_t) (recv[2] <<8) + recv[3];
+ char err_id = recv[5];
+ if(err_id != 0){
+ printerr_reg1(m_pc, err_id);
+ }
+
return 0;
}
@@ -83,8 +125,6 @@
m_i2c.write(CCS811_I2C_ADDR, read, 1, false);
m_i2c.read(CCS811_I2C_ADDR, hid, 1, false);
-// m_pc.printf("%X\r\n", hid[0]);
-
if (hid[0] == 0x81) {
return true;
} else {
@@ -106,3 +146,41 @@
}
+int CCS811::readErr(){
+ char i2c_buf[2];
+
+ i2c_buf[0] = CCS811_REG_ERROR_ID;
+ m_i2c.write(CCS811_I2C_ADDR, i2c_buf, 1);
+ m_i2c.read(CCS811_I2C_ADDR, i2c_buf, 1);
+ return i2c_buf[0];
+}
+
+int CCS811::readStatus(){
+ char i2c_buf[2];
+
+ i2c_buf[0] = CCS811_REG_STATUS;
+ m_i2c.write(CCS811_I2C_ADDR, i2c_buf, 1);
+ m_i2c.read(CCS811_I2C_ADDR, i2c_buf, 1);
+ return i2c_buf[0];
+}
+
+void printerr(Serial& m_pc, int errid){
+ if (errid == CCS811_ERR_HWID){
+ m_pc.printf("CCS811_ERR_HWID\r\n");
+ }else if(errid == CCS811_ERR_INVALID_APP){
+ m_pc.printf("CCS811_ERR_INVALID_APP\r\n");
+ }else if(errid == CCS811_ERR_INVALID_APPMODE){
+ m_pc.printf("CCS811_ERR_INVALID_APPMODE\r\n");
+ }else if(errid == 0){
+ m_pc.printf("printerr called but no error\r\n");
+ return;
+ }else{
+ m_pc.printf("Unknown error: %d\r\n", errid);
+ }
+ exit(errid);
+}
+
+void printerr_reg1(Serial& m_pc, int errid){
+ m_pc.printf("err reg: %X\r\n", errid);
+ exit(errid);
+}
\ No newline at end of file