From 000d449d88b3880932ef4d64bedff64f2e65032b Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 13 Dec 2022 23:50:39 +0100 Subject: [PATCH] drivers/sht2x: use sys/checksum/crc8 --- drivers/sht2x/Kconfig | 1 + drivers/sht2x/Makefile.dep | 1 + drivers/sht2x/sht2x.c | 21 +++------------------ 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/drivers/sht2x/Kconfig b/drivers/sht2x/Kconfig index afc924019f..72e952ce06 100644 --- a/drivers/sht2x/Kconfig +++ b/drivers/sht2x/Kconfig @@ -15,6 +15,7 @@ menuconfig MODULE_SHT2X bool "SHT2x humidity and temperature sensor" depends on HAS_PERIPH_I2C depends on TEST_KCONFIG + select MODULE_CHECKSUM select MODULE_PERIPH_I2C select MODULE_ZTIMER_MSEC diff --git a/drivers/sht2x/Makefile.dep b/drivers/sht2x/Makefile.dep index df142e43bd..7651bd6d13 100644 --- a/drivers/sht2x/Makefile.dep +++ b/drivers/sht2x/Makefile.dep @@ -1,2 +1,3 @@ +USEMODULE += checksum USEMODULE += ztimer_msec FEATURES_REQUIRED += periph_i2c diff --git a/drivers/sht2x/sht2x.c b/drivers/sht2x/sht2x.c index b46fd23c91..34bec9c977 100644 --- a/drivers/sht2x/sht2x.c +++ b/drivers/sht2x/sht2x.c @@ -26,6 +26,7 @@ #include +#include "checksum/crc8.h" #include "log.h" #include "periph/i2c.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; } -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 */ static uint8_t sht2x_checkcrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum) { - uint8_t crc = 0; - 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; + return crc8(data, nbrOfBytes, POLYNOMIAL, 0) != checksum; } /**