MCP2515 CANbus library

NOTE: This was originally written as instructions for using the MCP2515 CAN controller with Macchina 1.X series

So you have some CAN devices, and a CAN controller sharing board space with a nice I/O-studded microcontroller, and a basic idea of what you might be looking for on the bus (see my previous topic here: http://rechargecar.com/forums/macchina-and-protocols/can-bus-basics ).

Now what?

Now you program. Luckily we've adapted a library that simplifies communication between the Atmel microcontroller and the Microchip CAN controller on the Macchina. It's open-source, and we host it on Github. Here are a few ways to view and download the library:
The Github project is located at: https://github.com/rechargecar/mcp2515
If you just want to download the code, you can click here: https://github.com/rechargecar/mcp2515/archive/master.zip
If you're familiar with Git, here's a path to the repository: https://github.com/rechargecar/mcp2515.git

THE LIBRARY
In order for the Arduino environment to find the library, you'll first need to "install" it. Instructions for library installation can be found here: http://arduino.cc/en/Guide/Libraries

The MCP2515 library is a pretty straightforward affair (at least as straightforward as an abstraction layer for a bidirectional CAN message buffer and transceiver can be) that lets you build up CAN messages and send them out on the bus, and read any waiting messages from the bus.

All of the control functions depend on a custom data structure that represents a CAN 2.0 B (or 2.0 A) message frame. The functions do a pretty good job of masking and unmasking some of the configuration bits and putting them in their own fields within the Frame struct. Below is the definition of that data structure:

The fields in the Frame struct match up well with the sections of CAN frames ( documented here: http://en.wikipedia.org/wiki/CAN_bus#Frames ), but are made a little more accessible to you as the coder. The methods of the library's MCP2515 class take care of all of the bit masking based on the values in the Frame struct's fields.

There's an example Arduino project included in the library that provides a very basic example of reading messages from the CAN bus. It covers all the pin assignments and other setup you'll need to go through before using the MCP2515 features exposed by the library.

The first steps involve constructing an MCP2515 object, setting up the SPI peripheral, initializing the CAN interface before and in the setup() function.

Then the program settles into a loop where it checks the MCP2515's receive buffer for any new packets on the bus. If there are new packets, the program reads them into a Frame struct, and then outputs the Frame message's fields, including any data payload over the serial line. There is also a section that is commented out that will send any received packets back out onto the CAN bus, but with the ID and each data byte incremented by 1. This can be useful for debugging purposes, to make sure the interface is connected correctly.

Here's a Gist with all the code from the example, but if you have the library, you already have the example: https://gist.github.com/4177820

Hopefully this can serve as a good start for finding out what's going on on your CAN bus, and sending your own messages out on it.

Comments