Skip to content

Commit

Permalink
Add cht8310 support (#1008)
Browse files Browse the repository at this point in the history
* add CHT8310 support

* fix humidity measurement

* make chip id logic safer to prevent braking the old sensor

* fixed mistake in sensor_id

---------

Co-authored-by: Your Name <[email protected]>
  • Loading branch information
artin961 and Your Name authored Dec 24, 2023
1 parent d2ce54f commit 0403abd
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions src/driver/drv_cht8305.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ static byte channel_temp = 0, channel_humid = 0;
static float g_temp = 0.0, g_humid = 0.0;
static softI2C_t g_softI2C;
static float g_calTemp = 0, g_calHum = 0;
static uint16_t sensor_id=0;


static void CHT8305_ReadEnv(float* temp, float* hum) {
uint8_t buff[4];
unsigned int th, tl, hh, hl;

if(sensor_id==0x8215)
{
//Make dymmy write to Oneshot register to trigger a measurement, sensor is otherwise sleeping
Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0x0F);
Soft_I2C_WriteByte(&g_softI2C, 0x00);
Soft_I2C_WriteByte(&g_softI2C, 0x00);
Soft_I2C_Stop(&g_softI2C);
}

Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0x00);
Soft_I2C_Stop(&g_softI2C);
Expand All @@ -36,14 +47,33 @@ static void CHT8305_ReadEnv(float* temp, float* hum) {
Soft_I2C_ReadBytes(&g_softI2C, buff, 4);
Soft_I2C_Stop(&g_softI2C);

//In case we have the new sensor 8310, overwrite humidity data reading it from 0x01, as it cannot be directrly read from 0x00, there is parity
if(sensor_id==0x8215)
{
Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0x01);
Soft_I2C_Stop(&g_softI2C);

Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR | 1);
Soft_I2C_ReadBytes(&g_softI2C, buff+2, 2);
Soft_I2C_Stop(&g_softI2C);
}

th = buff[0];
tl = buff[1];
hh = buff[2];
hl = buff[3];

(*temp) = ((th << 8 | tl) * 165.0 / 65535.0 - 40.0)+ g_calTemp;

(*hum) = ((hh << 8 | hl) * 100.0 / 65535.0)+ g_calHum;
if(sensor_id==0x8215)
{
(*temp)=((th << 8 | tl)>>1)/128.0+g_calTemp;
(*hum) = ((((hh << 8 | hl) & 0x7fff) / 32768.0 ) * 100.0)+ g_calHum;
}
else
{
(*temp) = ((th << 8 | tl) * 165.0 / 65535.0 - 40.0)+ g_calTemp;
(*hum) = ((hh << 8 | hl) * 100.0 / 65535.0)+ g_calHum;
}

}

Expand Down Expand Up @@ -78,10 +108,8 @@ commandResult_t CHT_cycle(const void* context, const char* cmd, const char* args
}
// startDriver CHT8305
void CHT8305_Init() {

uint8_t buff[4];


g_softI2C.pin_clk = 9;
g_softI2C.pin_data = 14;
g_softI2C.pin_clk = PIN_FindPinIndexForRole(IOR_CHT8305_CLK, g_softI2C.pin_clk);
Expand All @@ -90,13 +118,41 @@ void CHT8305_Init() {
Soft_I2C_PreInit(&g_softI2C);

Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0xfe); //manufacturer ID
Soft_I2C_WriteByte(&g_softI2C, 0xfe); //manufacturer ID 2 bytes
Soft_I2C_Stop(&g_softI2C);
Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR | 1);
Soft_I2C_ReadBytes(&g_softI2C, buff, 2);
Soft_I2C_Stop(&g_softI2C);

addLogAdv(LOG_INFO, LOG_FEATURE_SENSOR, "DRV_CHT8304_init: ID: %02X %02X", buff[0], buff[1]);

//Read Sensor version separately on the last 2 bytes of the buffer
Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0xff);
Soft_I2C_Stop(&g_softI2C);

Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR | 1);
Soft_I2C_ReadBytes(&g_softI2C, buff+2, 2);
Soft_I2C_Stop(&g_softI2C);

//Identify chip ID and keep if for later use
sensor_id=(buff[2] << 8 | buff[3]);

addLogAdv(LOG_INFO, LOG_FEATURE_SENSOR, "DRV_CHT8305_init: ID: %02X %02X %04X", buff[0], buff[1], sensor_id);


if(sensor_id==0x8215)
{//it should be 8310 id is 0x8215, we enable low power mode, so only 50nA are drawn from sensor,
//but need to write something to one shot register to trigger new measurement

Soft_I2C_Start(&g_softI2C, CHT8305_I2C_ADDR);
Soft_I2C_WriteByte(&g_softI2C, 0x03); //config register
Soft_I2C_WriteByte(&g_softI2C, 0x48); //enable shutdown default is 0x08
//setting bit 6 enables low power mode,
//to get new measurement need to write to one shot register
Soft_I2C_WriteByte(&g_softI2C, 0x80); //as default 0x80
Soft_I2C_Stop(&g_softI2C);

}

//cmddetail:{"name":"CHT_Calibrate","args":"[DeltaTemp][DeltaHumidity]",
//cmddetail:"descr":"Calibrate the CHT Sensor as Tolerance is +/-2 degrees C.",
Expand Down

0 comments on commit 0403abd

Please sign in to comment.