FlexCAN_T4 and when to use mailboxes

nicedream

New member
I'm trying to use a Teensy 4.1 to relay CAN bus messages to my car's speedometer, while modifying certain specific messages for speedometer calibration.

I wanted to start off with a very simple setup of just plain forwarding with no changes to the data, so I used the BiDirectionalForward.ino example that is included in the FlexCAN_T4 library. It worked just fine, so I added a bit of data manipulation to it to see if I could block my car's upshift indicator light by zeroing out a few bits. That worked with no problem as well:

Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
CAN_message_t msg;

void setup(void) {
  can1.begin();
  can1.setBaudRate(500000);
  can2.begin();
  can2.setBaudRate(500000);
}

void loop() {
  if ( can1.read(msg) ) {
    
    if (msg.id == 0x18F) {
      msg.buf[6] &= 0xC0;
    }
    
    can2.write(msg);
  }
  else if ( can2.read(msg) ) {
    can1.write(msg);
  }
}

As you can see, there is very little changed from the BiDirectionalForward.ino example. I just added a check for message ID 0x18F, and apply a bitmask to one of the buffers when found.

I would imagine that I could add similar statements to the loop() in order to apply a ratio to the speedometer data, but I don't know if this is the "best" way to do it. The main reason is because in most of the FlexCAN_T4 code I see, people are using the mailboxes and/or FIFO features of the library, and I'm really not sure exactly how or why these are used.

I've done some programming in my life, although my skillset now is in the database realm. So I think I'm a reasonably technical person. I looked on this forum and on the FlexCAN_T4 github site for a basic explanation of how the mailboxes work or when they are useful, but from what I've seen they all seem to assume with an existing familiarity of the concept.

  • Do mailboxes enable more efficient processing (in other words, something more intelligent than just constantly looping through looking for a message to process)?
  • Do they offer more robust message handling (preventing a message from being dropped for whatever reason)?
  • For my purposes, is there any reason I would not want to use the very simple method of CAN message modification and relaying in my example above?
 
Back
Top