From 2d19138463b3b6b81fb4b21bb0c01d97c221f13c Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 5 Apr 2024 23:38:28 +0200 Subject: [PATCH] 0.8.106 * fix bootloop with CMT and NRF on ESP32 #1566 #1562 --- src/CHANGES.md | 3 ++ src/app.cpp | 7 ++-- src/app.h | 4 +-- src/hm/{hmRadio.h => NrfRadio.h} | 61 ++++++++++++++++++++------------ src/hm/hmInverter.h | 2 +- src/hm/radio.h | 2 +- src/hms/cmt2300a.h | 4 +-- src/hms/hmsRadio.h | 8 ++--- src/plugins/Display/Display.h | 10 +++--- src/web/RestApi.h | 4 +-- 10 files changed, 61 insertions(+), 44 deletions(-) rename src/hm/{hmRadio.h => NrfRadio.h} (92%) diff --git a/src/CHANGES.md b/src/CHANGES.md index 48534a7f..bc4eb51f 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,8 @@ # Development Changes +## 0.8.106 - 2024-04-05 +* fix bootloop with CMT and NRF on ESP32 #1566 #1562 + ## 0.8.105 - 2024-04-05 * cleanup of `defines.h` * fix compile of esp32-minimal diff --git a/src/app.cpp b/src/app.cpp index f22e6906..0577aa8f 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -45,9 +45,7 @@ void app::setup() { esp_task_wdt_reset(); - if(mConfig->nrf.enabled) { - mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs, mConfig->nrf.pinSclk, mConfig->nrf.pinMosi, mConfig->nrf.pinMiso); - } + mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf); #if defined(ESP32) if(mConfig->cmt.enabled) { mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->cmt.pinSclk, mConfig->cmt.pinSdio, mConfig->cmt.pinCsb, mConfig->cmt.pinFcsb, mConfig->sys.region); @@ -141,8 +139,7 @@ void app::setup() { void app::loop(void) { esp_task_wdt_reset(); - if(mConfig->nrf.enabled) - mNrfRadio.loop(); + mNrfRadio.loop(); #if defined(ESP32) if(mConfig->cmt.enabled) diff --git a/src/app.h b/src/app.h index 2fc37ea8..49d78f56 100644 --- a/src/app.h +++ b/src/app.h @@ -17,7 +17,7 @@ #include "defines.h" #include "appInterface.h" #include "hm/hmSystem.h" -#include "hm/hmRadio.h" +#include "hm/NrfRadio.h" #if defined(ESP32) #include "hms/hmsRadio.h" #endif @@ -409,7 +409,7 @@ class app : public IApp, public ah::Scheduler { void notAvailChanged(void); HmSystemType mSys; - HmRadio<> mNrfRadio; + NrfRadio<> mNrfRadio; Communication mCommunication; bool mShowRebootRequest = false; diff --git a/src/hm/hmRadio.h b/src/hm/NrfRadio.h similarity index 92% rename from src/hm/hmRadio.h rename to src/hm/NrfRadio.h index f187d4fa..2b228fb6 100644 --- a/src/hm/hmRadio.h +++ b/src/hm/NrfRadio.h @@ -8,8 +8,9 @@ #include #include "SPI.h" -#include "radio.h" +#include "Radio.h" #include "../config/config.h" +#include "../config/settings.h" #if defined(SPI_HAL) #include "nrfHal.h" #endif @@ -28,10 +29,10 @@ const char* const rf24AmpPowerNames[] = {"MIN", "LOW", "HIGH", "MAX"}; //----------------------------------------------------------------------------- // HM Radio class //----------------------------------------------------------------------------- -template -class HmRadio : public Radio { +template +class NrfRadio : public Radio { public: - HmRadio() { + NrfRadio() { mDtuSn = DTU_SN; mIrqRcvd = false; #if defined(SPI_HAL) @@ -40,12 +41,18 @@ class HmRadio : public Radio { mNrf24.reset(new RF24(CE_PIN, CS_PIN, SPI_SPEED)); #endif } - ~HmRadio() {} + ~NrfRadio() {} - void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN, uint8_t sclk = SCLK_PIN, uint8_t mosi = MOSI_PIN, uint8_t miso = MISO_PIN) { - DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup")); + void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgNrf24_t *cfg) { + DPRINTLN(DBG_VERBOSE, F("NrfRadio::setup")); - pinMode(irq, INPUT_PULLUP); + mCfg = cfg; + //uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN, uint8_t sclk = SCLK_PIN, uint8_t mosi = MOSI_PIN, uint8_t miso = MISO_PIN + + if(!mCfg->enabled) + return; + + pinMode(mCfg->pinIrq, INPUT_PULLUP); mSerialDebug = serialDebug; mPrivacyMode = privacyMode; @@ -56,7 +63,7 @@ class HmRadio : public Radio { #ifdef ESP32 #if defined(SPI_HAL) - mNrfHal.init(mosi, miso, sclk, cs, ce, SPI_SPEED); + mNrfHal.init(mCfg->pinMosi, mCfg->pinMiso, mCfg->pinSclk, mCfg->pinCs, mCfg->pinCe, SPI_SPEED); mNrf24.reset(new RF24(&mNrfHal)); #else #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 @@ -64,7 +71,7 @@ class HmRadio : public Radio { #else mSpi.reset(new SPIClass(VSPI)); #endif - mSpi->begin(sclk, miso, mosi, cs); + mSpi->begin(mCfg->pinSclk, mCfg->pinMiso, mCfg->pinMosi, mCfg->pinCs); #endif #else //the old ESP82xx cannot freely place their SPI pins @@ -75,7 +82,7 @@ class HmRadio : public Radio { #if defined(SPI_HAL) mNrf24->begin(); #else - mNrf24->begin(mSpi.get(), ce, cs); + mNrf24->begin(mSpi.get(), mCfg->pinCe, mCfg->pinCs); #endif mNrf24->setRetries(3, 15); // wait 3*250 = 750us, 16 * 250us -> 4000us = 4ms @@ -99,21 +106,24 @@ class HmRadio : public Radio { } // returns true if communication is active - bool loop(void) override { + void loop(void) { + if(!mCfg->enabled) + return; + if (!mIrqRcvd && !mNRFisInRX) - return false; // first quick check => nothing to do at all here + return; // first quick check => nothing to do at all here if(NULL == mLastIv) // prevent reading on NULL object! - return false; + return; if(!mIrqRcvd) { // no news from nRF, check timers if ((millis() - mTimeslotStart) < innerLoopTimeout) - return true; // nothing to do, still waiting + return; // nothing to do, still waiting if (mRadioWaitTime.isTimeout()) { // timeout reached! mNRFisInRX = false; rx_ready = false; - return false; + return; } // otherwise switch to next RX channel @@ -132,7 +142,7 @@ class HmRadio : public Radio { mNrf24->setChannel(mRfChLst[tempRxChIdx]); isRxInit = false; - return true; // communicating, but changed RX channel + return; // communicating, but changed RX channel } else { // here we got news from the nRF mIrqRcvd = false; @@ -145,7 +155,7 @@ class HmRadio : public Radio { if(mNRFisInRX) { DPRINTLN(DBG_WARN, F("unexpected tx irq!")); - return false; + return; } mNRFisInRX = true; @@ -181,18 +191,23 @@ class HmRadio : public Radio { } } rx_ready = false; // reset - return mNRFisInRX; + return; } } - return false; + return; } bool isChipConnected(void) const override { + if(!mCfg->enabled) + return false; return mNrf24->isChipConnected(); } void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override { + if(!mCfg->enabled) + return; + DPRINT_IVID(DBG_INFO, iv->id); DBGPRINT(F("sendControlPacket cmd: ")); DBGHEXLN(cmd); @@ -279,13 +294,14 @@ class HmRadio : public Radio { } uint8_t getDataRate(void) const { - if(!mNrf24->isChipConnected()) + if(!isChipConnected()) return 3; // unknown return mNrf24->getDataRate(); } bool isPVariant(void) const { - return mNrf24->isPVariant(); + if(!isChipConnected()) + return mNrf24->isPVariant(); } private: @@ -413,6 +429,7 @@ class HmRadio : public Radio { } uint64_t mDtuRadioId = 0ULL; + cfgNrf24_t *mCfg = nullptr; const uint8_t mRfChLst[RF_CHANNELS] = {03, 23, 40, 61, 75}; // channel List:2403, 2423, 2440, 2461, 2475MHz uint8_t mTxChIdx = 0; uint8_t mRxChIdx = 0; diff --git a/src/hm/hmInverter.h b/src/hm/hmInverter.h index 1d7e6620..9053c19f 100644 --- a/src/hm/hmInverter.h +++ b/src/hm/hmInverter.h @@ -22,7 +22,7 @@ #include #include "../config/settings.h" -#include "radio.h" +#include "Radio.h" /** * For values which are of interest and not transmitted by the inverter can be * calculated automatically. diff --git a/src/hm/radio.h b/src/hm/radio.h index 31643980..12e80850 100644 --- a/src/hm/radio.h +++ b/src/hm/radio.h @@ -33,7 +33,7 @@ class Radio { virtual uint16_t getBaseFreqMhz() { return 0; } virtual uint16_t getBootFreqMhz() { return 0; } virtual std::pair getFreqRangeMhz(void) { return std::make_pair(0, 0); } - virtual bool loop(void) = 0; + virtual void loop(void) = 0; Radio() : mTxBuf{} {} diff --git a/src/hms/cmt2300a.h b/src/hms/cmt2300a.h index 23911b15..ed3aab54 100644 --- a/src/hms/cmt2300a.h +++ b/src/hms/cmt2300a.h @@ -6,7 +6,7 @@ #ifndef __CMT2300A_H__ #define __CMT2300A_H__ -#if defined(CONFIG_IDF_TARGET_ESP32S3) && defined(SPI_HAL) +#if defined(SPI_HAL) #include "cmtHal.h" #else #include "esp32_3wSpi.h" @@ -545,7 +545,7 @@ class Cmt2300a { } private: - #if defined(CONFIG_IDF_TARGET_ESP32S3) && defined(SPI_HAL) + #if defined(SPI_HAL) cmtHal mSpi; #else esp32_3wSpi mSpi; diff --git a/src/hms/hmsRadio.h b/src/hms/hmsRadio.h index 54975197..4b043ae2 100644 --- a/src/hms/hmsRadio.h +++ b/src/hms/hmsRadio.h @@ -7,7 +7,7 @@ #define __HMS_RADIO_H__ #include "cmt2300a.h" -#include "../hm/radio.h" +#include "../hm/Radio.h" //#define CMT_SWITCH_CHANNEL_CYCLE 5 @@ -24,16 +24,16 @@ class CmtRadio : public Radio { mTxBuf.fill(0); } - bool loop() override { + void loop() override { mCmt.loop(); if((!mIrqRcvd) && (!mRqstGetRx)) - return false; + return; getRx(); if(CmtStatus::SUCCESS == mCmt.goRx()) { mIrqRcvd = false; mRqstGetRx = false; } - return false; + return; } bool isChipConnected(void) const override { diff --git a/src/plugins/Display/Display.h b/src/plugins/Display/Display.h index e263d667..0ce1522c 100644 --- a/src/plugins/Display/Display.h +++ b/src/plugins/Display/Display.h @@ -7,7 +7,7 @@ #include #include "../../hm/hmSystem.h" -#include "../../hm/hmRadio.h" +#include "../../hm/NrfRadio.h" #include "../../utils/helper.h" #include "../plugin_lang.h" #include "Display_Mono.h" @@ -25,9 +25,9 @@ class Display { mMono = NULL; } - void setup(IApp *app, display_t *cfg, HMSYSTEM *sys, RADIO *hmradio, RADIO *hmsradio, uint32_t *utcTs) { + void setup(IApp *app, display_t *cfg, HMSYSTEM *sys, RADIO *nrfRadio, RADIO *hmsradio, uint32_t *utcTs) { mApp = app; - mHmRadio = hmradio; + mNrfRadio = nrfRadio; mHmsRadio = hmsradio; mCfg = cfg; mSys = sys; @@ -149,7 +149,7 @@ class Display { mDisplayData.totalYieldDay = totalYieldDay; mDisplayData.totalYieldTotal = totalYieldTotal; bool nrf_en = mApp->getNrfEnabled(); - bool nrf_ok = nrf_en && mHmRadio->isChipConnected(); + bool nrf_ok = nrf_en && mNrfRadio->isChipConnected(); #if defined(ESP32) bool cmt_en = mApp->getCmtEnabled(); bool cmt_ok = cmt_en && mHmsRadio->isChipConnected(); @@ -231,7 +231,7 @@ class Display { uint32_t *mUtcTs = nullptr; display_t *mCfg = nullptr; HMSYSTEM *mSys = nullptr; - RADIO *mHmRadio = nullptr; + RADIO *mNrfRadio = nullptr; RADIO *mHmsRadio = nullptr; uint16_t mRefreshCycle = 0; diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 665a3333..fdc54fa4 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -40,7 +40,7 @@ class RestApi { mApp = app; mSrv = srv; mSys = sys; - mRadioNrf = (HmRadio<>*)mApp->getRadioObj(true); + mRadioNrf = (NrfRadio<>*)mApp->getRadioObj(true); #if defined(ESP32) mRadioCmt = (CmtRadio<>*)mApp->getRadioObj(false); #endif @@ -1112,7 +1112,7 @@ class RestApi { private: IApp *mApp = nullptr; HMSYSTEM *mSys = nullptr; - HmRadio<> *mRadioNrf = nullptr; + NrfRadio<> *mRadioNrf = nullptr; #if defined(ESP32) CmtRadio<> *mRadioCmt = nullptr; #endif