6 years, 8 months ago.

CAN filter from mbed API

It appears that the mbed API has acceptance filter support (at least for the LPC1549). documentation seems sparse on how to use this appropriately.

I have found examples like this

handle = can.filter(0x100, 0xff, CANStandard);

But many thread I find say it does not work and they have to work on a lower level (at least on the LPC1768/9) and use something like this.

h1 = can->filter(0x100, 0x700, CANStandard); // Accept standard messages with id 0x100-0x1FF

The LPC1549 (I believe the 11C24) have a different CAN peripheral than the LPC1768/9 so not much carries over on the low level.

Assuming filtering works on the LPC1549, how is it intended to be used? It appears that you create filters, then you run a "read" command for each filter you created (handle?) which searches the hardware buffer and returns a message if there is one that fits the filter. This is what I gather from the samples I can find using filters.

Is this correct usage?

Thanks

Hello Toyomasa Watarai, I see you assigned to this question. I was wondering if you had any updates?

posted by Travis Travelstead 08 Aug 2014

I have read more about the ID and masking when building the filters. I think I understand that well enough, it would still be nice to get some more information, but what I am still not sure of is how the filters should be implemented.

It appears you create message filter handles which I am guessing are sent to the peripheral in order to filter the incoming messages once they are created. It then appears use the read function (depending on API) to read from the buffer with the "handle" or filter passed with it so you only read the next available packet that passes that filter.

The best example I could find is using CMSIS, but is easy enough to read and compare to the mbed API.

can->reset(); // reset and clear all filters (accept all messages)
h1 = can->filter(0x100, 0x700, CANStandard); // Accept standard messages with id 0x100-0x1FF
h2 = can->filter(0x200, 0x7FF, CANStandard); // Accept standard message 0x200 as well
[...]
h2 = can->filter(0x300, 0x7FF, CANStandard, h2); // Accept standard message 0x300, overwrite 0x200
 
CANMessage msg = CANMessage();
int r = can.read(msg, h1); // Should only return messages with id 0x100-0x1FF
int r = can.read(msg, h2); // Should only return messages with id 0x300
int r = can.read(msg);     // Returns 0x100-0x1FF if pending, otherwise 0x300, otherwise any message

From this it appears I can make several "handles" and when reading I use them to filter the one or more messages I want to get during that reading.

Without testing this should work to read only the ID 270, correct?

handle = can.filter(0x270, 0x7FF, CANStandard);

CANMessage CANmes;

if(canBus.read(CANmes, handle)){
    //deal with message
}
posted by Travis Travelstead 11 Aug 2014

It's not working... :-(

posted by Shimon S. 20 Nov 2016

I'd be very interested to see an answer (I'm using LPC1768)...

posted by Chris Mabey 17 Oct 2017

It might be worth posting a new question to help get it noticed. I am surprised that the CAN interface does not have a clean

Here is a reference to somebody that coded their own way of adding filters. Looks a little complex, but at least it is a reference to a working method.

https://os.mbed.com/users/ym1784/code/CAN_MONITOR/file/fe1f886dff76/main.cpp

Looking at the documentation it looks like there might be a filter interface now. Not sure when this was added. https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/interfaces/digital/CAN/#api

  /** Filter out incomming messages
  *
  * @param id the id to filter on
  * @param mask the mask applied to the id
  * @param format format to filter on (Default CANAny)
  * @param handle message filter handle (Optional)
  *
  * @returns
  * 0 if filter change failed or unsupported,
  * new filter handle if successful
  */

  int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
posted by Travis Travelstead 17 Oct 2017
Be the first to answer this question.

Assigned to Toyomasa Watarai 6 years, 8 months ago.

This means that the question has been accepted and is being worked on.