1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/sht2x: use sys/checksum/crc8

This commit is contained in:
Gunar Schorcht 2022-12-13 23:50:39 +01:00
parent 009afd1173
commit 000d449d88
3 changed files with 5 additions and 18 deletions

View File

@ -15,6 +15,7 @@ menuconfig MODULE_SHT2X
bool "SHT2x humidity and temperature sensor" bool "SHT2x humidity and temperature sensor"
depends on HAS_PERIPH_I2C depends on HAS_PERIPH_I2C
depends on TEST_KCONFIG depends on TEST_KCONFIG
select MODULE_CHECKSUM
select MODULE_PERIPH_I2C select MODULE_PERIPH_I2C
select MODULE_ZTIMER_MSEC select MODULE_ZTIMER_MSEC

View File

@ -1,2 +1,3 @@
USEMODULE += checksum
USEMODULE += ztimer_msec USEMODULE += ztimer_msec
FEATURES_REQUIRED += periph_i2c FEATURES_REQUIRED += periph_i2c

View File

@ -26,6 +26,7 @@
#include <math.h> #include <math.h>
#include "checksum/crc8.h"
#include "log.h" #include "log.h"
#include "periph/i2c.h" #include "periph/i2c.h"
#include "ztimer.h" #include "ztimer.h"
@ -407,29 +408,13 @@ static int read_sensor_poll(const sht2x_t* dev, cmd_t command, uint16_t *val)
return SHT2X_OK; return SHT2X_OK;
} }
static const uint16_t POLYNOMIAL = 0x131; /* P(x)=x^8+x^5+x^4+1 = 100110001 */ static const uint8_t POLYNOMIAL = 0x31; /* P(x)=x^8+x^5+x^4+1 = 100110001 */
/** /**
* @brief Calculate 8-Bit checksum with given polynomial * @brief Calculate 8-Bit checksum with given polynomial
*/ */
static uint8_t sht2x_checkcrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum) static uint8_t sht2x_checkcrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
{ {
uint8_t crc = 0; return crc8(data, nbrOfBytes, POLYNOMIAL, 0) != checksum;
uint8_t byteCtr;
for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
{
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit)
{
if ((crc & 0x80) != 0)
crc = (crc << 1) ^ POLYNOMIAL;
else
crc = (crc << 1);
}
}
if (crc != checksum)
return 1;
else
return 0;
} }
/** /**