CAN controller


570 posts   Page 3 of 23   1, 2, 3, 4, 5, 6 ... 23
by bertr2d2 » Sat Aug 11, 2012 11:28 am
I have CANalyzer available,


Great ! If you have enough time could you also test:
http://clientes.netvisao.pt/anbadeol/mcp2515.html
please ?
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by bertr2d2 » Sat Aug 11, 2012 11:33 am
Great ! If you have enough time could you also test:
http://clientes.netvisao.pt/anbadeol/mcp2515.html
please ?


Responding to myself:

This driver should be more up to date:
http://git.pengutronix.de/?p=mkl/linux- ... 2515-async
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by Olli » Sat Aug 11, 2012 2:04 pm
Hi,

can anyone tell me how to set up the MCP2515 driver on Raspbian (2012-07-15-wheezy-raspbian.zip)?
So I want to know where I can load each file and where I have to install these.
Or have I to change my Linux version to get the driver?

Thank you very much for your effort

MfG
Olli
Posts: 2
Joined: Sat Aug 11, 2012 1:49 pm
by bertr2d2 » Sat Aug 11, 2012 4:31 pm
Hi Olli,

you have to compile and install the new Kernel: install the source and apply the board definition patch.
The board definition is under this directory:
/usr/src/linux-3.1.9+/arch/arm/mach-bcm2708
I would suggest AxelB's patch because it has some debug code getting the right IRQ and compile the appropriate CAN modules (can, can-dev, can-raw ...) including mcp251x as modules.
After compiling and installing you can find some hints here: http://lnxpps.de/rpie

I have compiled the Kernel on the RPi itself which took more than 5 hours:
# cd /usr/src
# git clone --depth 1 https://github.com/raspberrypi/linux.git
# cd linux
# make menuconfig
# make
# # install the kernel

## some useful tools
# cd /usr/src
# wget http://www.pengutronix.de/software/libs ... .8.tar.bz2
# # compile and install
# git clone git://gitorious.org/linux-can/can-utils.git can-util
## compile and install

Regards

bertr2d2
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by Olli » Sat Aug 11, 2012 7:08 pm
Hi,

thanks bertr2d2!

Which "new" Kernel do you mean?
I only know the Kernels from http://www.bootc.net/projects/raspberry-pi-kernel/
If it's not the meant Kernel, where can I find it?
I can't install the Kernel from Chris's website, because it's for "armel" and I have "armhf" at this time.

MfG
Olli
Posts: 2
Joined: Sat Aug 11, 2012 1:49 pm
by bertr2d2 » Sat Aug 11, 2012 7:22 pm
Hi Olli,

just use the kernel version you have already installed. The version doesn't matter - all of the actual RPi
are fresh enough. But you have to copy the new compiled kernel to your boot dir.

Regards
bertr2d2
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by AxelB » Sun Aug 12, 2012 10:11 am
@Olli:
I did exactly what bertr2d2 described, but cross compiled the kernel. You can find the decription at http://elinux.org/Rpi_kernel_compilation

Regards

Axel
Posts: 6
Joined: Fri Aug 10, 2012 12:23 pm
by AxelB » Mon Aug 13, 2012 1:54 pm
Hi all,

I started the first tests with my current installation, using the mcp251x driver from the kernel sources. I set the CAN to 500k and started sending messages using CANalyzer. I defined a single message (Id 0x123, DLC 8) and started candump logging into a file. When I transfer this message with a cycle time of 5ms (resulting in a bus load of approx. 5%), I can observe that some frames get lost as CANalyzer reports 1117 frames transmitted and the log file only contains 1052 lines.
When I decrease the cycle time to 2 ms (~12% bus load), I see 3248 transmitted frames and only 1422 received frames.
Is this something, which is in the expected range of this driver?
I also tried to use the driver from http://git.pengutronix.de/?p=mkl/linux- ... 2515-async, but I wasn't able to get it running, as I always get the error BUG: scheduling while atomic: sudo/1543/0x00000002.

Regards

Axel
Posts: 6
Joined: Fri Aug 10, 2012 12:23 pm
by bertr2d2 » Mon Aug 13, 2012 2:13 pm
@AxelB,

Thanks for the test - it's far from what I have expected. What does
Code: Select all
ip -s -d link show can0

say ?

Regards

Gerd
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by AxelB » Mon Aug 13, 2012 3:04 pm
OK, I found my trouble. For some reason my irq assignment doesn't work. No I fixed it and it seems to be much better.
@bertr2d2: When I execute ip -s -d link show can0 and compare the received packets with the number of packets sent with CANalyzer, it seems to work fine with ~90% busload (4 messages with 1ms cycle) @ 500k:
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 restart-ms 0
    bitrate 500000 sample-point 0.833
    tq 166 prop-seg 4 phase-seg1 5 phase-seg2 2 sjw 1
    mcp251x: tseg1 3..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 6000000
    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
    1555520    194440   1       0       1       0
    TX: bytes  packets  errors  dropped carrier collsns
    0          0        0       0       0       0
Posts: 6
Joined: Fri Aug 10, 2012 12:23 pm
by bertr2d2 » Mon Aug 13, 2012 4:31 pm
@AlexB

that looks much better now :mrgreen:
Could you repeat your test with 1Mbit until you get lost frames to see what the limit is ?
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by maddin1234 » Mon Aug 13, 2012 10:23 pm
Hello AlexB,
first of all, thanks for the posts.

Can you tell, what was the problem with your interrupt assignment?
Is the problem in your patch for board configuration you posted before?

I think many users would be interested in a detailed guide how to set up GPIO interrupts.
(perhaps independent from the MCP2515)
* Changes to board configuration
* kernel compilation
* making module
* installing module
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by bertr2d2 » Tue Aug 14, 2012 9:28 pm
Hi maddin1234,

AlexB seems to be buys or away. Alex already referenced a link for Kernel compilation,
which is good source of information.

You can find the board definition file in linux the source here :
./arch/arm/mach-bcm2708/bcm2708.c
Just patch this file (look above in this thread) and do 'make menuconfig'.
Mark CAN, CAN-DEV, CAN-RAW, CAN-BCM and MCP251x as module
and do make; make modules_install.
You need to copy the Kernel (.arch/arm/boot/Image) to /boot/kernel.img.
It is a good idea to save a copy of this file first.

For the impatient ones, I've made a tar archive for Rasbian (Linux 3.1.9):
http://lnxpps.de/rpie/
Modules + Kernel + Kernel .config + libsocketcan + can-utils

Regards,

bertr2d2
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by maddin1234 » Tue Aug 14, 2012 10:28 pm
Hello Bertr2d2,
thanks for the image. I think many people will be thankfull.
I'm a beginner with linux and so have problems with things that seems to be very clear for experts.
An example: make menuconfig doesn't work, because ncurses is missing.

To my previous post:
I got interrupt running with parts of your code gpio-test and the posted patches.
The main reason for my question was to find out if I "copied" the problem AlexB spoke from.
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by bertr2d2 » Tue Aug 14, 2012 11:03 pm
maddin1234, would you be so kind to write down the steps to get CAN working
and report them here in the forum afterwards ? Maybe some of us have more
experience with Linux but forget things which seems to be negligible. For starters
this may be an important hint (such as installing ncurses-dev).

Regarding the IRQ problem from AlexB, just start and we will see how we could
solve the problem if it exists in your code.
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by maddin1234 » Wed Aug 15, 2012 2:49 pm
Hello,
ok, I will give it a try from the point-of-wiev of a beginner.
Please feel free to edit/correct this.

Starting right from the beginning from a windows PC:

1.) Download Raspbian-Image and put it to a SD-card as
described here: http://www.elinux.org/RPi_Easy_SD_Card_Setup

2.) Download http://lnxpps.de/rpie/can-test.tar.bz2
unpack it (for example with 7Zip) and copy the can-test folder
to the SD-card partition that is accessible from windows

3.) boot your RaspberryPi and make the settings in raspi-config

4.) change the user to root,to get the permissions for the following operations
(You will see, that the prompt changes from $ to #, that means from a
non-privileged user to a privileged user. So, for the next steps, watch out
for the prompt)

$ sudo su


5) go to the folder where the partition with can-test is mounted
# cd /boot


6.) overwrite existing kernel with the new one and copy the folder
to home-directory of pi (or where you want it to be)
# cp can-test/kernel.img /boot
# cp -a can-test /home/pi


7.) reboot your Raspi to let it come up with the new kernel
# shutdown -r now


8.) go to the folder with the modules and install them
$ 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


9.) this step is not necessary
check if the modules are there with
# lsmod
($ lsmod should also work, but we are "root" currently)
should list all the 6 modules

10.) activate the interface with
# ip link set can0 up type can bitrate 500000

gives no error message, but when I run
# dmesg | less

it shows:
mcp251x spi0.0: bit-timing not yet defined
mcp251x spi0.0: unable to set initial baudrate


This is the point, where I am stuck at the moment.
Can I provide the frequency as a parameter in the insmod command,
do I have to recompile the MCP251x-module or do I have to recompile
the kernel, because the oscillator frequency is defined in the board-definition file?


At least, I can see Error-Frames, so the first step is done, the MCP2515
is doing something.

Some other things, that should be described or changed in future:

1.) with the new kernel, my mouse doesn't work anymore in X-enviroment
2.) What happens when update / upgrade is performed? I think our kernel
will possibly be overwritten and we have do recompile again.
3.) I want to try to build an "automatic data-logger", that means,
starting CAN-logging after power-up without need of typing anything.
http://www.raspberrypi-spy.co.uk/2012/06/auto-login-auto-load-lxde/
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by maddin1234 » Wed Aug 15, 2012 3:01 pm
what i didn't write:
I have a 25MHz clock for the MCP2515 and not 16MHz as in the example described.
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by bertr2d2 » Thu Aug 16, 2012 1:22 am
A simple 'ifconfig can0 up' will stop the nasty dmesg entries
and your CAN is ready to use. But ...
The MPC2515 clockrate is a serious problem with the precompiled
kernel - I used 16Mhz. This will not work because the CAN bus
speed is calculated in regards of the clockrate set in the board
definition. You have three options:
1.) replace the oscillator
2.) change the board definition and compile the kernel
3.) maybe change the bitrate with the 16/25 quotient in mind
(don't know if this works).
IMO there is no option for the mcp251x module, but I might be wrong.
You could add this, if it doesn't exist.

Anyway, for testing you can put the CAN controller into loopback mode
and send&receive messages. If this works, you only need to change
the clockrate.

BTW: Thought, that you want to compile the kernel yourself and describe the procedure.

Regards

bertr2d2
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by maddin1234 » Thu Aug 16, 2012 8:38 am
Hello,
yes, I am working on compiling the kernel (and writing a complete description), but I thougt, it would be good to have it done the easy way, before trying the hard way.

I changed the board-definition and started the "make" allready, now waiting........to see if it worked.

But I came to a new question doing it:
In your can-test file, you have a kernel and the module *.ko files.
On the homepage you "insmod" all the modules.
As far as I know, insmod means "add the modules to the kernel on runtime".
When doing "make menuconfig" I found that all these modules seem to be
allready in the kernel sources and can be compiled to the kernel when selected.
So why do we have to "insmod" the modules, when they are allready
in the kernel after recompiling?
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by bertr2d2 » Thu Aug 16, 2012 10:06 am
Hi,

As far as I know, insmod means "add the modules to the kernel on runtime".
When doing "make menuconfig" I found that all these modules seem to be
allready in the kernel sources and can be compiled to the kernel when selected.
So why do we have to "insmod" the modules, when they are allready
in the kernel after recompiling?


I don't know if I got your point here. Compiling the modules only means that they are
there. With 'make modules_install' you could install them to the normal
position under /lib/modules/<kernel_version>/kernel and resolving the dependencies.

With insmod you could always load modules. They don't need to be under /lib/modules/...
It's kind of quick&dirty or testing.

I would alway suggest to compile CAN & mcp251x as modules not as direct kernel part.

Regards

Gerd
Posts: 86
Joined: Wed Aug 08, 2012 10:12 pm
by maddin1234 » Thu Aug 16, 2012 5:57 pm
Hi,
the make worked, I was able to loopback.

Before posting the way, I want to retry it to be sure I didn't forget to write down a step.

In one of your posts, you mentioned mcp2515-async, but I didn't manage to download it.
I only find some kind of a module history.
Could you tell me a wget ... like you did it for libsocketcan?
Should I download this package before compiling the kernel? Does it replace existing files,
which are used during kernel make?

And I found a strange thing about installing the modules.
When I booted, non of the modules was shown by lsmod.
Then I installed spi-bcm2708 with insmod and after this,
lsmod showed mcp251x, spidev, candev, spi-bcm2708.
Why are all these modules active? I thaught insmod doesn't
care about dependencys.
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by maddin1234 » Fri Aug 17, 2012 4:11 pm
Hi,
here is the description to enable a raspbian distribution
to communicate with the can controller mcp2515

I was starting with Raspbian "wheezy"
2012-07-15-wheezy-raspbian
on the SD-card and inital raspi-config done

1.) change user to root
$ sudo su

2.) update and upgrade
I was asked to keep or to overwrite one file, I choosed "N" to keep it
# apt-get update
# apt-get -y dist-upgrade

3.) install git and ncurses-dev
(If you want to "make menuconfig", ncurses-dev is needed)
# apt-get -y install git ncurses-dev

4.) download kernel sources
# cd /usr/src
# git clone --depth 1 git://github.com/raspberrypi/linux.git
# cd linux

5.) copy the compiletime configuration out of the intial kernel to
the sources for the new kernel
(do not use this line, if you want to have another configuration file)
# zcat /proc/config.gz > .config

6.) edit .config with make menuconfig
# make menuconfig

This shows how I set the configurations
[*] Networking support --->
....<M> CAN bus subsystem support --->
........<M> Raw CAN Protocol (raw access with CAN-ID filtering)
........<M> Broadcast Manager CAN Protocol (with content filtering)
............CAN Device Drivers --->
................<M> Platform CAN drivers with Netlink support
................[*] CAN bit-timing calculation
................<M> Microchip MCP251x SPI CAN controllers
................[*] CAN devices debugging messages

....Device Drivers --->
........[*] SPI support --->
............<M> BCM2798 SPI controller driver (SPI0)
............<M> User mode SPI driver support
.......-*- GPIO Support --->
............[*] /sys/class/gpio/... (sysfs interface)

7.) patch the board definition bcm2708.c

Get a patchfile to the raspi with windows:
create a new txt-file and name it for example bcm2708.diff
copy the text of the posted patchfile from AlexB into that file
and save it on an usb-stick, if you have another clock frequency
change it in the patch-file or later in the patched bcm2708.c file

to mount a usb-stick follow
http://raspi.tv/2012/mount-a-usb-flash-drive-on-raspberry-piebo

apply a patch to board-configuration
# cd /usr/src/linux/arch/arm/mach-bcm2708
# cp /media/usbstick/bcm2708.diff .
# patch -p0 <mcp2515_patch.diff

8.) make a new kernel
(put the three commands to one command-line, because it will take very long)
# cd /usr/src/linux
# make; make modules;make modules_install

9.) replacing the old kernel with the new one
# cp arch/arm/boot/Image /boot/kernel.img

10.) reboot and login again
# shutdown -r now

11.) install can-tools
$ sudo su
# cd /home/pi
# mkdir can
#cd can

get socketcan and compile/install it
# wget http://www.pengutronix.de/software/libs ... .8.tar.bz2
# tar xvjf libsocketcan-0.0.8.tar.bz2
# rm libsocketcan-0.0.8.tar.bz2
# cd libsocketcan-0.0.8
# .configure
# make
# make install
# cd ..

get can-util
# git clone git://gitorious.org/linux-can/can-utils.git can-util
# cd can-util
# make
# make install
# cd ..


now you are ready to use can

load the modules
# modprobe spi-bcm2708
# modprobe can
# modprobe can-dev
# modprobe can-raw
# modprobe can-bcm
# modprobe mcp251x
# ip link set can0 up type can bitrate 500000


to see if you are receiving messages
# candump can0


have fun

maddin1234
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by maddin1234 » Fri Aug 17, 2012 4:36 pm
answer to the oszilation frequency issue:

as a dirty hack, it works to set the bitrate to 320000 (=500000*16/25)
then CNF1, CNF2 and CNF3 are set to the values for 500000kBit at a 25MHz MCP2515 clock
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by maddin1234 » Fri Aug 17, 2012 9:39 pm
Hi,
now having can0 running, I am thinking about expanding my hardware with second can controller.
As far as I read, there is only one GPIO Interrupt. So I will try to connect both controllers with "OR"
to the GPIO25.
Are the modules able to work both on the same interrupt?

For board definition I would try

Code: Select all
static struct spi_board_info bcm2708_spi_devices[] = {
      {
            .modalias = "mcp2515",
            .max_speed_hz = 10000000,
            .platform_data = &mcp251x_info,
            .irq = 110,
            .bus_num = 0,
            .chip_select = 0,
            .mode = SPI_MODE_0,
      } , {
            .modalias = "mcp2515",
            .max_speed_hz = 10000000,
            .platform_data = &mcp251x_info,
            .irq = 110,
            .bus_num = 0,
            .chip_select = 1,
            .mode = SPI_MODE_0, 
      }
};


Can anyone say if this will work, before I change the hardware?

What about re-recompiling the kernel?
When I change the BCM2708.c, only some modules are compile by the make.
I tryed it before (to switch between 16MHz and 25 MHz) but the change didn't
have any effect. I also tryed to delete all .o and .ko files from the modules I have
to insmod for using can, but also no success.
How can I assure, that all modules which depend on the board configuration change
will be compiled new?
Posts: 68
Joined: Sat Aug 04, 2012 8:33 pm
by bertr2d2 » Sat Aug 18, 2012 7:07 am
Hi maddin1234,

Are the modules able to work both on the same interrupt?

two CAN controller on the same interrupt won't work without modifying
the module code. IMHO you need another GPIO for the second interrupt.
BTW: The setup won't be very efficient: Both CAN must be asked for new
data if they share the same interrupt.

How can I assure, that all modules which depend on the board configuration change
will be compiled new?

Changing the board definition will only change the kernel. That's where the structure is stored. Make is smart enough to realize, which code depends on each other and just compile only
which are needed to compile. The mcp251x module asking the kernel for the informations
on startup so it doesn't need to be recompiled if the structure changed. 'make module' should compile the modules.

Regards

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