skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Mon Nov 16, 2015 2:40 pm

msperl wrote:If you are asking: can I run multiple can boards (mcp251x or otherwise)?

Then the answer is yes: you can easily do that - right now I run 2 mcp2515 and it works fine (with the correct device-tree).

You can even add some kernel based CAN-bridging between the two interfaces by using the cangw tool to configure the routing inside the kernel (including some modifications to the data in the middle).
Hi msperl, if I want to run a dual mcp2551 do I add another entry to config.txt like:

dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=25

Will it automatically use CE1 for the chipselect ?

skpang

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: CAN controller

Mon Nov 16, 2015 7:40 pm

Have a look at the device tree overlay, the chip select is hard coded in there.
https://github.com/raspberrypi/linux/issues/848
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Mon Nov 16, 2015 11:33 pm

DougieLawson wrote:Have a look at the device tree overlay, the chip select is hard coded in there.
https://github.com/raspberrypi/linux/issues/848
Hi Dougie, thanks for the reply. Looking the mcp2515-spi0.0.dts it doesn't say SPI 0 chip select is on GPIO8/CE0 but it is working for CAN0. I want to add another mcp2515 so it would be CAN1.

Looking at /boot/overlays/ on a Pi there is only a .dtb file how do I get the .dts file? I assume somewhere I can select the chip select to GPPIO7/CE1 for CAN1.

skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Mon Nov 16, 2015 11:43 pm

Ok, I've found the .dts for for CAN1 https://github.com/raspberrypi/linux/bl ... verlay.dts

Looks like the only difference is for the CAN0 and CAN1 overlay is spi0.0 and spi0.1

So spi0.0 uses GPIO8/CE0
and spi0.1 uses GPPIO7/CE1 ?

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: CAN controller

Mon Nov 16, 2015 11:43 pm

You can use the dts compiler to read the blob (dtb file) or read /proc/device-tree and generate something that looks a bit like the source.

Or look at: https://github.com/raspberrypi/linux/tr ... m/boot/dts where the current source lives.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Tue Nov 17, 2015 10:51 am

Still not quite working with two mcp2515 devices. The original setup works fine with one mcp2515.
I've added another device and connected the Chip Select to GPPIO7/CE1. In the /boot/config.txt file I've added the line:
dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=25

With an logic analyser connected, during boot up I can see both the CE1 and CE0 toggles individually so it looks like the Pi is initialising both devices.

When I try to bring up CAN0 by sudo /sbin/ip link set can0 up type can bitrate 500000 I noticed it is talking to CE1, it used to be CE0. Then I send some packets ./cansend can0 7DF#0201050000000000 it is going out on the new device.

I then try to bring up CAN1 but it says "RTNETLINK answers: Device or resource busy". I took down CAN0, then managed to bring up CAN1.

Any reason why I can't have CAN0 and CAN1 up at the same time?

The only thing I'm not sure is the interrupt pin. Both devices is connected to GPIO25 as stated in the overlay. Maybe it need individual pin but it is hard coded at the moment.

skpang

xxrosi
Posts: 3
Joined: Wed Nov 25, 2015 6:16 pm

Re: CAN controller

Wed Nov 25, 2015 6:56 pm

Hi all,
I read through this thread and else to find what could be problem I am facing. I have typical configuration: PICAN CAN-Bus bord RPi B+ running Raspbian Jessie 4.1.10+. I have it connected to Stiebel Eltron CAN Bus. JP3 is shortened. Configuring can0 interface according instruction on skpang site wasn't problem. Testing mcp2515 using script on this thread worked as well - output was as shown on the post. The problem is that sending any can frame with cansend will result in ERROR-PASSIVE state. Here's output from candump:

[email protected] ~ $ candump -t z -a -x -e any,0:0,#FFFFFFFF
(000.000000) can0 RX - - 20000004 [8] 00 08 00 00 00 00 00 00 ERRORFRAME
controller-problem{tx-error-warning}
(000.026198) can0 RX - - 20000004 [8] 00 20 00 00 00 00 00 00 ERRORFRAME
controller-problem{tx-error-passive}

I don't what is bitrate on the bus, os I tried all bitrates, but it didn't help.

The interface before sending frame looks like this:

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can state ERROR-ACTIVE restart-ms 0
bitrate 20000 sample-point 0.875
tq 3125 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 8000000
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

and after:

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can state ERROR-PASSIVE restart-ms 0
bitrate 20000 sample-point 0.875
tq 3125 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 8000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 1 1 0
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 1 1 0 0

I have measure voltage on the can bus and it looks good before sending ~2.5V on both H and L meaning it is recessive. After sending frame voltage on H goes up and L - down - dominant. It will stay dominant until I reset interface to ERROR-ACTIVE. Is it correct behaviour?



rosi

Blongo
Posts: 4
Joined: Mon Mar 07, 2016 2:58 pm

Re: CAN controller

Tue Mar 08, 2016 12:42 pm

Hi there,

as I'm currently trying to make a CAN controller work on a raspberry 2 I reached a point where I have no clue what to do anymore.

My setup:
- Raspberry Pi 2
- Raspbian Wheezie (Jessie on a different sd-card)
- Kernel 4.1.17 before now 4.1.19
- CAN Controller USB2CAN - 8devices

I followed the instruction to compile the kernel from this site:
https://www.raspberrypi.org/documentati ... uilding.md

Code: Select all

- git clone --depth=1 https://github.com/raspberrypi/linux
- sudo apt-get install bc
- cd linux
- KERNEL=kernel7
- make bcm2709_defconfig // For this entry i used make menuconfig since I wanted to add the drivers for the controller, I added From Networking Support -> CAN bus subsystem support -> CAN Device Drivers -> Serial/USB serial CAN Adaptors and from there under CAN USB interfaces -> 8 devices USB2can interface andsaved it as .config // I added Serial/USB serial CAN Adaptors because a friend
- make -j4 zImage modules dtbs //-j4 for the use of 4 cores, It went quite well and in about 1 hour the whole thing was compiled, where I was told it would take up to 8 hours
- sudo make modules_install
- sudo cp arch/arm/boot/dts/*.dtb /boot/
- sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
- sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
- sudo scripts/mkknlimg arch/arm/boot/zImage /boot/$KERNEL.img
Everything worked like a charm and I think I had the Kernel 4.1.17 before and now I have the 4.1.19 so I must have done something right but if I type in "ifconfig" there is no CAN0 displayed.

My friend made it work about a year ago and he used rpi-build, which has been discontinued and isn't working anymore under Jessie. So no help from that site.

Is there anybody who made the USB2Can Controller work and can tell me the thing that I'm missing?
Any help is appreciated.

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: CAN controller

Tue Mar 15, 2016 10:38 pm

msperl wrote:right now I still focus on getting the spi driver working fast.
is this work finished?

BTW, does mcp2515 (especially mcp251x module) get some advantage of DMA usage? I doubt because CAN-packets are too short (only a few bytes) and related SPI-transfers are too short too.
msperl wrote:As soon as I finish the spi-stuff I can have another look at the mcp251x driver where to improve (besides assigning 2 more GPIOs to handle the reads slightly faster)
any news? ;)

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: CAN controller

Tue Mar 15, 2016 10:45 pm

skpang wrote:The only thing I'm not sure is the interrupt pin. Both devices is connected to GPIO25 as stated in the overlay. Maybe it need individual pin but it is hard coded at the moment.
Are you out of GPIO pins?
IMHO pin sharing is a bad idea even if it is supported by the software.

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

Re: CAN controller

Thu Mar 17, 2016 12:35 pm

SPI-DMA does not help and would in this case actually be counter-productive.
a) right now the DMA-engine will produce 1 interrupt per dma channel used (and SPI requires 2 channels
b) interrupt driven SPI works very well for transfers up to 64 bytes (fifo size) and in our case of < 16 bytes will trigger only once compared to DMA
c) the transfer duration of 16 bytes @10MHz SPI clock (the max of mcp2515) is actually less than 0.000015s and for those situations the SPI driver is implementing polling (which should improve the performance, as irq and wakeup latencies are avoided)

As for sharing interrupt lines between different mcp2515: I believe the mcp2515 driver does not support this and would require a patch.
Also "basic" discovery which mcp2515 has really triggered the irq takes longer and is blocking the SPI bus unnecessarily for a NOP.

So better to use a dedicated interrupt line - or even better: use the main _and_ the auxiliar spi device, as then multiple cpus can work in parallel.

There is even a small benefit (i.e: loosing less packets in high congestion cases) using 3 interrupt lines per mcp2515 - one as the main irq and then the 2 lines for each receive buffer. That way we can immediately trigger "message" reception before asking the mcp2515 for its status bits... I once had a small patch that would allow that.

Blongo
Posts: 4
Joined: Mon Mar 07, 2016 2:58 pm

Re: CAN controller

Thu Mar 31, 2016 1:00 pm

Blongo wrote: Everything worked like a charm and I think I had the Kernel 4.1.17 before and now I have the 4.1.19 so I must have done something right but if I type in "ifconfig" there is no CAN0 displayed.

My friend made it work about a year ago and he used rpi-build, which has been discontinued and isn't working anymore under Jessie. So no help from that site.

Is there anybody who made the USB2Can Controller work and can tell me the thing that I'm missing?
Any help is appreciated.
A quick replay to my youngerself.
The problem was in the kernel distribution. I took the Kernel v4.05, installed the socketcan and can-utils and everything worked like a charm.
I'm having no seriuous clue what the problem really was about but I think it could be the Kernel Header files that were missing for the newer versions.

So maybe that will help someone someday somehow :) Its might not always be the best choice to install the latest software:)

mrip
Posts: 7
Joined: Mon Apr 25, 2016 7:21 pm

Re: CAN controller

Mon Apr 25, 2016 8:19 pm

Hi guys. That's a great forum about the Controller Area Network, but unfortunately my questions are still unanswered.

I have a simple configuration of a network containing two end-points. The end-points are an RPiv2+PiCAN2 Shield (http://skpang.co.uk/catalog/images/rasp ... AN2UGB.pdf ) and an RPiv3 + the same PiCAN2.

I have followed the indications from the user guide, but when I execute ./cansend can0 7DF#0201050000000000, I am not able to sniff any traffic with ./candump. Nor the Wireshark software detects any outbound traffic.

Does anybody have any suggestion?

https://drive.google.com/open?id=0B6hoJ ... GlRLXlGYUE

https://drive.google.com/open?id=0B6hoJ ... 5qZ2hWUWNR

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: CAN controller

Tue Apr 26, 2016 7:32 pm

are both ends having same configuration? (speed and so on)
is there any error if you try to send ten packets?
did you enable terminating resistors? (see paragraph 1.6 of pican 2 user guide)

1bonp
Posts: 2
Joined: Wed May 11, 2016 10:19 am

RPi3 with Pican2 Problems

Thu May 12, 2016 9:44 am

Hello everyone!

I recently purchased a brand new RaspberryPi 3 Model B+ for my internship project.
The goal is to establish a CAN connection beetween the microcontroller and a device
(which is a battery but it isn't the purpose of my post).

For that i also ordered the Pican2 board from the skpgang website:
skpang.co.uk/catalog/pican2-canbus-board-for-raspberry-pi-2-p-1475.html

Now, i followed every tutorial available on the site, and the "easy to install driver thing" simply won't work.
The Pi can't send any frame. I don't know what to do so far, and i'm blocked since 2 weeks and I can't find the problem.
Any help/suggestion/support would be gladly appreciated.

Now here is the detail of everything i've done so far:

-First of all, I installed the latest Rasbian downloaded from the Raspberry site. I've not encountered any problems formatting my SD card and writing the image. The Rpi boots

normally, i have a HDMI monitor connected to it directly and also a keyboard and a mouse. The first thing i do after configuring the boot is

Code: Select all

sudo apt-get update
sudo apt-get upgrade
in order to have the latest updates. The uname -a command returns me
Linux raspberrypi 4.1.19-v7+
-After a reboot, i added the last 3 lines in config.txt as stated in the tutorial to enable spi and the overlays of mcp-2515 and bcm2835 drivers.

Code: Select all

dtparam=spi=on
dtoverlay=mcp-2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
-Again, i reboot, and i bring up the can interface with this command:

Code: Select all

sudo /sbin/ip link set can0 up type can bitrate 250000 
I use 250 baudrate because the battery uses that transfering speed. But it's not important at the moment. The command ifconfig can0 returns me the can0 connection


-Now, for installing can-utils I used different methods: the result is exactly the same and i can't figure out why. I did a reset of the Rasbian Jessie for each method and

the result is still the same.

1st method

Code: Select all

sudo apt-get install can-utils
sudo reboot
2nd method
As in the tutorial at
http://skpang.co.uk/blog/archives/1165

I did:

Code: Select all

sudo wget http://www.skpang.co.uk/dl/can-test_pi2.zip
unzip can-test_pi2.zip
cd can-test_pi2.zip
for executing the programs as cansend, candump

3rd method

http://elinux.org/Can-utils
I followed exactly what's evoked on the tutorial.
I installed these packages first, because the compiling of the can-utils gave me some errors:

Code: Select all

sudo aptitude install automake autoconf
sudo aptitude install libtool


Now the thing is: whenever i try to use the cansend command, it executes fine.
However,

Code: Select all

 cansend can0 123#13371337 
should send on can0 this frame.
The command it executes.
When i try to use candump can0, the console stays on hold until i'm obliged to ctrl^C

This is the output after sending my frame (the output is exactly the same after accomplishing all the methods)

Image
Image

I don't know what to do so far, i followed every step and it should work but it doesn't..
Is it because it is a RaPi3 and not 2 ?
Strange because the GPIO ports are exactly the same between the 2 models.

I tried also using wireshard to record any incoming input. I used a Usb Pcan connected directly to the 4 way screw terminal
With the software installed on the pc Pcan View i tried sending a frame every 1000ms but the device doesn't record any input.

Image

I noticed there is a LED on the right side of the 4way screw terminal, near the R8 resistance. It's always off, i'm asking myself if there is a connectivity problems between the Can module and the Pi (strange because when i try to bring the can0 interface up, it recognises my device .. )




Thank you in advance for considering helping me.

Filip


EDIT:
Okay , so finnally i discovered what was wrong all this time: if you want to use the Raspberry Pi with an external device, you MUST enable the terminator resistor to avoid reflections/perturbation of the signal. Once you enabled it just follow the steps and it will work

1bonp
Posts: 2
Joined: Wed May 11, 2016 10:19 am

Re: CAN controller

Fri May 13, 2016 10:38 am

Can somebody tell me how can i create a script that executes some commands in console ?

Like for example, i want to use the command

Code: Select all

cansend can0 123#13371337
Every 3 seconds, i want to send a frame to can0 periodically.

How can I do that ?

skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Fri May 13, 2016 8:12 pm

Can someone from the Raspberry Pi team please try CAN with the latest Jessie release 2016-05-10.

I think CAN is broken. When I try to bring up the interface it says Cannot find device "can0".

skpang

skpang
Posts: 18
Joined: Mon Jul 29, 2013 10:09 pm
Location: Essex, UK
Contact: Website

Re: CAN controller

Sat May 14, 2016 9:41 am

Found the problem on the latest Jessie release 2016-05-10.

They changed the syntax for the overlay. The config.txt file need to be updated.

sudo nano /boot/config.txt

Changed from this:
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25

to this
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25

Thanks to msperl for his help.

skpang

DirkS
Posts: 9501
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: CAN controller

Sat May 14, 2016 1:14 pm

skpang wrote:They changed the syntax for the overlay. The config.txt file need to be updated.
AFAIK '-overlay' was never needed, but a lot of users did it anyway, probably because of the actual filename of the overlay (see e.g. the overlay readme for kernel 4.0: https://github.com/raspberrypi/linux/tr ... s/overlays)
The change is that '-ooverlay' is not accepted anymore.

mrip
Posts: 7
Joined: Mon Apr 25, 2016 7:21 pm

Re: CAN controller

Sun Jun 12, 2016 8:53 pm

Hello guys, i'm trying to make a platform similarly to a SCADA Platform, using the CAN protocol for monitoring some parameters.
I take some data from the sensors I'm monitoring, transmit them on the CAN network and the final purpose is to store them into an SQL database (Mysql or SQLite, if smbd has a point, i'm curious about it), and after the db is ok I'm trying to display them into a dynamic webpage, showing a real-time graph of the parameters.

However, I'm stuck at this point:
https://drive.google.com/open?id=0B6hoJ ... 2lVbi13UzA

I receive the data from the transmitter node (both nodes are RPi), but I don't know how to use the CSVWriter or SQLiteWriter-classes-> http://python-can.readthedocs.io/en/lat ... litewriter <--- to be able to store the data at least into a CSV file, if directly into a database it's not possible or it's too difficult for me.

That's the script with the lines I was thinking about, but surely it's not exactly what it should be:

Code: Select all

#!/usr/bin/env python
#Libraries
import time, can, sys, os, csv

bus = can.interface.Bus(channel='can0', bustype='socketcan_ctypes')
notifier = can.Notifier(bus, [can.Printer()])

outputFile = open('output.csv', 'w', newline='')
outputWriter = csv.writer(outputFile)
outputWriter.writerow( [can.Printer()] )
outputFile.close()
Thank you :)

hassan05
Posts: 1
Joined: Mon Jun 13, 2016 3:13 pm

Re: CAN controller

Mon Jun 13, 2016 3:19 pm

I am trying to use two MCP 2515 at the same time.
First I started with only one can (can0) connected, it worked
Now, both the cans (can0, can1) are up but the one which was specified last in the /boot/config.txt is the only one working
Any idea about the possible reason(s)?

falco
Posts: 2
Joined: Thu Jun 30, 2016 1:31 am

Re: CAN controller

Thu Jun 30, 2016 1:40 am

Hey there,

I was wondering, if anybody of the Raspberry Team already tried to get CAN working on the latest release of Rapbian (4.4.x) on a Raspberry Pi3.
I'm getting crazy to solve that.

My steps were like in http://www.forum-raspberrypi.de/Thread- ... spberry-pi the only thing i changed was line:

Code: Select all

dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 
to

Code: Select all

dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25 
After a reboot when trying to get the CAN interface up using

Code: Select all

sudo /sbin/ip link set can0 up type can bitrate 500000
it tells me can0 device not found.

I also installed the can-utils.

Every answer/hint/recommendation will be highly appreciated.
Thank you all for your help!!

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

Re: CAN controller

Thu Jun 30, 2016 4:22 am

Hi Team,

Good to see you again,
I have one doubt in model A+.

Here my requirement:
  • I have to convert my pressure sensor voltage having range 0 - 10 V , resolution 0.05 V into physical value[0-200 bar] then i have to send the same into CAN-BUS.

    I checked with A+ model which having 40 GPIO , I can see 2 SPI port
    . So I planning to connect my pressure sensor value to one of the SPI and another SPI for CAN-communication.

    What I want now is whether model A+ is capable of handling 2 SPI independently [One for ADC communication and another one for SPI to CAN communication] ? :roll: Block diagram is attached
If you have any suggestion or I understood wrongly please guide and correct me.
Attachments
pMc to CAN.png
Block Diagram
pMc to CAN.png (14.24 KiB) Viewed 5943 times

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

Re: RPi3 with Pican2 Problems

Thu Jun 30, 2016 10:24 am

Hi ,

Have you solved your problem ?

Is there any solution to your problem ?

Even i planning to buy Pi3 Model B , can you please tell can i go for Pi3 or Pi2 for CAN Bus interface ?

Regards,
Senthil

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

Re: CAN controller

Thu Jun 30, 2016 10:48 am

Hi Skpang,

Thanks for your support,

I need few information in the Pi3 Model B.
  • whether can i use the PICAN2 for my CAN communication?
    whether can i use 2nd SPI for other communication?like ADC?
Regards,
Senthil
skpang wrote:There is a PiCAN board for the Raspberry Pi.
http://skpang.co.uk/catalog/pican-canbu ... -1196.html

Installing drivers for Raspberry Pi 2.
http://skpang.co.uk/blog/archives/1165

Example of writing your own software.
http://skpang.co.uk/blog/archives/1199

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