commit 5fc3f95ee0bc30ab62e9a3fffec06fc5e5fc5f96 Author: Jan Mrna Date: Sun Jun 12 22:25:57 2022 +0200 Added crc files diff --git a/crc8.c b/crc8.c new file mode 100644 index 0000000..a8ed0f3 --- /dev/null +++ b/crc8.c @@ -0,0 +1,31 @@ +/* + * crc.c + * + * Created on: Jun 9, 2021 + * Author: user + */ + +#include "crc8.h" + +/* TODO add polynomial and init value to make this function + * generic */ + +/* 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; +} diff --git a/crc8.h b/crc8.h new file mode 100644 index 0000000..6820352 --- /dev/null +++ b/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 ((uint8_t)0x31) +#define CRC8_INIT ((uint8_t)0xFF) + +uint8_t crc8_calculate(const uint8_t *data, uint16_t count); + +#endif /* INC_CRC8_H_ */