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

Re: CAN controller

Mon Aug 26, 2013 9:02 pm

KingAlex1985 wrote:I used this command for cross-compiling from Linux (in my case: Kubuntu 13.04):
"sudo apt-get install gcc-arm-linux-gnueabi make ncurses-dev"
Looking at what this package provides : http://packages.ubuntu.com/raring/amd64 ... i/filelist

It gives you the commands "arm-linux-gnueabi-gcc".
To check this command is in the path, you can begin to type "arm" then press "TAB" key two times, to see what the auto-completion propose. This command should be one of the proposals.

Then you have to change the CROSS COMPIL prefix to match this one (in every "make" command of the tutorial):

Code: Select all

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j 3
Zeta

KingAlex1985
Posts: 8
Joined: Tue Aug 13, 2013 6:11 am

Re: CAN controller

Tue Aug 27, 2013 5:30 pm

Hello Zeta,
thank you so much. I was so stupid. I used this command

Code: Select all

make ARCH=arm CROSS_COMPILE=arm-rpi-linux-gnueabi- -j 3
instead of your right command:

Code: Select all

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j 3
Now its compiling. I'm using an old computer with a "Intel Core 2 Duo (Conroe) E6600"-CPU. We will see how long this will take.

Thanks again.

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

Re: CAN controller

Tue Aug 27, 2013 6:34 pm

KingAlex1985 wrote:I was so stupid. I used this command ... instead of your right command ...
Don't say you are stupid, just that you have learn something new ! ;)
I spent probably a year cumulatively in the last ten years figuring out these kind of traps, and I still have a lot to find...

The gcc prefix are well defined (what each field means), but you can find a lot of compilers that are compatible with the same architecture under a lot of different names (depending on which fields are used in the prefix) as you just experienced.
The first command, from the wiki, is based on a self built gcc, where the prefix contained rpi. I could have used the same name as Ubuntu, but didn't know at this time, and was probably following what someone else had done before me on the wiki...
So both commands are good, they only depend on what you installed on your computer.
KingAlex1985 wrote:Now its compiling. I'm using an old computer with a "Intel Core 2 Duo (Conroe) E6600"-CPU. We will see how long this will take.
Expect about 2/3 hours. I was not behind the pc while it compiled, but it is the range of time it should take.
To build it faster, don't forget to give make the "-j3" to use simultaneous jobs which in turn allows to use all the cores of your CPU. The number "3" here should be adapted to the number of cores your processor has (no real rules here, but usually nbcore+1, including hyper-threading cores). For a E6600, 3 seems a good fit.
If building on the RPi itself, 2 should be used, no real gain from 3 is to be expected.

Good luck !

KingAlex1985
Posts: 8
Joined: Tue Aug 13, 2013 6:11 am

Re: CAN controller

Tue Aug 27, 2013 8:00 pm

Hello Zeta,
so the compiling didn't take a long time. Only an hour. But I'm not sure if I'm finished with the compiling. I did it only until this last entry in your tutorial:
===================
You should now have the complete new kernel/modules/firmware in the "/opt/raspberrypi/build" directory. If you want to use it, simply puts its contents to the root directory. It is possible to do it directly without using a temporary build directory, but this method has the advantage of being possible on a remote machine, and to allow easier save of the binary generated (simply archive this directory if you need to give it to someone else).
===================
But haven't done the next steps "MCP2515 Asynchronous Driver" and "SPI low latency patch". Is it still important for me?

And whats the right way to bring it on my raspberry pi. What I have done, is to copy all the files which are in the directory "/opt/raspberrypi/build/boot" from my Kubuntu into the directory "/boot/" of the raspberry pi. These are the files: "bootcode.bin fixup.dat kernel.img start.elf". Then I restarted my pi with "sudo reboot".

After that I tried to initialize my PICAN-Module with the following commands that I got from this page PICAN CAN-Bus Board for Raspberry Pi:
======================
$ sudo su
# cd /home/pi/can-test
# insmod spi-bcm2708.ko
# insmod can.ko
# insmod can-dev.ko
# insmod can-raw.ko
# insmod can-bcm.ko
# insmod mcp251x.ko
=======================
Notice: the folder "/home/pi/can-test" with all the files like "candump" and "cansend" I had still available.

The good answer is that my Bluetooth Dongle now will be detected. But the bad answer is that my PICAN-Module doesnt work.
What should I do next, to fix my problems.

Thank you in advance.
Alex

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

Re: CAN controller

Tue Aug 27, 2013 8:49 pm

KingAlex1985 wrote:Hello Zeta,
so the compiling didn't take a long time. Only an hour. But I'm not sure if I'm finished with the compiling. I did it only until this last entry in your tutorial:
===================
You should now have the complete new kernel/modules/firmware in the "/opt/raspberrypi/build" directory. If you want to use it, simply puts its contents to the root directory. It is possible to do it directly without using a temporary build directory, but this method has the advantage of being possible on a remote machine, and to allow easier save of the binary generated (simply archive this directory if you need to give it to someone else).
===================
But haven't done the next steps "MCP2515 Asynchronous Driver" and "SPI low latency patch". Is it still important for me?
If everything compiled correcly, then it is good ! You may still have some things to tune before everything works, but you've reached an important step !

So the tutorial was not that clear now that you point it up.
The first part is only using the stock options from the kernel, like the MCP251x driver that is availlable in the kernel source code.
This is "enough" to use the CAN bus, but as you can see from the previous discussions on this forum thread, not enough for users that wanted high bus rate and load.
So some people have pointed out that the non official (as in "not in the kernel git tree") MCP2515 driver allowed higher performances but not compatible with MCP2510 anymore, and msperl developped a "low latency" patch for the SPI to handle requests faster and avoid buffer overflow in the MCP driver (only 2 buffers).
These two points are presented in separate parts of the wiki page, but could be done at once, by applying the patchs and settings the options in the menuconfig step.
You may need this to have good performances, but it should be able to work without it, at least to test it.
KingAlex1985 wrote: And whats the right way to bring it on my raspberry pi. What I have done, is to copy all the files which are in the directory "/opt/raspberrypi/build/boot" from my Kubuntu into the directory "/boot/" of the raspberry pi. These are the files: "bootcode.bin fixup.dat kernel.img start.elf". Then I restarted my pi with "sudo reboot".
Normally, you should also copy the files in the build/lib/ directory as modules compiled for this kernel are here.
That care of the user rights there (I made the mistake once...), files should be owned by root, not the user. The "mv" command keeps the ownership while moving the files, where the "cp" command creates new files with the user launching the command.
This part of the wiki should be rewritten more clearly with explicit steps to do...
What I did was generally to tar.gz the folder, copy it to a USB stick, then plug it on the raspberry pi, extract it in a temporary folder to check things, an "cp" it to the correct place as root user. If PC was close to the raspberry pi and on the same network, I would have "scp" it (file copy through ssh).
KingAlex1985 wrote:After that I tried to initialize my PICAN-Module with the following commands that I got from this page PICAN CAN-Bus Board for Raspberry Pi:
======================
$ sudo su
# cd /home/pi/can-test
# insmod spi-bcm2708.ko
# insmod can.ko
# insmod can-dev.ko
# insmod can-raw.ko
# insmod can-bcm.ko
# insmod mcp251x.ko
=======================
Notice: the folder "/home/pi/can-test" with all the files like "candump" and "cansend" I had still available.
Ok. So next step is to debug what is happening.
Can you describe more what doesn't work ?
Do you have errors messages ?
Have you tried the "ip" command to setup it with baudrate and other parameters ?
Did you configured the correct SPI clock, MCP oscillator frequency and GPIO pin used for the interrupts line when compiling the kernel ?

To help you you can try the following commands (written from memories, can't check them now):
- "dmesg | tail -50" : display the last 50 lines of the kernel messages. You can also look at the "/var/log/messages" file which may contain a bit more
- "lsmod" : lists the loaded modules, run before and after the insmod commands to check the modules are corectly loaded
- "cat /proc/interrupts" : display interrupts handled by the system. You should find the spi lines there for example.
- "ls /dev/can*" : to check the device is there (this must appear after the ip config step I think...)
- "ifconfig /dev/can0" : for more details on the can0 line (sent/received messages...)
You can find more details on the useful commands in the previous pages of this thread.
KingAlex1985 wrote:The good answer is that my Bluetooth Dongle now will be detected.
Did you do something special in the kernel configuration to enable bluetooth ? Which .config file did you start with ?

KingAlex1985
Posts: 8
Joined: Tue Aug 13, 2013 6:11 am

Re: CAN controller

Wed Aug 28, 2013 12:30 pm

Hello Zeta,

ok it was wrong message from me that my bluetooth works. It doesn't. I used this german homepage to configure the kernel for bluetooth: Bluetooth-Guide

What I have done to copy the files from my Kubuntu-PC to my raspberry pi, was this way:
copy all files from "/opt/raspberrypi/build/boot/" (PC) to "/boot/" on the pi.

And the "firmware" and "modules"-directories with all the files and subdirectories, i copied from "/opt/raspberrypi/build/lib/" (PC) to "/lib/" on the pi. (But first I deleted the old "firmware" and "modules" directories on the pi.)

After that, I copied the "cantest"-folder with all the files (can-bcm.ko, can-calc-bit-timing, candump, cangen, can.ko, canplayer, cansend, kernel.img, libsocketcan.so.2.2.0,spi-bcm2708.ko, canbusload, can-dev.ko, canfdtest cangw canlogserver can-raw.ko cansniffer libsocketcan.la mcp251x.ko) from this homepage into "/home/pi/".

Then I tried this commands:
===============================
$ sudo su
# cd /home/pi/can-test
# insmod spi-bcm2708.ko
# insmod can.ko
# insmod can-dev.ko
# insmod can-raw.ko
# insmod can-bcm.ko
# insmod mcp251x.ko
============================

And got the following error-messages:
====================================
Error: could not insert module spi-bcm2708.ko: Invalid module format
Error: could not insert module can.ko: Invalid module format
Error: could not insert module can-dev.ko: Invalid module format
Error: could not insert module can-raw.ko: Invalid module format
Error: could not insert module can-bcm.ko: Invalid module format
Error: could not insert module mcp251x.ko: Invalid module format
====================================

Only after the top mentioned commands, I would / should do the ip-command. But it also fails:
# ip link set can0 up type can bitrate 500000

But I don't understand what you mean with this question:
Did you configured the correct SPI clock, MCP oscillator frequency and GPIO pin used for the interrupts line when compiling the kernel ?
I did all the things that you mentioned in your tutorial.

using "lsmod" with the os-image from the PICAN-Module shows me corect things:
===============================
Module Size Used by
mcp251x 9939 0
can_bcm 12454 0
can_raw 6566 0
can_dev 9303 1 mcp251x
can 24173 2 can_raw, can_bcm
spi_bcm2708 4905 0
===============================
But when I use my own compiled kernel I can't see any of the entries. Only some other (not important for me) entries.

I'm baffled now. :-(

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

Re: CAN controller

Wed Aug 28, 2013 6:48 pm

KingAlex1985 wrote:ok it was wrong message from me that my bluetooth works. It doesn't. I used this german homepage to configure the kernel for bluetooth: Bluetooth-Guide
OK. Let's first focus on CAN, then we will check this out.
I don't have much experience with bluetooth on the raspi yet. It may be that the kernel you just compiled don't have the needed modules for Bluetooth activated. Which .config file did you use ? The same than in the tutorial or the one from Raspian ? The one in Raspian may have more modules activated by default.
Also, have you checked if it works on a stock Raspian, without the CAN modifications ?
KingAlex1985 wrote:Then I tried this commands:
===============================
$ sudo su
# cd /home/pi/can-test
# insmod spi-bcm2708.ko
# insmod can.ko
# insmod can-dev.ko
# insmod can-raw.ko
# insmod can-bcm.ko
# insmod mcp251x.ko
============================

And got the following error-messages:
====================================
Error: could not insert module spi-bcm2708.ko: Invalid module format
Error: could not insert module can.ko: Invalid module format
Error: could not insert module can-dev.ko: Invalid module format
Error: could not insert module can-raw.ko: Invalid module format
Error: could not insert module can-bcm.ko: Invalid module format
Error: could not insert module mcp251x.ko: Invalid module format
====================================
Ok, so here is the problem. There is a mismatch between the kernel and the modules... At least the errors points out that the modules were found, so the installation seems to be Ok, only something to fix in the files.
You can be on the wrong kernel somehow. You can check using "uname -a" command.
Or it has to do with the modules. You can check they are present and compiled for the good architecture with "file /lib/modules/YOUR-KERNEL-NAME/kernel/drivers/net/can/*" (which should, otherwise the kernel would probably have trouble to start other modules).
Take also look at the "dmesg" command output, as it may provides more details on the error.
KingAlex1985 wrote:Only after the top mentioned commands, I would / should do the ip-command. But it also fails:
# ip link set can0 up type can bitrate 500000
Ok. It seems normal this fails, as the can modules are not loaded. I didn't know when I asked you where exactly it failed, so included this question.
KingAlex1985 wrote:But I don't understand what you mean with this question:
Did you configured the correct SPI clock, MCP oscillator frequency and GPIO pin used for the interrupts line when compiling the kernel ?
I did all the things that you mentioned in your tutorial.
I wanted to ask if you had checked the example configuration of the wiki is the same as your device. It appears to have a slight difference on the crystal from : http://www.skpang.co.uk/catalog/images/ ... _rev_B.pdf. I had a 20MHz crystal, and the PiCAN seems to use a 16MHz crystal. The interrupt is still GPIO_25 and SPI bus at 10MHz is ok. Did you checked this ?

But this can't be the cause of the errors you have yet. It will manifest if you configure it for a CAN baudrate, as it will be 20% less. For example, configure it for 125kbps, and you will only have 100kbps. This can normally be overcome at run time, without recompiling, but is easier if the module is configured accordingly to your hardware.

dorinde
Posts: 2
Joined: Wed Aug 28, 2013 2:44 pm

Re: CAN controller

Fri Aug 30, 2013 9:57 am

Hi all

First I want to thank you all for your efforts with the mcp2515 microcontrolller.
Unfortunately I run quickly into trouble. I follow Zeta's turorial http://elinux.org/RPi_CANBus, I CROSS_COMPILE on Ubuntu,
I compiled directly on Raspberry, but nothing works.
I managed to add the necessary modules to my kernel and compile everthing.
./candump show nothing....i really have no idea what to do anymore.
Here are the response from the commans:
dmesg

Code: Select all

bcm2708_spi bcm2708_spi.0: master is unqueued, this is deprecated
bcm2708_spi bcm2708_spi.0: SPI Controller at 0x20204000 (irq 80)
can: controller area network core (rev 20120528 abi 9)
NET: Registered protocol family 29
CAN device driver interface
can: raw protocol (rev 20120528)
can: broadcast manager protocol (rev 20120528 t)
mcp2515 spi0.0: can0: device registered (cs=0, irq=195)
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
.
.
.
mcp2515 spi0.0: can0: bit-timing not yet defined
Loading kernel module for a network device with CAP_SYS_MODULE (deprecated).  
	Use CAP_NET_ADMIN and alias netdev-can instead.
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: writing CNF: 0x00 0xbf 0x02
lsmod

Code: Select all

Module                  Size  Used by
mcp2515                 4416  0 
can_bcm                 9348  0 
can_raw                 4872  4 
spidev                  3908  0 
can_dev                 5436  1 mcp2515
can                    18396  2 can_bcm,can_raw
spi_bcm2708             3992  0 
snd_bcm2835            11396  0 
snd_pcm                52776  1 snd_bcm2835
snd_page_alloc          2700  1 snd_pcm
snd_seq                36892  0 
snd_seq_device          3608  1 snd_seq
snd_timer              14736  2 snd_pcm,snd_seq
snd                    35840  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
evdev                   7604  6 
leds_gpio               1648  0 
led_class               1788  1 leds_gpio
Here I used mcp2515 and in menuconfig I deactivated "Microchip MCP251x SPI CAN controllers".

cat /proc/interrupts

Code: Select all

  CPU0       
  3:      33759   ARMCTRL  BCM2708 Timer Tick
 32:    1025561   ARMCTRL  dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
 52:          0   ARMCTRL  BCM2708 GPIO catchall handler
 65:          2   ARMCTRL  ARM Mailbox IRQ
 66:          1   ARMCTRL  VCHIQ doorbell
 75:          1   ARMCTRL
 77:      11648   ARMCTRL  bcm2708_sdhci (dma)
 80:        486   ARMCTRL  bcm2708_spi.0
 83:         19   ARMCTRL  uart-pl011
 84:      19005   ARMCTRL  mmc0
195:          0      GPIO  can0
FIQ:              usb_fiq
Err:          0
ifconfig can0

Code: Select all

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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:10 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
ip -s -d link show can0

Code: Select all

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.850 
    tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
    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     
I also didn't used SPI low latency patch, is just the SPI driver from linux.
Probably a stupid question: Why no one make an image with a version that runs?...I mean Raspbian and CAN Bus.
I really apreciate any help and I thanks in advance.
Dorin

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

Re: CAN controller

Fri Aug 30, 2013 12:33 pm

Dorin,

seems to me that you missed to setup the CAN correctly:

Code: Select all

mcp2515 spi0.0: can0: device registered (cs=0, irq=195)
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined
mcp2515 spi0.0: can0: bit-timing not yet defined

Code: Select all

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 
just do:

Code: Select all

ip link set can0 type can bitrate 125000 
ifconfig can0 up
BTW: slightly outdated but still useful: http://lnxpps.de/rpie/
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

dorinde
Posts: 2
Joined: Wed Aug 28, 2013 2:44 pm

Re: CAN controller

Sat Aug 31, 2013 10:02 pm

First of all thanks for your answer, bertr2d2

I'm not exactly sure where are the setups.
I tried with bitrate 125000 an also 50000 and no matter what I do, I get the same result:
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
theoretically, I should have a response from candump with just the bcm2708 patch file, the other file mcp2515 is just for improvement.
Any idea what can I do next?
BTW: slightly outdated but still useful: http://lnxpps.de/rpie/
I think that that was my the first starting point.
Actually there is just 3 places from which you can get information
1. http://lnxpps.de/rpie/
2. Zeta's tuturial http://elinux.org/RPi_CANBus
3. this Forum

Questions:
what is contained in vc folder and where should that be copied
I must also to copy the entire "linux" folder into raspberry

thanks in advance.
Dorin

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

Re: CAN controller

Sat Aug 31, 2013 10:14 pm

Dorin,

what happens, when you typed

Code: Select all

ip link set can0 type can bitrate 125000 
ifconfig can0 up
in (console, dmesg)? Is the transceiver connected and wired correctly ?
Is there a second CAN device on the bus with the same speed ?
Do you have a sketch of your setup ?
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

stephenWebb
Posts: 5
Joined: Wed Sep 04, 2013 6:59 am

Re: CAN controller

Wed Sep 04, 2013 7:09 am

I have the same symptoms as KingAlex1985.

I am able to talk to my device on the CAN bus using the RaspberryPi and a Peak-USB Can device.

When I use the pican nothing is transmitted as the link is always in the state STOPPED. ie.

Code: Select all

ip -details link show can0
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.850
    tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
In contrast, with the PiCan removed and a Peak USB device connected the link show:

Code: Select all

ip -details link show can0
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10
    link/can
    can state ERROR-ACTIVE restart-ms 0
    bitrate 500000 sample-point 0.875
    tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
    pcan_usb: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 8000000
Any suggestions on how to proceed would be greatly appreciated.

stephenWebb
Posts: 5
Joined: Wed Sep 04, 2013 6:59 am

Re: CAN controller

Thu Sep 05, 2013 2:41 am

The solution is an updated mcp2515.c which can be found at http://lnxpps.de/rpie/mcp2515_mod.c

With the new mcp2515.ko installed, I now get:

Code: Select all

# ip link set can0 type can bitrate 500000 restart-ms 100
# ip -details link show can0
5: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT qlen 10
    link/can
    can state STOPPED (berr-counter tx 0 rx 0) restart-ms 100
    bitrate 500000 sample-point 0.850
    tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
# ifconfig can0 up
# ip -details link show can0
5: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10
    link/can
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 100
    bitrate 500000 sample-point 0.850
    tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000

stephenWebb
Posts: 5
Joined: Wed Sep 04, 2013 6:59 am

Re: CAN controller

Fri Sep 06, 2013 6:51 am

OK perhaps I spoke too soon about having a solution with the raspberryPi and the PICAN board.

The driver now works well in loopback mode, i.e.

Code: Select all

ip link set can0 type can bitrate 500000 loopback on
ifconfig can0 up
and I can use cangen and candump with data coming through just fine.

However, connecting to a physical CAN bus and sending a single message results in zero bytes sent and a tx error count of 128. ie:

Code: Select all

# ip -d -s link show can0
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10
    link/can
    can <LOOPBACK> state ERROR-ACTIVE (berr-counter tx 128 rx 0) restart-ms 0
    bitrate 500000 sample-point 0.850
    tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
    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
My reading leads me to think the driver is now working as loopback is implemented in the MCP2515 chip and the bit timing values (tq 100 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1) make sense for the requested 500Kb baud rate.

I now guess that the PICAN is not doing the correct thing.

Has anyone been successful using the PICAN board on a RaspberryPi?

stephenWebb
Posts: 5
Joined: Wed Sep 04, 2013 6:59 am

Re: CAN controller

Mon Sep 09, 2013 1:28 am

I noticed that the clock value I was using was not appropriate for the PICAN board as the PICAN has a 16Mhx crystal.

With arch/arm/mach-bcm2708/bcm2708.c patched appropriately

Code: Select all

-   .oscillator_frequency   = 20000000,
+   .oscillator_frequency   = 16000000,

-		.max_speed_hz = 10000000,
+		.max_speed_hz = 8000000,
and a new kernel installed I no longer get errors reported by the mcp2515 when I send a packet (using cansend).

However, I still cannot send or receive using the PICAN when it is attached to a physical CAN bus (loopback works well).

Any suggestions gratefully received.

Mark One
Posts: 1
Joined: Wed Sep 11, 2013 9:47 pm

Re: CAN controller

Wed Sep 11, 2013 10:25 pm

Hi,
I'm in need of some help! ;)

My background: I'm good with windows, I've written lots of VBA, Matlab and I've started with some Python. I've not had any contact with Linux until I started with my Pi recently. I have done plenty of work with CAN bus.

I'm really keen to create a CAN logger. I've bought a PI-CAN from SK Pang.

- Firstly I tried the SK Pang linked Kernel from: http://lnxpps.de/rpie/. On reboot, the keyboard wont respond at all. I've tried it with 3.2 and 3.6 Raspbian

- I've then tried to compile my own Kernel using the wiki here http://elinux.org/RPi_CANBus. I used Ubuntu to cross compile. As good as the guide is, it took me ages to fumble through as I'm so unfamiliar with Linux. I think I've learned quite a lot, but after transferring the build over to the Pi, I can't get it to boot. I get the colour splash screen, "hold shift for recovery mode", black screen, nothing. I'd put a fresh noobs 3.6 build onto the Pi before starting this.

I really just want to start writing some code to work with the CAN interface. Is there an SD card image I can work with somewhere?
Any assistance much appreciated.

KingAlex1985
Posts: 8
Joined: Tue Aug 13, 2013 6:11 am

Re: CAN controller

Thu Sep 12, 2013 8:47 am

@ stephenWebb:

Hello Stephen,
i got your kernel and tried it out.
I loaded the modules and brought the can0 interface up with your following commands:

Code: Select all

# insmod /lib/modules/3.6.11-cutdown+/kernel/drivers/spi/spi-bcm2708.ko
# insmod /lib/modules/3.6.11-cutdown+/kernel/drivers/net/can/mcp2515.ko
# ip link set can0 type can bitrate 250000
# ifconfig can0 up
And it worked fine.
But where can i find the executable files "candump" and "cansend" that you mentioned above?!

When I use the following command:
# ip -d -s link show can0

I get this output:

Code: Select all

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10
    link/can 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 250000 sample-point 0.850 
    tq 200 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
    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      
What is "loopback" for? I tried also with loopback but when I use the command "sudo ifconfig can0 up" I get the error-message: "Device or resource busy".

Using this command:
# ip -d -s link show can0

I get this:

Code: Select all

3: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT qlen 10
    link/can 
    can <LOOPBACK> state STOPPED (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 250000 sample-point 0.850 
    tq 200 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
    mcp2515: tseg1 2..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 10000000
    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      
Thank you in advance.
Alex

stephenWebb
Posts: 5
Joined: Wed Sep 04, 2013 6:59 am

Re: CAN controller

Fri Sep 13, 2013 12:18 am

Hi KingAlex1985

1. The can utilities are from socketcan. I build them using my cross-compiler by doing:

Code: Select all

mkdir socketcan;cd socketcan
svn co http://svn.berlios.de/svnroot/repos/socketcan

cd can-utils
export CC=/opt/xtools/chain/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc
export CPPFLAGS=-I/opt/xtools/RaspberryPi/usr/include
export LDFLAGS=-L/opt/xtools/RaspberryPi/usr/lib
make KERNEL_LOCATION=/opt/raspberrypi/linux
make KERNEL_LOCATION=/opt/raspberrypi/linux DESTDIR=/opt/raspberrypi/build install
2. Loopback mode enables testing the connection from Linux to the mcp2515 chip. It is a mode supported by the mcp2515 chip where transmitted packets are echoed back as received packets.

Note that the kernel I sent you has an clock rate which is inappropriate for the PICAN 16MHz crystal. I have an updated kernel I can make available if you are keen to keep trying to get the PICAN working. Note that I have not been able to get the PICAN working with a physical CAN bus even with the updated kernel. I have stopped working on the PICAN util I have more time (and access to an oscilloscope).


3.

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

Re: CAN controller

Tue Sep 24, 2013 12:39 pm

Hi All,

I have just finished reading the thread! Many thanks to all who have posted there solutions.
I'm an owner of a PICAN and tried to follow all the steps you all took to get it work. But it is hard to find what is still needed to be done and what not. Also what kernel versions the patches have to be applied to. Is it possible to adjust the manuals to an updated version?

For me it is also not clear if there is now a working solution at all for the PICAN? I was planning to connect the PICAN to my Heating to collect the values send by it.

Regards,
Marco

Koenkk
Posts: 148
Joined: Sun Jul 29, 2012 10:22 pm
Location: Netherlands

Re: CAN controller

Fri Sep 27, 2013 10:18 am

Hi,

I have exactly the same setup and board as stephenWebb. For me the PICAN isn't working either (loopback is working, but output isn't). Don't buy this board before a working solution has been found!

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

Re: CAN controller

Mon Sep 30, 2013 1:45 pm

I had a quick look at the pictures of the RPI board as well as the schematics - and the board seem to match the "general" setup that has been in use and documented initially by bertr2d2.

So it should work out of the box with a 3.6.y kernel.
The only thing that might be good to check are the config settings that the driver has applied!

so typically for 500KHz CAN bus speed you should have a line like this when loading the module:
mcp251x spi0.0: CNF: 0x00 0xb5 0x01

if it is not those values, then you seem to have a setup that has a different:
* clock for the CAN Controller
* CAN-bus speed

Another thing to check if reception works is: "watch ifconfig can0"

If the RX packets are increasing, then you are in good shape.
Then you should also be able to use the "simple" candump util to see those packets (I still use the original version of bertr2d2 at: http://lnxpps.de/rpie/ - comes with the kernel)

If the counter there is not 0, but stays stuck after a few initial packets, then welcome to the bug of the "stock" mcp251x driver, which gets a "hickup" if too many packets get sent in a short window - it then stops to receive packets (at least with the 3.6.X kernel) and I believe this also blocks transmisssion...

The only solution here is to use the alternate mcp2515 driver, which does NOT show this issue (but that will mean you will have to compile it separately).

Hope this helps, martin

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

Re: CAN controller

Mon Sep 30, 2013 2:28 pm

Coming back from hibernation over summer I restart my work on the CAN/SPI now with the 3.11.y GIT kernel.

There seem to be at least one hickup that got introduced since 3.6.y that blocks booting the Raspberry pie!

The original code by Gerhard uses: gpio_to_irq(MCP2515_CAN_INT_GPIO_PIN)

for some reason when using this this construct the kernel does not boot and stays stuck - you have to replace the kernel with the emergency kernel to get the system back to work.

But as soon as you hardcode the interrupt to number 195 it works fine...

So for everyone who wants to "test" with the 3.11.y branch - here my current diff of the device config:

Code: Select all

diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c
index 3fe7626..0c30461 100644
--- a/arch/arm/mach-bcm2708/bcm2708.c
+++ b/arch/arm/mach-bcm2708/bcm2708.c
@@ -54,6 +54,12 @@
 #include <mach/vcio.h>
 #include <mach/system.h>
 
+#include <linux/can/platform/mcp251x.h>
+#include <linux/gpio.h>
+#include <linux/irq.h>
+
+#define MCP2515_CAN_INT_GPIO_PIN 25
+
 #include <linux/delay.h>
 
 #include "bcm2708.h"
@@ -546,16 +552,26 @@ static struct platform_device bcm2708_spi_device = {
        .resource = bcm2708_spi_resources,
 };
 
+static struct mcp251x_platform_data mcp251x_info = {
+       .oscillator_frequency   = 16000000,
+       .board_specific_setup   = NULL,
+       .irq_flags              = IRQF_TRIGGER_FALLING|IRQF_ONESHOT,
+       .power_enable           = NULL,
+       .transceiver_enable     = NULL,
+};
+
 #ifdef CONFIG_BCM2708_SPIDEV
 static struct spi_board_info bcm2708_spi_devices[] = {
-#ifdef CONFIG_SPI_SPIDEV
        {
-               .modalias = "spidev",
-               .max_speed_hz = 500000,
+               .modalias = "mcp2515",
+               .max_speed_hz = 1000000,
+               .platform_data = &mcp251x_info,
                .bus_num = 0,
                .chip_select = 0,
                .mode = SPI_MODE_0,
-       }, {
+       }
+#ifdef CONFIG_SPI_SPIDEV
+       ,{
                .modalias = "spidev",
                .max_speed_hz = 500000,
                .bus_num = 0,
@@ -690,6 +706,17 @@ static void bcm2708_power_off(void)
        }
 }
 
+static void bcm2708_mcp251x_init(void) {
+       bcm2708_spi_devices[0].irq =
+#if 0
+               gpio_to_irq(MCP2515_CAN_INT_GPIO_PIN);
+#else
+       195;
+#endif
+       printk(KERN_INFO " BCM2708 mcp251x_init:  got IRQ %d for MCP2515\n", bcm
+       return;
+};
+
 void __init bcm2708_init(void)
 {
        int i;
@@ -747,6 +774,7 @@ void __init bcm2708_init(void)
        system_serial_low = serial;
 
 #ifdef CONFIG_BCM2708_SPIDEV
+       bcm2708_mcp251x_init();
        spi_register_board_info(bcm2708_spi_devices,
                        ARRAY_SIZE(bcm2708_spi_devices));
 #endif
So if someone can figure out how the get the "translation" to work, then please share it!

Note that there is also something else that is "strange" in general:
the "spidev" default config does not really get compiled in (but spidev does load and show the "devices" in the stock kernel) and I have not yet figured out where it really resides...
This may now reside already in the devicetree (but I am still unsure if devicetree is already used with the 3.11.y branch).

I did not find anything conclusive to this yet - any insight is appreciated...

Ciao,
Martin

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

Re: CAN controller

Mon Sep 30, 2013 8:54 pm

msperl wrote: So if someone can figure out how the get the "translation" to work, then please share it!
Ok - after opening an issue for this on github (https://github.com/raspberrypi/linux/issues/389) I got some feedback from popcornmix and he said that there are some macros that should not do anything strange.

In the end I found out that these macros were not applied, but a c function was called instead.
Something must have changed between 3.6 and 3.11 that triggers that.

So for all who are interrested - here the latest patch to get CAN working with a 3.11.y kernel:

Code: Select all

diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c
index 52cf7c9..1eed495 100644
--- a/arch/arm/mach-bcm2708/bcm2708.c
+++ b/arch/arm/mach-bcm2708/bcm2708.c
@@ -54,6 +54,12 @@
 #include <mach/vcio.h>
 #include <mach/system.h>
 
+#include <linux/can/platform/mcp251x.h>
+#include <mach/gpio.h>
+#include <linux/irq.h>
+
+#define MCP2515_CAN_INT_GPIO_PIN 25
+
 #include <linux/delay.h>
 
 #include "bcm2708.h"
@@ -546,19 +552,29 @@ static struct platform_device bcm2708_spi_device = {
        .resource = bcm2708_spi_resources,
        .dev = {
                .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
-       },
+       }, 
+};
+
+static struct mcp251x_platform_data mcp251x_info = {
+       .oscillator_frequency   = 16000000,
+       .board_specific_setup   = NULL,
+       .irq_flags              = IRQF_TRIGGER_FALLING|IRQF_ONESHOT,
+       .power_enable           = NULL,
+       .transceiver_enable     = NULL,
 };
 
 #ifdef CONFIG_BCM2708_SPIDEV
 static struct spi_board_info bcm2708_spi_devices[] = {
-#ifdef CONFIG_SPI_SPIDEV
        {
-               .modalias = "spidev",
-               .max_speed_hz = 500000,
+               .modalias = "mcp2515",
+               .max_speed_hz = 1000000,
+               .platform_data = &mcp251x_info,
                .bus_num = 0,
                .chip_select = 0,
                .mode = SPI_MODE_0,
-       }, {
+       }
+#ifdef CONFIG_SPI_SPIDEV
+       ,{
                .modalias = "spidev",
                .max_speed_hz = 500000,
                .bus_num = 0,
@@ -693,6 +709,13 @@ static void bcm2708_power_off(void)
        }
 }
 
+static void bcm2708_mcp251x_init(void) {
+       bcm2708_spi_devices[0].irq =
+               gpio_to_irq(MCP2515_CAN_INT_GPIO_PIN);
+       printk(KERN_INFO " BCM2708 mcp251x_init:  got IRQ %d for MCP2515\n", bcm
+       return;
+};
+
 void __init bcm2708_init(void)
 {
        int i;
@@ -750,6 +773,7 @@ void __init bcm2708_init(void)
        system_serial_low = serial;
 
 #ifdef CONFIG_BCM2708_SPIDEV
+       bcm2708_mcp251x_init();
        spi_register_board_info(bcm2708_spi_devices,
                        ARRAY_SIZE(bcm2708_spi_devices));
 #endif
Ciao, Martin

Koenkk
Posts: 148
Joined: Sun Jul 29, 2012 10:22 pm
Location: Netherlands

Re: CAN controller

Tue Oct 01, 2013 9:29 am

Hello msperl,

One strange part of your patch,

Code: Select all

.dev = {
                .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
           },
Isn't there by default (I used https://github.com/raspberrypi/linux/co ... dd584fb0d0).
Also,

Code: Select all

 +      printk(KERN_INFO " BCM2708 mcp251x_init:  got IRQ %d for MCP2515\n", bcm
causes a compilation error (bcm undeclared and ");" missing), I changed it to this (not sure if it's good though):

Code: Select all

+   printk(KERN_INFO " BCM2708 mcp251x_init:  got IRQ %d for MCP2515\n", bcm2708_spi_devices[0].irq);
I use mcp251x driver with the 3.11 kernel and your patch.

Code: Select all

dmesg -> mcp251x spi0.0: CNF: 0x00 0xb5 0x01 (seems to be fine)
ip link set can0 up type can bitrate 500000
ifconfig can0 up

On second terminal: 
watch ifconfig can0

./cansend can0 123#deadbeef

On second terminal:
Every 2.0s: ifconfig can0                                                                                                                                                                                                                             Tue Oct  1 09:29:07 2013

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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:10
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

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

Re: CAN controller

Tue Oct 01, 2013 10:15 am

OK - probably copy/paste issues...
If needed take the "source" from github: https://raw.github.com/msperl/linux/759 ... -bcm2708.c

As for everything else: Seems as if the config is fine from the config perspective.

So I also assume that if you run the following:
while true; do can-utils/cansend can0 000\#deadbeef; done
you will get after 10-16 packets the message: "write: No buffer space available"

and the TX-counters are still 0 and RX as well.

Do you have another device connected to the CAN bus with the same CAN speed? (even if it is not sending any packets itself) ?

Because CAN _requires_ a peer to acknowledge a correctly received transfer (simmilar to I2C) - otherwise the mcp2515 will try to send out the first packet in a continuous sequence until it gets acknowledged... And the TX-counters only increase when the packet sent out gets acknowledged...

Because if I configure my RPI with the wrong CAN-speed, then I can replicate the scenario described above.

Martin

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