Electronic – Controller area network received message filtering mechanism

cancommunicationstm8

I am working on some CAN communication project which allows us to control some external device attached to one node from some other node.
The CAN bus is a broadcast network and hence a message sent by a node will be received by every other CAN node on the bus.

My requirement is that I need to implement a filtration mechanism for the nodes so that a node will only act upon those received messages for which it is configured (using some GUI utility). So I need to implement this filter mechanism in my code. I am using an STM8SAF52Ax microcontroller with IAR Embedded Workbench as my development environment.

How could I implement this filter process of CAN messages? I also did web search and read many other tutorials, but still I am unable to figure
out what is to be done. There are use of CAN filter ID and filter mask, but I am unable to decide what value to be loaded in these and how to
calculate these values.

Best Answer

It is very straight-forward: only act when you receive a message you want, and ignore everything else. Simply keep a list of CAN identifiers and then take action based on them. It is a pure software problem.

CAN controllers always have built-in filter registers, but these are almost always too crude to be used in semi-advanced applications, because they use bit masking. The only use of such filters in practice, is when you have a CAN node which should only listen to one or maybe two CAN messages.

EDIT: note that these filters typically use a bitwise NOR of all existing CAN identifiers. Which is why the filters make less and less sense, the more CAN identifiers you need to support. It is a fundamental design mistake in the CAN filter mechanism in most CAN controllers.

The reason why you would want to use such filters is to reduce the work load of the CPU. Yet some of the dumber CAN controllers will still yield an interrupt for message received, even when the message didn't pass the filter.

So my advise for this case is to forget all about hardware filters and do all checks of CAN id in software.

For a large amount of different CAN ids, you will need to use a sorted table and efficiently search (binary search) through it to see if they received CAN id was valid.

The real question here is if you have a CAN driver for the given MCU or not. Coding a CAN driver yourself is not trivial, it usually takes 1-2 months to develop a fully functional CAN driver, given that you already know CAN and C programming.