NordicSemiconductor / Nordic-Thingy52-FW

Nordic Thingy:52 software development kit. This kit is designed to assist users in developing their own custom firmware for Thingy. Please see http://www.nordicsemi.com/thingy for the latest news and software releases.
Other
210 stars 133 forks source link

CCS811 driver replicates error in vendor documentation. #31

Closed pabigot closed 5 years ago

pabigot commented 5 years ago

This code appears to have the same bug reported at sparkfun/Qwiic_BME280_CCS811_Combo#8. Repeating the summary:

The code for configuring the CCS811 ENV_DATA values faithfully represents the instructions in the CCS811 Programming guide section 16, being:

i2c_buff[0] = ((RH % 1000) / 100) > 7 ? (RH/1000 + 1)<<1 : (RH/1000)<<1;
i2c_buff[1] = 0;
if(((RH % 1000) / 100) > 2 && (((RH % 1000) / 100) < 8)) {
  i2c_buff[0] |= 1;
}

The demonstration code is wrong, as it results in storing a value that is one unit too large when the fractional part is strictly between 0.7 and 0.8. In that case the whole value is incremented and the half-value is added, applying the half-round operation twice.

An appropriate replacement given the limited resolution of humidity data here is probably something like:

tx_buff[1] = (rh_ppth + 3) / 5;
tx_buff[3] = (250 + temp_mdeg) / 500;

to round the MSB of each to the nearest half-unit.

koffes commented 5 years ago

Thank you for this observation @pabigot. I have added the issue to our internal system and we will evaluate it.

hmhalvorsen commented 5 years ago

Looking into the code it shows that the fractional part cannot be strictly between 0.7 and 0.8, rendering this logic error nonexistent. This is due to the resolution used for the sensor data.