From fab804309e657f2074a4c54a0a64abb9a49e2701 Mon Sep 17 00:00:00 2001 From: rejoe2 Date: Mon, 18 Dec 2023 14:40:49 +0100 Subject: [PATCH] based on .27 - fix MI communication incl. "fastNext" - further restructuring of tx->rx channel bindings --- src/defines.h | 2 +- src/hm/CommQueue.h | 26 +++++++++---------- src/hm/Communication.h | 57 ++++++++++++++++++++++++++++++++++-------- src/hm/hmRadio.h | 18 +++++++------ 4 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/defines.h b/src/defines.h index 540c20be..6a8a55cd 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 2601 +#define VERSION_PATCH 2701 //------------------------------------- typedef struct { diff --git a/src/hm/CommQueue.h b/src/hm/CommQueue.h index d1beedfc..dc688bc3 100644 --- a/src/hm/CommQueue.h +++ b/src/hm/CommQueue.h @@ -16,22 +16,22 @@ class CommQueue { public: CommQueue() {} - void addImportant(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { + void addImportant(Inverter<> *iv, uint8_t cmd) { dec(&mRdPtr); - mQueue[mRdPtr] = queue_s(iv, cmd, delOnPop, true); + mQueue[mRdPtr] = queue_s(iv, cmd, true); } - void add(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { - mQueue[mWrPtr] = queue_s(iv, cmd, delOnPop, false); + void add(Inverter<> *iv, uint8_t cmd) { + mQueue[mWrPtr] = queue_s(iv, cmd, false); inc(&mWrPtr); } - void chgCmd(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { - mQueue[mWrPtr] = queue_s(iv, cmd, delOnPop, false); + void chgCmd(Inverter<> *iv, uint8_t cmd) { + mQueue[mWrPtr] = queue_s(iv, cmd, false); } uint8_t getFillState(void) { - DPRINTLN(DBG_INFO, "wr: " + String(mWrPtr) + ", rd: " + String(mRdPtr)); + //DPRINTLN(DBG_INFO, "wr: " + String(mWrPtr) + ", rd: " + String(mRdPtr)); return abs(mRdPtr - mWrPtr); } @@ -48,8 +48,8 @@ class CommQueue { bool delOnPop; bool isDevControl; queue_s() {} - queue_s(Inverter<> *i, uint8_t c, bool d, bool dev) : - iv(i), cmd(c), attempts(5), ts(0), delOnPop(d), isDevControl(dev) {} + queue_s(Inverter<> *i, uint8_t c, bool dev) : + iv(i), cmd(c), attempts(5), ts(0), isDevControl(dev) {} }; protected: @@ -78,8 +78,8 @@ class CommQueue { cb(true, &mQueue[mRdPtr]); } - void cmdDone(bool force = false) { - if(!mQueue[mRdPtr].delOnPop && !force) { + void cmdDone(bool keep = false) { + if(keep) { mQueue[mRdPtr].attempts = 5; add(mQueue[mRdPtr]); // add to the end again } @@ -95,8 +95,8 @@ class CommQueue { mQueue[mRdPtr].attempts--; } - void incrAttempt(void) { - mQueue[mRdPtr].attempts++; + void incrAttempt(uint8_t nrAttempts = 1) { + mQueue[mRdPtr].attempts += nrAttempts ; } void inc(uint8_t *ptr) { diff --git a/src/hm/Communication.h b/src/hm/Communication.h index 765c1dbc..d044a50f 100644 --- a/src/hm/Communication.h +++ b/src/hm/Communication.h @@ -30,9 +30,9 @@ class Communication : public CommQueue<> { mInverterGap = inverterGap; } - void addImportant(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { + void addImportant(Inverter<> *iv, uint8_t cmd) { mState = States::RESET; // cancel current operation - CommQueue::addImportant(iv, cmd, delOnPop); + CommQueue::addImportant(iv, cmd); } void addPayloadListener(payloadListenerType cb) { @@ -84,7 +84,7 @@ class Communication : public CommQueue<> { q->iv->curFrmCnt = 0; mIsRetransmit = false; if(NULL == q->iv->radio) - cmdDone(true); // can't communicate while radio is not defined! + cmdDone(false); // can't communicate while radio is not defined! mState = States::START; q->iv->mCmd = q->cmd; @@ -165,7 +165,8 @@ class Communication : public CommQueue<> { closeRequest(q, false); break; } - mIsRetransmit = false; + if ((IV_MI != q->iv->ivGen) || (0 == q->attempts) ) + mIsRetransmit = false; mFirstTry = false; // for correct reset while(!q->iv->radio->mBufCtrl.empty()) { @@ -183,6 +184,7 @@ class Communication : public CommQueue<> { closeRequest(q, true); else closeRequest(q, false); + q->iv->radio->mBufCtrl.pop(); return; // don't wait for empty buffer } else if(IV_MI == q->iv->ivGen) { if(parseMiFrame(p, q)) @@ -205,6 +207,11 @@ class Communication : public CommQueue<> { bool fastNext = true; if(q->iv->miMultiParts < 6) { mState = States::WAIT; + if((millis() > mWaitTimeout && mIsRetransmit) || !mIsRetransmit) { + miRepeatRequest(q); + return; + } + } else { mHeu.evalTxChQuality(q->iv, true, (4 - q->attempts), q->iv->curFrmCnt); if(((q->cmd == 0x39) && (q->iv->type == INV_TYPE_4CH)) @@ -213,14 +220,17 @@ class Communication : public CommQueue<> { miComplete(q->iv); fastNext = false; } - closeRequest(q, true); + if(fastNext) { - DPRINTLN(DBG_INFO, F("we should enqueue sth immediately....")); + miNextRequest(q->iv->type == INV_TYPE_4CH ? MI_REQ_4CH : MI_REQ_CH1, q); + //DPRINTLN(DBG_INFO, F("we should enqueue sth immediately....")); // immediately send out regular production data request // and reset mWaitTimeout //mWaitTimeout = mWaitTimeout - *mInverterGap; //chgCmd((q->iv->type == INV_TYPE_4CH) ? MI_REQ_4CH : MI_REQ_CH1); //mState = States::RESET; + } else { + closeRequest(q, true); } } @@ -354,8 +364,11 @@ class Communication : public CommQueue<> { return false; // CRC8 is wrong, frame invalid } - if((*frameId & ALL_FRAMES) == ALL_FRAMES) + if((*frameId & ALL_FRAMES) == ALL_FRAMES) { mMaxFrameId = (*frameId & 0x7f); + if(mMaxFrameId > 8) // large payloads, e.g. AlarmData + incrAttempt(mMaxFrameId - 6); + } frame_t *f = &mLocalBuf[(*frameId & 0x7f) - 1]; memcpy(f->buf, &p->packet[10], p->len-11); @@ -526,12 +539,15 @@ class Communication : public CommQueue<> { q->iv->radioStatistics.rxSuccess++; else if(q->iv->mGotFragment) q->iv->radioStatistics.rxFail++; // got no complete payload - else { + else q->iv->radioStatistics.rxFailNoAnser++; // got nothing - } mWaitTimeout = millis() + *mInverterGap; - cmdDone(q->delOnPop); + bool keep = false; + if(q->isDevControl) + keep = !crcPass; + + cmdDone(keep); q->iv->mGotFragment = false; q->iv->mGotLastMsg = false; q->iv->miMultiParts = 0; @@ -754,6 +770,25 @@ class Communication : public CommQueue<> { //mState = States::WAIT; } + void miRepeatRequest(const queue_s *q) { + setAttempt(); // if function is called, we got something, and we necessarily need more transmissions for MI types... + if(*mSerialDebug) { + DPRINT_IVID(DBG_WARN, q->iv->id); + DBGPRINT(F("resend request (")); + DBGPRINT(String(q->attempts)); + DBGPRINT(F(" attempts left): 0x")); + DBGHEXLN(q->cmd); + } + + q->iv->radio->sendCmdPacket(q->iv, q->cmd, 0x00, true); + + mWaitTimeout = millis() + MI_TIMEOUT; + mWaitTimeout_min = mWaitTimeout; + //mState = States::WAIT; + mIsRetransmit = false; + } + + void miStsConsolidate(const queue_s *q, uint8_t stschan, record_t<> *rec, uint8_t uState, uint8_t uEnum, uint8_t lState = 0, uint8_t lEnum = 0) { //uint8_t status = (p->packet[11] << 8) + p->packet[12]; uint16_t statusMi = 3; // regular status for MI, change to 1 later? @@ -860,7 +895,7 @@ class Communication : public CommQueue<> { //closeRequest(iv, iv->miMultiParts > 5); //mHeu.setGotAll(iv); - //cmdDone(true); + //cmdDone(false); if(NULL != mCbPayload) (mCbPayload)(RealTimeRunData_Debug, iv); diff --git a/src/hm/hmRadio.h b/src/hm/hmRadio.h index 5fead045..493c0bc9 100644 --- a/src/hm/hmRadio.h +++ b/src/hm/hmRadio.h @@ -121,7 +121,9 @@ class HmRadio : public Radio { uint32_t startMicros = micros(); uint32_t loopMillis = millis(); - mRxChIdx = (mTxChIdx + 3) % RF_MAX_CHANNEL_ID; // start with a fixed offset + mRxChIdx = mRxChannels - 2; // ensure, we start receiving with first relative channel.... + //mRxChannels - 1; // + //(mTxChIdx + mRxChannels) % RF_MAX_CHANNEL_ID; // start with a fixed offset while ((millis() - loopMillis) < mRxTmoOuterLoop) { while ((micros() - startMicros) < mRxTmoInnerLoop) { // listen (4088us or?) 5110us to each channel if (mIrqRcvd) { @@ -247,7 +249,7 @@ class HmRadio : public Radio { void prepareReceive(Inverter<> *iv) { if (iv->mIsSingleframeReq) { - mRxTmoOuterLoop = 65; // SINGLEFR_TIMEOUT + mRxTmoOuterLoop = 60; // SINGLEFR_TIMEOUT return; } @@ -256,24 +258,24 @@ class HmRadio : public Radio { if (iv->type == INV_TYPE_4CH) { mRxChannels = 3; mRxTmoOuterLoop = 300; - mRxTmoInnerLoop = 5110; + mRxTmoInnerLoop = 5110; //10220; //4088; // 6132; // // } else if (iv->type == INV_TYPE_2CH) { mRxChannels = 2; - mRxTmoOuterLoop = 250; + mRxTmoOuterLoop = 240; mRxTmoInnerLoop = 10220; } else { // INV_TYPE_1CH mRxChannels = 2; - mRxTmoOuterLoop = 200; + mRxTmoOuterLoop = 180; mRxTmoInnerLoop = 5110; } } else { //3rd gen defaults mRxChannels = 3; - mRxTmoOuterLoop = iv->mCmd == AlarmData ? 600 : 400; + mRxTmoOuterLoop = iv->mCmd == AlarmData ? 600 : 420; mRxTmoInnerLoop = 5110; } } else { // 2nd gen defaults - mRxChannels = 2; - mRxTmoOuterLoop = 250; + mRxChannels = 3; + mRxTmoOuterLoop = 240; mRxTmoInnerLoop = 5110; } }