Simvik

Acceptance filter

Decide whether a frame passes an ID-and-mask filter. An easy embedded C problem (CAN), run and graded in the browser.

easy · CAN · can · filters · bits

A CAN controller receives every frame on the bus, so hardware acceptance filters throw away the ones this node does not need before they reach the CPU. A mask filter has two values:

  • mask: a 1 bit means "this ID bit must match", a 0 bit means "don't care",
  • filter: the values the cared-about bits must have.

Write can_accept(id, filter, mask) for 11-bit standard IDs.

Starter code

bool can_accept(uint16_t id, uint16_t filter, uint16_t mask) {
  (void)mask;
  return id == filter;
}

Example tests

One exact ID

show(0x123, 0x123, 0x7FF);
show(0x124, 0x123, 0x7FF);

Expected output:

0x123 with filter 0x123 mask 0x7FF: accept
0x124 with filter 0x123 mask 0x7FF: reject

A group of IDs

show(0x350, 0x350, 0x7F0);
show(0x35F, 0x350, 0x7F0);
show(0x360, 0x350, 0x7F0);

Expected output:

0x350 with filter 0x350 mask 0x7F0: accept
0x35F with filter 0x350 mask 0x7F0: accept
0x360 with filter 0x350 mask 0x7F0: reject

Learn it first

  • CAN · Arbitration, bit timing, frames, CAN FD and error handling.

More CAN problems