Teensy 4.1, FlexCAN_T4, Filtering

Harry888

Member
Hello,

I my project I use FlexCAN_T4.

I would line FlexCAN_T4 to filter out all standard messages except 0x123 and 0x124.
But if I send another message, then 0x123 or 0x124 I receive them too.

How can I achieve that FlexCAN_T4 filter works.

Here is an example.
------------

FlexCAN_T4 <CAN1, RX_SIZE_256, TX_SIZE_16> Can1 = FlexCAN_T4 <CAN1, RX_SIZE_256, TX_SIZE_16>();
CanMsg rxMsg;

void setup(void)
{
Can1.begin();
Can1.setBaudRate(250000);

Can1.setFIFOFilter(REJECT_ALL);
Can1.enableFIFO();
Can1.setFIFOFilterRange(0, 0x123, 0x124, STD);
}

void loop()
{
if(Can1.read(rxMsg))
Serial.printf("Msg.ID 0x%06X\n", rxMsg.id);
}
 
Hello,

I my project I use FlexCAN_T4.

I would line FlexCAN_T4 to filter out all standard messages except 0x123 and 0x124.
But if I send another message, then 0x123 or 0x124 I receive them too.

How can I achieve that FlexCAN_T4 filter works.

Here is an example.
------------

FlexCAN_T4 <CAN1, RX_SIZE_256, TX_SIZE_16> Can1 = FlexCAN_T4 <CAN1, RX_SIZE_256, TX_SIZE_16>();
CanMsg rxMsg;

void setup(void)
{
Can1.begin();
Can1.setBaudRate(250000);

Can1.setFIFOFilter(REJECT_ALL);
Can1.enableFIFO();
Can1.setFIFOFilterRange(0, 0x123, 0x124, STD);
}

void loop()
{
if(Can1.read(rxMsg))
Serial.printf("Msg.ID 0x%06X\n", rxMsg.id);
}
I've never played with filter ranges. Only message ID filters. But I want to help you so this is my guess of what would work.
Can't guarantee anything.

Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
CAN_message_t rxMsg;

void setup(void)
{
  Serial.begin(115200);
  Can1.begin();
  Can1.setBaudRate(250000);

  // Reject all messages
  Can1.setFIFOFilter(REJECT_ALL);

  // Enable FIFO
  Can1.enableFIFO();

  // Allow only messages with IDs 0x123 and 0x124
  Can1.setFIFOFilter(0, 0x123, STD); // Filter for 0x123
  Can1.setFIFOFilter(1, 0x124, STD); // Filter for 0x124

  // Optionally: Set up interrupt or polling
}

void loop()
{
  // Polling the CAN bus for messages
  if (Can1.read(rxMsg))
  {
    Serial.printf("Msg.ID: 0x%03X\n", rxMsg.id);
  }
}
 
Hallo,
thank you for a quick response.

I have pasted the code in a new project. With PCAN-View I can send every MsgID I wish.
here is the output of the program.

Here is the program output

09:48:12.157 -> Msg.ID: 0x100
09:48:13.227 -> Msg.ID: 0x101
09:48:15.976 -> Msg.ID: 0x104
09:48:17.303 -> Msg.ID: 0x113
09:48:19.409 -> Msg.ID: 0x123
09:48:21.772 -> Msg.ID: 0x124
09:48:23.721 -> Msg.ID: 0x125
09:48:24.981 -> Msg.ID: 0x131
09:48:26.534 -> Msg.ID: 0x132

It seems like the filter is not working, or I do something wrong
 
I solved it.

First enable FIFO then SetFilters....

Here is the solution

#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
CAN_message_t rxMsg;

void setup(void)
{
Serial.begin(115200);
Can1.begin();
Can1.setBaudRate(250000);

// Enable FIFO
Can1.enableFIFO();

// Reject all messages
Can1.setFIFOFilter(REJECT_ALL);

// Allow only messages with IDs 0x123 and 0x124
Can1.setFIFOFilter(0, 0x123, STD); // Filter for 0x123
Can1.setFIFOFilter(1, 0x124, STD); // Filter for 0x124

// Optionally: Set up interrupt or polling
}

void loop()
{
// Polling the CAN bus for messages
if (Can1.read(rxMsg))
{
Serial.printf("Msg.ID: 0x%03X\n", rxMsg.id);
}
}
 
Back
Top