temp: [#61] CRC check.

This commit is contained in:
RedHawk 2023-06-13 19:52:53 +03:00
parent 4613fce370
commit cb64ae7e0f
2 changed files with 18 additions and 24 deletions

View File

@ -31,7 +31,7 @@ SensorTempSHT20::SensorTempSHT20(I2C* pi2c)
: _pi2c(pi2c), _dev_addr(0x40), _up_flag(false), : _pi2c(pi2c), _dev_addr(0x40), _up_flag(false),
_com_read_hold(0xe3), _com_read_nohold(0xf3), _com_read_hold(0xe3), _com_read_nohold(0xf3),
_com_write_ur(0xe6), _com_read_ur(0xe7), _com_write_ur(0xe6), _com_read_ur(0xe7),
_com_soft_reset(0xfe) _com_soft_reset(0xfe), _polynominal(0x131)
{ {
this->read(); this->read();
} }
@ -71,32 +71,24 @@ uint16_t SensorTempSHT20::read()
raw_temp |= (rbuf[0] << 8); raw_temp |= (rbuf[0] << 8);
raw_temp |= (rbuf[1] & 0xfc); raw_temp |= (rbuf[1] & 0xfc);
crc = rbuf[2]; crc = rbuf[2];
LOG_WARNING("Raw data: %04x; CRC: %x", raw_temp, crc);
//TODO: crc check here. if (this->crc_check(rbuf, 2, crc))
LOG_WARNING("Temperature sensor reported crc mismatch.\nRaw data: %04x; CRC: %x", raw_temp, crc);
return raw_temp; return raw_temp;
} }
/* bool SensorTempSHT20::crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum)
const int16_t POLYNOMIAL = 0x131;
int8_t SHT2x_CheckCrc(int8_t data[], int8_t nbrOfBytes, int8_t checksum)
{ {
int8_t crc = 0; uint8_t crc = 0;
int8_t bit; uint8_t bit;
int8_t byteCtr; uint8_t byteCtr;
//calculates 8-Bit checksum with given polynomial //Calculates 8-Bit checksum with given _polynomial
for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
{ {
crc ^= (data[byteCtr]); crc ^= (data[byteCtr]);
for ( bit = 8; bit > 0; --bit) for ( bit = 8; bit > 0; --bit)
{ crc = (crc & 0x80) ? ((crc << 1) ^ this->_polynominal) : (crc << 1);
if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
else crc = (crc << 1);
} }
return crc != checksum;
} }
if (crc != checksum) return 1;
else return 0;
}
*/

View File

@ -18,6 +18,7 @@ public:
bool is_up(); bool is_up();
private: private:
uint16_t read(); uint16_t read();
bool crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum);
I2C* _pi2c; I2C* _pi2c;
const uint8_t _dev_addr; const uint8_t _dev_addr;
@ -27,6 +28,7 @@ private:
const uint8_t _com_write_ur; //user register const uint8_t _com_write_ur; //user register
const uint8_t _com_read_ur; //user register const uint8_t _com_read_ur; //user register
const uint8_t _com_soft_reset; const uint8_t _com_soft_reset;
const uint16_t _polynominal;
}; };
#endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */ #endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */