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
- Gert van Loo
- Posts: 2487
- Joined: Tue Aug 02, 2011 7:27 am
Re: CAN controller
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 ... i2c-gpio.c
I suggest you have a look at that to start with.
http://lxr.linux.no/linux+*/drivers/i2c ... i2c-gpio.c
I suggest you have a look at that to start with.
Re: CAN controller
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
Any suggestions on SPI? would be good if I can access it using python
- Gert van Loo
- Posts: 2487
- Joined: Tue Aug 02, 2011 7:27 am
Re: CAN controller
There are now SPI Linux drivers for the Raspberry-Pi SPI interface available. I expect these to be standard in all future distributions.
Re: CAN controller
See older thread on CANbus here:
http://www.raspberrypi.org/phpBB3/viewt ... lit=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.
http://www.raspberrypi.org/phpBB3/viewt ... lit=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.
- Gert van Loo
- Posts: 2487
- Joined: Tue Aug 02, 2011 7:27 am
Re: CAN controller
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.gja wrote:See older thread on CANbus here:
http://www.raspberrypi.org/phpBB3/viewt ... lit=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.
Re: CAN controller
I just ordered the AVR CAN-SPI adapter.
Looking forward to try and get it to work.
Looking forward to try and get it to work.
Re: CAN controller
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.Gert van Loo wrote: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.gja wrote:See older thread on CANbus here:
http://www.raspberrypi.org/phpBB3/viewt ... lit=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.
--
Kári
Re: CAN controller
Indeed.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
I'll do it with conversion then.
Re: CAN controller
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.
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.

Re: CAN controller
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...
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...
Re: CAN controller
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
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

Re: CAN controller
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...
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...
Re: CAN controller
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
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
Re: CAN controller
@gja:
@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?
I see.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 knowFor the kernel build the voltage level doesn't matter.
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: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 http://www.bootc.net/projects/raspberry-pi-kernel/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.
@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?
Re: CAN controller
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.
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.
Re: CAN controller
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
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
Re: CAN controller
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.
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.
Re: CAN controller
This looks inspiring:
http://lnxpps.de/can2udpe/openwrt/
http://lnxpps.de/can2udpe/openwrt/
Re: CAN controller
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?
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?
Re: CAN controller
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....
@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....
Re: CAN controller
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.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
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.
Re: CAN controller
Losless 1Mbit reception may indeed be tricky.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.
It will probably be useful to do some performance testing, but we have to get it working first.
Re: CAN controller
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...(!)
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...(!)
Re: CAN controller
@gja
thanks for the hint, I tried to ask to the forum moderator and I'm waiting for an answer.
thanks for the hint, I tried to ask to the forum moderator and I'm waiting for an answer.