SCD4x CRC check
This commit is contained in:
parent
98e6a7f93d
commit
e6e97a2547
@ -331,9 +331,10 @@ int main(void)
|
|||||||
/* Read SCD4x data (if connected) */
|
/* Read SCD4x data (if connected) */
|
||||||
if (scd4x_is_connected == 1)
|
if (scd4x_is_connected == 1)
|
||||||
{
|
{
|
||||||
scd4x_read_measurement(&CO2,
|
if (scd4x_read_measurement(&CO2, &T_SCD4x, &RH_SCD4x) != SCD4X_OK) {
|
||||||
&T_SCD4x,
|
/* read failed, either I2C fail or CRC error */
|
||||||
&RH_SCD4x);
|
// TODO something
|
||||||
|
}
|
||||||
if (CO2 > 0) {
|
if (CO2 > 0) {
|
||||||
co2_valid = 1;
|
co2_valid = 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,11 +80,9 @@ int8_t scd4x_read_measurement(uint16_t *co2, int16_t *temperature, uint16_t *rel
|
|||||||
buffer[0] = SCD4X_READ_MEASUREMENT >> 8;
|
buffer[0] = SCD4X_READ_MEASUREMENT >> 8;
|
||||||
buffer[1] = SCD4X_READ_MEASUREMENT & 0x00ff;
|
buffer[1] = SCD4X_READ_MEASUREMENT & 0x00ff;
|
||||||
result = i2c_transmit(SCD4X_I2C_ADDRESS<<1, buffer, 2);
|
result = i2c_transmit(SCD4X_I2C_ADDRESS<<1, buffer, 2);
|
||||||
|
if (result != I2C_OK) {
|
||||||
// TODO: Proc to vraci NACK? Vyresit.
|
|
||||||
/*if (result != I2C_OK) {
|
|
||||||
return SCD4X_ERROR;
|
return SCD4X_ERROR;
|
||||||
}*/
|
}
|
||||||
uart_enable_interrupts();
|
uart_enable_interrupts();
|
||||||
LL_mDelay(1); // 10 ms should be enough
|
LL_mDelay(1); // 10 ms should be enough
|
||||||
uart_disable_interrupts();
|
uart_disable_interrupts();
|
||||||
@ -95,12 +93,22 @@ int8_t scd4x_read_measurement(uint16_t *co2, int16_t *temperature, uint16_t *rel
|
|||||||
{
|
{
|
||||||
return SCD4X_ERROR;
|
return SCD4X_ERROR;
|
||||||
}
|
}
|
||||||
|
/* Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3 */
|
||||||
// TODO checksum
|
|
||||||
// Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3
|
|
||||||
uint32_t co2_ticks = (buffer[0] << 8) + buffer[1];
|
uint32_t co2_ticks = (buffer[0] << 8) + buffer[1];
|
||||||
|
uint8_t co2_crc = buffer[2];
|
||||||
uint32_t t_ticks = (buffer[3] << 8) + buffer[4];
|
uint32_t t_ticks = (buffer[3] << 8) + buffer[4];
|
||||||
|
uint8_t t_crc = buffer[5];
|
||||||
uint32_t rh_ticks = (buffer[6] << 8) + buffer[7];
|
uint32_t rh_ticks = (buffer[6] << 8) + buffer[7];
|
||||||
|
uint8_t rh_crc = buffer[8];
|
||||||
|
|
||||||
|
/* check CRC-8 checksum */
|
||||||
|
uint8_t crc_correct = crc8_calculate(buffer, 2) == co2_crc;
|
||||||
|
crc_correct &= crc8_calculate(buffer + 3, 2) == t_crc;
|
||||||
|
crc_correct &= crc8_calculate(buffer + 6, 2) == rh_crc;
|
||||||
|
if (!crc_correct) {
|
||||||
|
return SCD4X_CRC8_ERROR;
|
||||||
|
}
|
||||||
|
/* copy to output variables */
|
||||||
int t_degC = -450 + 10 * 175 * t_ticks / 65535;
|
int t_degC = -450 + 10 * 175 * t_ticks / 65535;
|
||||||
int rh_pRH = 100 * rh_ticks / 65535;
|
int rh_pRH = 100 * rh_ticks / 65535;
|
||||||
if (rh_pRH > 100) {
|
if (rh_pRH > 100) {
|
||||||
|
Loading…
Reference in New Issue
Block a user