Zeta
Posts: 72
Joined: Wed Dec 12, 2012 9:51 pm

Re: CAN controller

Thu Dec 12, 2013 6:41 pm

Hi Shan,
shanlasani wrote:which programming platform (C++, or Python) should i use to do this job....
The one you are the most comfortable with ?

Plain C is enough for a simple task like answering to a message. Take a look at the messages above, (with the link to wikipedia) for some examples.
If you want to do more (like drive a UI) and know better some other language, then choose them (for me it's C++/Qt).

Martin presented here what is needed to do it with Python : http://www.raspberrypi.org/phpBB3/viewt ... 54#p456654

chadwick
Posts: 3
Joined: Wed Dec 18, 2013 2:57 am

Re: CAN controller

Wed Dec 18, 2013 3:03 am

Has anyone had any luck getting this board from MikroElektronika to work with the RPi? http://www.mikroe.com/add-on-boards/com ... on/canspi/

It is a 5V board, so I've hooked to up with voltage divisor resistors on each GPIO pin, but haven't had any luck getting actual CAN data to or from the board. Both the old and new mcp251x and mcp2515 drivers can initialize the mcp2515 via SPI, but don't seem to actually transfer data from the kernel to the chip itself.

I've verified the board works on my bus with an Arduino setup, so it's not bad hardware... at least not the board.

Any suggestions on what I might be able to look for? It feels like something must be mis-wired given that the mcp2515 initialized okay via SPI.

Almaz
Posts: 7
Joined: Sun Oct 20, 2013 10:40 pm

Re: CAN controller

Wed Dec 18, 2013 1:49 pm

Hello)
I get a problem.
My raspberry receive a packets in loop with:

Code: Select all

res = read( param.sock, &frameReceive, sizeof( rameReceive ) );
if( res>0 )
{
perror("read\n");
exit( EXIT_FAILURE );

Also i connected to the can-bus via can hacker.
After some time, read () function simply ceases to operate - raspberry stop to receive packets. But Can hacker continues to log packets!
In command line on raspberry i enter once and again:

Code: Select all

ip -details -statistics link show can0 
It show me no any error. But no increase the count of received packets!
After the:

Code: Select all

ifconfig can0 down
comand

Code: Select all

ip -details -statistics link show can0
begin to show the increasing of received packets, and my program, stopped on read() function, begin to receive packets!
Why can socket stop to receive packet? Is there any way to detect this situation from program and make to "restart" the interface as it do the "ifconfig can0 down"?
Thanks for any advice)

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: CAN controller

Thu Dec 19, 2013 2:37 am

Hi chadwick ,

You should just put the resistor divider to the SO pins. You need to protect the Pi from 5V signal but not the opposite.
If you use resistor divider to the other pins you will reduce the 3.3 v to 2.2V. this mean no clock and data IN signal always 0.


The specification for mcp2515 high level input is 0.7 * VDD = 3.5 Volts. Technically it won't work but often it does.

Use an adafruit/sparkfun voltage converter adapter.

Update and the INT pin also
Daniel
Last edited by danjperron on Sun Dec 22, 2013 11:02 am, edited 1 time in total.

Almaz
Posts: 7
Joined: Sun Oct 20, 2013 10:40 pm

Re: CAN controller

Thu Dec 19, 2013 10:15 am

Сontinuation:
When Can0 stop to receive packets, it seems like the can interface go to bus-off state. But this state does not detected with

Code: Select all

ip -details -statistics link show can0
. In addition to this i configure can0 interface with restart option:

Code: Select all

ip link set can0 up type can restart-ms 100 bitrate 1000000
. No bus-off detected - no restart. On oscilloscope i see, that there is dissapears signals CS and SCK from Raspberry on SPI interface ( i use MCP2515 ). I will write again: sending from command line:

Code: Select all

ifconfig can0 down
begin the receiving of packets. If instead of sending "ifconfig can0 down" i try to transmit packets from raspberry, i get error "no buffer space available". Why it is happen? And how to detect this situation from program?

cadsenthilkumar
Posts: 29
Joined: Wed Sep 25, 2013 11:09 am

Re: CAN controller

Thu Dec 19, 2013 11:53 am

danjperron wrote:I prefer to have it more automatic,

This is the best method I found so far,

P.S. I'm log as root. If you are not , use the sudo command.

1- from a stock raspbian we need to change the kernel

Code: Select all

cd /tmp
wget http://lnxpps.de/rpie/rpi_can_3611_spi_dma.tar.xz
cd /
tar Jxvf /tmp/rpi_can_3611_spi_dma.tar.xz 
depmod -a
reboot

2- We need to update the /etc/modprobe.d/raspi-blacklist.conf file
Enable the spi-bcm2708 driver and blacklist the mcp251x driver

Code: Select all

#blacklist spi-bcm2708
blacklist mcp251x
3- Let's add the spi-config and the new mcp2515 modules in file /etc/modules

Code: Select all

# MCP2515 configuration with /INT on GPIO25 and 16MHz clock
spi-config devices=\
bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release

#and of course the mcp2515 driver 
mcp2515
4- And now the network can0 auto connect. Add these in the /etc/network/interfaces

Code: Select all

auto can0
iface can0 inet manual
     pre-up /sbin/ip link set can0 type can bitrate 125000
     up /sbin/ifconfig can0 up
     down /sbin/ifconfig can0 down
and reboot!

if everything is ok , the command ifconfig should report something like

Code: Select all

root@RpiTest:~# ifconfig
can0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:16  Metric:1
          RX packets:6674 errors:0 dropped:0 overruns:0 frame:0
          TX packets:58915 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 lg file transmission:10 
          RX bytes:6695 (6.5 KiB)  TX bytes:176745 (172.6 KiB)
I don't know if this is the best method but it is working fine for me.

Daniel
Hi ,
its nice to see your post..vcan is working.
did you checked with the real can hardware?
when i do with connecting the can hardware is not working..
candump is not showing the transmitted bits...
Help me at this.
Thanks .

cadsenthilkumar
Posts: 29
Joined: Wed Sep 25, 2013 11:09 am

Re: CAN controller

Thu Dec 19, 2013 12:52 pm

HI all
i gone through all above post.But i didn't get any idea to compile or per-compiled kernel what i want to do ??????? :?
How to configure the Rpi to communicate with CAN BUS through SPI from RPi...
Help me i'm in demand ..!!!!!!!
Pls post the step by step procedure to make my Rpi to communicate with CAN bus ..


I have NOOBS -Raspbian installed in SD card., Home made SPI to CAN board with 8MHz oscillator, MCP2515 and MCP2551

Zeta , Martin ,Gerd pls i need your help dude ....


Thanks in advance.. :)
All is well

notro
Posts: 695
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: CAN controller

Thu Dec 19, 2013 2:28 pm

I have a kernel that I release for my framebuffer drivers.
To help out people in this thread, I have included CONFIG_CAN_MCP251X and Martin's spi-config module.
My intention has been to test it with a MCP2515 module I have, but it's not my top priority, and my display work takes all my spare time.
If someone would be so kind to test the kernel it would be great.

The kernel is installed with rpi-update. See README: https://github.com/notro/rpi-firmware/tree/master

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: CAN controller

Thu Dec 19, 2013 2:32 pm

you use could my method
http://www.raspberrypi.org/phpBB3/viewt ... 18#p447418

but change the frequency in step3 to 8Mhz.

Daniel

cadsenthilkumar
Posts: 29
Joined: Wed Sep 25, 2013 11:09 am

Re: CAN controller

Fri Dec 20, 2013 5:44 am

danjperron wrote:you use could my method
http://www.raspberrypi.org/phpBB3/viewt ... 18#p447418

but change the frequency in step3 to 8Mhz.

Daniel
I have changed the frequency as 8Mhz in step3,when i connect the real hardware
ip -s -d link show can0 is showing
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10
link/can
can state STOPPED restart-ms 0
bitrate 500000 sample-point 0.75
tq 500 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
mcp251x: tseg1 3..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
clock 4000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 0 0 0
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0

the received packets is not increasing .when i give candump its waiting.
help me with the possible solution for this.
Thanks in advance.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: CAN controller

Fri Dec 20, 2013 12:36 pm

Hi cadsenthilkumar

What is on the other side of the CAN bus?


Daniel

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: CAN controller

Sat Dec 21, 2013 5:20 am

Hi cadsenthilkumar,

I did check the method and didn't find any problem.

I changed the crystal to 8Mhz and everything was ok.

Maybe the best way to check is to use two MCP2515 on the raspberry pi and see if you can send from one to the other.

On the First MCP2515 I use the schematic from this site.

http://skpang.co.uk/catalog/pican-canbu ... -1196.html


And for the second MCP2515 I use CE1 instead of CE0 and GPIO24 instead of GPIO25

I add the second device into the /etc/modules

Code: Select all

root@RpiTest:~# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835

# MCP2515 configuration w[code]
ith /INT on GPIO25 and 16MHz clock
spi-config devices=\
bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release,bus=0:cs=1:modalias=mcp2515:speed=10000000:gpioirq=24:pd=20:pds32-0=8000000:pdu32-4=0x2002:force_release
#and of course the mcp2515 driver
mcp2515
root@RpiTest:~#[/code]

B.T.W. The second MCP2515 run with a 8Mhz crystal.


and into /etc/network/interfaces

Code: Select all

root@RpiTest:~# cat /etc/network/interfaces
auto lo

iface lo inet loopback
#iface eth0 inet dhcp

allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
#pre-up ifdown eth0; echo -n '1-1.1:1.0' >/sys/bus/usb/drivers/smsc95xx/unbind
#post-down echo -n '1-1.1:1.0' >/sys/bus/usb/drivers/smsc95xx/bind
iface default inet dhcp

auto can0
iface can0 inet manual
     pre-up /sbin/ip link set can0 type can bitrate 125000
     up /sbin/ifconfig can0 up
     down /sbin/ifconfig can0 down

auto can1
iface can1 inet manual
     pre-up /sbin/ip link set can1 type can bitrate 125000
     up /sbin/ifconfig can1 up
     down /sbin/ifconfig can1 down
root@RpiTest:~# 
I open 2 ssh consoles

On the first one I type

Code: Select all

root@RpiTest:~# candump can0
On the second console I type

Code: Select all

root@RpiTest:~# cansend can1 7ef#0101050000000000
And I got a respond on the first console

Code: Select all

can0  7EF   [8]  01 01 05 00 00 00 00 00
This is a neat trick to check if the canbus is working on the Rpi side.

Image

Daniel

chadwick
Posts: 3
Joined: Wed Dec 18, 2013 2:57 am

Re: CAN controller

Sun Dec 22, 2013 6:47 am

[That makes perfect sense but I hadn't really considered the direction of the signals until you said this. D'oh!

I redid the circuit so that only the INT and MISO pins had voltage dividers and the rest were connected directly to the CAN-Bus board. It worked perfectly the first time. Thank you very much for the pointer.

What the final test of this looked like, with 2 Arduinos sending & watching for CAN Bus data in addition to the RPi:
Image

Now I can throw the RPi inside of my Jeep so I can log interior CAN Bus data without having to have the laptop connected to an Arduino all the time. That'll save me a lot of time (and a cold butt this time of year) reverse engineering what data is on that bus.
danjperron wrote:Hi chadwick ,

You should just put the resistor divider to the SO pins. You need to protect the Pi from 5V signal but not the opposite.
If you use resistor divider to the other pins you will reduce the 3.3 v to 2.2V. this mean no clock and data IN signal always 0.


The specification for mcp2515 high level input is 0.7 * VDD = 3.5 Volts. Technically it won't work but often it does.

Use an adafruit/sparkfun voltage converter adapter.


Daniel

cadsenthilkumar
Posts: 29
Joined: Wed Sep 25, 2013 11:09 am

Re: CAN controller

Tue Dec 24, 2013 4:36 am

HI Daniel
other side of the CAN bus is CAN Analyzer.

danjperron wrote:Hi cadsenthilkumar

What is on the other side of the CAN bus?


Daniel

Senthilkumar R

cadsenthilkumar
Posts: 29
Joined: Wed Sep 25, 2013 11:09 am

Re: CAN controller

Tue Dec 24, 2013 4:58 am

Hi daniel

http://2.bp.blogspot.com/-7OWduP6U0fQ/U ... enshot.png

Above link show the CAN circuit that i used . If you have a free time check this circuit with your RPi board .

thanks for your responding daniel :)


Happy Christmas daniel :D


Regards
Senthilkumar

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: CAN controller

Tue Dec 24, 2013 1:27 pm

The CAN bus needed 5V . I don't think it will work on 3.3V.

There is new Microchip driver , MCP2562 . It is able to use 3.3v for the digital side and 5V for the driver.

Check this schematic http://skpang.co.uk/catalog/images/rasp ... _rev_B.pdf

Try to put 5V on the CAN bus driver. You just need to add a resistor divider to protect the mcp2515.

Daniel

msperl
Posts: 353
Joined: Thu Sep 20, 2012 3:40 pm

Re: CAN controller

Tue Dec 24, 2013 1:42 pm

There is actually the SN65HVD23x Series that is 3.3V only.
It works quite well for my use-case and does not require any level shifting.
But it might draw too much current from the 3v3 rail of the rpi when transmitting lots of data.

probably low frequency can bus are more susceptible here than faster ones as a bit will be in dominant state for longer periods, and this will drain the source...

Martin

SteffenBHT
Posts: 4
Joined: Wed Oct 23, 2013 8:33 am

Re: CAN controller

Thu Dec 26, 2013 8:56 am

Hallo

my PiCan works on the Pi and loopback works also.

Now I want to read the data on the bus permanently and store them in a file (e.g. textfile). Could sb tell me the right configuration for my module i can realize that?

thanks and best regards
Steffen

chadwick
Posts: 3
Joined: Wed Dec 18, 2013 2:57 am

Re: CAN controller

Mon Dec 30, 2013 12:52 am

Once I got things working, I had some pretty quick success using an RPi to look at the CAN-buses on my car:

http://chadgibbons.com/2013/12/29/hacki ... r-can-bus/

Image

SteffenBHT
Posts: 4
Joined: Wed Oct 23, 2013 8:33 am

Re: CAN controller

Mon Dec 30, 2013 8:45 am

chadwick wrote:Once I got things working, I had some pretty quick success using an RPi to look at the CAN-buses on my car:

http://chadgibbons.com/2013/12/29/hacki ... r-can-bus/
Great Thanks, i think this is exactly what i am looking for!!

stenniswood
Posts: 6
Joined: Thu Jan 02, 2014 5:26 pm

Re: CAN controller

Thu Jan 02, 2014 5:32 pm

There is a ready made CAN controller available for $40 at :

http://www.beyond-kinetics.com/ProductD ... tCode=1827

This board is based on the 2515 chip controller and should work with socketCAN or any other 2515 code you have available. Beyond kinetics also offers their own driver (soon to be released).

It also provides a 2 amp power regulator,
3 push buttons (gpio)
4 LEDs (gpio)

All boards have been tested. Hope this meets your requirements.
Steve

lmi
Posts: 1
Joined: Tue Jan 07, 2014 9:26 am

Re: CAN controller

Tue Jan 07, 2014 9:30 am

bertr2d2 wrote:
Almaz wrote: Did you shortened JP3 for the 120 Ohm termination ? I got the same interface these days (thx Sukkin :-) ) and its working fine here.
Gerd
Probably a stupid question from a beginner, but how did you shortened the JP3 on this board?

lmi

bertr2d2
Posts: 98
Joined: Wed Aug 08, 2012 10:12 pm

Re: CAN controller

Tue Jan 07, 2014 10:14 am

lmi wrote:
bertr2d2 wrote:
Almaz wrote: Did you shortened JP3 for the 120 Ohm termination ? I got the same interface these days (thx Sukkin :-) ) and its working fine here.
Gerd
Probably a stupid question from a beginner, but how did you shortened the JP3 on this board?
lmi
Soldering a 2 pin Pin-Header an used a jumper. Don't know anymore if R4 was already soldered (should be 120R).

Regards

Gerd
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

Xanatos
Posts: 9
Joined: Tue Sep 24, 2013 12:27 pm

Re: CAN controller

Sun Jan 12, 2014 1:48 pm

Hello,

I tried to bring up my CAN interface from SK Pang again following the latest way documented on http://lnxpps.de/rpie/.

I downloaded the latest Raspbian 2013-12-20 and write it to SD. Booting it up and doing rpi-update, apt-get update / apt-get-upgrade. All this runs successful. Now I downloaded the pre-compiled update from lnxapps.de and extracted it to / with tar Jxvf /home/pi/rpi_can_310x_spi.tar.xz. After depmod -a no error is showing and all looks great. But the modprobe brings the error what stops me now :?

modprobe spi-bcm2708
ERROR: could not insert 'spi_bcm2708': Exec format error

On dmesg is writes:
[109504.339114] spi_bcm2708: disagrees about version of symbol module_layout

The same on the other modules. What is wrong with my installation? Can anybody help me?

Regards,
Marco

bertr2d2
Posts: 98
Joined: Wed Aug 08, 2012 10:12 pm

Re: CAN controller

Sun Jan 12, 2014 7:37 pm

Hi Marco,
Xanatos wrote:Hello,

I tried to bring up my CAN interface from SK Pang again following the latest way documented on http://lnxpps.de/rpie/.

I downloaded the latest Raspbian 2013-12-20 and write it to SD. Booting it up and doing rpi-update, apt-get update / apt-get-upgrade. All this runs successful. Now I downloaded the pre-compiled update from lnxapps.de and extracted it to / with tar Jxvf /home/pi/rpi_can_310x_spi.tar.xz. After depmod -a no error is showing and all looks great. But the modprobe brings the error what stops me now :?

modprobe spi-bcm2708
ERROR: could not insert 'spi_bcm2708': Exec format error

On dmesg is writes:
[109504.339114] spi_bcm2708: disagrees about version of symbol module_layout

The same on the other modules. What is wrong with my installation? Can anybody help me?

Regards,
Marco
you may have already upgraded your kernel from 3.10.24+ to a newer one. You could give
http://lnxpps.de/rpie/rpi_can_310x_spi_dma.tar.xz a try or compile the modules yourself.
Have a look at Damian's description I have linked at http://lnxpps.de/rpie/raspi-anleitung.txt.

Regards

Gerd
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

Return to “Interfacing (DSI, CSI, I2C, etc.)”