Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize MCP2515Class::endPacket() by up to 2x #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

timurrrr
Copy link

@timurrrr timurrrr commented Aug 5, 2020

Optimize MCP2515Class::endPacket()

  1. Use LOAD TX BUFFER instruction to set TXBn* registers sequentially, as well
    as to write into the TX data registers.
    This is done with a single CS pull, and requires sending N+6 bytes over SPI.
    The old code was doing N+5 writeRegister() calls, each doing 1 CS pull and
    sending 3 bytes.

  2. Use the 1-byte RTS SPI instruction instead of WRITEing to TXBnCTRL.
    This reduces the number of bytes sent over SPI by 2.

For N = 8,

  • we now send 15 bytes over SPI before the while(...TXBnCTRL...) loop vs 42
    (2.8x reduction)
  • we do just 2 CS pulls before the while(...TXBnCTRL...) loop vs 13
    (6.5x reduction)

We still do at least 3 CS pulls and send at least 10 bytes over SPI for the
condition of the while(...TXBnCTRL...) loop, to clear TXnIF and to check for
errors.

That means for N = 8, in the case when we do zero iterations of the while loop,
we end up sending 25 bytes over SPI instead of 52, which is >2x reduction.

1) Use LOAD TX BUFFER instruction to set TXBn* registers sequentially, as well
   as to write into the TX data registers.
   This is done with a single CS pull, and requires sending N+6 bytes over SPI.
   The old code was doing N+5 writeRegister() calls, each doing 1 CS pull and
   sending 3 bytes.

2) Use the 1-byte RTS SPI instruction instead of WRITEing to TXBnCTRL.
   This reduces the number of bytes sent over SPI by 2.

For N = 8,
- we now send 15 bytes over SPI before the while(...TXBnCTRL...) loop vs 42
  (2.8x reduction)
- we do just 2 CS pulls before the while(...TXBnCTRL...) loop vs 13
  (6.5x reduction)

We still do at least 3 CS pulls and send at least 10 bytes over SPI for the
condition of the while(...TXBnCTRL...) loop, to clear TXnIF and to check for
errors.

That means for N = 8, in the case when we do zero iterations of the while loop,
we end up sending 25 bytes over SPI instead of 52, which is >2x reduction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant