Raspberry connected to a CAN  bus via Levelshifter using SPI interface Levelshifter TXB0108 (TXB0108 8-Bit Bidirectional Voltage-Level Translator With Auto …) CAN Modul MCP251

Wiring Raspberry PI

Caution: !!!!!!!!!3,3V vs 5V !!!!!!! -> do NOT connect directly SPI to MCP 2515 as voltag Levels do not fit

 

  • Levelshifter TXB0108 (TXB0108 8-Bit Bidirectional Voltage-Level Translator With Auto …)
  • CAN Modul MCP2515

  

Raspberry

Raspberry
PIN (GPIO)

Function

LevelShifter

3,3V side

LevelShifter

5V side

CAN Modul

19 (12)

SDI

A1

B1

SI

20

GND

GND

GND

GND

21 (13)

SDO

A2

B2

SO

22 (6)

INT

A5

B5

INT

23 (14)

SCK

A3

B3

SCK

24 (10)

CS

A4

B4

CS

 

 

  • 5 V side level shifter AND CAN Bus Modul with own power supply! 
  • GND is common for all modules: for the own power supply of the CAN Modul, the Levelshifter (both sides Low Voltage 3,3V + High Voltag 5V and the Raspberry)

 Raspi-Lvl-Shifter_CAN-Bus_v1_Steckplatine

CAN-BUS:

L,H and GND

 

I guess a three wire cabeling including a common mass (GND) beside H and L wire will bring higher stability of the CAN bus expecially havong very long distances

 

First results

 

Raspberry terminal opputput with proof that the SPI device is woring with the needed overlay: 

 

pi@HERB-raspi3ModB:~ $ sudo vcdbg log msg

001635.017: brfs: File read: /mfs/sd/config.txt

001635.630: brfs: File read: 685 bytes

….

002286.484: brfs: File read: 4932808 bytes

002292.791: brfs: File read: /mfs/sd/bcm2710-rpi-3-b.dtb

002292.852: Loading 'bcm2710-rpi-3-b.dtb' to 0x4bc4c8 size 0x627f

002432.095: brfs: File read: 25215 bytes

002434.224: brfs: File read: /mfs/sd/config.txt

002434.463: dtparam: spi=on

002449.068: brfs: File read: 685 bytes

002463.373: brfs: File read: /mfs/sd/overlays/mcp2515-can0.dtbo

002481.415: Loaded overlay 'mcp2515-can0'

002481.471: dtparam: oscillator=8000000

002482.292: dtparam: interrupt=25

002483.701: dtparam: spimaxfrequency=500000

002566.096: brfs: File read: 1833 bytes

002579.187: brfs: File read: /mfs/sd/overlays/spi0-hw-cs.dtbo

002592.263: Loaded overlay 'spi0-hw-cs'

003627.251: gpioman: gpioman_get_pin_num: pin EMMC_ENABLE not defined

003743.712: Device tree loaded to 0x2eff9700 (size 0x682a)

003745.794: gpioman: gpioman_get_pin_num: pin SDCARD_CONTROL_POWER not defined

004894.994: vchiq_core: vchiq_init_state: slot_zero = 0xfad80000, is_master = 1

004899.241: hdmi: HDMI:hdmi_get_state is deprecated, use hdmi_get_display_state instead

 

 

pi@HERB-raspi3ModB:~ $ ifconfig -a

can0: flags=193<UP,RUNNING,NOARP>  mtu 16

        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

……

………

 

 

  1. restuls

 

==> CAN Bus is there and accessible (in principle)

 

pi@HERB-raspi3ModB:~ $ ls /sys/bus/spi/devices/spi0.0/net

can0

 

pi@HERB-raspi3ModB:~ $ ls /sys/bus/spi/devices/spi0.0/net/can0/

addr_assign_type  dormant            name_assign_type  speed

address           duplex             netdev_group      statistics

addr_len          flags              operstate         subsystem

broadcast         gro_flush_timeout  phys_port_id      tx_queue_len

carrier           ifalias            phys_port_name    type

carrier_changes   ifindex            phys_switch_id    uevent

device            iflink             power

dev_id            link_mode          proto_down

dev_port          mtu                queues

 


 

  1. Setup CAN Interface

 

Setup CAN interface, using 125k baudrate:

 

pi@piv2:~ $ sudo ip link set can0 up type can bitrate 125000 triple-sampling on

 

 

pi@HERB-raspi3ModB:~ $  sudo ip link set can0 up

 

pi@HERB-raspi3ModB:~ $  ifconfig -a can0

                                                

can0: flags=193<UP,RUNNING,NOARP>  mtu 16

        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 

 

Automatic link config at boot time - if you want to setup interface at startup you could add to /etc/network/interfaces

 

auto can0
iface can0 can static
        bitrate 125000

 

Turn off the Can bus:

pi@HERB-raspi3ModB:~ $ sudo /sbin/ip link set can0 down

 

Activate with loopback on (to test if CAN device is OK):

pi@HERB-raspi3ModB:~ $ sudo ip link set can0 up type can bitrate 125000 loopback on

 

6. Send something (use 2 Terminal Windows)

 

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

 

 

7. Grab some data from can bus in second terminal windows:

 

candump any,0:0

 

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
^C

pi@piv2:~ $

 

 

8. Turn OFF Can device:

sudo /sbin/ip link set can0 down

 

9. Start CAN device with some settings

 

sudo ip link set can0 up type can bitrate 125000 triple-sampling on restart-ms 100

sudo ifconfig can0 txqueuelen 1000

 

 

10.  concret settings:

pi@HERB-raspi3ModB:~ $ sudo /sbin/ip link set can0 down

pi@HERB-raspi3ModB:~ $ sudo ip link set can0 up type can bitrate 125000 triple-sampling on

 

# read bus for messages

pi@HERB-raspi3ModB:~ $ candump can0 

  can0  300   [0]

  can0  300   [0]

  can0  300   [0]

  can0  000   [8]  43 41 4E 20 54 65 73 74     --> message to address 000

 

 

# send message to bus

cansend can0 000#43414e2054657374

 

Other commands

 

Send random packets (some stress test to can bus)

pi@HERB-raspi3ModB:~ $ cangen can0 -v

 

cangen can0 -g 500 -I C8 -L 8 -D r

 

-g 500      # gap of 500ms

-I C8     # fixed can ID

-L 8       # length 8 Byte data

-D r      # random data

cangen -h .. Help

 

 

Analysis

Show the settings and status

pi@HERB-raspi3ModB:~ $ ip -s -d link show can0

3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 1000

    link/can  promiscuity 0

    can <LOOPBACK,TRIPLE-SAMPLING> state ERROR-ACTIVE restart-ms 100

  bitrate 125000 sample-point 0.875

  tq 500 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          2          2          0         numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

    RX: bytes  packets  errors  dropped overrun mcast  

    40         92       0       0       0       0      

    TX: bytes  packets  errors  dropped carrier collsns

    577        92       9       9       0       0