From c7d039ac8c50fbece47a31cdcd699d87a7b3d119 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 2 Jun 2022 08:20:16 +0200 Subject: [PATCH] * added RX channel 40 * improved RF24 ISR * reduced AP active time to 60s (will be increase once a client is connected) * added `yield` without success -> random reboot (cause 4) --- tools/esp8266/app.cpp | 9 +++++- tools/esp8266/config.h | 2 +- tools/esp8266/defines.h | 2 +- tools/esp8266/hmRadio.h | 64 ++++++++++++++++++++++++---------------- tools/esp8266/hmSystem.h | 4 +-- tools/esp8266/main.cpp | 4 +-- tools/esp8266/mqtt.h | 3 +- 7 files changed, 54 insertions(+), 34 deletions(-) diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 10a765a6a..530475226 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -152,6 +152,8 @@ void app::setup(uint32_t timeout) { void app::loop(void) { Main::loop(); + mSys->Radio.loop(); + if(checkTicker(&mRxTicker, 5)) { bool rxRdy = mSys->Radio.switchRxCh(); @@ -187,6 +189,7 @@ void app::loop(void) { } mSys->BufCtrl.popBack(); + yield(); } @@ -309,6 +312,7 @@ bool app::buildPayload(uint8_t id) { else crc = crc16(mPayload[id].data[i], mPayload[id].len[i], crc); } + yield(); } if(crc == crcRcv) return true; @@ -341,7 +345,7 @@ void app::processPayload(bool retransmit) { else mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); } - mSys->Radio.switchRxCh(100); + mSys->Radio.switchRxCh(300); } } else { @@ -352,6 +356,7 @@ void app::processPayload(bool retransmit) { for(uint8_t i = 0; i < (mPayload[iv->id].maxPackId); i ++) { memcpy(&payload[offs], mPayload[iv->id].data[i], (mPayload[iv->id].len[i])); offs += (mPayload[iv->id].len[i]); + yield(); } offs-=2; if(mSerialDebug) { @@ -367,6 +372,7 @@ void app::processPayload(bool retransmit) { } } } + yield(); } } @@ -629,6 +635,7 @@ void app::showLiveData(void) { } } modHtml += ""; + yield(); } modHtml += F("
Last received data requested at: ") + getDateTimeStr(iv->ts) + F("
"); modHtml += F(""); diff --git a/tools/esp8266/config.h b/tools/esp8266/config.h index b5806ed8b..f7e89ebe6 100644 --- a/tools/esp8266/config.h +++ b/tools/esp8266/config.h @@ -21,7 +21,7 @@ // time during the ESP will act as access point on connection failure (to // station) in seconds -#define WIFI_AP_ACTIVE_TIME 3*60 +#define WIFI_AP_ACTIVE_TIME 60 // default device name #define DEF_DEVICE_NAME "ESP-DTU" diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 147e88298..d7f36c395 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -16,7 +16,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 4 -#define VERSION_PATCH 13 +#define VERSION_PATCH 14 //------------------------------------- diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index a539ee4fd..708e9ef5a 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -13,7 +13,7 @@ #define DTU_RADIO_ID ((uint64_t)0x1234567801ULL) #define DUMMY_RADIO_ID ((uint64_t)0xDEADBEEF01ULL) -#define RX_LOOP_CNT 400 +#define RX_LOOP_CNT 600 const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"}; @@ -55,6 +55,7 @@ class HmRadio { mRxChLst[1] = 23; mRxChLst[2] = 61; mRxChLst[3] = 75; + mRxChLst[4] = 40; mRxChIdx = 0; mRxLoopCnt = RX_LOOP_CNT; @@ -66,6 +67,7 @@ class HmRadio { mSendCnt = 0; mSerialDebug = false; + mIrqRcvd = false; } ~HmRadio() {} @@ -102,31 +104,39 @@ class HmRadio { } } - void handleIntr(void) { - uint8_t pipe, len; - packet_t *p; - + void loop(void) { DISABLE_IRQ; - while(mNrf24.available(&pipe)) { - if(!mBufCtrl->full()) { - p = mBufCtrl->getFront(); - memset(p->packet, 0xcc, MAX_RF_PAYLOAD_SIZE); - p->rxCh = mRxChLst[mRxChIdx]; - len = mNrf24.getPayloadSize(); - if(len > MAX_RF_PAYLOAD_SIZE) - len = MAX_RF_PAYLOAD_SIZE; - - mNrf24.read(p->packet, len); - mBufCtrl->pushFront(p); - } - else { - bool tx_ok, tx_fail, rx_ready; - mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // reset interrupt status - mNrf24.flush_rx(); // drop the packet - break; + if(mIrqRcvd) { + mIrqRcvd = false; + bool tx_ok, tx_fail, rx_ready; + mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH + RESTORE_IRQ; + uint8_t pipe, len; + packet_t *p; + while(mNrf24.available(&pipe)) { + if(!mBufCtrl->full()) { + p = mBufCtrl->getFront(); + p->rxCh = mRxChLst[mRxChIdx]; + len = mNrf24.getPayloadSize(); + if(len > MAX_RF_PAYLOAD_SIZE) + len = MAX_RF_PAYLOAD_SIZE; + + mNrf24.read(p->packet, len); + mBufCtrl->pushFront(p); + } + else { + mNrf24.flush_rx(); // drop the packet + break; + } + yield(); } } - RESTORE_IRQ; + else + RESTORE_IRQ; + } + + void handleIntr(void) { + mIrqRcvd = true; } uint8_t getDefaultChannel(void) { @@ -183,7 +193,7 @@ class HmRadio { return valid; } - bool switchRxCh(uint8_t addLoop = 0) { + bool switchRxCh(uint16_t addLoop = 0) { mRxLoopCnt += addLoop; if(mRxLoopCnt != 0) { mRxLoopCnt--; @@ -263,7 +273,7 @@ class HmRadio { } uint8_t getRxNxtChannel(void) { - if(++mRxChIdx >= 4) + if(++mRxChIdx >= 5) mRxChIdx = 0; return mRxChLst[mRxChIdx]; } @@ -272,13 +282,15 @@ class HmRadio { uint8_t mTxChLst[1]; //uint8_t mTxChIdx; - uint8_t mRxChLst[4]; + uint8_t mRxChLst[5]; uint8_t mRxChIdx; uint16_t mRxLoopCnt; RF24 mNrf24; BUFFER *mBufCtrl; uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE]; + + volatile bool mIrqRcvd; }; #endif /*__RADIO_H__*/ diff --git a/tools/esp8266/hmSystem.h b/tools/esp8266/hmSystem.h index 9565b16cd..a5b665e56 100644 --- a/tools/esp8266/hmSystem.h +++ b/tools/esp8266/hmSystem.h @@ -36,8 +36,8 @@ class HmSystem { p->id = mNumInv; p->serial.u64 = serial; memcpy(p->chMaxPwr, chMaxPwr, (4*2)); - DPRINT("SERIAL: " + String(p->serial.b[5], HEX)); - DPRINTLN(" " + String(p->serial.b[4], HEX)); + //DPRINT("SERIAL: " + String(p->serial.b[5], HEX)); + //DPRINTLN(" " + String(p->serial.b[4], HEX)); if(p->serial.b[5] == 0x11) { switch(p->serial.b[4]) { case 0x21: p->type = INV_TYPE_1CH; break; diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index e5e89a319..3656b3420 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -105,10 +105,10 @@ void Main::loop(void) { } } - if(++mHeapStatCnt >= 10) { + /*if(++mHeapStatCnt >= 10) { mHeapStatCnt = 0; stats(); - } + }*/ } } diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 4f9bc4b7d..91a0dffd9 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -36,7 +36,8 @@ class mqtt { if(!mClient->connected()) reconnect(); - mClient->publish(top, msg); + if(mClient->connected()) + mClient->publish(top, msg); } }