CAN Interface


103 posts   Page 1 of 5   1, 2, 3, 4, 5
by rghiriti » Fri Jan 13, 2012 10:54 am
Hello all!

Getting young people to program is a lovely idea, but I think there is an aspect of software engineering that is less promoted, and that is the Embedded Systems Programming. In my many discussion with people within the industry (automotive and aerospace, mainly), the overriding concern was that there are not enough embedded systems engineers around. Working with low-level hardware, needing to understand a bit more than just the programming language, seem to be in short supply.

One of the things I would be interested in connecting to the Raspberry Pi that would address one of the skills shortage, is a CAN controller, which is a standard in the automotive and other industries as well. I was thinking of finding a way to interface the Pi with a CAN network, and I found a Microchip standalone CAN controller, driven over SPI, MCP2515. I was also thinking of looking for a similar device, but driven via I2C, or even interfacing a simple microcontroller that has both I2C/SPI and CAN to act as a bridge.

I only thought about it last night and I did not have much time to think it through, but I would like to hear from people who would have similar projects. I want to use this thread as a sounding board for ideas on how to go about connecting a CAN interface to the Raspberry Pi. Any comments are welcome!

Cheers!

"If it were easy, everyone would do it."
Posts: 1
Joined: Tue Jan 10, 2012 1:24 pm
by TonyD » Fri Jan 13, 2012 2:47 pm
I've used a SPI interfaced CAN chip before with a simple micro and it worked great.

Interfacing with the SPI MCP2515 with the RPi should be straight forward especially when we get some SPI drivers for the RPi.

As to I2C CAN chips I thought NXP might have some but couldn't find anything.
Tony
User avatar
Posts: 350
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
by mccleanp » Fri Jan 13, 2012 3:43 pm
Another alternative (for the less hardware inclined) would be to use a USB CAN interface such as http://www.peak-system.com/Pro.....wUid%5D=16.

More expensive probably, but it comes with a GPL linux driver and full support for the linux SocketCAN interface.
Posts: 3
Joined: Wed Oct 12, 2011 1:38 pm
by Gert van Loo » Fri Jan 13, 2012 4:06 pm

the overriding concern was that there are not enough embedded systems engineers around. Working with low-level hardware, needing to understand a bit more than just the programming language, seem to be in short supply.


Thank you very much for that. It is the exact reason why I put a PIC on the Gertboard. With pupils I would first start with learning a simple programs. (Output text, for loops etc.) Next step is to do simple I/O. I would use the "Raspi Dom method" (Which is using the Linux file system) to drive the GPIO's.  There might some more steps but finally you should try programming a CPU which has nothing except the hardware. There you must set up the UART and write your own 'putc' routine to get text out. I always find using interrupts is a major breakthrough in understanding computers. (Coupled with programming a timer). Next is critical code (What happens if you change a variable in the main code and in the interrupt routine.) From then on they should be able to do the rest themselves.

(I hope somebody ports a PIC IDE on the Raspberry Pi. What is funny is that the Gertboard +Raspberry-Pi would be cheaper then the standard ICD2 debugger which Microchip offers)

My only objection with the PICs is that they sometimes force you to do things in a very convoluted way. But that may be a good introduction to "The weird and confounding way of the computer world".
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2112
Joined: Tue Aug 02, 2011 7:27 am
by to » Fri Jan 13, 2012 4:35 pm
I'm a complete newb when it comes to both PIC or CAN, but I know Atmel have some chips made for the can bus and they have good dev environments, documentation that even hobbyists can understand and lots of examples. Maybe something like that could be added?
Posts: 11
Joined: Tue Dec 27, 2011 4:57 pm
by Tomo2k » Fri Jan 13, 2012 5:33 pm
SPI is a much better way to talk to a CANbus anyway, as I2C doesn't really have the bandwidth.

Looking at the datasheet for that chip and the MCP2551 CAN transceiver, I think it could go onto an expansion board plugged straight into the RPi GPI with no additional PSU at all.

- One complication is the level translation between 3V3 and 5V SPI.
Are the buffer chips used on Gertboard fast enough to do this?

Looks like the chips are about 3 quid total from Farnell (though they don't appear do a PDIP MCP2515), so the parts cost for a CANbus expansion board would be quite reasonable.
Posts: 126
Joined: Mon Dec 19, 2011 10:00 pm
by Gert van Loo » Fri Jan 13, 2012 6:06 pm

- One complication is the level translation between 3V3 and 5V SPI.
Are the buffer chips used on Gertboard fast enough to do this?


Sorry but they are buffers, not level translators. I hesitated to use level translators and decided against them as it was an education board and would complicate building the hardware. If I recall: you can drive 5V logic from 3V3 the only thing you need is that the buffers are 5V tolerant. As the Gertboard takes any 20-pin compatible 74xx244 there might be one available which is 5V tolerant. Speed is more a matter of capacitive load, wire length, termination (e.g. reflection) etc.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2112
Joined: Tue Aug 02, 2011 7:27 am
by Firefox » Sat Jan 14, 2012 3:44 pm
mccleanp said:


Another alternative (for the less hardware inclined) would be to use a USB CAN interface such as http://www.peak-system.com/Pro.....wUid%5D=16.

More expensive probably, but it comes with a GPL linux driver and full support for the linux SocketCAN interface.



We use those at work - they're about £250 a pop IIRC, and not always 100% reliable in our experience (they sometimes get into a funny state and need unplugging and plugging back in again to work).

A Raspberry Pi equipped with a CAN controller+transceiver shield would be an awesome replacement: it could do traffic logging, ECU simulation, bridging to Ethernet, CAN-enabling of plant, all sorts (as well as replacing common-or-garden dumb USB CAN adaptors). They probably wouldn't cost more than £50 each all-in once you'd added the shield, a plastic box, and an SD card.

Everywhere I look at work I can see something that could be improved by adding a Raspberry Pi!   =)
Posts: 8
Joined: Sun Aug 21, 2011 8:30 am
by Tomo2k » Sat Jan 14, 2012 3:59 pm
Gert said:



- One complication is the level translation between 3V3 and 5V SPI.
Are the buffer chips used on Gertboard fast enough to do this?


Sorry but they are buffers, not level translators.


Ok, thanks.

As the RPi GPIO isn't 5V tolerant and there isn't much 3V3 current available, it's probably better for a dedicated CANbus adapter to use level translators rather than 'faking it' with tolerant 3V3 buffers.

Do you have any experience with any level translators?

- I see that Maxim have a pretty wide range of very fast ones, but I've never needed to do this before so I don't know what to look for.
Posts: 126
Joined: Mon Dec 19, 2011 10:00 pm
by Ianw » Sun Jan 15, 2012 2:59 pm
There are quite a few Bluetooth CAN Bus interfaces available, I'm not too sure how effective they are but might be one avenue to explore? The typical price is around £20 for an ODBII device - Much less than I could build one for.

I created a CAN interface from components for an Android device and it ended up costing around £120 to make from scratch.
Posts: 46
Joined: Sun Dec 25, 2011 10:54 am
by Fletchybaby » Thu Feb 02, 2012 10:17 pm
Hooking up a CAN interface is certainly something that I'm after doing with the rPi. I've hooked a MCP2510 to a MPC555 before & it was relatively straightforward. Not the most performant of CAN controllers but just fine for a lot of purposes as long as it doesn't have to deal with loads of message IDs. A nice little NXP transceiver to go with it, smashing. I haven't delved into the specs of the rPi or Gertboard yet, but a decent amount of current could be required (e.g. a NXP 82C250 could sink 160mA in a bus fault condition). I need to investigate a bit more...

I'd go with SPI every time compared to I2C. Maybe its just me, but I find SPI far easier to scope/analyze/debug.

Why stop at CAN though?! I'd love to get hold of other automotive expansions, LIN, Flexray, MOST, OBDII, etc.
Posts: 1
Joined: Thu Feb 02, 2012 9:55 pm
by gja » Sat Mar 03, 2012 9:33 pm
There is already a CANbus implementation for Linux.
It's called SocketCAN.
http://en.wikipedia.org/wiki/SocketCAN

It turns the CAN interface into a network device.
It supports the MCP2515 over SPI.

I believe it's even part of the mainline kernel.

We have been working with an older version of SocketCAN on an ARM9 platform and basic operation is pretty stable. Not sure on the current status though.

Here's an example for enabling socketcan with the MCP2515 driver on another platform, could be useful as inspiration:

https://www.ridgerun.com/developer/wiki/index.php/How_to_configure_and_use_CAN_bus#MC2515

I've just found the code in the Raspberry Pi repository:

https://github.com/raspberrypi/linux/tree/rpi-patches/drivers/net/can

So who has a Raspberry Pi and an MCP2515 setup? :-)
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by hzrnbgy » Sun Mar 04, 2012 2:43 am
IanW said:


There are quite a few Bluetooth CAN Bus interfaces available, I"m not too sure how effective they are but might be one avenue to explore? The typical price is around £20 for an ODBII device - Much less than I could build one for.

I created a CAN interface from components for an Android device and it ended up costing around £120 to make from scratch.


if you can handle SMD soldering, I've used this level translator before and they work perfectly

http://www.mouser.com/Search/P.....T2002DP118
Posts: 106
Joined: Mon Dec 26, 2011 10:55 pm
by Jim Manley » Sun Mar 04, 2012 6:31 pm
It appears that CAN-USB interfaces start around $14 for the adapter cables used to connect a laptop to a vehicle's CAN bus:

http://www.google.com/search?q.....=0CI4BEK0E

Of course, the USB device drivers that come with the vehicle engine control analysis/editing software used with these cables (that starts around $100 and goes up quickly from there) are for Windows.  However, if there are open CAN driver sources, perhaps they will work with these cables?

CAN controllers come in low and high speed versions, so, that also needs to be taken into account, and I assume that's a SMOP ("small matter of programming") as the hardware grunts like to say when passing off the really difficult tasks to us software geniuses ;)
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
User avatar
Posts: 1358
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, USA
by gja » Sun Mar 04, 2012 9:17 pm
The cheap "USB-CAN converters" are mostly ELM327 based.

IIRC the ELM 327 CAN interface are not full CAN. They are OBD diagnostics interfaces. The ELM 327 is based on a microcontroller that filters data from the vehicle CANbus.
So for monitoring a car, they can do the basics.

The next step up from the ELM327 is the newer STN1110 OBD interface. It's faster than the ELM327 and cheaper.
http://www.obdsol.com/stn1110/
But still it is very focused on vehicle applications.

However if you want to monitor "any" (not strictly vehicle) CAN bus (industrial applications etc), you'd probably be better off (in my opinion) with the raw power of an SPI to CAN interface as the MCP2515 together with a CAN transceiver IC.

This MCP2515 interface is already supported in Linux under socketcan. You "just" need the hardware. Basically it's two IC's and some added components. It could be added to the Gert-board? Or it could be made into a small plug-on board for the GPIO connector so e.g. it could be easily mounted in a car for logging.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by Jim Manley » Mon Mar 05, 2012 2:08 am
I assumed the intended application was for basic vehicle monitoring, so, that's why I pointed out the very cheap diagnostic cables.  If the drivers exist (or can be built via simple recompile), that would probably keep the educationally-minded audience entertained for life.  Hooking up a $35 single board educational computer to a full-tilt industrial CAN network via a controller that costs well into the hundreds of dollars, for starters (as the intended audience isn't going to have the chops to develop their own) is a bit of a mismatch

If someone who does know what they're doing provides at least a PCB and parts sources, if not a kit or pre-assembled unit, then the industrial control R-Pi variant would start to make sense, except that then they'll need access to somewhere that would allow them to hook up their untested stuff to a CAN network controlling big iron that can definitely cause some damage to equipment and products, if not also people (e.g., industrial robots - stand by for SkyNet! ;)   Yeah, good luck with that.

Anywhere that's rigged for training on industrial controllers is already going to have much more adequate and appropriate development and testing tools available.  If someone were bore-sighted on doing this, though, more power to them, as they would definitely learn something.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
User avatar
Posts: 1358
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, USA
by gja » Mon Mar 05, 2012 7:06 am
I agree that having full access to a CANbus interface can present some security issues. You don't want to start sending your own CAN messages in a vehicle if you're not exactly sure what you're doing.

However, the MCP2515 does have a listen-only mode. So you could hook it up to a car for monitoring without any possibility of doing damage.

Besides, adding the aspect of danger and responsibility in a CANbus training is also quite educational, no? :-)
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Mon Mar 05, 2012 7:42 pm
In case anyone has an ELM327 OBD-USB interface and a Linux PC at hand: here's a link to some pointers on how to install a scantool utility. (also CAN-Bluetooth info inside)

http://ubuntuforums.org/archiv.....01550.html

Not sure if it can be used without GUI.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by plugwash » Mon Mar 05, 2012 8:42 pm
Gert said:


Sorry but they are buffers, not level translators. I hesitated to use level translators and decided against them as it was an education board and would complicate building the hardware. If I recall: you can drive 5V logic from 3V3


Depends WHICH 5V logic, you have to be careful to check the levels especially with devices that have schmitt trigger inputs. In general inputs advertised as "TTL compatible" on 5V CMOS devices should be OK to drive from 3.3V signals.

If you need a buffer or other device to go from 3.3V to 5V I would suggest using a part from the 74HCT series


the only thing you need is that the buffers are 5V tolerant. As the Gertboard takes any 20-pin compatible 74xx244 there might be one available which is 5V tolerant.


The LVC family should fit the bill and checking farnell the 74LVC244 does exist.
Forum Moderator
Forum Moderator
Posts: 2436
Joined: Wed Dec 28, 2011 11:45 pm
by FlappySocks » Tue Mar 06, 2012 9:16 pm
I'm interested in this project.  I'm willing to put some money up for PCBs too.

I have been thinking along the lines of putting together an automotive expansion PCB, which would be useful for anyone that wants to use their Raspberry Pi in a motor vehicle. It would have the following:

GPS

CAN bus

Opto isolated IO's

12v-5v DC-DC Converter (if it's cost effective)

I'm experimenting at the moment with a cost effective gps receiver that is suitable for the Raspberry Pi. It connects to the UART via a MAX3232. I have access to a vehicle tracking platform & online maps, which with a GSM dongle can send its location back (including any data it captures).

I used to run a part-time company making bespoke ECU's for BMWs a while back, but my main activity over the past 20 years has been vehicle tracking & networking.  One thing that would be usfull to me is a cheap diagnostic device that I can send customers, and ask them to plug into their tracking equipment so that I can remote monitor it. The Raspberry would be ideal for this, which is why I am happy to put up some money.

I also thought it might be fun thing to play with and see what I can do with it, particularly with the CAN bus.

Could do with some help designing the psb.
Posts: 7
Joined: Tue Mar 06, 2012 8:15 pm
by tzj » Tue Mar 06, 2012 9:39 pm
@ StuartJ

your list seems a little short, I"ve been wanting to do an automotive network educadion kit for a while, but am now a bit stuck...
http://www.raspberrypi.org/for.....nd-control
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)
User avatar
Posts: 301
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK
by FlappySocks » Wed Mar 07, 2012 1:22 am
@tzj, thats quite a list, and would require a lot of of hardware.  Simple on/off stuff is easy to do with Raspberry Pi, but you would quickly run out of outputs.  Fuel injection, you can do by building something like a Megasquirt.  Here is one I built for my car. It could be plugged into the Raspberry by using the RS232 connector on the left hand side.



My goal is to build something that will be useful for the person that just wants to put their Raspberry in their car, for something simple like data logging, and running their spotify player.

The RS232 breakout pcb by Zuzebox could be a good start, and add CAN to that.

Anyone with CAN experience want to help?
Posts: 7
Joined: Tue Mar 06, 2012 8:15 pm
by tzj » Wed Mar 07, 2012 3:02 am
kudos on the fuel injection board ;)

well from checking out various topics on here, I too came to realise that the pi can"t do all the controlling by itself but relay info from the bus and other systems, and send setting changes to those systems, due to the main fact the pi has no clock.
the gpu on the pi would be, well guessing it would be able to display most visuals in the vehicle... the list is just to show what could be "emulated" for educating students in automotive network programming using "cheap" system boards.
http://www.themagpi.com <---- Checkout the MagPi
(The MagPi - Co-Founder)
User avatar
Posts: 301
Joined: Thu Jan 05, 2012 3:09 pm
Location: Barnsley, SY, UK
by FlappySocks » Wed Mar 07, 2012 3:16 am
You can get the date & time from the board I wish to produce with its gps.

I"m thinking it might be a good idea to multiplex the UART to provide an additional rs232 as the gps uses it.
Posts: 7
Joined: Tue Mar 06, 2012 8:15 pm
by gja » Wed Mar 07, 2012 8:23 am
Schematic on p26 from MCP2515 application AN212 from Microchip:
http://ww1.microchip.com/downl.....00212c.pdf

It handles the part from CANbus to SPI interface.
Check earlier posts on level conversion between Raspberry Pi SPI and MCP2515 SPI.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm