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

Reduce the number of bytes over SPI for MCP2515Class::parsePacket() by up to 2.8x #46

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

Conversation

timurrrr
Copy link

@timurrrr timurrrr commented Jul 31, 2020

  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!)

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!)
@timurrrr
Copy link
Author

timurrrr commented Aug 4, 2020

Came up with an even better idea, force-pushed an update into the branch so you can avoid looking at the less optimal intermediate solution.

@timurrrr timurrrr changed the title Reduce the number of bytes over SPI for MCP2515Class::parsePacket() by up to 60% Reduce the number of bytes over SPI for MCP2515Class::parsePacket() by up to 2.8x Aug 4, 2020
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