Sunday, February 1, 2015

CAN Bus to UART using an Adafruit Pro Trinket

I have been fighting with the onboard CAN controller on my BeagleBone Black trying to
get it to work properly for months. In the process I learned about pin muxing, bone-capemgr and compiling overlays. I was able to bring up the can0 interface and receive can messages with candump but the interface was unstable. It would randomly hang and stop receiving messages. If I tried to send anything with cansend I would get a kernel stack traces in dmesg and the can0 interface would hang.  I tried Angstrom, Ubuntu and the latest Debian images all with the same results. Maybe my BBB board has a hardware problem. It is one of the early Rev B boards. Or maybe I'm running into some sort of kernel driver bug. Whatever the issue I am done trying to figure out what the eff is wrong with it and I just want to move forward with my project. (and yes I was using a transceiver). I considered bypassing the onboard can controller and talking to a MCP2515 over SPI but that gets into compiling a custom kernel for the BBB and I don't want to use up a SPI port. I mention all of this because I know someone out there will say "why didn't you just use the onboard can controller". I tried and I am done fighting with it.

My attempt at using the CAN controller on
the BBB with an MCP2562 transceiver

What now?

Once I decided I wasn't going to get the onboard BBB can controller to work I started thinking about options. My first attempt at a workaround was an Arduino Uno with a Seeed CAN Bus shield connected to the BBB with a USB cable. I read the data from the serial port created over the USB connection. While this worked it was a bit bulky and fragile. The USB cable is much too big, the Uno with a shield is big and the Uno would reset when serial communication started (though there is a workaround for this).

I took a break from this project for a while and in the meantime I ordered a few Adafruit Trinkets and Pro Trinkets just to play around with. The Pro Trinket has the same microcontroller as the Uno but the board is much, much smaller.

Size comparison between Uno, Pro Trinket and Trinket.
(Photo credit: Adafruit)

Pro Trinket CAN to UART Converter

The Pro Trinket has SPI and UART plus a bunch of digital and analog pins. After playing around with the Trinket I realized I could build a CAN Bus to UART converter that would be able to push CAN data into the BBB over a UART. The BeagleBone is a 3.3volt device so I used the 3volt version of the Pro Trinket so I didn't have to use any level shifters. For the CAN Bus controller I used an MCP2515 and for the CAN transceiver I used an MCP2562. The MCP2515 connects to the Arduino using a SPI connection. Here is the circuit I came up with:



Here is a Fritzing breadboard diagram of the circuit:

The UART serial connection between the Arduino and the BeagleBone runs at 115,200 bps so you might drop a few packets on a very busy CAN bus running at 500kbps. My Digital Dashboard project is only going to have a few devices broadcasting CAN packets so the UART speed will be more than enough for my purposes. The current version only transmits CAN data to the BBB. It should be fairly trivial to connect the BBB UART1 TX to the RX on the Arduino. It would also need a bit of code to parse the message received on the UART and write it to the CAN bus. I'm planning on adding the functionality soon and I'll write up another post when I have that done. I'm planning on changing the Arduino code to use an External Interrupt so the Trinket can perform a few other functions instead of just polling for incoming messages.

The Arduino Code

The way this works is the MCP2515 has an INT (interrupt) pin that signals when a CAN message has been received. The INT pin drives Pin3 on the Arduino low and triggers it to read messages from the MCP2515 buffer. Once a message has been read from the MCP2515 buffer it is formatted into a NMEA-ish string that is written to the serial port and transmitted to the BBB on UART1.

Screenshot of CAN data coming in on UART1

 Here is the Arduino code:



Setting up the BeagleBone

The pins on the BeagleBone need to be configured to be used as a UART. I used Adafruit's python IO library to do this. When you install their library it will create the overlays needed to configure the pins. Follow the instructions on their learn site here: https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black. Here is the code to receive the CAN messages on BBB UART1:


Costs

The price for these parts wasn't bad at all

Adafruit Pro Trinket 3v - $9.95
16Mhz Crystal - $1.12
Two 22pF ceramic capacitors - $0.66
MCP2515 CAN controller - $2.18
MCP2562 CAN transceiver - $1.12

Total: $15.03


Well that's it for now. Next I'm going to work on modifying the code to use External Interrupts and then see if I can get it setup to receive messages over the UART. I also need to get this thing moved from the breadboard and soldered on to some protoboard.




Resources
http://www.embedded-things.com/bbb/enable-canbus-on-the-beaglebone-black/
http://www.adafruit.com/product/2010
https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/uart
http://ww1.microchip.com/downloads/en/DeviceDoc/21801d.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/25167B.pdf

1 comment:

  1. Nicely done; but I honestly think you're doing this the hardway. Not all CAN are created equal. Each OEM have different baud rate and network topolgy, also, CAN itself is a pain in the @#$ to control. Even a wire that is not twisted right can be the problem and has to be proper length, as it adds ghosting on the network (framing errors). I applaude you for doing this on your own but as you experienced,,, its pain. I would've just bought one since they have come down in prices alot... http://arduinodev.com/hardware/obd-kit/.

    ReplyDelete

Please note all comments are moderated by me before they appear on the site. It may take a day or so for me to get to them. Thanks for your feedback.