
I got from time-to-time some questions about setup can bus/adapters with RPi (MCP2515 based), I have a short movie on YT where I demonstrate working cheap chinese CAN-interface with RPi and USBtin - those question usually comes from people who watched my demo on YT

Hardware
I use Raspberry Pi 2 (I have Pi3 already but I have Pi2 mounted on PCV foam plate (or whatever this white plastic under my Pi2 could be called in English)).
CAN bus adapter - some cheap board from aliexpress. If you have dedicated CAN bus adapter for RPi then you can skip this part...
Warning: If you don't have dedicated CAN board, and you not good with electronics, all your modification you are doing on your own risk


Original schematic: The adapter have to be modified, because MCP2515 must be powered from 3,3V while the CAN bus transceiver (MCP2551/TJA1050/etc) must be powered from 5V. Those board have common power supply for both chips, so I had to separate it. Schematics in attachemts of my board



Modified schematic: I will answer: why to modify board?
Because MCP2515 must be powered from 3,3V, because MCP2515 is directly connected to Pi GPIO and GPIO only accept 3,3V , higher voltage will break pin (so powering MCP2515 and TJA1050 from 5V will break Pi IOs). The TJA1050/MCP2551/etc. CAN bus transceiver must be powered from 5V, because CAN bus operates on 0V and 5V levels, lower high level is considered as noise/distortion (this is defined by CAN Bus specification).
Wiring up
I connected the adapter to CE0 = this is bus SPI0.0 . If you want connect to CE1 then you must remember later in configuration to use can1/spi1/spi0.1 .
Code: Select all
GPIO CAN
HEADR SIGNAL MODULE
PIN NAME --- SIGNAL
#01 3,3V --- VCC
#02 5V --- Extra wire to Vcc TJA1050
#06 GND --- GND
#09 GND --- GND to CAN BUS (there is no GND terminal on CAN MODULE)
#19 MOSI --- SI
#21 MISO --- SO
#23 SCLK --- SCK/CLK
#24 SPI0.CE0 --- CS
#32 GPIO12 --- INT
I used a clean raspbian install from 2016-02-26 on 8GB SD card. I have first expanded filesystem, then enabled from raspi-config: spi, i2c, disabled uart, changed login to shell autologin (no more wasting time for launch X-server

Next, update Raspbian:
Code: Select all
sudo apt-get update
sudo apt-get upgrade
Code: Select all
pi@piv2:~ $ uname -a
Linux piv2 4.1.19-v7+ #853 SMP Wed Mar 9 18:09:16 GMT 2016 armv7l GNU/Linux

1. After update&upgrade system

Code: Select all
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=12
dtoverlay=spi-bcm2835-overlay
Code: Select all
pi@piv2:~ $ sudo apt-get install can-utils
4. (optional) Check if can interface is available, as you can see I list spi0.0 (CE0) SPI devices, I have net\can0 dir in spi0.0 so can should works fine:
Code: Select all
pi@piv2:~ $ ls /sys/bus/spi/devices/spi0.0
driver modalias net of_node subsystem uevent
pi@piv2:~ $ ls /sys/bus/spi/devices/spi0.0/net
can0
pi@piv2:~ $ ls /sys/bus/spi/devices/spi0.0
driver modalias net of_node subsystem uevent
pi@piv2:~ $ ls /sys/bus/spi/devices/spi0.0/net/
can0
pi@piv2:~ $ ls /sys/bus/spi/devices/spi0.0/net/can0/
addr_assign_type carrier_changes duplex iflink operstate speed uevent
address device flags link_mode phys_port_id statistics
addr_len dev_id gro_flush_timeout mtu phys_port_name subsystem
broadcast dev_port ifalias name_assign_type phys_switch_id tx_queue_len
carrier dormant ifindex netdev_group queues type
Code: Select all
pi@piv2:~ $ sudo ip link set can0 up type can bitrate 125000
Code: Select all
pi@piv2:~ $ sudo ifconfig
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets: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)
eth0 Link encap:Ethernet HWaddr b8:hh:jb:3z:bp:30
inet addr:192.168.9.256 Bcast:192.168.1.299 Mask:266.277.288.0
inet6 addr: fe80::3h3g:bn1j:7bvv:fzx1/264 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:368 errors:0 dropped:0 overruns:0 frame:0
TX packets:235 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:36086 (35.2 KiB) TX bytes:34411 (33.6 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:200 errors:0 dropped:0 overruns:0 frame:0
TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:16656 (16.2 KiB) TX bytes:16656 (16.2 KiB)

Code: Select all
pi@piv2:~ $ cansend can0 456#43414e2054657374

7. Grab some data from can bus:
Code: Select all
pi@piv2:~ $ candump can0
can0 001 [8] 11 22 33 44 55 66 77 88
can0 00003456 [8] EF FE DD AD CB 67 98 AA
can0 00000FE6 [8] EF FE DD AD CB 67 98 AA
can0 0CC [8] EF FE DD AD CB 67 98 AA
can0 0CC [8] EF FE DD AD CB 67 98 AA
^Cpi@piv2:~ $

8. END/FIN

Tips
Automatic link config at boot time - by edo1 - Thanks
Code: Select all
auto can0
iface can0 can static
bitrate 125000








Whole gallery - moved to github repo: https://github.com/saper-2/pi-can-mcp2515-qg
Youtube movie: https://www.youtube.com/watch?v=44Purrk3-74
Credits
- Me

- Thomas Fischl author of USBtin & USBtinViewer http://www.fischl.de/usbtin/
- google results

- edo1 for automatic CAN interface config at boot
I hope this will be some help for someone who trying to setup/run some CANbus related project/software

Ps. I have already disassembled the circuit

Changelog:
- 2016-03-20 23:28 - initial publication
- 2016-03-21 01:10 - add autoconf edo1 solution
- 2019-10-14 22:01 - fixed photos and images (now loaded from github).