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
195 stars 65 forks source link

Issus with filters and mailboxes #8

Closed robinhellstrom closed 3 years ago

robinhellstrom commented 4 years ago

I've having trouble getting CAN-messages through the mailboxes that I try to filter out messages with.

Maybe you could clarify in the README.md more about how the thoughts are on filters and mailboxes.

When I set up the code with this settings then will I only get msg 256 on CAN1 and nothing on CAN2 or CAN3:

#include <FlexCAN_T4.h>

// CAN creation
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> CAN_1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> CAN_2;
FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> CAN_3;

// CAN-messages (1&2)
static CAN_message_t TX_210;
static CAN_message_t TX_256;
static CAN_message_t TX_500;

// CAN1-messages
static CAN_message_t TX_310;
static CAN_message_t TX_704;
static CAN_message_t TX_705;
static CAN_message_t TX_260;
static CAN_message_t TX_261;

// CAN2-messages
static CAN_message_t TX_122; 

// CAN3-messages
static CAN_message_t TX_021204B8;
static CAN_message_t TX_03C3F7FC;
static CAN_message_t TX_09C050B8;
static CAN_message_t TX_0E03D7F8;
static CAN_message_t TX_0EE0500A;
static CAN_message_t TX_10500048; 
static CAN_message_t TX_13F041BA; 
static CAN_message_t TX_15B04028;
static CAN_message_t TX_16801028;
static CAN_message_t TX_1760E008;
static CAN_message_t TX_19500008;
static CAN_message_t TX_19F010FC;
static CAN_message_t TX_1093F7FC;
static CAN_message_t TX_1163F7FC;
static CAN_message_t TX_000FFFFE;

setup() {

    // Configure CAN interfaces
    CAN_1.begin();
    CAN_1.setBaudRate(500000);
    CAN_1.setMBFilter(REJECT_ALL);
    CAN_1.setMBFilter(MB0, 0x255);
    CAN_1.setMBFilter(MB1, 0x258);
    CAN_1.setMBFilter(MB2, 0x259);
    CAN_1.setMBFilter(MB3, 0x26A);
    CAN_1.setMBFilter(MB4, 0x300);
    CAN_1.setMBFilter(MB5, 0x301);
    CAN_1.setMBFilter(MB6, 0x302);
    CAN_1.setMBFilter(MB7, 0x501);
    CAN_1.setMBFilter(MB8, 0x502);
    CAN_1.setMBFilter(MB9, 0x710);
    CAN_1.enhanceFilter(MB0);
    CAN_1.enhanceFilter(MB1);
    CAN_1.enhanceFilter(MB2);
    CAN_1.enhanceFilter(MB3);
    CAN_1.enhanceFilter(MB4);
    CAN_1.enhanceFilter(MB5);
    CAN_1.enhanceFilter(MB6);
    CAN_1.enhanceFilter(MB7);
    CAN_1.enhanceFilter(MB8);
    CAN_1.enhanceFilter(MB9);

    CAN_2.begin();
    CAN_2.setBaudRate(500000);
    CAN_2.setMBFilter(REJECT_ALL);
    CAN_2.setMBFilter(MB0, 0x250, STD);
    CAN_2.setMBFilter(MB1, 0x255, STD);
    CAN_2.setMBFilter(MB2, 0x258, STD);
    CAN_2.setMBFilter(MB3, 0x259, STD);
    CAN_2.setMBFilter(MB4, 0x501, STD);
    CAN_2.setMBFilter(MB5, 0x502, STD);
    CAN_2.enhanceFilter(MB0);
    CAN_2.enhanceFilter(MB1);
    CAN_2.enhanceFilter(MB2);
    CAN_2.enhanceFilter(MB3);
    CAN_2.enhanceFilter(MB4);
    CAN_2.enhanceFilter(MB5);

    CAN_3.begin();
    CAN_3.setBaudRate(125000);
    CAN_3.setMB(MB20, TX);
    CAN_3.setMB(MB21, TX);
    CAN_3.setMB(MB22, TX);
    CAN_3.setMB(MB23, TX);
    CAN_3.setMB(MB24, TX);
    CAN_3.setMB(MB25, TX);
    CAN_3.setMB(MB26, TX);
    CAN_3.setMB(MB27, TX);
    CAN_3.setMB(MB28, TX);
    CAN_3.setMB(MB29, TX);
    CAN_3.setMB(MB30, TX);
    CAN_3.setMB(MB31, TX);
    CAN_3.setMB(MB32, TX);
    CAN_3.setMB(MB33, TX);
    CAN_3.setMB(MB34, TX);
    CAN_3.setMB(MB35, TX);
    CAN_3.setMB(MB36, TX);
    CAN_3.setMBFilter(REJECT_ALL);
    CAN_3.setMBFilter(MB4, 0x0C505226, EXT);    // MB4-7 are extended frame
    CAN_3.setMBFilter(MB5, 0x1EF00066, EXT);
    CAN_3.setMBFilter(MB6, 0x1F200002, EXT);
    CAN_3.enhanceFilter(MB4);
    CAN_3.enhanceFilter(MB5);
    CAN_3.enhanceFilter(MB6);
}

void checkCAN() {

  CAN_message_t msg;

  if (CAN_1.read(msg)) {
    Serial.print("CAN 1 : ");
    Serial.println(msg.id, HEX);
    switch (msg.id) {
      case 0x258:
        //Do stuff...
        break;
      case 0x259:
        //Do stuff...
        break;
      case 0x255:
        //Do stuff...
        break;
      case 0x26A:
        //Do stuff...
        break;
      case 0x300:
        //Do stuff...
        break;
      case 0x301:
        //Do stuff...
        break;
      case 0x302:
        //Do stuff...
        break;
      case 0x501:
        //Do stuff...
        break;
      case 0x502:
        //Do stuff...
        break;
      case 0x710:
        //Do stuff...
        break;

      default:
        break;
    }
  }

  if (CAN_2.read(msg)) {
    Serial.print("CAN 2 : ");
    Serial.println(msg.id, HEX);
    switch (msg.id) {
      case 0x250:
        //Do stuff...
        break;
      case 0x258:
        //Do stuff...
        break;
      case 0x259:
        //Do stuff...
        break;
      case 0x255:
        //Do stuff...
        break;
      case 0x501:
        //Do stuff...
        break;
      case 0x502:
        //Do stuff...
        break;
      default:
        break;

    }
  }

  if (CAN_3.read(msg)) {
    Serial.print("CAN 3 : ");
    Serial.println(msg.id, HEX);
    switch (msg.id) {
      case 0x1EF00066:
        //Do stuff...
        break;
      default:
        break;

    }
  }
}

void loop() {
    checkCAN();
}

When I change the seup to this do I get all the messages:

setup() {

    // Configure CAN interfaces
    CAN_1.begin();
    CAN_1.setBaudRate(500000);
    CAN_1.setMBFilter(ACCEPT_ALL);
    .......
tonton81 commented 4 years ago

All messages from CAN1 you mean? Or all 3 busses? Why are you using filter range for CAN1 then overriding it with a regular filter? Can you try one or the other? You can also use msg.mb to identify which mailbox the message came from

robinhellstrom commented 4 years ago

@tonton81 there was a typo, I'm not using FilterRange at the same time as regular filters. All CAN1 traffic except 0x258 disappears when using the filters above.

Are there any special MB:s that don't work with standard frame messages?

tonton81 commented 4 years ago

Not that i am aware of. All busses use the same code, all controllers should respond the same. You can print out the mailbox info with mailboxStatus() to confirm mailboxes should be all set to standard and not extended, else the filtering depends on that flag

Also try without enhancements, although i highly think theyre not affecting it, you are filtering a single ID, so technically you don't need to enhance it as only one frame will pass that filter, hardware blocks everything else, then the SMB would attempt to send the other frame to another mailbox that actually accepts it.

Also curious, did you try having ONLY CAN1 enabled in your sketch without the other ones enabled?

Also make sure you are using the current github copy, last updated Feb 23

Also, for: CAN_2.setMBFilter(MB2, 0x258, STD); Only use STD for setMB, else it acts as a 2nd ID value for the mailbox

robinhellstrom commented 4 years ago

I'll give your suggestions a go tomorrow, do I command the mailboxStatus() after I've initiated Serial.begin() in the setup()-loop?

tonton81 commented 4 years ago

After yes, or whenever you use setMB, so you can see the mailbox layout which are TX, RX STD, and RX EXT

Also, be sure those CANIDs are standard, they can exist with extended frames as well. Example, 0x300 could be an EXTENDED frame as well, and by accepting ALL, it gets collected in the extended mailboxes, and when filtering for standard, you stop receiving it. A data log of ACCEPT_ALL showing msg.flags.extended showing 0 or 1 would be good confirmation of this to make sure

0 == STANDARD, 1 == EXTENDED