diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 2d5516d3..189a7117 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -317,12 +317,12 @@ bool app::buildPayload(uint8_t id) { for(uint8_t i = 0; i < mPayload[id].maxPackId; i ++) { if(mPayload[id].len[i] > 0) { if(i == (mPayload[id].maxPackId-1)) { - crc = crc16(mPayload[id].data[i], mPayload[id].len[i] - 2, crc); + crc = Hoymiles::crc16(mPayload[id].data[i], mPayload[id].len[i] - 2, crc); crcRcv = (mPayload[id].data[i][mPayload[id].len[i] - 2] << 8) | (mPayload[id].data[i][mPayload[id].len[i] - 1]); } else - crc = crc16(mPayload[id].data[i], mPayload[id].len[i], crc); + crc = Hoymiles::crc16(mPayload[id].data[i], mPayload[id].len[i], crc); } yield(); } diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index e7e9ab61..00967f76 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -166,7 +166,7 @@ class app { while(length > 0) { len = (length < 32) ? length : 32; mEep->read(start, buf, len); - crc = crc16(buf, len, crc); + crc = Hoymiles::crc16(buf, len, crc); start += len; length -= len; } diff --git a/tools/esp8266/crc.cpp b/tools/esp8266/crc.cpp index 7c64d642..29f9b82f 100644 --- a/tools/esp8266/crc.cpp +++ b/tools/esp8266/crc.cpp @@ -5,6 +5,8 @@ #include "crc.h" +namespace Hoymiles { + uint8_t crc8(uint8_t buf[], uint8_t len) { uint8_t crc = CRC8_INIT; for(uint8_t i = 0; i < len; i++) { @@ -33,3 +35,4 @@ uint16_t crc16(uint8_t buf[], uint8_t len, uint16_t start) { } return crc; } +} // namespace Hoymiles \ No newline at end of file diff --git a/tools/esp8266/crc.h b/tools/esp8266/crc.h index c0323ebb..1c8de9e0 100644 --- a/tools/esp8266/crc.h +++ b/tools/esp8266/crc.h @@ -14,7 +14,10 @@ #define CRC16_MODBUS_POLYNOM 0xA001 -uint8_t crc8(uint8_t buf[], uint8_t len); -uint16_t crc16(uint8_t buf[], uint8_t len, uint16_t start = 0xffff); +namespace Hoymiles { + uint8_t crc8(uint8_t buf[], uint8_t len); + uint16_t crc16(uint8_t buf[], uint8_t len, uint16_t start = 0xffff); + +} #endif /*__CRC_H__*/ diff --git a/tools/esp8266/hmInverter.h b/tools/esp8266/hmInverter.h index 32a7505c..3b443873 100644 --- a/tools/esp8266/hmInverter.h +++ b/tools/esp8266/hmInverter.h @@ -81,6 +81,7 @@ class Inverter { RECORDTYPE *record; // pointer for values uint16_t chMaxPwr[4]; // maximum power of the modules (Wp) char chName[4][MAX_NAME_LENGTH]; // human readable name for channel + bool initialized; // needed to check if the inverter was correctly added (ESP32 specific - union types are never null) Inverter() { ts = 0; @@ -88,6 +89,7 @@ class Inverter { powerLimit[1] = 0x0000; // devControlRequest = false; devControlCmd = 0xff; + initialized = false; fwVersion = 0; } @@ -103,6 +105,7 @@ class Inverter { memset(name, 0, MAX_NAME_LENGTH); memset(chName, 0, MAX_NAME_LENGTH * 4); memset(record, 0, sizeof(RECORDTYPE) * listLen); + initialized = true; } uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId) { diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 57d95ec8..7decd2c2 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -59,7 +59,12 @@ template amplifierPower])); + DPRINT(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_")); + DPRINTLN(DBG_INFO, String(rf24AmpPowerNames[config->amplifierPower])); mNrf24.setPALevel(config->amplifierPower & 0x03); mNrf24.startListening(); @@ -170,12 +176,12 @@ class HmRadio { mTxBuf[10 + (++cnt)] = ((data[1] ) ) & 0xff; // setting for persistens handling } // crc control data - uint16_t crc = crc16(&mTxBuf[10], cnt+1); + uint16_t crc = Hoymiles::crc16(&mTxBuf[10], cnt+1); mTxBuf[10 + (++cnt)] = (crc >> 8) & 0xff; mTxBuf[10 + (++cnt)] = (crc ) & 0xff; // crc over all cnt +=1; - mTxBuf[10 + cnt] = crc8(mTxBuf, 10 + cnt); + mTxBuf[10 + cnt] = Hoymiles::crc8(mTxBuf, 10 + cnt); sendPacket(invId, mTxBuf, 10 + (++cnt), true); } @@ -194,10 +200,10 @@ class HmRadio { mTxBuf[18] = 0x00; mTxBuf[19] = 0x00; } - uint16_t crc = crc16(&mTxBuf[10], 14); + uint16_t crc = Hoymiles::crc16(&mTxBuf[10], 14); mTxBuf[24] = (crc >> 8) & 0xff; mTxBuf[25] = (crc ) & 0xff; - mTxBuf[26] = crc8(mTxBuf, 26); + mTxBuf[26] = Hoymiles::crc8(mTxBuf, 26); sendPacket(invId, mTxBuf, 27, true); } @@ -210,7 +216,7 @@ class HmRadio { CP_U32_BigEndian(&mTxBuf[5], (DTU_ID >> 8)); mTxBuf[9] = pid; if(calcCrc) { - mTxBuf[10] = crc8(mTxBuf, 10); + mTxBuf[10] = Hoymiles::crc8(mTxBuf, 10); sendPacket(invId, mTxBuf, 11, false); } } @@ -224,7 +230,7 @@ class HmRadio { buf[i-1] = (buf[i] << 1) | (buf[i+1] >> 7); } - uint8_t crc = crc8(buf, *len-1); + uint8_t crc = Hoymiles::crc8(buf, *len-1); bool valid = (crc == buf[*len-1]); return valid; diff --git a/tools/esp8266/hmSystem.h b/tools/esp8266/hmSystem.h index 386484fa..1070445a 100644 --- a/tools/esp8266/hmSystem.h +++ b/tools/esp8266/hmSystem.h @@ -52,7 +52,10 @@ class HmSystem { case 0x21: p->type = INV_TYPE_1CH; break; case 0x41: p->type = INV_TYPE_2CH; break; case 0x61: p->type = INV_TYPE_4CH; break; - default: DPRINTLN(DBG_ERROR, F("unknown inverter type: 11") + String(p->serial.b[4], HEX)); break; + default: + DPRINT(DBG_ERROR, F("unknown inverter type: 11")); + DPRINTLN(DBG_ERROR, String(p->serial.b[4], HEX)); + break; } } else @@ -88,7 +91,7 @@ class HmSystem { INVERTERTYPE *getInverterByPos(uint8_t pos, bool check = true) { DPRINTLN(DBG_VERBOSE, F("hmSystem.h:getInverterByPos")); - if((mInverter[pos].serial.u64 != 0ULL) || false == check) + if((mInverter[pos].initialized && mInverter[pos].serial.u64 != 0ULL) || false == check) return &mInverter[pos]; else return NULL;