From 3319c3ccdcc9e6d2979f45b809e59c02316a64fa Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Wed, 19 Apr 2017 12:15:24 -0700 Subject: [PATCH] AP_BattMonitor: Use a common PEC helper --- .../AP_BattMonitor/AP_BattMonitor_SMBus.cpp | 43 +++++++++++++++++++ .../AP_BattMonitor/AP_BattMonitor_SMBus.h | 7 +++ .../AP_BattMonitor_SMBus_Maxell.cpp | 40 ----------------- .../AP_BattMonitor_SMBus_Maxell.h | 4 -- .../AP_BattMonitor_SMBus_Solo.cpp | 41 ------------------ .../AP_BattMonitor_SMBus_Solo.h | 4 -- 6 files changed, 50 insertions(+), 89 deletions(-) create mode 100644 libraries/AP_BattMonitor/AP_BattMonitor_SMBus.cpp diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus.cpp new file mode 100644 index 0000000000..7097be4658 --- /dev/null +++ b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus.cpp @@ -0,0 +1,43 @@ +#include "AP_BattMonitor_SMBus.h" + +#define AP_BATTMONITOR_SMBUS_PEC_POLYNOME 0x07 // Polynome for CRC generation + +/// get_PEC - calculate packet error correction code of buffer +uint8_t AP_BattMonitor_SMBus::get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const +{ + // exit immediately if no data + if (len == 0) { + return 0; + } + + // prepare temp buffer for calcing crc + uint8_t tmp_buff[len+3]; + tmp_buff[0] = i2c_addr << 1; + tmp_buff[1] = cmd; + tmp_buff[2] = tmp_buff[0] | (uint8_t)reading; + memcpy(&tmp_buff[3],buff,len); + + // initialise crc to zero + uint8_t crc = 0; + uint8_t shift_reg = 0; + bool do_invert; + + // for each byte in the stream + for (uint8_t i=0; i _dev; uint8_t _pec_confirmed; // count of the number of times PEC has been confirmed as working bool _pec_supported; // true if pec is supported diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Solo.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Solo.cpp index 0b2dba6c2c..b2970d98a7 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Solo.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_SMBus_Solo.cpp @@ -189,44 +189,3 @@ uint8_t AP_BattMonitor_SMBus_Solo::read_block(uint8_t reg, uint8_t* data, uint8_ return bufflen; } -#define SMBUS_PEC_POLYNOME 0x07 // Polynome for CRC generation - -/// get_PEC - calculate packet error correction code of buffer -uint8_t AP_BattMonitor_SMBus_Solo::get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const -{ - // exit immediately if no data - if (len <= 0) { - return 0; - } - - // prepare temp buffer for calcing crc - uint8_t tmp_buff[len+3]; - tmp_buff[0] = i2c_addr << 1; - tmp_buff[1] = cmd; - tmp_buff[2] = tmp_buff[0] | (uint8_t)reading; - memcpy(&tmp_buff[3],buff,len); - - // initialise crc to zero - uint8_t crc = 0; - uint8_t shift_reg = 0; - bool do_invert; - - // for each byte in the stream - for (uint8_t i=0; i _dev; uint8_t _button_press_count; };