mirror of
https://github.com/esphome/esphome.git
synced 2025-10-28 21:53:48 +00:00
[helper] Make crc8 function more flexible to avoid reimplementation in individual components (#10201)
This commit is contained in:
@@ -89,7 +89,7 @@ void AGS10Component::dump_config() {
|
||||
bool AGS10Component::new_i2c_address(uint8_t newaddress) {
|
||||
uint8_t rev_newaddress = ~newaddress;
|
||||
std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
|
||||
data[4] = calc_crc8_(data, 4);
|
||||
data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
|
||||
if (!this->write_bytes(REG_ADDRESS, data)) {
|
||||
this->error_code_ = COMMUNICATION_FAILED;
|
||||
this->status_set_warning();
|
||||
@@ -109,7 +109,7 @@ bool AGS10Component::set_zero_point_with_current_resistance() { return this->set
|
||||
|
||||
bool AGS10Component::set_zero_point_with(uint16_t value) {
|
||||
std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
|
||||
data[4] = calc_crc8_(data, 4);
|
||||
data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
|
||||
if (!this->write_bytes(REG_CALIBRATION, data)) {
|
||||
this->error_code_ = COMMUNICATION_FAILED;
|
||||
this->status_set_warning();
|
||||
@@ -184,7 +184,7 @@ template<size_t N> optional<std::array<uint8_t, N>> AGS10Component::read_and_che
|
||||
auto res = *data;
|
||||
auto crc_byte = res[len];
|
||||
|
||||
if (crc_byte != calc_crc8_(res, len)) {
|
||||
if (crc_byte != crc8(res.data(), len, 0xFF, 0x31, true)) {
|
||||
this->error_code_ = CRC_CHECK_FAILED;
|
||||
ESP_LOGE(TAG, "Reading AGS10 version failed: crc error!");
|
||||
return optional<std::array<uint8_t, N>>();
|
||||
@@ -192,20 +192,5 @@ template<size_t N> optional<std::array<uint8_t, N>> AGS10Component::read_and_che
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<size_t N> uint8_t AGS10Component::calc_crc8_(std::array<uint8_t, N> dat, uint8_t num) {
|
||||
uint8_t i, byte1, crc = 0xFF;
|
||||
for (byte1 = 0; byte1 < num; byte1++) {
|
||||
crc ^= (dat[byte1]);
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (crc & 0x80) {
|
||||
crc = (crc << 1) ^ 0x31;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
} // namespace ags10
|
||||
} // namespace esphome
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ags10 {
|
||||
@@ -99,16 +99,6 @@ class AGS10Component : public PollingComponent, public i2c::I2CDevice {
|
||||
* Read, checks and returns data from the sensor.
|
||||
*/
|
||||
template<size_t N> optional<std::array<uint8_t, N>> read_and_check_(uint8_t a_register);
|
||||
|
||||
/**
|
||||
* Calculates CRC8 value.
|
||||
*
|
||||
* CRC8 calculation, initial value: 0xFF, polynomial: 0x31 (x8+ x5+ x4+1)
|
||||
*
|
||||
* @param[in] dat the data buffer
|
||||
* @param num number of bytes in the buffer
|
||||
*/
|
||||
template<size_t N> uint8_t calc_crc8_(std::array<uint8_t, N> dat, uint8_t num);
|
||||
};
|
||||
|
||||
template<typename... Ts> class AGS10NewI2cAddressAction : public Action<Ts...>, public Parented<AGS10Component> {
|
||||
|
||||
@@ -29,22 +29,6 @@ namespace am2315c {
|
||||
|
||||
static const char *const TAG = "am2315c";
|
||||
|
||||
uint8_t AM2315C::crc8_(uint8_t *data, uint8_t len) {
|
||||
uint8_t crc = 0xFF;
|
||||
while (len--) {
|
||||
crc ^= *data++;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (crc & 0x80) {
|
||||
crc <<= 1;
|
||||
crc ^= 0x31;
|
||||
} else {
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
bool AM2315C::reset_register_(uint8_t reg) {
|
||||
// code based on demo code sent by www.aosong.com
|
||||
// no further documentation.
|
||||
@@ -86,7 +70,7 @@ bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
|
||||
humidity = raw * 9.5367431640625e-5;
|
||||
raw = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
|
||||
temperature = raw * 1.9073486328125e-4 - 50;
|
||||
return this->crc8_(data, 6) == data[6];
|
||||
return crc8(data, 6, 0xFF, 0x31, true) == data[6];
|
||||
}
|
||||
|
||||
void AM2315C::setup() {
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
// SOFTWARE.
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace am2315c {
|
||||
@@ -39,7 +39,6 @@ class AM2315C : public PollingComponent, public i2c::I2CDevice {
|
||||
void set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
|
||||
|
||||
protected:
|
||||
uint8_t crc8_(uint8_t *data, uint8_t len);
|
||||
bool convert_(uint8_t *data, float &humidity, float &temperature);
|
||||
bool reset_register_(uint8_t reg);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ void HTE501Component::setup() {
|
||||
this->write(address, 2, false);
|
||||
uint8_t identification[9];
|
||||
this->read(identification, 9);
|
||||
if (identification[8] != calc_crc8_(identification, 0, 7)) {
|
||||
if (identification[8] != crc8(identification, 8, 0xFF, 0x31, true)) {
|
||||
this->error_code_ = CRC_CHECK_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
@@ -46,7 +46,8 @@ void HTE501Component::update() {
|
||||
this->set_timeout(50, [this]() {
|
||||
uint8_t i2c_response[6];
|
||||
this->read(i2c_response, 6);
|
||||
if (i2c_response[2] != calc_crc8_(i2c_response, 0, 1) && i2c_response[5] != calc_crc8_(i2c_response, 3, 4)) {
|
||||
if (i2c_response[2] != crc8(i2c_response, 2, 0xFF, 0x31, true) &&
|
||||
i2c_response[5] != crc8(i2c_response + 3, 2, 0xFF, 0x31, true)) {
|
||||
this->error_code_ = CRC_CHECK_FAILED;
|
||||
this->status_set_warning();
|
||||
return;
|
||||
@@ -67,24 +68,5 @@ void HTE501Component::update() {
|
||||
this->status_clear_warning();
|
||||
});
|
||||
}
|
||||
|
||||
unsigned char HTE501Component::calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to) {
|
||||
unsigned char crc_val = 0xFF;
|
||||
unsigned char i = 0;
|
||||
unsigned char j = 0;
|
||||
for (i = from; i <= to; i++) {
|
||||
int cur_val = buf[i];
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (((crc_val ^ cur_val) & 0x80) != 0) // If MSBs are not equal
|
||||
{
|
||||
crc_val = ((crc_val << 1) ^ 0x31);
|
||||
} else {
|
||||
crc_val = (crc_val << 1);
|
||||
}
|
||||
cur_val = cur_val << 1;
|
||||
}
|
||||
}
|
||||
return crc_val;
|
||||
}
|
||||
} // namespace hte501
|
||||
} // namespace esphome
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace hte501 {
|
||||
@@ -19,7 +19,6 @@ class HTE501Component : public PollingComponent, public i2c::I2CDevice {
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
unsigned char calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to);
|
||||
sensor::Sensor *temperature_sensor_;
|
||||
sensor::Sensor *humidity_sensor_;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "lc709203f.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lc709203f {
|
||||
@@ -189,7 +190,7 @@ uint8_t Lc709203f::get_register_(uint8_t register_to_read, uint16_t *register_va
|
||||
// Error on the i2c bus
|
||||
this->status_set_warning(
|
||||
str_sprintf("Error code %d when reading from register 0x%02X", return_code, register_to_read).c_str());
|
||||
} else if (this->crc8_(read_buffer, 5) != read_buffer[5]) {
|
||||
} else if (crc8(read_buffer, 5, 0x00, 0x07, true) != read_buffer[5]) {
|
||||
// I2C indicated OK, but the CRC of the data does not matcth.
|
||||
this->status_set_warning(str_sprintf("CRC error reading from register 0x%02X", register_to_read).c_str());
|
||||
} else {
|
||||
@@ -220,7 +221,7 @@ uint8_t Lc709203f::set_register_(uint8_t register_to_set, uint16_t value_to_set)
|
||||
write_buffer[1] = register_to_set;
|
||||
write_buffer[2] = value_to_set & 0xFF; // Low byte
|
||||
write_buffer[3] = (value_to_set >> 8) & 0xFF; // High byte
|
||||
write_buffer[4] = this->crc8_(write_buffer, 4);
|
||||
write_buffer[4] = crc8(write_buffer, 4, 0x00, 0x07, true);
|
||||
|
||||
for (uint8_t i = 0; i <= LC709203F_I2C_RETRY_COUNT; i++) {
|
||||
// Note: we don't write the first byte of the write buffer to the device.
|
||||
@@ -239,20 +240,6 @@ uint8_t Lc709203f::set_register_(uint8_t register_to_set, uint16_t value_to_set)
|
||||
return return_code;
|
||||
}
|
||||
|
||||
uint8_t Lc709203f::crc8_(uint8_t *byte_buffer, uint8_t length_of_crc) {
|
||||
uint8_t crc = 0x00;
|
||||
const uint8_t polynomial(0x07);
|
||||
|
||||
for (uint8_t j = length_of_crc; j; --j) {
|
||||
crc ^= *byte_buffer++;
|
||||
|
||||
for (uint8_t i = 8; i; --i) {
|
||||
crc = (crc & 0x80) ? (crc << 1) ^ polynomial : (crc << 1);
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
void Lc709203f::set_pack_size(uint16_t pack_size) {
|
||||
static const uint16_t PACK_SIZE_ARRAY[6] = {100, 200, 500, 1000, 2000, 3000};
|
||||
static const uint16_t APA_ARRAY[6] = {0x08, 0x0B, 0x10, 0x19, 0x2D, 0x36};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lc709203f {
|
||||
@@ -38,7 +38,6 @@ class Lc709203f : public sensor::Sensor, public PollingComponent, public i2c::I2
|
||||
private:
|
||||
uint8_t get_register_(uint8_t register_to_read, uint16_t *register_value);
|
||||
uint8_t set_register_(uint8_t register_to_set, uint16_t value_to_set);
|
||||
uint8_t crc8_(uint8_t *byte_buffer, uint8_t length_of_crc);
|
||||
|
||||
protected:
|
||||
sensor::Sensor *voltage_sensor_{nullptr};
|
||||
|
||||
@@ -50,28 +50,13 @@ bool MLX90614Component::write_emissivity_() {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t MLX90614Component::crc8_pec_(const uint8_t *data, uint8_t len) {
|
||||
uint8_t crc = 0;
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
uint8_t in = data[i];
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
bool carry = (crc ^ in) & 0x80;
|
||||
crc <<= 1;
|
||||
if (carry)
|
||||
crc ^= 0x07;
|
||||
in <<= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
bool MLX90614Component::write_bytes_(uint8_t reg, uint16_t data) {
|
||||
uint8_t buf[5];
|
||||
buf[0] = this->address_ << 1;
|
||||
buf[1] = reg;
|
||||
buf[2] = data & 0xFF;
|
||||
buf[3] = data >> 8;
|
||||
buf[4] = this->crc8_pec_(buf, 4);
|
||||
buf[4] = crc8(buf, 4, 0x00, 0x07, true);
|
||||
return this->write_bytes(reg, buf + 2, 3);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ class MLX90614Component : public PollingComponent, public i2c::I2CDevice {
|
||||
protected:
|
||||
bool write_emissivity_();
|
||||
|
||||
uint8_t crc8_pec_(const uint8_t *data, uint8_t len);
|
||||
bool write_bytes_(uint8_t reg, uint16_t data);
|
||||
|
||||
sensor::Sensor *ambient_sensor_{nullptr};
|
||||
|
||||
@@ -12,7 +12,7 @@ void TEE501Component::setup() {
|
||||
this->write(address, 2, false);
|
||||
uint8_t identification[9];
|
||||
this->read(identification, 9);
|
||||
if (identification[8] != calc_crc8_(identification, 0, 7)) {
|
||||
if (identification[8] != crc8(identification, 8, 0xFF, 0x31, true)) {
|
||||
this->error_code_ = CRC_CHECK_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
@@ -45,7 +45,7 @@ void TEE501Component::update() {
|
||||
this->set_timeout(50, [this]() {
|
||||
uint8_t i2c_response[3];
|
||||
this->read(i2c_response, 3);
|
||||
if (i2c_response[2] != calc_crc8_(i2c_response, 0, 1)) {
|
||||
if (i2c_response[2] != crc8(i2c_response, 2, 0xFF, 0x31, true)) {
|
||||
this->error_code_ = CRC_CHECK_FAILED;
|
||||
this->status_set_warning();
|
||||
return;
|
||||
@@ -62,24 +62,5 @@ void TEE501Component::update() {
|
||||
});
|
||||
}
|
||||
|
||||
unsigned char TEE501Component::calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to) {
|
||||
unsigned char crc_val = 0xFF;
|
||||
unsigned char i = 0;
|
||||
unsigned char j = 0;
|
||||
for (i = from; i <= to; i++) {
|
||||
int cur_val = buf[i];
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (((crc_val ^ cur_val) & 0x80) != 0) // If MSBs are not equal
|
||||
{
|
||||
crc_val = ((crc_val << 1) ^ 0x31);
|
||||
} else {
|
||||
crc_val = (crc_val << 1);
|
||||
}
|
||||
cur_val = cur_val << 1;
|
||||
}
|
||||
}
|
||||
return crc_val;
|
||||
}
|
||||
|
||||
} // namespace tee501
|
||||
} // namespace esphome
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tee501 {
|
||||
@@ -16,8 +16,6 @@ class TEE501Component : public sensor::Sensor, public PollingComponent, public i
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
unsigned char calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to);
|
||||
|
||||
enum ErrorCode { NONE = 0, COMMUNICATION_FAILED, CRC_CHECK_FAILED } error_code_{NONE};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user