Skip to content

Commit

Permalink
Optimize MCP2515Class::parsePacket()
Browse files Browse the repository at this point in the history
1) Use the RX STATUS command instead of READ(CANINTF) to check availability of
   data to read. This reduces SPI usage for a simple "is data available" check
   from 3 bytes to 2.

2) Use the READ RX BUFFER command to read the RXFn* registers as well as the
   received data. This requires doing only a single CS pull, and only N+6 bytes
   transferred over SPI. READ RX BUFFER also takes care of resetting the RXnIF
   flag.

If my math is right, the old code needed N+6 readRegister() calls for a standard
frame, and N+9 readRegister() calls for an extended frame, plus one
modifyRegister() call to reset the RXnIF flag. Each readRegister() call requires
a CS pull and transferring 3 bytes over SPI.

For N = 8,
- we now send 16 bytes over SPI vs 45 for a standard frame (>2.8x reduction!)
- we now do just 2 CS pulls vs 16 for a standard frame (8x reduction!)
  • Loading branch information
timurrrr committed Aug 4, 2020
1 parent bcdd7ac commit 75e91ef
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions src/MCP2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,49 +212,73 @@ int MCP2515Class::endPacket()

int MCP2515Class::parsePacket()
{
int n;

uint8_t intf = readRegister(REG_CANINTF);
SPI.beginTransaction(_spiSettings);
digitalWrite(_csPin, LOW);
SPI.transfer(0xb0); // RX STATUS
uint8_t rxStatus = SPI.transfer(0x00);
digitalWrite(_csPin, HIGH);
SPI.endTransaction();

if (intf & FLAG_RXnIF(0)) {
int n;
if (rxStatus & 0x40) {
n = 0;
} else if (intf & FLAG_RXnIF(1)) {
} else if (rxStatus & 0x80) {
n = 1;
} else {
_rxId = -1;
_rxExtended = false;
_rxRtr = false;
_rxDlc = 0;
_rxIndex = 0;
_rxLength = 0;
return 0;
}

_rxExtended = (readRegister(REG_RXBnSIDL(n)) & FLAG_IDE) ? true : false;

uint32_t idA = ((readRegister(REG_RXBnSIDH(n)) << 3) & 0x07f8) | ((readRegister(REG_RXBnSIDL(n)) >> 5) & 0x07);
SPI.beginTransaction(_spiSettings);
digitalWrite(_csPin, LOW);
// Send READ RX BUFFER instruction for RXBnSIDH(n).
SPI.transfer(0b10010000 | (n * 0x04));
uint8_t regSIDH = SPI.transfer(0x00);
uint8_t regSIDL = SPI.transfer(0x00);
_rxExtended = (regSIDL & FLAG_IDE) ? true : false;

// We could just skip the extended registers for standard frames, but that
// would actually add more overhead, and increase complexity.
uint8_t regEID8 = SPI.transfer(0x00);
uint8_t regEID0 = SPI.transfer(0x00);
uint8_t regDLC = SPI.transfer(0x00);
uint32_t idA = (regSIDH << 3) | (regSIDL >> 5);
if (_rxExtended) {
uint32_t idB = (((uint32_t)(readRegister(REG_RXBnSIDL(n)) & 0x03) << 16) & 0x30000) | ((readRegister(REG_RXBnEID8(n)) << 8) & 0xff00) | readRegister(REG_RXBnEID0(n));
uint32_t idB =
((uint32_t)(regSIDL & 0x03) << 16)
| ((uint32_t)regEID8 << 8)
| regEID0;

_rxId = (idA << 18) | idB;
_rxRtr = (readRegister(REG_RXBnDLC(n)) & FLAG_RTR) ? true : false;
_rxRtr = (regDLC & FLAG_RTR) ? true : false;
} else {
_rxId = idA;
_rxRtr = (readRegister(REG_RXBnSIDL(n)) & FLAG_SRR) ? true : false;
_rxRtr = (regSIDL & FLAG_SRR) ? true : false;
}
_rxDlc = readRegister(REG_RXBnDLC(n)) & 0x0f;

_rxDlc = regDLC & 0x0f;
_rxIndex = 0;

if (_rxRtr) {
_rxLength = 0;
} else {
_rxLength = _rxDlc;

for (int i = 0; i < _rxLength; i++) {
_rxData[i] = readRegister(REG_RXBnD0(n) + i);
// Get the data.
for (uint8_t i = 0; i < _rxLength; i++) {
_rxData[i] = SPI.transfer(0x00);
}
}

modifyRegister(REG_CANINTF, FLAG_RXnIF(n), 0x00);

// Don't need to unset the RXnIF(n) flag as this is done automatically when
// setting the CS high after a READ RX BUFFER instruction.
digitalWrite(_csPin, HIGH);
SPI.endTransaction();
return _rxDlc;
}

Expand Down

0 comments on commit 75e91ef

Please sign in to comment.