Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
CAN Bus — bxCAN Controller
Complete STM32F407 bxCAN guide. MCR/MSR/BTR registers, bit timing, transmit mailboxes, receive FIFOs, message filters, and loopback test in bare-metal C.
CAN Bus Overview
CAN (Controller Area Network) is a robust serial protocol designed for automotive and industrial use. Unlike UART or SPI, CAN is a multi-master bus — any node can transmit at any time, with hardware collision detection and arbitration. Up to 127 nodes share one twisted-pair cable (CANH/CANL).
The STM32F407 has two bxCAN (basic extended CAN) controllers. bxCAN supports standard 11-bit IDs (CAN 2.0A) and extended 29-bit IDs (CAN 2.0B), 0–8 data bytes per frame, and speeds from 10kbps to 1Mbps.
CAN on STM32F407
| Feature | CAN1 | CAN2 |
|---|---|---|
| Base Address | 0x40006400 | 0x40006800 |
| APB1 IRQ (TX) | 19 | 63 |
| APB1 IRQ (RX0) | 20 | 64 |
| TX Pin | PA12 (AF9) or PD1, PB9 | PB6 (AF9) or PB13 |
| RX Pin | PA11 (AF9) or PD0, PB8 | PB5 (AF9) or PB12 |
| TX Mailboxes | 3 | 3 (shared with CAN1) |
| RX FIFOs | 2 (FIFO0, FIFO1) | 2 |
| Filter banks | 28 shared (14 for each) | — |
CAN Bit Timing — BTR Register
CAN bit timing is configured via the CAN_BTR register. Each bit is divided into segments. Getting this right is critical — all nodes must use the same bit rate, and the sample point position affects noise immunity.
One CAN bit = (SJW + BS1 + BS2) time quanta (TQ)
TQ = 1 / (f_APB1 / (BRP + 1))
Bit rate = 1 / ((BRP+1) × (1 + BS1 + BS2) / f_APB1)
Example: 500 kbps at APB1 = 42 MHz
BRP=5 → TQ = 6/42MHz = 142.85 ns
BS1=11, BS2=2, SJW=0
Bit time = (1+11+2) × 142.85ns = 2000ns = 500kbps ✓
Sample point at 85.7% ((1+BS1)/(1+BS1+BS2))
| CAN_BTR Field | Bits | Description |
|---|---|---|
| BRP[9:0] | [9:0] | Baud Rate Prescaler. TQ = (BRP+1)/f_APB1. |
| TS1[3:0] | [19:16] | Time Segment 1 (Phase_Seg1 + Prop_Seg). BS1+1 time quanta. |
| TS2[2:0] | [22:20] | Time Segment 2 (Phase_Seg2). BS2+1 time quanta. |
| SJW[1:0] | [25:24] | Resynchronization Jump Width. SJW+1 time quanta. |
| LBKM | [30] | Loop Back Mode. 1=loopback (testing without external bus). |
| SILM | [31] | Silent Mode. 1=receive only, no transmit. |
| Bit Rate | APB1 | BRP | TS1 | TS2 | SJW | BTR Value |
|---|---|---|---|---|---|---|
| 1 Mbps | 42 MHz | 2 | 11 | 2 | 0 | 0x001B0002 |
| 500 kbps | 42 MHz | 5 | 11 | 2 | 0 | 0x001B0005 |
| 250 kbps | 42 MHz | 11 | 11 | 2 | 0 | 0x001B000B |
| 125 kbps | 42 MHz | 23 | 11 | 2 | 0 | 0x001B0017 |
Core bxCAN Registers
CAN_MCR — Master Control Register
| Bit | Name | Description |
|---|---|---|
| 0 | INRQ | Initialization Request. Write 1 to enter init mode (required for BTR configuration). Wait INAK in MSR. |
| 1 | SLEEP | Sleep Mode Request. Write 1 to enter sleep. Clear to wake up. |
| 4 | NART | No Automatic Retransmission. 1=send each frame only once. 0=retry until ACKed. |
| 6 | ABOM | Automatic Bus-Off Management. 1=auto-recover from bus-off condition. |
| 7 | AWUM | Automatic Wakeup Mode. 1=auto-exit sleep on CAN activity. |
| 15 | RESET | bxCAN Software Reset. Write 1 to reset. |
| 16 | DBF | Debug Freeze. 1=CAN reception/transmission halted when MCU is halted by debugger. |
CAN_MSR — Master Status Register
| Bit | Name | Set When |
|---|---|---|
| 0 | INAK | Initialization Acknowledge — CAN is in init mode. Poll after writing INRQ=1. |
| 1 | SLAK | Sleep Acknowledge — CAN is in sleep mode. |
| 3 | ERRI | Error Interrupt flag. |
| 4 | WKUI | Wakeup interrupt. |
| 8 | RXM | Receive mode — CAN is currently receiving a frame. |
| 9 | SAMP | Last sample point value of RX pin. |
| 10 | RX | CAN RX pin level. |
CAN Initialization and Message Transmission
/*
* CAN1 initialization at 500 kbps
* TX=PB9 (AF9), RX=PB8 (AF9)
* APB1 = 42 MHz
* Loopback mode for testing (no external transceiver needed)
*/
#include <stdint.h>
#define RCC_AHB1ENR (*((volatile uint32_t*)0x40023830))
#define RCC_APB1ENR (*((volatile uint32_t*)0x40023840))
#define GPIOB_MODER (*((volatile uint32_t*)0x40020400))
#define GPIOB_AFRH (*((volatile uint32_t*)0x40020424))
#define CAN1_BASE 0x40006400UL
#define CAN_MCR (*((volatile uint32_t*)(CAN1_BASE + 0x000)))
#define CAN_MSR (*((volatile uint32_t*)(CAN1_BASE + 0x004)))
#define CAN_TSR (*((volatile uint32_t*)(CAN1_BASE + 0x008)))
#define CAN_RF0R (*((volatile uint32_t*)(CAN1_BASE + 0x00C)))
#define CAN_IER (*((volatile uint32_t*)(CAN1_BASE + 0x014)))
#define CAN_BTR (*((volatile uint32_t*)(CAN1_BASE + 0x01C)))
/* Transmit mailbox 0 */
#define CAN_TI0R (*((volatile uint32_t*)(CAN1_BASE + 0x180)))
#define CAN_TDT0R (*((volatile uint32_t*)(CAN1_BASE + 0x184)))
#define CAN_TDL0R (*((volatile uint32_t*)(CAN1_BASE + 0x188)))
#define CAN_TDH0R (*((volatile uint32_t*)(CAN1_BASE + 0x18C)))
/* Receive FIFO0 mailbox 0 */
#define CAN_RI0R (*((volatile uint32_t*)(CAN1_BASE + 0x1B0)))
#define CAN_RDT0R (*((volatile uint32_t*)(CAN1_BASE + 0x1B4)))
#define CAN_RDL0R (*((volatile uint32_t*)(CAN1_BASE + 0x1B8)))
#define CAN_RDH0R (*((volatile uint32_t*)(CAN1_BASE + 0x1BC)))
/* Filter registers */
#define CAN_FMR (*((volatile uint32_t*)(CAN1_BASE + 0x200)))
#define CAN_FM1R (*((volatile uint32_t*)(CAN1_BASE + 0x204)))
#define CAN_FS1R (*((volatile uint32_t*)(CAN1_BASE + 0x20C)))
#define CAN_FFA1R (*((volatile uint32_t*)(CAN1_BASE + 0x214)))
#define CAN_FA1R (*((volatile uint32_t*)(CAN1_BASE + 0x21C)))
#define CAN_F0R1 (*((volatile uint32_t*)(CAN1_BASE + 0x240)))
#define CAN_F0R2 (*((volatile uint32_t*)(CAN1_BASE + 0x244)))
void CAN1_Init(uint8_t loopback) {
/* Enable clocks */
RCC_AHB1ENR |= (1<<1); /* GPIOB */
RCC_APB1ENR |= (1<<25); /* CAN1 */
/* PB8=RX, PB9=TX → AF9 */
GPIOB_MODER &= ~((3<<16)|(3<<18));
GPIOB_MODER |= (2<<16)|(2<<18);
GPIOB_AFRH &= ~((0xF<<0)|(0xF<<4));
GPIOB_AFRH |= (9<<0)|(9<<4); /* AF9 for PB8, PB9 */
/* Exit sleep mode */
CAN_MCR &= ~(1<<1); /* Clear SLEEP */
while (CAN_MSR & (1<<1)); /* Wait SLAK cleared */
/* Enter init mode */
CAN_MCR |= (1<<0); /* INRQ=1 */
while (!(CAN_MSR & (1<<0))); /* Wait INAK */
/* Set options */
CAN_MCR &= ~(1<<4); /* NART=0: auto-retransmit */
CAN_MCR |= (1<<6); /* ABOM=1: auto bus-off recovery */
/* BTR: 500kbps at 42MHz — BRP=5, TS1=11(0xB), TS2=2(0x2), SJW=0 */
CAN_BTR = (loopback ? (1<<30) : 0) | /* Loopback mode bit */
(0 << 24) | /* SJW = 0 */
(1 << 20) | /* TS2 = 2-1 = 1 */
(10 << 16)| /* TS1 = 11-1 = 10 */
5; /* BRP = 5 */
/* Configure filter bank 0 — accept ALL messages to FIFO0 */
CAN_FMR |= 1; /* Filter init mode */
CAN_FM1R &= ~1; /* Mask mode for bank 0 */
CAN_FS1R |= 1; /* 32-bit scale for bank 0 */
CAN_FFA1R &= ~1; /* Assign to FIFO0 */
CAN_F0R1 = 0; /* Filter: accept all IDs */
CAN_F0R2 = 0; /* Mask: no bits checked */
CAN_FA1R |= 1; /* Activate filter bank 0 */
CAN_FMR &= ~1; /* Leave filter init mode */
/* Leave CAN init mode */
CAN_MCR &= ~(1<<0); /* Clear INRQ */
while (CAN_MSR & (1<<0)); /* Wait INAK cleared */
}
/*
* Send a standard CAN frame
* id: 11-bit standard ID
* data: up to 8 bytes
* len: data length (0-8)
*/
int CAN1_Send(uint16_t id, const uint8_t* data, uint8_t len) {
/* Wait for a free transmit mailbox (check TSR for empty mailbox) */
uint32_t timeout = 10000;
while (!(CAN_TSR & ((1<<26)|(1<<27)|(1<<28)))) {
if (!--timeout) return -1; /* No free mailbox */
}
/* Use mailbox 0 (TME0 = bit 26) */
/* TI0R: standard ID [31:21], RTR=0 (data frame), IDE=0 (standard), TXRQ=0 */
CAN_TI0R = ((uint32_t)id << 21); /* Standard ID, don't request TX yet */
CAN_TDT0R = len & 0xF; /* DLC (data length code) */
/* Fill data registers (little-endian, byte 0 in lowest bits) */
CAN_TDL0R = (len > 0 ? data[0] : 0) |
(len > 1 ? ((uint32_t)data[1] << 8) : 0) |
(len > 2 ? ((uint32_t)data[2] << 16) : 0) |
(len > 3 ? ((uint32_t)data[3] << 24) : 0);
CAN_TDH0R = (len > 4 ? data[4] : 0) |
(len > 5 ? ((uint32_t)data[5] << 8) : 0) |
(len > 6 ? ((uint32_t)data[6] << 16) : 0) |
(len > 7 ? ((uint32_t)data[7] << 24) : 0);
/* Trigger transmission: set TXRQ (bit 0 of TI0R) */
CAN_TI0R |= 1;
return 0;
}
/* Receive a message from FIFO0 */
int CAN1_Receive(uint16_t* id, uint8_t* data, uint8_t* len) {
if (!(CAN_RF0R & 0x3)) return -1; /* FMP0[1:0] = 0: no messages */
*id = (uint16_t)(CAN_RI0R >> 21); /* Extract 11-bit ID */
*len = CAN_RDT0R & 0xF;
uint32_t dl = CAN_RDL0R, dh = CAN_RDH0R;
if (*len > 0) data[0] = dl & 0xFF;
if (*len > 1) data[1] = (dl >> 8) & 0xFF;
if (*len > 2) data[2] = (dl >> 16) & 0xFF;
if (*len > 3) data[3] = (dl >> 24) & 0xFF;
if (*len > 4) data[4] = dh & 0xFF;
if (*len > 5) data[5] = (dh >> 8) & 0xFF;
if (*len > 6) data[6] = (dh >> 16) & 0xFF;
if (*len > 7) data[7] = (dh >> 24) & 0xFF;
CAN_RF0R |= (1<<5); /* RFOM0: Release mailbox */
return 0;
}
/* Loopback test */
int main(void) {
SystemClock_Config();
CAN1_Init(1); /* Loopback=1 for testing */
uint8_t tx_data[] = {0x01, 0x02, 0x03, 0x04};
CAN1_Send(0x123, tx_data, 4); /* Send frame with ID 0x123 */
delay_ms(10); /* Wait for loopback */
uint16_t rx_id; uint8_t rx_data[8], rx_len;
if (CAN1_Receive(&rx_id, rx_data, &rx_len) == 0) {
/* Successfully received our own message */
}
}
CAN Message Filters
Filters prevent the CPU from being interrupted by unwanted CAN messages. The bxCAN has 28 filter banks (14 assigned to CAN1, 14 to CAN2). Each bank can be configured as Identifier List (accept specific IDs) or Identifier Mask (accept IDs matching a pattern).
/* Filter example: Accept only ID 0x123 and 0x456 */
CAN_FMR |= 1; /* Enter filter init mode */
/* Bank 0 in list mode, 32-bit scale, accept IDs 0x123 and 0x456 */
CAN_FA1R &= ~1; /* Deactivate bank 0 during config */
CAN_FM1R |= 1; /* List mode (identifier list) */
CAN_FS1R |= 1; /* 32-bit scale */
CAN_FFA1R &= ~1; /* Route to FIFO0 */
/* In 32-bit list mode, each FR holds one ID */
/* Standard ID: bits [31:21] = ID, bit 2 = IDE, bit 1 = RTR */
CAN_F0R1 = (0x123UL << 21); /* First accepted ID */
CAN_F0R2 = (0x456UL << 21); /* Second accepted ID */
CAN_FA1R |= 1; /* Activate bank 0 */
CAN_FMR &= ~1; /* Leave filter init */
/* Mask mode: Accept any ID 0x1xx (ID bits 10:8 must match) */
CAN_FM1R &= ~(1<<1); /* Bank 1: mask mode */
CAN_FS1R |= (1<<1); /* 32-bit scale */
CAN_F1R1 = (0x100UL << 21); /* ID pattern: 0b001xxxxxxxx << 21 */
CAN_F1R2 = (0x700UL << 21); /* Mask: check top 3 bits only */
/* Accepts: 0x100 through 0x1FF */
Summary
CAN_MCR
INRQ: enter init mode. SLEEP: sleep mode. ABOM: auto bus-off recovery. Configure in init mode.
CAN_BTR
BRP+TS1+TS2 determines bit rate. LBKM=1 for loopback test. Only writable in init mode.
TX Mailboxes
3 mailboxes. Set TI0R (ID+TXRQ), TDT0R (DLC), TDL0R/TDH0R (data). Set TXRQ to send.
RX FIFO
Check RF0R FMP0 for message count. Read RI0R (ID) + RDL0R/RDH0R (data). Set RFOM0 to release.
Filters
Must configure at least one filter. Use FMR init mode to set. FA1R activates banks.
Physical Layer
Need CAN transceiver (MCP2562/SN65HVD230). STM32 pins: CANTX → TXD, CANRX ← RXD.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments