Tuesday, June 23, 2009

Playing a bit with SocketCan and its utilities

This time I had a better look at the SocketCan utilities that I mentioned in a previous post. As I have already told, you can get and install them with these commands:
svn checkout http://svn.berlios.de/svnroot/repos/socketcan/trunk canbus
cd canbus/can-utils/
make
make install

And you will end up with lots of new programs under /usr/local/bin:
asc2log canbusload candump cangen
canlogserver canplayer cansend cansniffer
isotpdump isotprecv isotpsend isotpsniffer
isotptun log2long slcand slcanpty vcan

To see how canbus works, I tried to setup a virtual can bus with these commands:

modprobe vcan can-raw
ip link add dev vcan-test type vcan
ifconfig vcan-test up

These basically creates a canbus named vcan-test and set the interface up.
Then I opened two terminals and ran the command candump on one terminal and the command cansend on the other terminal. These are the outputs:
# cansend vcan-test 5A1#11.2233.44556677.88
# cansend vcan-test 5A1#11.2233.44556677
# cansend vcan-test 1F334455#1122334455667788

# candump vcan-test
vcan-test 5A1 [8] 11 22 33 44 55 66 77 88
vcan-test 5A1 [7] 11 22 33 44 55 66 77
vcan-test 1F334455 [8] 11 22 33 44 55 66 77 88

That are the message id and the data. The number between the brackets is the data length and the last case is with an extended id.

This could also be the text format used by the slcan driver to communicate via serial port.
I have tried to use slcanpty to attach the vcan-test interface to a pseudo terminal but without success so far. I will have to try again soon as it could be the way to gather the data on the Linux machine without writing an additional custom layer.

Thursday, June 18, 2009

Two interesting CANBUS projects and some links

This CANalyser tool is pretty similar to what we want to do at the moment and is built with very similar chips. The project seems a bit outdated but the information is still very useful.
The tool is composed by a PIC microcontroller PIC18F458, and a MCP2551 CAN transceiver. The main features are listen to the CAN bus in active or passive mode and forward data to the serial port to the PC and send data to the CANBUS. This is exactly what we need now.
Digging further in the web pages I discover that they have built a real-time kernel to run their software on the PIC. There is a company behind this (Pragmatec) but they have released the sources under a GPL license. The website of the os is at PICOS18
There is a new version of the pages here but the page about the CANalyser tool is the same.

Another very similar tool is CAN2PIC. This one uses
PIC18F248 and MCP2551 CAN.
You can find there all the schematics of the circuit used and also all the software! So this page should provide all we need to get started and running very fast!

There are some other interesting links I found tonight:
  • GNUPIC: a small directory site with information on PIC programming with GNU/Linux
  • Another very good CANBUS learning page at CERN (yes they used it in LHC)
  • PICMICRO WebRing: A collection of websites about the Microchip PIC microcontroller and related projects

Sunday, June 14, 2009

Canbus programming under linux: SocketCAN

Well the good news is that someone already did the hard part for us!
The Linux kernel includes since several versions the support to the can protocol and provides what seems to be a very easy interface to it. It's called SocketCAN and it is modeled after the standard unix socket API.
The project page is at BerliOS and hosts the kernel patches (not needed anymore as it is included in mainstream) and some user space tools that I just downloaded and installed without a glitch. You can get them by accessing the project subversion repository with this command: svn checkout http://svn.berlios.de/svnroot/repos/socketcan/trunk canbus

More info on what is and how to use SocketCan can be found in the linux kernel documentation directory (available on the web here)

As for the device drivers, SocketCAN supports a few drivers alreay, two of which are particularly interesting: vcan and slcan. Vcan is a virtual CAN interface driver and is there for testing when there is no hardware available. This will be probably the first to try once I realize how the user space utilities work. The slcan driver is "For a bunch of CAN adaptors that are attached via a serial line ASCII protocol (for serial / USB adaptors)" and should match exactly what we are trying to do. I just wonder what is the data format the slcan drivers expects to receive from the serial port. I have only found a strange document here so far but the semantic is not clear to me and I will have to investigate this further.

Saturday, June 6, 2009

Getting the tools part 2: a matter of levels

After choosing the main component of the project in a previous post, we also have to connect it :-)
On one side we have the CANbus connector, on the other (probably) a PC used to understand what informations are going up and down on the CAN. Unfortunately we can't connect PC directly to CANbus, due the lacking of a suitable peripheral on PC. So we choosed a PIC18F4682 to do the raw job. So is it all done?

NO

Connecting directly CAN, PIC and PC probably will result in a big roast, with nothing to eat :( . So we will use these two tiny chips, ready on the breadboard:
Their work is to convert voltage levels in a suitable way.



MAX232CPE from Maxim is a level converter for RS232 (serial) protocol. It converts +12/-12 signals from PC serial port to +5v accepted from PIC, and do the opposite job on the other direction. To do this it needs some condensers, not yet installed, to produce the necessary voltages, cause it's main voltage is only +5V.




MCP2551 from Microchip converts CANbus signals (what is the voltage on Vectra CAN bus? we still don't know...) to PIC level. We HOPE that this interface chip will do the job, cause the real implementation of CANbus on Vectra (voltage, speed, protocols, messages...) is still totally unknown. MCP main voltage is also +5V.



On the left side of the first photo you can see a bunch of components and wires. This will be our 5V power plant. LM7805 with 2 condensers will give power to all our chips and components we will use in this project. Now it is time to connect everything: stay tuned!