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: ).

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:
If you just want to download the code, you can click here:
If you're familiar with Git, here's a path to the repository:

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:

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: ), 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:

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.