Skip to content

Commit

Permalink
* added RX channel 40
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
lumapu committed Jun 2, 2022
1 parent 247ae3e commit c7d039a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
9 changes: 8 additions & 1 deletion tools/esp8266/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -187,6 +189,7 @@ void app::loop(void) {
}

mSys->BufCtrl.popBack();
yield();
}


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -367,6 +372,7 @@ void app::processPayload(bool retransmit) {
}
}
}
yield();
}
}

Expand Down Expand Up @@ -629,6 +635,7 @@ void app::showLiveData(void) {
}
}
modHtml += "</div>";
yield();
}
modHtml += F("<div class=\"ts\">Last received data requested at: ") + getDateTimeStr(iv->ts) + F("</div>");
modHtml += F("</div>");
Expand Down
2 changes: 1 addition & 1 deletion tools/esp8266/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tools/esp8266/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 4
#define VERSION_PATCH 13
#define VERSION_PATCH 14


//-------------------------------------
Expand Down
64 changes: 38 additions & 26 deletions tools/esp8266/hmRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"};

Expand Down Expand Up @@ -55,6 +55,7 @@ class HmRadio {
mRxChLst[1] = 23;
mRxChLst[2] = 61;
mRxChLst[3] = 75;
mRxChLst[4] = 40;
mRxChIdx = 0;
mRxLoopCnt = RX_LOOP_CNT;

Expand All @@ -66,6 +67,7 @@ class HmRadio {
mSendCnt = 0;

mSerialDebug = false;
mIrqRcvd = false;
}
~HmRadio() {}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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--;
Expand Down Expand Up @@ -263,7 +273,7 @@ class HmRadio {
}

uint8_t getRxNxtChannel(void) {
if(++mRxChIdx >= 4)
if(++mRxChIdx >= 5)
mRxChIdx = 0;
return mRxChLst[mRxChIdx];
}
Expand All @@ -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__*/
4 changes: 2 additions & 2 deletions tools/esp8266/hmSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tools/esp8266/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ void Main::loop(void) {
}
}

if(++mHeapStatCnt >= 10) {
/*if(++mHeapStatCnt >= 10) {
mHeapStatCnt = 0;
stats();
}
}*/
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/esp8266/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class mqtt {

if(!mClient->connected())
reconnect();
mClient->publish(top, msg);
if(mClient->connected())
mClient->publish(top, msg);
}
}

Expand Down

4 comments on commit c7d039a

@stefan123t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lumapu, Klasse die Stelle hatte ich gestern abend auch überdacht, das ist ja jetzt wie aus dem Lehrbuch!
Im Interrupt Handler wird nur das als volatile bool mIrqRcvd; deklarierte Flag gesetzt und der mSys->Radio.loop(); in der app priorisiert.
Den InterruptStatus mNrf24.whatHappened im Handler sichern und erst danach mit mNrf24.available die Daten einsammeln.
Dadurch bleibt die Zeit ohne Interrupts zwischen DISABLE_IRQ; und RESTORE_IRQ; kürzer.
Und das m.E. überflüssige Setzen auf "valide" Werte mit memset(p->packet, 0xcc, MAX_RF_PAYLOAD_SIZE); ist auch weg.
Man könnte meinen Du hast gestern auch noch mal Embed mit Elliot gelesen 😄

@stefan123t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ach ja das MQTT Connect Problem hast Du auch gleich gelöst 🎩 Chapeau!
Vielleicht kannst Du noch was zu den folgenden beiden Anpassungen sagen:

  • #define RX_LOOP_CNT 600 ist das um längere Payloads, u.a. für den HM-1500 zu empfangen ?
  • mSys->Radio.switchRxCh(300); das ist m.W. nach der Dokumentation der RF24 Bibliothek das empfohlene Maximum ?

@lumapu
Copy link
Owner Author

@lumapu lumapu commented on c7d039a Jun 2, 2022 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lumapu
Copy link
Owner Author

@lumapu lumapu commented on c7d039a Jun 2, 2022 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.