CAN controller


541 posts   Page 1 of 22   1, 2, 3, 4, 5 ... 22
by zorce » Wed May 30, 2012 10:31 pm
Hi,

I just got my Raspberry Pi and i'am trying to find out a way to interface the pi with a CAN controller, is it possible to use the GPIOs for this? and if so any tips on how to get started with that kind of driver?

br, zorce
Posts: 2
Joined: Wed May 30, 2012 10:18 pm
by Gert van Loo » Thu May 31, 2012 1:33 pm
The BCM2835 device has no dedicated CAN interface (It has SPI, I2C, PCM etc. but not CAN) so you have to write the protocol using what is called 'bit banging'. There is code available which does that:
http://lxr.linux.no/linux+*/drivers/i2c/busses/i2c-gpio.c
I suggest you have a look at that to start with.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1984
Joined: Tue Aug 02, 2011 7:27 am
by zorce » Fri Jun 01, 2012 7:11 am
Thanks I will then probably use SPI instead with the MCP2551 and MCP2515 CAN controller and transceiver.

Any suggestions on SPI? would be good if I can access it using python
Posts: 2
Joined: Wed May 30, 2012 10:18 pm
by Gert van Loo » Fri Jun 01, 2012 7:25 am
There are now SPI Linux drivers for the Raspberry-Pi SPI interface available. I expect these to be standard in all future distributions.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1984
Joined: Tue Aug 02, 2011 7:27 am
by gja » Sat Jun 02, 2012 3:31 pm
See older thread on CANbus here:
viewtopic.php?f=41&t=2298&hilit=canbus

Zorce,
If you find a decent MCP2515 board available, please post here.
I have been looking around and found one for AVR:
http://www.avrcard.com/products/can_adapter.htm
But I still need to read up in the thread above about whether the level conversion is necessary or not.
Depending on the outcome I may order one.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by Gert van Loo » Sat Jun 02, 2012 8:14 pm
gja wrote:See older thread on CANbus here:
viewtopic.php?f=41&t=2298&hilit=canbus

Zorce,
.....
But I still need to read up in the thread above about whether the level conversion is necessary or not.
Depending on the outcome I may order one.


I looked up the schematic and the device. I had a quick look so you may need to check more thorough. From what I saw the device can work from 2.7 to 5V so although the schematic says 5V you should be able to connect it to the Raspi 3V3 and run it from there. That way you do not need a level converter.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1984
Joined: Tue Aug 02, 2011 7:27 am
by gja » Sat Jun 02, 2012 9:35 pm
I just ordered the AVR CAN-SPI adapter.
Looking forward to try and get it to work.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by kaos » Sun Jun 03, 2012 10:46 am
Gert van Loo wrote:
gja wrote:See older thread on CANbus here:
viewtopic.php?f=41&t=2298&hilit=canbus

Zorce,
.....
But I still need to read up in the thread above about whether the level conversion is necessary or not.
Depending on the outcome I may order one.


I looked up the schematic and the device. I had a quick look so you may need to check more thorough. From what I saw the device can work from 2.7 to 5V so although the schematic says 5V you should be able to connect it to the Raspi 3V3 and run it from there. That way you do not need a level converter.


That applies to the bus controller, but there is also a transceiver there (PCA82C251), and that seems, unfortunately, to be a 5V only device. Should still be usable with Raspi, but will need to be powered from the 5V rail, and level conversion.

--
Kári
Posts: 101
Joined: Mon Mar 26, 2012 8:14 pm
by gja » Sun Jun 03, 2012 4:11 pm
That applies to the bus controller, but there is also a transceiver there (PCA82C251), and that seems, unfortunately, to be a 5V only device. Should still be usable with Raspi, but will need to be powered from the 5V rail, and level conversion


Indeed.
I'll do it with conversion then.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Sat Jun 23, 2012 6:29 pm
Ok,
So a colleague pointed me to this level conversion board:
http://www.sparkfun.com/products/8745

So i have one wired up:
MISO : Raspberry Pi 3v3 Tx in - 5V Tx out to SPI-CAN Board
MOSI: 3v3 Rx out - 5V Rx in
SCLK: 3v3 Tx in - 5V Tx out

For the chip select: that would need an extra Tx, so an extra interface board (they have 2 Tx's and 2 Rx's)
However, I'm thinking of just leaving the chip select line hardwired so it's always on.
I'll see what works.
So for hardware, there's a starting point.

For software :
I've requested Chris Boot if he could include the necessary kernel modules for CANbus in the kernel builds he does. He's willing to, but he (and I) are wondering whether some extra config is necessary.
See here: http://www.bootc.net/projects/raspberry ... ment-22671

I have also found this: https://www.ridgerun.com/developer/wiki ... bus#MC2515
This looks like it's close to what needs to be done to get the MCP working in Linux.
In the coming days (weeks?) I will probably try to build an RPI kernel using this input and see where I get.

If anyone wants to jump in, feel free. :)
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Sat Jun 23, 2012 6:48 pm
https://www.ridgerun.com/developer/wiki ... bus#MC2515

About half-way down in the post I just noticed that a GPIO needs to be available to receive interrupts from the mcp2515.
Seems like the conversion board will be fully used then:

MISO : Raspberry Pi 3v3 Tx in - 5V Tx out to SPI-CAN Board
MOSI: 3v3 Rx out - 5V Rx in
SCLK: 3v3 Tx in - 5V Tx out
and
GPIO 3v3 Rx out - 5V Rx in interrupt from mcp2515.

Some more figuring out to do...
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by z3r0c00l » Wed Jun 27, 2012 4:48 pm
Hi,
i want to do exactly the same. But I think you can make it easier: I will run the MCP2515 from the 3V3 supply the rpi is using and only run the CAN Tranceiver (MCP2551) at 5V. Level conversion is only needed between those two chips. So only two signals lines RXCAN and TXCAN need level translation. And with the lower voltage for the MCP2515 it also saves some energy :-)

Furthermore for a quick and dirty solution it should also be possible to omit the level conversion chips, by only using a resistor divider between RXCAN-RXD. The MCP2551 should detect the 3V3 as high level.

The problem I have is the kernel configuraion, I don't get it yet :-)
Posts: 11
Joined: Wed Jun 27, 2012 4:29 pm
by gja » Sat Jun 30, 2012 7:36 am
Maybe it can be easier on 3v3, but since I'm using a pre-made SPI-CAN board I have done the conversion anyway.

For the kernel build the voltage level doesn't matter.
For my first attempt I used these instructions: http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux
and tried to do the menuconfig build.
The building of the kernel took way too long, I suspect my configuration file had almost everything selected.
When doing this I wanted to configure the kernel to include the MCP2515 driver under /net/can/device drivers but I could only select it as a module. Not sure why.
I also need to pick one of the new SPI drivers to insert in the kernel sources before my next attempt.
I've seen one from bitwizard.nl and one in Chris boot's build.
Off to some more reading up on that...
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by vlick » Mon Jul 02, 2012 10:42 am
Hi at all, I'm new on this thread.
Actually I also follow a very similar Raspberry thread (CAN Interface), and so I'll try to split myself on both.

I'd like to start with a design proposal: please feel free to comment and to improve/modify it.

1) HW Components for CAN

- Controller: Microchip MCP21515, package TSS0P20, 16 MHz clock, with 3.3 V. powered.

- Transceiver: TI SN65HVD251, package SOIC 8, 5V. supplied. It's widely used in Can HS applications, like J1939, NMEA2000 and ISO11783.

- Buffer/shifter (like 74AHCT126) to interface them

2)Resorces (pins) used:

- SPI: SOUT, SIN, CLK 3 dedicated

- Chip Select 1 dedicated (SFRM) or GPIO

- Reset 1 Hw driven or GPIO

- Interrupt 3 at least (up to 5) lines (GPIO) interrupt enabled

With 3 lines we can maximize the reading efficency (recommended), with 5 lines also the writing efficency.

I can share some schematics just to discuss better how to procede.

vlick
Posts: 12
Joined: Wed Apr 04, 2012 8:35 am
by z3r0c00l » Tue Jul 03, 2012 12:28 pm
@gja:

Maybe it can be easier on 3v3, but since I'm using a pre-made SPI-CAN board I have done the conversion anyway.


I see.

For the kernel build the voltage level doesn't matter.


i know

For my first attempt I used these instructions: http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux
and tried to do the menuconfig build.
The building of the kernel took way too long, I suspect my configuration file had almost everything selected.
When doing this I wanted to configure the kernel to include the MCP2515 driver under /net/can/device drivers but I could only select it as a module. Not sure why.
I also need to pick one of the new SPI drivers to insert in the kernel sources before my next attempt.
I've seen one from bitwizard.nl and one in Chris boot's build.
Off to some more reading up on that...


From what I read, the MCP2515 has to be attached to one of the SPI bus/select lines in the board definition file of the kernel, and there is my problem. I don't know how to do that. Would be cool if spi device drivers could be attached at runtime but I think this isn't possible at the moment. I refere to bootc:

The SPI driver exposes two device files (/dev/spidev-0.0 and /dev/spidev-0.1), one for each cable select line (CE0 and CE1 on RPi). Look in the /usr/include/linux/spi/spidev.h header for how to drive this properly. There are also Python bindings available and I’m sure there are others available for other languages too. If you want to bind a kernel SPI device driver to an SPI chipselect line, you’ll have to edit the source and build your own kernel. Sorry, but that’s how it works in 3.2.

from http://www.bootc.net/projects/raspberry-pi-kernel/

@vlick:

HW looks good. I will use one interrupt line only, speed doesn't matter to my application. Is the kernel driver capable of using more interrupt lines?
Posts: 11
Joined: Wed Jun 27, 2012 4:29 pm
by vlick » Tue Jul 03, 2012 5:20 pm
I'm more worried about SW side than HW side.

From what I know (but I'm still waiting for my Raspberry...) we could have at least two options from driver approach:
a) SocketCan, which is generally supported in kernel 2.6 (also with MCP2515)
b)Character driver

I don't know too much about option a, but the main points are about SPI management when using socket.
Option b could leave more freedom (maybe...) about using more interrupt lines (Tx, Rx, Generic) with different handlers for the same device.
One important point is, in both option, to possibly manage the "bottom half" of the SPI driver (which exchange data with the MCP) using tasklet instead of work queue for performance reason.
Posts: 12
Joined: Wed Apr 04, 2012 8:35 am
by gja » Tue Jul 03, 2012 8:00 pm
For the driver approach I'm definitely going for socketcan.
We have been using this at work for a few years with a 2.6 kernel. That sw came prepackaged, so unfortunately I don't know the config details.

The MCP kernel driver is able to use one interrupt line from the MCP2515.
https://www.ridgerun.com/developer/wiki ... upt_signal
Which is nice, since my MCP board has only one INT out.

If I followed the instructions from http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux ,
I can use the menuconfig build.
Under "Networking support" you select "CAN bus subsystem support" and go one level deeper and then select "CAN device drivers" and there you can include the CAN bit timing calculation and the MCP driver.

So since we need one extra GPIO for the interrupt line, I would suggest trying GPIO 25 since it's physically next to the SPI bus.
http://elinux.org/RPi_Low-level_periphe ... ut_.28GPIO.

However, where to configure this and how to include the spi...



I
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Tue Jul 03, 2012 8:39 pm
Next action:
I'm now replacing my vanilla RPI kernel sources with the kernel of Chris Boot to have the SPI driver in.

http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux
In these instructions, simply replace the git command with

git clone https://github.com/bootc/linux.git

So SPI is in, MCP2515 driver is in. Now off to find the config files we need to edit.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Fri Jul 06, 2012 7:39 pm
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by gja » Fri Jul 06, 2012 8:30 pm
From the link in the post above it seems that we need to be adding a filled in copy of the struct in
https://github.com/raspberrypi/linux/bl ... /mcp251x.h
in the platform/board definition file.

Apparently this Raspberry Pi uses a BCM2835 SoC.
However this SoC belongs to the family of the BCM2708.
So I think that the platform files are to be found here:
https://github.com/raspberrypi/linux/bl ... /bcm2708.c

Can anyone confirm this?
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by z3r0c00l » Sun Jul 08, 2012 11:59 am
I will also go with socketcan.

@gja:

I will also go with bootc kernel for the rpi because of the spi drivers. The file seems right, if you look at the changed files, the bcm2708.c is one of them. Nevertheless I am still lost....
Posts: 11
Joined: Wed Jun 27, 2012 4:29 pm
by moulinex » Mon Jul 09, 2012 5:44 am
gja wrote:For the driver approach I'm definitely going for socketcan.
We have been using this at work for a few years with a 2.6 kernel. That sw came prepackaged, so unfortunately I don't know the config details.

The MCP kernel driver is able to use one interrupt line from the MCP2515.
https://www.ridgerun.com/developer/wiki ... upt_signal
Which is nice, since my MCP board has only one INT out.

If I followed the instructions from http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux ,
I can use the menuconfig build.
Under "Networking support" you select "CAN bus subsystem support" and go one level deeper and then select "CAN device drivers" and there you can include the CAN bit timing calculation and the MCP driver.

So since we need one extra GPIO for the interrupt line, I would suggest trying GPIO 25 since it's physically next to the SPI bus.
http://elinux.org/RPi_Low-level_periphe ... ut_.28GPIO.

However, where to configure this and how to include the spi...

I


Hello, MCP2515 has only two receive buffer. If only INT pin is used you need to read read which buffer received message (one SPI transmission). One possibility is to use pins RX0BF and RX1BF which can be directly use as receive interrupts and indication which receive buffer is needed to be read.

With 1Mbit CAN speed you could have 3 messages within ~0,13 ms. Before this time you should need to read messages from buffers. So it is very important to optimize MCP2515 receiving handler.
Posts: 3
Joined: Sun Jul 08, 2012 12:00 pm
by gja » Tue Jul 10, 2012 7:50 pm
Hello, MCP2515 has only two receive buffer. If only INT pin is used you need to read read which buffer received message (one SPI transmission). One possibility is to use pins RX0BF and RX1BF which can be directly use as receive interrupts and indication which receive buffer is needed to be read.

With 1Mbit CAN speed you could have 3 messages within ~0,13 ms. Before this time you should need to read messages from buffers. So it is very important to optimize MCP2515 receiving handler.


Losless 1Mbit reception may indeed be tricky.
It will probably be useful to do some performance testing, but we have to get it working first.
Posts: 22
Joined: Sat Mar 03, 2012 9:24 pm
by vlick » Wed Jul 11, 2012 8:15 am
In my opinion is correct to start with some simple tests, but having clear in mind any weakness of this initial approach and how to overcome them: since this project aims to be usefull to a wide spread of applications performance shall be taken in very high consideration.

In order to do an additional step in this direction, I'll share a schematic I designed some years ago when I worked with PXA microcontroller when I added the CAN controller.
It was a board designed for marine and automotive applications.

PS: I tried the .pdf and .bmp files but these extensions are not allowed; the zip file is too big (234 KB) and I'd need some suggestions about how to share it since I'm not skilled in this item...(!)
Posts: 12
Joined: Wed Apr 04, 2012 8:35 am
by vlick » Sat Jul 14, 2012 10:36 am
@gja
thanks for the hint, I tried to ask to the forum moderator and I'm waiting for an answer.
Posts: 12
Joined: Wed Apr 04, 2012 8:35 am