PonomarevP
Posts: 13
Joined: Fri Jan 24, 2014 11:14 am

Re: CAN controller

Thu Feb 06, 2014 7:28 am

Zeta wrote:Which frequency was the SPI signal on the raspi ?

20 MHz, but the problem not only in frequency, but in signal length too. Here you can see it(open in new tab)
Image
Sorry for signal labels, they don't match. First is SCK, then MISO, MOSI and CS
Zeta wrote:There are two parameters about frequency : SPI frequency and MCP2515's crystal frequency (to have the correct CAN baudrate).
I found only the baudrate of CAN signal adjusting. I can set it in /etc/network/interfaces. And on schematic I use 16 MHz crystal, the same as on CAN-shield for Arduino

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

Re: CAN controller

Thu Feb 06, 2014 9:19 pm

PonomarevP wrote:20 MHz, but the problem not only in frequency, but in signal length too.
From the datasheet (http://ww1.microchip.com/downloads/en/D ... 21801G.pdf) max SPI speed is 10 MHz.
PonomarevP wrote:I found only the baudrate of CAN signal adjusting. I can set it in /etc/network/interfaces. And on schematic I use 16 MHz crystal, the same as on CAN-shield for Arduino
This has already be explained several times in this thread. For example in this message some pages before :
Zeta wrote:* the raspi must be configured with the correct bus frequency. If you don't know and don't have an oscilloscope to check, you can try the usual ones: 125/250/500 kbps or 1Mbps. There are also some other ones that are not much used (If I remember : 50/80/100/800kbps). The bitrate parameter of this example configures a 500kbps bus frequency:

Code: Select all

/sbin/ip link set can0 type can bitrate 500000
* the can driver set the bus frequency according to its crystal, so check that its crystal frequency is correctly set. The "pds32-0" argument here defines a 16MHz driver crystal, whereas the speed parameter only defines the SPI link speed between the CAN driver and the raspi, which has no impact on the CAN bus frequency :

Code: Select all

modprobe spi-config devices=\
bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pd$
If your are not using the spi-config module (you should!), then you have to configure this in the kernel board definition, like presented on the wiki : http://elinux.org/RPi_CANBus
Search for "max_speed_hz" and "oscillator_frequency".

sjoes
Posts: 3
Joined: Tue Feb 04, 2014 8:38 pm

Re: CAN controller

Thu Feb 06, 2014 9:20 pm

Hi all,

thanks to Zeta and Paul for their answers. Unfortunately it doesn't work :(
But it won't work, because "ifplugd" process will bring it back to life again, so you should kill this process, then down the interface, set loopback and
Kill ifplugd is a very good hint. But according to Zeta my driver doesn't support the loopback device.

Code: Select all

root@raspberrypi:/# ip link set can0 type can bitrate 125000 loopback on
RTNETLINK answers: Operation not supported
But I saw a very strange behavior. I connected my rpi with my CAN device (gleisbox, see http://lnxpps.de/can2udpe/openwrt/ but with rpi, http://lnxpps.de/rpie/). So I have rpi <- can -> gleisbox <- can -> hand controller. Every times when I connect the rpi to the gleisbox the controller stops working. The display is still active, but it does reacts on key presses... But when I disconnect the rpi, the controller is working again and all key press did are executed.
Also very strange is, when I listen via candump and send data via cansend nothing happens (no loopback mode), but in the moment when I plug the controller into the gleisbox can dump receives my data from cansend. Even when I send my data one minute ago.

So, any idea?
Is there a easy way to check that my MCPs are ok?
I also tested a distro from canberry ( http://www.industrialberry.com/download ... n-and-rtc/ ). Also not working :(.

Best regards, sjoes.

PonomarevP
Posts: 13
Joined: Fri Jan 24, 2014 11:14 am

Re: CAN controller

Fri Feb 07, 2014 7:30 am

sjoes, thanks for the link to canberry. Strange I haven't met it before. I've been looking for something like this
So I installed canberry to my SD card and launched it on RPi. Result was same as before, so I decided to change some values. After changing SPI speed (10 times lower) on my oscilloscope I saw the SPI signal similar to one I had on Arduino. When I connected raspberry to arduino and sent

Code: Select all

cansend can0 123#deadbeef
signal I saw it on oscilloscope with the acknowledge bit. But I still don't have input signal, when I reprogram Arduino to send CAN signal, though now this signal receives acknowledge bit

Code: Select all

candump can0
still has nothing. I noticed, that I have MISO signal on MCP2515 only when I send signal from Raspberry(some jump on each falling side of MOSI). When I send CAN with arduino I get RxD on pin2 of MCP2515, but on SPI I have deadmans cardiogram and when I try to read via candump nothing happens. Any ideas what to do with reading?

Thanks, Paul

rass
Posts: 1
Joined: Tue Feb 11, 2014 1:58 pm

Re: CAN controller

Tue Feb 11, 2014 2:01 pm

Hi All,

thanks for great thread about can bus.
I have one question. May I use this one:

http://www.futurlec.com/Mini_CAN_Board.shtml ?

Will it work with latest kernel ?

Thanks,
Rass.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Can't get anything working.

Thu Feb 13, 2014 7:56 pm

I tried downloading the modules, the rpi_can_310x...xz and tried the 3.10.25+ which is what uname returns, but it says it can't load them - wrong format.

There are instructions saying "compile your own", but all I can find is a scattering of things so I don't know what to compile. Do I have to turn on every module in the kernel and hope I've gotten them? Do I need external programs? Is there a manifest anywhere with all the steps clearly laid out? This forum is now 20 pages. I tried searching and I either get a few hundred posts, or a few which don't say anything except X works or X doesn't work or the same abbreviated "just build X'. I've already setup the cross-compile environment and have used it on other things.

I'm using the "current" raspian image updated. OK, there were no can modules already in the module tree, and the modules in the archive from http://lnxpps.de/rpie/rpi_can_310x_spi_dma.tar.xz weren't the right ones for the kernel. Are there a set of right ones available, and if not, what are all the parts I need and what are the steps to compile them?

uname -a returns:
Linux <hostname> 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l GNU/Linux

I've been cross compiling modules for years (my first linux was 0.99 and I wrote one of the kernel drivers). So I could do all of it, and know the process and what is going wrong. But I don't want to spend a week trying to fix it starting from my arduino 2515 driver and the raspian tree (and it is remarkably hard to get the exact build tree for the running kernel - they really need to include that in the packaging).

A few pointers would let me do it in a few minutes. What do I need or is there a specific post describing the entire process starting with nothing and going to the latest DMA version somewhere?

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

Re: CAN controller

Thu Feb 13, 2014 9:20 pm

deleted
Last edited by bertr2d2 on Fri Feb 14, 2014 12:44 am, edited 1 time in total.
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Thu Feb 13, 2014 11:20 pm

I didn't see the latter part of the webpage where the extra CAN modules were added. I started to go through but there are also links to offsite ("if you got your kernel from rpi-update"... ? what does that mean? https://github.com/raspberrypi/linux/issues/486 has a process at the end which is even more complicated).

I assume the changes to bcm..._spi are changing the module verison?

I checked using objcopy, and modprobe --dump-modversions and they were different than the stock kernel. (other symbols also had differences).

It won't be good if I have to go through all that trouble just to get another set of mismatched modules, but I will give it a try. Theoretically that procedure was followed to create the .tar.xz, so what would the difference be? The kernel hash isn't included with the tar.xz so I can't tell if that matches or not but I suspect it doesn't.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Fri Feb 14, 2014 5:08 am

I tried BOTH methods in the linked file at the top of http://lnxpps.de/rpie/raspi-anleitung.txt

Note: why couldn't they use some simple indenting or whitespace? The post uses the redirect symbol so I can't just copy the group of lines from the post and past them into my shell and insure everything would work in the right order with one operation. Instead, I have to copy each line individually.

And going through all that just gives me (for both):

git checkout 16eb921a96db3dc3555a65b53b99c15753e6b770
fatal: reference is not a tree: 16eb921a96db3dc3555a65b53b99c15753e6b770

Yes, this number is identical from https://github.com/raspberrypi/linux/issues/486

But I figured it out. I needed a greater --depth (=200 or more). I'm building now and and the script I used, more or less, follows.

You will need to set the kernel hash value and point to the config.gz, but it should then build the modules. One point is it patches .config, which may fail and/or ...make oldconfig could have some new, unset values, so the patch portion might need updating as the kernel moves forward.

I built usable CAN modules. I'm copying the entire lib since I might want to add more things later.

Code: Select all

#!/bin/sh
# kernel hash
HASH=16eb921a96db3dc3555a65b53b99c15753e6b770
# /proc/config.gz from the pi
CONFIGZ=~/pi.config.gz

mkdir ~/raspi
export BASEDIR=~/raspi
cd $BASEDIR
#increase the depth if the later checkout fails
git clone --depth 250 https://github.com/raspberrypi/linux.git
export KERNEL_SRC=$BASEDIR/linux
cd $KERNEL_SRC
git checkout $HASH
cd $BASEDIR
git clone git://github.com/raspberrypi/tools.git
export CCDIR=$BASEDIR/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin
export CCPREFIX=$CCDIR/arm-bcm2708-linux-gnueabi-
cd $KERNEL_SRC
zcat $CONFIGZ > .config
###PATCH
patch <<'EOF'
--- ../piconfig	2014-02-13 18:48:51.937533769 -0800
+++ .config	2014-02-13 18:50:53.162250865 -0800
@@ -922,6 +922,7 @@
 CONFIG_NET_EMATCH_U32=m
 CONFIG_NET_EMATCH_META=m
 CONFIG_NET_EMATCH_TEXT=m
+# CONFIG_NET_EMATCH_CANID is not set
 CONFIG_NET_EMATCH_IPSET=m
 CONFIG_NET_CLS_ACT=y
 CONFIG_NET_ACT_POLICE=m
@@ -975,7 +976,35 @@
 CONFIG_BAYCOM_SER_FDX=m
 CONFIG_BAYCOM_SER_HDX=m
 CONFIG_YAM=m
-# CONFIG_CAN is not set
+CONFIG_CAN=m
+CONFIG_CAN_RAW=m
+CONFIG_CAN_BCM=m
+# CONFIG_CAN_GW is not set
+
+#
+# CAN Device Drivers
+#
+CONFIG_CAN_VCAN=m
+# CONFIG_CAN_SLCAN is not set
+CONFIG_CAN_DEV=m
+CONFIG_CAN_CALC_BITTIMING=y
+# CONFIG_CAN_LEDS is not set
+# CONFIG_CAN_AT91 is not set
+CONFIG_CAN_MCP251X=m
+# CONFIG_CAN_SJA1000 is not set
+# CONFIG_CAN_C_CAN is not set
+# CONFIG_CAN_CC770 is not set
+
+#
+# CAN USB interfaces
+#
+# CONFIG_CAN_EMS_USB is not set
+# CONFIG_CAN_ESD_USB2 is not set
+# CONFIG_CAN_KVASER_USB is not set
+# CONFIG_CAN_PEAK_USB is not set
+# CONFIG_CAN_8DEV_USB is not set
+# CONFIG_CAN_SOFTING is not set
+CONFIG_CAN_DEBUG_DEVICES=y
 CONFIG_IRDA=m
 
 #
EOF
ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig
ARCH=arm CROSS_COMPILE=${CCPREFIX} make -j 3
mkdir $BASEDIR/modules
export MODULES_TEMP=$BASEDIR/modules
ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make modules_install
cd $BASEDIR
git clone https://github.com/msperl/spi-config
cd spi-config
ARCH=arm CROSS_COMPILE=${CCPREFIX} make KDIR=$KERNEL_SRC
ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} make KDIR=$KERNEL_SRC install
cd $MODULES_TEMP
tar -cjf rpi-can-modules.tar.bz2 *

moritzmhmk
Posts: 7
Joined: Mon Feb 17, 2014 7:37 pm

Re: CAN controller

Mon Feb 17, 2014 7:50 pm

Hi,

I followed this instructions: http://www.raspberrypi.org/phpBB3/viewt ... 18#p447418

The can0 interface shows up fine (ifconfig) but my usb ports stopped working :shock: (I cant use keyboard or wifi adapter (currently connected via LAN and using ssh))

Any tips on what I might have done wrong would be awesome ;)

My goal is to setup a srcp server on the rapsberry and control my Märklin model railway via the "Märklin Gleisbox". I did this successfully using an Arduino and Bluetooth but using the raspberry and having a srcp server with wifi would be awesome ;)

Cheers,
Moritz

PS I am NOT new to linux and the RPi but I did spend about 4hours reading to find a starting point on how to use the MCP2515 with the Pi and am close to hooking up the arduino via usb and using it that way -.-

PPS I have an old Rapsberry Model B (256MB Ram) - no idea if that is relevant... (I also have a new Model A but didn't try working with that yet lacking a powered usb hub...)

PPPS I am using 2014-01-07-wheezy-raspbian

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

Re: CAN controller

Mon Feb 17, 2014 9:27 pm

moritzmhmk wrote: PPPS I am using 2014-01-07-wheezy-raspbian
Which kernel do you run ('uname -a') now ?
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

moritzmhmk
Posts: 7
Joined: Mon Feb 17, 2014 7:37 pm

Re: CAN controller

Mon Feb 17, 2014 9:47 pm

bertr2d2 wrote:Which kernel do you run ('uname -a') now ?
Linux raspberrypi 3.6.11+ #1 PREEMPT Tue Oct 8 10:46:50 CEST 2013 armv6l GNU/Linux

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Talking, for a few frames

Tue Feb 18, 2014 4:28 pm

I managed to compile things, sorry for the frustration and thanks for the help.

I have it connected to a truck controller at 250K. I eventually got it so it would dump out 3-20+ frames, then it would be quiet. I would have to unload the spi_config module and then reload and reconfig to get it again, and I would get the same short burst and stop.

There is a reset pin which I'm leaving unconnected. I'm using a variant of http://microcontrollershop.com/product_ ... ts_id=4719 - an update that isolates the CAN bus side from the mcp side.

There is a reset line on the MCP2515 which I'm leaving unconnected. Should it be connected, and if so, just to a pullup or somewhere else?

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

Re: CAN controller

Tue Feb 18, 2014 7:20 pm

tz1 wrote:I eventually got it so it would dump out 3-20+ frames, then it would be quiet.
Looks like you are using the "mcp251x" driver and not the "mcp2515" (with a 5 in place of the x) one ?

If so, it is a know problem presented a lot of time in this thread... If not, then you should provide some more information about outputs of the usual commands to see where the problem is (interrupts, ifconfig, dmesg, ...).

Zeta.

Cky_2k
Posts: 5
Joined: Tue Jan 14, 2014 7:39 pm

Re: CAN controller

Thu Feb 20, 2014 2:31 am

Hey guys,

I finally got my RPI to read and send CAN messages. Now I would like to monitor the interior CAN so when a button is pressed on my steering wheel it will perform an action such as display Hello World in Terminal. I have searched for examples but had no luck. Could some one please post an example scrip or point me in the right direction.

Thanks!

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Sun Feb 23, 2014 8:03 pm

Zeta wrote:
tz1 wrote:I eventually got it so it would dump out 3-20+ frames, then it would be quiet.
Looks like you are using the "mcp251x" driver and not the "mcp2515" (with a 5 in place of the x) one ?
...
Zeta.
OK. I think I know the mcp2515 driver - the patch from http://clientes.netvisao.pt/anbadeol/mcp2515.html

Which doesn't apply cleanly nor will mcp2515.c compile as written with the current kernel.

I managed to get it to work as an "extra" module. (I don't like altering config or the makefiles as they can change the symbol CRCs unexpectedly). Right now I can load the module properly.

My work including the current download and build script (which tries to automate the entire build - set for the current kernel) is at github:

https://github.com/tz1/mcp2515async

Mainly it is that email patch modified to be an alongside build like spi-config, and with a few #define __driver... things which appear to be obsolete.

I can't test on CAN hardware until tomorrow, but will give an update as soon as I do.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Mon Feb 24, 2014 3:42 pm

That was it, I needed the mcp2515.c version of the driver. I now get the whole stream.

Again, I've placed my work at:

https://github.com/tz1/mcp2515async

Including the script that will automatically download and build all the pieces on a laptop or desktop (if you have the /proc/config.gz and the kernel hash available - I have included those for the current raspian as of this writing.).

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

modules

Mon Feb 24, 2014 8:47 pm

This is my set of modules (just the ones for mcp2515 and the can network) for the current raspian kernel.
Attachments
canmods-3.10.25+.tgz
modules for 3.10.25+ stock kernel
(44.64 KiB) Downloaded 301 times

tgy2013
Posts: 3
Joined: Fri Feb 28, 2014 9:29 am

Re: CAN controller

Fri Feb 28, 2014 9:49 am

Thanks tz1 for the downloads. The mcp2515 driver is much better than the mcp251x.
However, on multi-frame packets my RPi still appears to suffer buffer overflow or frame loss. ifconfig reports no error frames but the frame parameter on Rx packets is incremented. Do you have any ideas on parameters that can be tweaked to address this ?

BTW, running canstart.sh still causes the wireless lan interface to fail, as reported by several other users. It can be simply reinstated by a sequence of ifup wlan0, ifdown wlan0, ifupwlan0 so it is not a major issue.

satarassh
Posts: 3
Joined: Mon Mar 03, 2014 12:57 am

Re: CAN controller

Mon Mar 03, 2014 1:10 am

Hi everybody!

I am currently running my raspberry bcm2708 with 3.1.25+ kernel.
I bought two skpang PICAN can-bus modules http://skpang.co.uk/catalog/pican-canbu ... -1196.html

I am struggling for a week now, trying to install them and get them working but i am slowly loosing nerves after reading so many different guides how to do it.
I would really appreciate if anyone can post me image of an OS which can run this modules or if can anyone make me a guide how to do get it working 100%.

Linux is not so new for me, but in can-bus topic i am complete newb.

I would be really thankful if i can find a person who would do it for me :)

be good my raspberry-pi friends :)

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Mon Mar 10, 2014 6:18 pm

tgy2013 wrote:Thanks tz1 for the downloads. The mcp2515 driver is much better than the mcp251x.
However, on multi-frame packets my RPi still appears to suffer buffer overflow or frame loss. ifconfig reports no error frames but the frame parameter on Rx packets is incremented. Do you have any ideas on parameters that can be tweaked to address this ?
None that I can think of unless/until maybe we can use DMA (but interrupt triggered?).

If you are using ISO15765, you can set the flow control response frame to specify a ST_min value which will pace the packets. You can also set the blocksize to 1 so will require a FC response to each frame. It will slow things but it will be reliable.

CAN seems to be the worst case. At 250kBaud, I still lose frames (sniffing an ISO transaction, so the CFs are sequential). I'd lose more on 500k. On an Arduino, I don't lose any, but I'm using an interrupt and minimalist pull to buffer, though I would have to disable interrupts to do the SPI exchange for a transmit. But the arduino can communicate to the pi via USB or FTDI serial (up to 3 MBaud). I just don't know how (or have time to figure out) how to make that into a CAN network driver.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Mon Mar 10, 2014 6:24 pm

satarassh wrote: I am struggling for a week now, trying to install them and get them working but i am slowly loosing nerves after reading so many different guides how to do it.
I would really appreciate if anyone can post me image of an OS which can run this modules or if can anyone make me a guide how to do get it working 100%.
Install the current raspian, then do "apt-get update" to get to the latest (late Jan / early Feb) kernel. Then add the modules in the message a few above this one, or github https://github.com/tz1/mcp2515async which includes a script to build them on a desktop/laptop (it also builds what should be the same kernel, and you can alter things like adding framebuffer rotation). The readme there has links to the userspace can tools which you also need. There is also a script to load the modules and start sniffing CAN.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

Re: CAN controller

Mon Mar 10, 2014 8:44 pm

Overclocking may help with the missing entities, but I will also be looking into the mcp2515 source - there seems to be few things which might be better inlined or otherwise untangled or unrolled to reduce latency.

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

No missing packets

Tue Mar 11, 2014 6:41 pm

I just pushed an update to my github.

I am no longer missing packets at 250KBaud (as far as I can tell, ISO15765 at full speed for a long packet).

First, I'm overclocked to 1GHz. See the Tested Values section at http://elinux.org/RPiconfig since I've set the other parameters too including force_turbo.

The newer modules (changed/added only, you still need the earlier package) are attached including a DMA version of the spi driver (you need to override the old spi-bcm - either overwrite (after saving) or insmod with the path). https://github.com/notro/spi-bcm2708 - which he cloned from somewhere else, but it does save the CPU. See the wiki there for more info. There is also a "realtime" bool module parameter which I'm not setting.

I've tweaked the mcp2515.c a bit, and the canpi.sh script to add the spi-dma and checked it in.

I'm not sure if it is an either or both fix.
Attachments
mcpupdate.tgz
updated modules
(15.66 KiB) Downloaded 224 times

tz1
Posts: 22
Joined: Tue Jan 07, 2014 10:09 pm

isotp

Tue Mar 11, 2014 7:44 pm

While I was looking, I've also added isotp from can-modules (in the mcp2515async repo), so those can utils also work.
Attachments
isotp.zip
isotp.ko
(7.46 KiB) Downloaded 258 times

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