CAN controller

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?

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.

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.htmBut 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.

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.

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.

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

So i have one wired up:MISO : Raspberry Pi 3v3 Tx in - 5V Tx out to SPI-CAN BoardMOSI: 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#MC2515This 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.

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.

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_Linuxand 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...

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_Linuxand 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 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.

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.

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.

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.

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.

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.

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.

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...(!)