Electronic – Debugging a LPC23XX based CAN bus sniffer

buscanlpctiming

I am trying to get a CAN bus sniffer working, based on LPC2368. I keep getting a bus error interrupt, of type "form error". I suspect my timing settings. I verified that the sending party is transmitting at 125 kbit/s (8 μs bit duration). Here is my setting for a 72 MHz CPU clock and 125 kbit/s bus speed:

#define PULSE_BEFORE_SAMPLE 8
#define PULSE_AFTER_SAMPLE 3
#define PULSE_PER_BIT (PULSE_BEFORE_SAMPLE + PULSE_AFTER_SAMPLE + 1) 
#define TSEG1 (PULSE_BEFORE_SAMPLE - 1)
#define TSEG2 (PULSE_AFTER_SAMPLE - 1)
#define SJW 3 //sync jump width
#define SAM 0 //sampling 0=single sample 1=triple sample

#define CAN_PRESCALER 7 //((CPU_CLK_HZ / PCLK_DIVIDER / CAN1_BAUDRATE / PULSE_PER_BIT) - 1 )
...
PCLKSEL0 |= PCLK_CAN1 << 26; //clock source = CCLK/6
...
CAN1BTR = (CAN_PRESCALER | (SJW << 14) | (TSEG1 << 16) | (TSEG2 << 20) | (SAM << 23));

Am I making an obvious mistake here? The controller is running in listen-only mode, all other mode bits are set to 0.

Best Answer

I've worked with the Microchip CAN controller with SPI bus interface, MCP2515. One of the most difficult parts of setting up CAN is making sure that all the baud rate registers are set properly for all of devices on the bus. I'm not sure if you have access to the registers on the sending device, but being able to read the exact baud rate register values from the sender may give you an idea as to which register you have configured incorrectly. Also knowing the type of error if any is being detected by the sender may help you solve your CAN bus problem.

Microchip has an application note, AN754, that provides a guide to setting the CAN bus timing. It's not specific to Microchip. There are a few basic timing parameters you must set properly in order for the CAN bus communication to work. They are:

  • Tq = Time Quanta
  • Nominal Bit Time
  • Synchronization segment width
  • Propagation segment width
  • Phase segment 1 width
  • Phase segment 2 width

Make sure these values all match in your sender and receiver.