Designing CAN bus stack with software whitelist filter
Providing a C design where the hardware filter passes broad CAN IDs, the ISR reads each message, then performs a binary search on a sorted whitelist to decide processing. Allowed messages enqueue into a ring buffer for later handling. The code includes CAN frame struct, binary search function, ISR pseudo-code, and a thread-safe ring buffer to keep ISR minimal and efficient.
On microcontrollers with fairly coarse acceptance filtering, a common pattern is to let the hardware accept a superset of what you care about and then do fine–grained filtering in software. The key points for the software layer are:
- Keep the whitelist in a sorted array of IDs to allow O(log N) lookup.
- Make the ISR as short as possible: read the frame, binary–search the whitelist, and either enqueue the frame for later processing or discard it.
- Decouple the ISR from the application with a ring buffer/queue.
A minimal design might look like this:
#include <stdint.h>
#include <stdbool.h>
#define RX_QUEUE_SIZE 32
typedef struct {
uint32_t id;
uint8_t dlc;
uint8_t data[8];
} can_frame_t;
static const uint32_t whitelist[] = {
0x100, 0x101, 0x200, 0x201, 0x300, 0x400
};
static const size_t whitelist_len = sizeof(whitelist)/sizeof(whitelist[0]);
static can_frame_t rx_queue[RX_QUEUE_SIZE];
static volatile unsigned rx_head = 0;
static volatile unsigned rx_tail = 0;
static inline bool is_whitelisted(uint32_t id) {
// standard binary search on sorted whitelist
size_t lo = 0, hi = whitelist_len;
while (lo < hi) {
size_t mid = lo + (hi - lo)/2;
if (id == whitelist[mid]) {
return true;
} else if (id < whitelist[mid]) {
hi = mid;
} else {
lo = mid + 1;
}
}
return false;
}
// called from the CAN receive ISR
void CANx_RX_IRQHandler(void) {
// Read a frame from the hardware registers/FIFO.
can_frame_t frame;
frame.id = CANx->RIR >> 21; // example for standard ID
frame.dlc = CANx->RDTR & 0x0F;
frame.data[0] = CANx->RDLR & 0xFF;
/* ... fill in the rest of data bytes ... */
// Clear hardware receive flag/FIFO to be ready for next message.
CANx->RF0R |= CAN_RF0R_RFOM0;
if (!is_whitelisted(frame.id)) {
return; // discard
}
// Enqueue for processing in main context
unsigned next_head = (rx_head + 1) % RX_QUEUE_SIZE;
if (next_head != rx_tail) { // check for overflow
rx_queue[rx_head] = frame;
rx_head = next_head;
} else {
// queue full, drop frame or set an overrun flag
}
}
bool can_receive(can_frame_t *out) {
if (rx_tail == rx_head) {
return false; // no frames
}
*out = rx_queue[rx_tail];
rx_tail = (rx_tail + 1) % RX_QUEUE_SIZE;
return true;
}
Some additional considerations:
- Configure the hardware acceptance filters to a broad mask that encompasses all IDs in
whitelist to reduce ISR load.
- Mark the whitelist
const and keep it in flash; if it needs to be updated at runtime, protect it with a mutex or only modify it with interrupts disabled, because the ISR reads it concurrently.
- Keep the ISR small: defer any heavy processing to a lower–priority task that reads from
rx_queue.
- If you need to handle extended IDs or different bit layouts, adjust the ID extraction accordingly and store a canonical representation in the whitelist.
- If the whitelist is large, you can precompute the midpoint indices or use a perfect hash, but for tens of IDs a simple binary search is efficient and deterministic enough for an ISR.
This structure gives you a clear separation between the low–level driver, the software filter, and the application, while keeping the ISR deterministic and efficient.