Seeed_Arduino_CAN icon indicating copy to clipboard operation
Seeed_Arduino_CAN copied to clipboard

MCP2515 - Inverted frame sending

Open andrejrcarvalho opened this issue 2 years ago • 2 comments

Bug description I'm using your library to send CAN frames to a network that implements the UAVCAN stack. In this stack, the frames should be sent in a correct order to be possible to decode the full message. When the network frequency is low, like 10KBs, the 3 buffers of the MCP2515 are used to send the frames. This lead to an inverted sent of the frames.

Example: I have 3 frames to send, and the frames are added to the queue with the following order:

  1. Frame 0
  2. Frame 1
  3. Frame 2

In the MCP2515, the buffers are populated with that frames and should lock like this:

Buffer 0 Buffer 1 Buffer 2
Frame 0 Frame 1 Frame 2

When the MCP2515 sends the frames, the output is inverted. According to the datasheet the MCP2515 sends first, the frame that are in the buffer with the higher number. Following that logic, the output should look like this:

  1. Frame 2
  2. Frame 1
  3. Frame 0

To prove this, I have made a test with 2 nodes, one sending the messages and other receiving then.

The network is working at 10KBs and to analyze the message I have used the Drone CAN GUI tool

The output is this:

08-03-2023 12h06m22s

And should be like this:

08-03-2023 12h14m52s

To make the result above I have inferted the for loop used to get the next available buffer:

byte mcp2515_can::mcp2515_getNextFreeTXBuf(byte* txbuf_n) {
    byte status = mcp2515_readStatus() & MCP_STAT_TX_PENDING_MASK;
    byte i;

    *txbuf_n = 0x00;

    if (status == MCP_STAT_TX_PENDING_MASK) {
        return MCP_ALLTXBUSY; 
    }

    for (i = MCP_N_TXBUFFERS - nReservedTx; i >= 0; i--) {//<<<<<----------  THIS LINE ( before iterate from 0 to 3 and now iterate from 3 to 0)
        if ((status & txStatusPendingFlag(i)) == 0) {
            *txbuf_n = txCtrlReg(i) + 1;
            mcp2515_modifyRegister(MCP_CANINTF, txIfFlag(i), 0);
            return MCP2515_OK;
        }
    }

    return MCP_ALLTXBUSY;
}

andrejrcarvalho avatar Mar 08 '23 12:03 andrejrcarvalho

PR?

techie66 avatar Aug 03 '23 17:08 techie66