Skip to content

Commit f01f9e4

Browse files
committed
Stabilize onReceive(...) callback handling
1 parent 304f577 commit f01f9e4

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/CAN.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,13 @@ void CANClass::onReceive(void(*callback)(int))
386386
pinMode(_irq, INPUT);
387387

388388
if (callback) {
389-
attachInterrupt(digitalPinToInterrupt(_irq), CANClass::onIntLow, LOW);
389+
SPI.usingInterrupt(digitalPinToInterrupt(_irq));
390+
attachInterrupt(digitalPinToInterrupt(_irq), CANClass::onInterrupt, LOW);
390391
} else {
391392
detachInterrupt(digitalPinToInterrupt(_irq));
393+
#ifdef SPI_HAS_NOTUSINGINTERRUPT
394+
SPI.notUsingInterrupt(digitalPinToInterrupt(_irq));
395+
#endif
392396
}
393397
}
394398

@@ -464,9 +468,13 @@ void CANClass::reset()
464468
delayMicroseconds(10);
465469
}
466470

467-
void CANClass::handleIntLow()
471+
void CANClass::handleInterrupt()
468472
{
469-
while (parsePacket() != 0) {
473+
if (readRegister(REG_CANINTF) == 0) {
474+
return;
475+
}
476+
477+
while (parsePacket()) {
470478
_onReceive(available());
471479
}
472480
}
@@ -515,9 +523,9 @@ void CANClass::writeRegister(uint8_t address, uint8_t value)
515523
digitalWrite(_ss, HIGH);
516524
}
517525

518-
void CANClass::onIntLow()
526+
void CANClass::onInterrupt()
519527
{
520-
CAN.handleIntLow();
528+
CAN.handleInterrupt();
521529
}
522530

523531
CANClass CAN;

src/CAN.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class CANClass : public Stream {
5353
private:
5454
void reset();
5555

56-
void handleIntLow();
56+
void handleInterrupt();
5757

5858
uint8_t readRegister(uint8_t address);
5959
void modifyRegister(uint8_t address, uint8_t mask, uint8_t value);
6060
void writeRegister(uint8_t address, uint8_t value);
6161

62-
static void onIntLow();
62+
static void onInterrupt();
6363

6464
private:
6565
SPISettings _spiSettings;

0 commit comments

Comments
 (0)