Interfacing to the Microchip MCP2515DM-BM in Linux


CAN (Controller-Area Network) is a protocol that is commonly used in the automotive industry, but which has other applications as well. Normally if you want to monitor a CAN bus you need a CAN monitor which costs several hundreds of dollars, such as the PCAN-USB adapter from Peak System. But a much cheaper alternative exists in the MCP2515 CAN Bus Monitor Demo Board, from Microchip, currently available at the very fair price of 2 for US$55 (+ shipping). This little board measures 82x48mm and connects to your PC via USB.

The board comes with example software for Windows (without source code) but to make use of it properly you'll need to write your own software. This page provides most of the information you'll require, and a sample application in Linux.

The two demo boards connected with the provided cable
MCP2515DM-BM boards

Close up of the board

Getting Started

To help you get started, you might like:

USB Protocol

To write software to talk to the board, you'll probably want a USB library such as libusb. That's what my example code below uses. Some important information for talking to the board:

  • Vendor ID: 0x4d8
  • Product ID: 0x0070
  • Use configuration #1, and interface #0 with endpoints 0x01 (out) and 0x81 (in).
  • All communication uses 64 byte interrupt transfers.

The format of the 64 byte buffers is pretty simple, and can be determined without much difficulty by looking at the firmware source code. The important fields are:

  • Bytes 0..51: CAN messages (see below for details). [IN/OUT]
  • Byte 52: Number of CAN messages to send. I have only tried sending a single message at a time. [OUT]
  • Byte 55: Transmit Error Counter. [IN]
  • Byte 56: Receive Error Counter. [IN]
  • Byte 57: CANSTAT register - see datasheet for details. [IN]
  • Byte 58: CANCTRL register - see datasheet for details. [OUT]
  • Byte 60: SPI command. For incoming messages, this echos back the command that was sent. Commands are 0xC0 (CAN reset), 0x03 (read register), 0x02 (write register), 0x80 (RTS - ?), 0xB0 (RD Status - ?) and 0xD0 (Read Firmware Version). [IN/OUT]
  • Byte 61: Register. Set to the register to read or write. The response to a read request contains the same value. [IN/OUT]
  • Byte 62: Data. The value to be written to a register for outgoing messages, or retrieved from a register for incoming messages. [IN/OUT]

CAN message format

  • Byte 0: Most significant bit (bit 7) indicates the presence of a CAN message. Bit 5 indicates an extended (29 bit) identifier. Bit 4 indicates a remote transmission request (RTR). Bits 0-3 contain the data length.
  • [Standard ID] Bytes 1-2: Byte 1 and bits 5-7 of byte 2 contain the identifier, big endian.
  • [Extended ID] Bytes 1-4: From most significant to least significant, the message ID is in byte 1, byte 2 (bits 5-7), byte 2 (bits 0-1), byte 3, and byte 4.
  • Next [len] bytes: If this is not an RTR message, the content of the message follows (up to 8 bytes).

If no more CAN messages follow, the next byte will be set to zero. Otherwise, bit 7 will be set.

Example code