Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 394 of 9365. Communication Protocols and Data TransferFree lesson

Message Filtering and Acceptance Masks

Configure hardware-level acceptance filters to allow a microcontroller to ignore irrelevant messages and process only the specific data packets required for its task.

In this lesson, you will learn how to configure hardware-level filters to ensure your microcontroller only reacts to the specific CAN messages it actually needs.

Why Filter Messages?

In a CAN network, the bus is shared, meaning every node "sees" every message transmitted. If your microcontroller (like a motor controller) had to process every single packet—including sensor data for climate control or dashboard alerts—your CPU would be overwhelmed by interrupts. Instead of wasting cycles discarding irrelevant data in software, we offload this task to the CAN controller hardware.

The Mechanism: ID, Mask, and Filter

To decide whether to accept a message, the hardware performs a bitwise comparison between the incoming message Identifier (ID) and your pre-configured Acceptance Filter, constrained by an Acceptance Mask.

  • The Mask: Think of this as a stencil. A 1 in the mask means "must match," while a 0 means "don't care."
  • The Filter: This is the pattern the incoming ID must match where the mask has a 1.

If the operation (Incoming_ID & Mask) == (Filter & Mask) evaluates to true, the message is stored in the receive buffer. If not, the hardware silently ignores it, preventing any CPU overhead.

Practical Example

Suppose you only care about messages with IDs in the range 0x100 to 0x103. In binary, these are:

  • 0x100 (001 0000 0000)
  • 0x103 (001 0000 0011)

You want to ignore the last two bits but match everything else. You would configure:

  • Mask: 0x7FC (Binary: 111 1111 1100)
  • Filter: 0x100 (Binary: 001 0000 0000)
// Pseudo-register configuration
CAN_FilterMask = 0x7FC; // Bits 0 and 1 are "don't care"
CAN_FilterID   = 0x100; // Matches binary 001 0000 00xx

Because the mask has 0s in the last two positions, the hardware ignores those bits during the comparison. Any message from 0x100 to 0x103 will trigger an interrupt, while 0x104 or 0x200 will be discarded at the hardware level.

Key Takeaway: Hardware message filtering allows you to preserve CPU bandwidth by using masks and filters to perform pattern matching on CAN IDs directly at the physical layer.