1 reply [Last post]
Andy Cook
Joined: 2012-06-22

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

We encourage feedback and any augmentations or improvements you might see fit to contribute back to the project. So if you want to fork it, make some changes, and submit a pull request ( https://help.github.com/articles/using-pull-requests ), we'll be glad to review the proposed changes and roll it back into the codebase if we think it will be helpful. And you're free to fork it into your own project, too, as long as you keep it open! We're using the LGPL license, so, while the library itself has to remain open, it doesn't force you to open-source your own code that might happen to use the library. We see this as a reasonable balance that allows a good amount of flexibility while still encouraging contribution back into the community.

Along with making projects very accessible and public, Github also makes sharing code snippets really easy, in the form of "Gists". A Gist is essentially a new Git repository, but trivial to create and share. So you could use it to start up a new project, or just show someone the order in which you like initializing all your I/O and data structures before reading the status from your battery charger. You can make new Gists here: https://gist.github.com/

If you want to embed Gists in your forum posts, there is a shorthand way to do that. All you have to do is type [gist:####] where #### is the numeric ID of the Gist you want to embed.

I'll follow this post up with a description of the library and a basic usage example.

Andy Cook
Joined: 2012-06-22

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.