From c317422ed76089fe0900b2e80bf676ee63ddf70f Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 6 Sep 2022 12:57:21 +1200 Subject: [PATCH] Move crc16 to helpers (#3780) --- esphome/components/am2320/am2320.cpp | 24 +++--------------------- esphome/components/modbus/modbus.cpp | 16 ---------------- esphome/components/modbus/modbus.h | 2 -- esphome/components/senseair/senseair.cpp | 20 ++------------------ esphome/components/senseair/senseair.h | 1 - esphome/core/helpers.cpp | 15 +++++++++++++++ esphome/core/helpers.h | 3 +++ 7 files changed, 23 insertions(+), 58 deletions(-) diff --git a/esphome/components/am2320/am2320.cpp b/esphome/components/am2320/am2320.cpp index 8ab48a348e..ec501f2d36 100644 --- a/esphome/components/am2320/am2320.cpp +++ b/esphome/components/am2320/am2320.cpp @@ -4,33 +4,15 @@ // - Arduino - AM2320: https://github.com/EngDial/AM2320/blob/master/src/AM2320.cpp #include "am2320.h" -#include "esphome/core/log.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" namespace esphome { namespace am2320 { static const char *const TAG = "am2320"; -// ---=== Calc CRC16 ===--- -uint16_t crc_16(uint8_t *ptr, uint8_t length) { - uint16_t crc = 0xFFFF; - uint8_t i; - //------------------------------ - while (length--) { - crc ^= *ptr++; - for (i = 0; i < 8; i++) { - if ((crc & 0x01) != 0) { - crc >>= 1; - crc ^= 0xA001; - } else { - crc >>= 1; - } - } - } - return crc; -} - void AM2320Component::update() { uint8_t data[8]; data[0] = 0; @@ -98,7 +80,7 @@ bool AM2320Component::read_data_(uint8_t *data) { checksum = data[7] << 8; checksum += data[6]; - if (crc_16(data, 6) != checksum) { + if (crc16(data, 6) != checksum) { ESP_LOGW(TAG, "AM2320 Checksum invalid!"); return false; } diff --git a/esphome/components/modbus/modbus.cpp b/esphome/components/modbus/modbus.cpp index 845fa92e95..4d75675d0f 100644 --- a/esphome/components/modbus/modbus.cpp +++ b/esphome/components/modbus/modbus.cpp @@ -35,22 +35,6 @@ void Modbus::loop() { } } -uint16_t crc16(const uint8_t *data, uint8_t len) { - uint16_t crc = 0xFFFF; - while (len--) { - crc ^= *data++; - for (uint8_t i = 0; i < 8; i++) { - if ((crc & 0x01) != 0) { - crc >>= 1; - crc ^= 0xA001; - } else { - crc >>= 1; - } - } - } - return crc; -} - bool Modbus::parse_modbus_byte_(uint8_t byte) { size_t at = this->rx_buffer_.size(); this->rx_buffer_.push_back(byte); diff --git a/esphome/components/modbus/modbus.h b/esphome/components/modbus/modbus.h index 400e29e08b..629ab6dcce 100644 --- a/esphome/components/modbus/modbus.h +++ b/esphome/components/modbus/modbus.h @@ -40,8 +40,6 @@ class Modbus : public uart::UARTDevice, public Component { std::vector devices_; }; -uint16_t crc16(const uint8_t *data, uint8_t len); - class ModbusDevice { public: void set_parent(Modbus *parent) { parent_ = parent; } diff --git a/esphome/components/senseair/senseair.cpp b/esphome/components/senseair/senseair.cpp index 50b9e01f17..7a98584201 100644 --- a/esphome/components/senseair/senseair.cpp +++ b/esphome/components/senseair/senseair.cpp @@ -1,4 +1,5 @@ #include "senseair.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" namespace esphome { @@ -42,7 +43,7 @@ void SenseAirComponent::update() { return; } - uint16_t calc_checksum = this->senseair_checksum_(response, 11); + uint16_t calc_checksum = crc16(response, 11); uint16_t resp_checksum = (uint16_t(response[12]) << 8) | response[11]; if (resp_checksum != calc_checksum) { ESP_LOGW(TAG, "SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum); @@ -60,23 +61,6 @@ void SenseAirComponent::update() { this->co2_sensor_->publish_state(ppm); } -uint16_t SenseAirComponent::senseair_checksum_(uint8_t *ptr, uint8_t length) { - uint16_t crc = 0xFFFF; - uint8_t i; - while (length--) { - crc ^= *ptr++; - for (i = 0; i < 8; i++) { - if ((crc & 0x01) != 0) { - crc >>= 1; - crc ^= 0xA001; - } else { - crc >>= 1; - } - } - } - return crc; -} - void SenseAirComponent::background_calibration() { // Responses are just echoes but must be read to clear the buffer ESP_LOGD(TAG, "SenseAir Starting background calibration"); diff --git a/esphome/components/senseair/senseair.h b/esphome/components/senseair/senseair.h index c03a0848e9..bcec638f79 100644 --- a/esphome/components/senseair/senseair.h +++ b/esphome/components/senseair/senseair.h @@ -23,7 +23,6 @@ class SenseAirComponent : public PollingComponent, public uart::UARTDevice { void abc_disable(); protected: - uint16_t senseair_checksum_(uint8_t *ptr, uint8_t length); bool senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length); sensor::Sensor *co2_sensor_{nullptr}; diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index d82e452c3d..3aca944a36 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -62,6 +62,21 @@ uint8_t crc8(uint8_t *data, uint8_t len) { } return crc; } +uint16_t crc16(const uint8_t *data, uint8_t len) { + uint16_t crc = 0xFFFF; + while (len--) { + crc ^= *data++; + for (uint8_t i = 0; i < 8; i++) { + if ((crc & 0x01) != 0) { + crc >>= 1; + crc ^= 0xA001; + } else { + crc >>= 1; + } + } + } + return crc; +} uint32_t fnv1_hash(const std::string &str) { uint32_t hash = 2166136261UL; for (char c : str) { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 6bed743010..1459f0ef55 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -149,6 +149,9 @@ template T remap(U value, U min, U max, T min_out, T max /// Calculate a CRC-8 checksum of \p data with size \p len. uint8_t crc8(uint8_t *data, uint8_t len); +/// Calculate a CRC-16 checksum of \p data with size \p len. +uint16_t crc16(const uint8_t *data, uint8_t len); + /// Calculate a FNV-1 hash of \p str. uint32_t fnv1_hash(const std::string &str);