Skip to content

Commit

Permalink
Interrupts: Reliabiity fixes
Browse files Browse the repository at this point in the history
- Fix: Crashes were happening due to bad handling of interrupts. This should be fixed in this update, testing ongoing
- Remove all calls to radio.maskIRQ();, reduces delays due to SPI transfers and improves response times
- Due to previous, re-write all interrupt handling to be done completely at the gateway layer via logic, not through radio masking or stopping/starting of the interrupt handler
- Add delays to interrupt handling routines to minimize CPU usage while maintaining performance
- Declare related interrupt variables as volatile
  • Loading branch information
TMRh20 committed Jul 27, 2020
1 parent aeee05b commit 3508adc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
31 changes: 15 additions & 16 deletions RF24Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,11 @@ int RF24Gateway::setIP( char *ip_addr, char *mask) {
/***************************************************************************************/
void RF24Gateway::interrupts(bool enable){
if(enable){
while(interruptInProgress){ delay(1); }
interruptsEnabled = enable;
radio.maskIRQ(1,1,0);
}else{
}else{
while(interruptInProgress){ usleep(100); }
interruptsEnabled = 0;
while(interruptInProgress){ delay(1); }
radio.maskIRQ(1,1,1);

while(interruptInProgress){ usleep(500); }
}
}

Expand All @@ -307,18 +304,17 @@ void RF24Gateway::interrupts(bool enable){
void RF24Gateway::update(bool interrupts){

if(interrupts){
interruptInProgress = 1;
uint32_t intTimer = millis();
while(!interruptsEnabled){
delay(1);
if(millis()-intTimer>100){ //Wait up to 100ms for interrupts to be re-enabled
usleep(750);
if(millis()-intTimer>1000){ //Wait up to 1s for interrupts to be re-enabled
interruptInProgress = 0;
return;
}
}
interruptInProgress = 1;
radio.maskIRQ(1,1,1);
handleRadioIn();
handleTX();
radio.maskIRQ(1,1,0);
interruptInProgress = 0;
}else{
handleRadioIn();
Expand All @@ -334,16 +330,19 @@ void RF24Gateway::poll(uint32_t waitDelay){

handleRX(waitDelay);

if(interruptInProgress){return;}
radio.maskIRQ(1,1,1);
while(interruptInProgress){ usleep(100); }
interruptsEnabled = 0;
while(interruptInProgress){ usleep(500); }

//gateway.poll() is called manually when using interrupts, so if the radio RX buffer is full, or interrupts have been missed, check for it here.
if(radio.rxFifoFull()){
fifoCleared=true;
handleRadioIn();
handleTX();
interruptsEnabled = 1;
return;
}
handleRadioOut();
radio.maskIRQ(1,1,0);
interruptsEnabled = 1;
}
/***************************************************************************************/

Expand Down Expand Up @@ -645,9 +644,9 @@ void RF24Gateway::handleRX(uint32_t waitDelay){
}
std::cout << std::endl;
#endif
rxQueue.pop();
}

rxQueue.pop();
}

/***************************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions RF24Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class RF24Gateway {
void handleRadioIn();
void handleRX(uint32_t waitDelay=0);
void handleTX();
bool interruptInProgress;
bool interruptsEnabled;
volatile bool interruptInProgress;
volatile bool interruptsEnabled;

int configDevice(uint16_t address);
int allocateTunDevice(char *dev, int flags, uint16_t address);
Expand Down

0 comments on commit 3508adc

Please # to comment.