tonton81 / FlexCAN_T4

FlexCAN (CAN 2.0 / CANFD) Library for Teensy 3.x and 4.0
https://forum.pjrc.com/threads/56035-FlexCAN_T4-FlexCAN-for-Teensy-4
MIT License
196 stars 66 forks source link

Filtering make messages disapear #65

Open Concombre74 opened 1 year ago

Concombre74 commented 1 year ago

I'm trying to use enhanced filtering on a teensy 4.1. The following code, that I use is basicaly your exemple mailbox_filtering_example_with_interrupts. I have juste changed the Can, Mailbox, and ID's for the ones I use :

#include <FlexCAN_T4.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can0;

#define NUM_TX_MAILBOXES 2
#define NUM_RX_MAILBOXES 6
void setup(void) {
  Serial.begin(115200); delay(400);
  Can0.begin();
  Can0.setBaudRate(250000);
  Can0.setMaxMB(NUM_TX_MAILBOXES + NUM_RX_MAILBOXES);
  for (int i = 0; i<NUM_RX_MAILBOXES; i++){
    Can0.setMB((FLEXCAN_MAILBOX)i,RX,EXT);
  }
  for (int i = NUM_RX_MAILBOXES; i<(NUM_TX_MAILBOXES + NUM_RX_MAILBOXES); i++){
    Can0.setMB((FLEXCAN_MAILBOX)i,TX,EXT);
  }
  Can0.setMBFilter(REJECT_ALL);
  Can0.enableMBInterrupts();
  Can0.onReceive(MB0,canSniff);
  Can0.onReceive(MB4,canSniff);

  Can0.setMBFilter(MB0, 0x10AAFFFE);
  Can0.enhanceFilter(MB0);
  Can0.setMBFilter(MB4, 0x10EAFFFE);
  Can0.enhanceFilter(MB4);
  Can0.mailboxStatus();
}

void canSniff(const CAN_message_t &msg) {
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print("  OVERRUN: "); Serial.print(msg.flags.overrun);
  Serial.print("  LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  Serial.print(" TS: "); Serial.print(msg.timestamp);
  Serial.print(" ID: "); Serial.print(msg.id, HEX);
  Serial.print(" Buffer: ");
  for ( uint8_t i = 0; i < msg.len; i++ ) {
    Serial.print(msg.buf[i], HEX); Serial.print(" ");
  } Serial.println();
}

void loop() {
  Can0.events();
}

As explained in your ReadMe : "Lets say for example you want a mailbox to store IDs 0x1 and 0x5: myCan.setMBFilter(MB6, 0x1, 0x5); You will notice that not only frames 0x1 and 0x5 are received, but also 0x3 and 0x7. This is because the 2 out of 3 bits are different between both IDs. Once you enable: myCan.enhanceFilter(MB6);"

If I didn't use "Can0.enhanceFilter(MB0);" Both messages with ID 0x10AAFFFE and 0x10EAFFFE appears in MB0. If i use it, only message with ID 0x10AAFFFE pop's up, but then the message with ID 0x10EAFFFE is simply lost. it never appears in MB4.

Is there a way to redirect it to the good mailbox without modifying the filtering and mailbox system I use?

tonton81 commented 1 year ago

MB0 should only receive one frame if you have one ID filtered, if no filter try distribute(), so it copies it to another valid callback that accepts it. did you turn fifo off? whats the output of mailboxStatus()

Concombre74 commented 1 year ago

Turning fifo off didnt' change anything but it seams that using distribute() works. Due to the resemblances of the 2 ID's I use filtering made MB0 accept the wrong message in addition of the good one.