From 9b6c86454b485ca342300a0d663270281b9a242c Mon Sep 17 00:00:00 2001 From: dooku Date: Wed, 9 Jun 2021 18:33:13 +0200 Subject: [PATCH 1/2] Added CRC8 generating function --- fw/Core/Inc/crc8.h | 22 ++++++++++++++++++++++ fw/Core/Src/crc8.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 fw/Core/Inc/crc8.h create mode 100644 fw/Core/Src/crc8.c diff --git a/fw/Core/Inc/crc8.h b/fw/Core/Inc/crc8.h new file mode 100644 index 0000000..e799f7e --- /dev/null +++ b/fw/Core/Inc/crc8.h @@ -0,0 +1,22 @@ +/* + * crc.h + * + * Created on: Jun 9, 2021 + * Author: user + */ + +#ifndef INC_CRC8_H_ +#define INC_CRC8_H_ + +#include "stdint.h" + +/* + * Definitions & macros + */ + +#define CRC8_POLYNOMIAL 0x31 +#define CRC8_INIT 0xFF + +uint8_t crc8_calculate(const uint8_t *data, uint16_t count); + +#endif /* INC_CRC8_H_ */ diff --git a/fw/Core/Src/crc8.c b/fw/Core/Src/crc8.c new file mode 100644 index 0000000..312cdf4 --- /dev/null +++ b/fw/Core/Src/crc8.c @@ -0,0 +1,28 @@ +/* + * crc.c + * + * Created on: Jun 9, 2021 + * Author: user + */ + +#include "crc8.h" + +/* Stolen from Sensirion SCD4x datasheet, section 3.11 */ +uint8_t crc8_calculate(const uint8_t *data, uint16_t count) +{ + uint16_t current_byte; + uint8_t crc = CRC8_INIT; + uint8_t crc_bit; + /* calculates 8-Bit checksum with given polynomial */ + for (current_byte = 0; current_byte < count; ++current_byte) { + crc ^= (data[current_byte]); + for(crc_bit = 8; crc_bit > 0; --crc_bit) { + if (crc & 0x80) { + crc =(crc << 1) ^ CRC8_POLYNOMIAL; + } else { + crc = (crc << 1); + } + } + } + return crc; +} From 6b8e2f5903994147f161a8113c87f2fed58f9fff Mon Sep 17 00:00:00 2001 From: dooku Date: Wed, 9 Jun 2021 18:52:44 +0200 Subject: [PATCH 2/2] Fixed warning for implicit declaration of LL_mDelay --- fw/Core/Inc/sht4x.h | 3 +++ fw/Core/Src/sht4x.c | 1 + 2 files changed, 4 insertions(+) diff --git a/fw/Core/Inc/sht4x.h b/fw/Core/Inc/sht4x.h index 0e133f3..b21b67b 100644 --- a/fw/Core/Inc/sht4x.h +++ b/fw/Core/Inc/sht4x.h @@ -10,7 +10,9 @@ #include "stdint.h" #include "stm32l0xx_ll_i2c.h" +#include "stm32l0xx_ll_utils.h" #include "i2c.h" +#include "crc8.h" /* * Defines & macros @@ -24,6 +26,7 @@ #define SHT4X_OK 0 #define SHT4X_ERROR -1 // generic error +#define SHT4X_CRC8_ERROR -2 // checksum failed /* * Data types diff --git a/fw/Core/Src/sht4x.c b/fw/Core/Src/sht4x.c index 50521da..59aced5 100644 --- a/fw/Core/Src/sht4x.c +++ b/fw/Core/Src/sht4x.c @@ -34,6 +34,7 @@ int sht4x_measure(int *temperature, int *relative_humidity) if (result != I2C_OK) { return SHT4X_ERROR; } + // TODO checksum // Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3 uint32_t t_ticks = (buffer[0] << 8) + buffer[1]; uint32_t rh_ticks = (buffer[3] << 8) + buffer[4];