mirror of
https://github.com/esphome/esphome.git
synced 2025-09-15 01:32:19 +01:00
Add standardized CRC helper functions (#4798)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "pipsolar.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pipsolar {
|
||||
@@ -768,7 +769,7 @@ uint8_t Pipsolar::check_incoming_length_(uint8_t length) {
|
||||
|
||||
uint8_t Pipsolar::check_incoming_crc_() {
|
||||
uint16_t crc16;
|
||||
crc16 = cal_crc_half_(read_buffer_, read_pos_ - 3);
|
||||
crc16 = crc16be(read_buffer_, read_pos_ - 3);
|
||||
ESP_LOGD(TAG, "checking crc on incoming message");
|
||||
if (((uint8_t) ((crc16) >> 8)) == read_buffer_[read_pos_ - 3] &&
|
||||
((uint8_t) ((crc16) &0xff)) == read_buffer_[read_pos_ - 2]) {
|
||||
@@ -797,7 +798,7 @@ uint8_t Pipsolar::send_next_command_() {
|
||||
this->command_start_millis_ = millis();
|
||||
this->empty_uart_buffer_();
|
||||
this->read_pos_ = 0;
|
||||
crc16 = cal_crc_half_(byte_command, length);
|
||||
crc16 = crc16be(byte_command, length);
|
||||
this->write_str(command);
|
||||
// checksum
|
||||
this->write(((uint8_t) ((crc16) >> 8))); // highbyte
|
||||
@@ -824,8 +825,8 @@ void Pipsolar::send_next_poll_() {
|
||||
this->command_start_millis_ = millis();
|
||||
this->empty_uart_buffer_();
|
||||
this->read_pos_ = 0;
|
||||
crc16 = cal_crc_half_(this->used_polling_commands_[this->last_polling_command_].command,
|
||||
this->used_polling_commands_[this->last_polling_command_].length);
|
||||
crc16 = crc16be(this->used_polling_commands_[this->last_polling_command_].command,
|
||||
this->used_polling_commands_[this->last_polling_command_].length);
|
||||
this->write_array(this->used_polling_commands_[this->last_polling_command_].command,
|
||||
this->used_polling_commands_[this->last_polling_command_].length);
|
||||
// checksum
|
||||
@@ -892,42 +893,5 @@ void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand poll
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t Pipsolar::cal_crc_half_(uint8_t *msg, uint8_t len) {
|
||||
uint16_t crc;
|
||||
|
||||
uint8_t da;
|
||||
uint8_t *ptr;
|
||||
uint8_t b_crc_hign;
|
||||
uint8_t b_crc_low;
|
||||
|
||||
uint16_t crc_ta[16] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
|
||||
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef};
|
||||
|
||||
ptr = msg;
|
||||
crc = 0;
|
||||
|
||||
while (len-- != 0) {
|
||||
da = ((uint8_t) (crc >> 8)) >> 4;
|
||||
crc <<= 4;
|
||||
crc ^= crc_ta[da ^ (*ptr >> 4)];
|
||||
da = ((uint8_t) (crc >> 8)) >> 4;
|
||||
crc <<= 4;
|
||||
crc ^= crc_ta[da ^ (*ptr & 0x0f)];
|
||||
ptr++;
|
||||
}
|
||||
|
||||
b_crc_low = crc;
|
||||
b_crc_hign = (uint8_t) (crc >> 8);
|
||||
|
||||
if (b_crc_low == 0x28 || b_crc_low == 0x0d || b_crc_low == 0x0a)
|
||||
b_crc_low++;
|
||||
if (b_crc_hign == 0x28 || b_crc_hign == 0x0d || b_crc_hign == 0x0a)
|
||||
b_crc_hign++;
|
||||
|
||||
crc = ((uint16_t) b_crc_hign) << 8;
|
||||
crc += b_crc_low;
|
||||
return (crc);
|
||||
}
|
||||
|
||||
} // namespace pipsolar
|
||||
} // namespace esphome
|
||||
|
Reference in New Issue
Block a user