xref: /qemu/docs/system/devices/can.rst (revision 8a780cd212647a6013c8ea59e0929dad996e2c54)
187f14eaaSLucas RamageCAN Bus Emulation Support
287f14eaaSLucas Ramage=========================
3e73e0948SPavel PisaThe CAN bus emulation provides mechanism to connect multiple
4*8a780cd2SSamuel Tardieuemulated CAN controller chips together by one or multiple CAN buses
5*8a780cd2SSamuel Tardieu(the controller device "canbus"  parameter). The individual buses
6e73e0948SPavel Pisacan be connected to host system CAN API (at this time only Linux
7e73e0948SPavel PisaSocketCAN is supported).
8e73e0948SPavel Pisa
9*8a780cd2SSamuel TardieuThe concept of buses is generic and different CAN controllers
10de4d4adbSPavel Pisacan be implemented.
11de4d4adbSPavel Pisa
12de4d4adbSPavel PisaThe initial submission implemented SJA1000 controller which
13de4d4adbSPavel Pisais common and well supported by by drivers for the most operating
14de4d4adbSPavel Pisasystems.
15e73e0948SPavel Pisa
16e73e0948SPavel PisaThe PCI addon card hardware has been selected as the first CAN
17e73e0948SPavel Pisainterface to implement because such device can be easily connected
186fe6d6c9SPeter Maydellto systems with different CPU architectures (x86, PowerPC, Arm, etc.).
19e73e0948SPavel Pisa
20de4d4adbSPavel PisaIn 2020, CTU CAN FD controller model has been added as part
21ac9574bcSStefan Weilof the bachelor thesis of Jan Charvat. This controller is complete
22de4d4adbSPavel Pisaopen-source/design/hardware solution. The core designer
23de4d4adbSPavel Pisaof the project is Ondrej Ille, the financial support has been
24de4d4adbSPavel Pisaprovided by CTU, and more companies including Volkswagen subsidiaries.
25de4d4adbSPavel Pisa
26e73e0948SPavel PisaThe project has been initially started in frame of RTEMS GSoC 2013
27e73e0948SPavel Pisaslot by Jin Yang under our mentoring  The initial idea was to provide generic
28e73e0948SPavel PisaCAN subsystem for RTEMS. But lack of common environment for code and RTEMS
29e73e0948SPavel Pisatesting lead to goal change to provide environment which provides complete
30e73e0948SPavel Pisaemulated environment for testing and RTEMS GSoC slot has been donated
31e73e0948SPavel Pisato work on CAN hardware emulation on QEMU.
32e73e0948SPavel Pisa
33ac9574bcSStefan WeilExamples how to use CAN emulation for SJA1000 based boards
3487f14eaaSLucas Ramage----------------------------------------------------------
35e73e0948SPavel PisaWhen QEMU with CAN PCI support is compiled then one of the next
36e73e0948SPavel PisaCAN boards can be selected
37e73e0948SPavel Pisa
3887f14eaaSLucas Ramage(1) CAN bus Kvaser PCI CAN-S (single SJA1000 channel) board. QEMU startup options::
3987f14eaaSLucas Ramage
40e73e0948SPavel Pisa    -object can-bus,id=canbus0
41e73e0948SPavel Pisa    -device kvaser_pci,canbus=canbus0
4287f14eaaSLucas Ramage
4387f14eaaSLucas RamageAdd "can-host-socketcan" object to connect device to host system CAN bus::
4487f14eaaSLucas Ramage
45e73e0948SPavel Pisa    -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0
46e73e0948SPavel Pisa
4787f14eaaSLucas Ramage(2) CAN bus PCM-3680I PCI (dual SJA1000 channel) emulation::
4887f14eaaSLucas Ramage
49e73e0948SPavel Pisa    -object can-bus,id=canbus0
50e73e0948SPavel Pisa    -device pcm3680_pci,canbus0=canbus0,canbus1=canbus0
51e73e0948SPavel Pisa
5287f14eaaSLucas RamageAnother example::
5387f14eaaSLucas Ramage
54e73e0948SPavel Pisa    -object can-bus,id=canbus0
55e73e0948SPavel Pisa    -object can-bus,id=canbus1
56e73e0948SPavel Pisa    -device pcm3680_pci,canbus0=canbus0,canbus1=canbus1
57e73e0948SPavel Pisa
5887f14eaaSLucas Ramage(3) CAN bus MIOe-3680 PCI (dual SJA1000 channel) emulation::
5987f14eaaSLucas Ramage
60e73e0948SPavel Pisa    -device mioe3680_pci,canbus0=canbus0
61e73e0948SPavel Pisa
62e73e0948SPavel PisaThe ''kvaser_pci'' board/device model is compatible with and has been tested with
6387f14eaaSLucas Ramagethe ''kvaser_pci'' driver included in mainline Linux kernel.
64e73e0948SPavel PisaThe tested setup was Linux 4.9 kernel on the host and guest side.
6587f14eaaSLucas Ramage
6687f14eaaSLucas RamageExample for qemu-system-x86_64::
67e73e0948SPavel Pisa
68a1d30f28SThomas Huth    qemu-system-x86_64 -accel kvm -kernel /boot/vmlinuz-4.9.0-4-amd64 \
69e73e0948SPavel Pisa      -initrd ramdisk.cpio \
70e73e0948SPavel Pisa      -virtfs local,path=shareddir,security_model=none,mount_tag=shareddir \
71e73e0948SPavel Pisa      -object can-bus,id=canbus0 \
72e73e0948SPavel Pisa      -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
73e73e0948SPavel Pisa      -device kvaser_pci,canbus=canbus0 \
74e73e0948SPavel Pisa      -nographic -append "console=ttyS0"
75e73e0948SPavel Pisa
7687f14eaaSLucas RamageExample for qemu-system-arm::
77e73e0948SPavel Pisa
78e73e0948SPavel Pisa    qemu-system-arm -cpu arm1176 -m 256 -M versatilepb \
79e73e0948SPavel Pisa      -kernel kernel-qemu-arm1176-versatilepb \
80e73e0948SPavel Pisa      -hda rpi-wheezy-overlay \
81e73e0948SPavel Pisa      -append "console=ttyAMA0 root=/dev/sda2 ro init=/sbin/init-overlay" \
82e73e0948SPavel Pisa      -nographic \
83e73e0948SPavel Pisa      -virtfs local,path=shareddir,security_model=none,mount_tag=shareddir \
84e73e0948SPavel Pisa      -object can-bus,id=canbus0 \
85e73e0948SPavel Pisa      -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
86e73e0948SPavel Pisa      -device kvaser_pci,canbus=canbus0,host=can0 \
87e73e0948SPavel Pisa
88e73e0948SPavel PisaThe CAN interface of the host system has to be configured for proper
89e73e0948SPavel Pisabitrate and set up. Configuration is not propagated from emulated
90e73e0948SPavel Pisadevices through bus to the physical host device. Example configuration
9187f14eaaSLucas Ramagefor 1 Mbit/s::
92e73e0948SPavel Pisa
93e73e0948SPavel Pisa  ip link set can0 type can bitrate 1000000
94e73e0948SPavel Pisa  ip link set can0 up
95e73e0948SPavel Pisa
96e73e0948SPavel PisaVirtual (host local only) can interface can be used on the host
9787f14eaaSLucas Ramageside instead of physical interface::
98e73e0948SPavel Pisa
99e73e0948SPavel Pisa  ip link add dev can0 type vcan
100e73e0948SPavel Pisa
101e73e0948SPavel PisaThe CAN interface on the host side can be used to analyze CAN
10287f14eaaSLucas Ramagetraffic with "candump" command which is included in "can-utils"::
103e73e0948SPavel Pisa
104e73e0948SPavel Pisa  candump can0
105e73e0948SPavel Pisa
106de4d4adbSPavel PisaCTU CAN FD support examples
10787f14eaaSLucas Ramage---------------------------
108de4d4adbSPavel PisaThis open-source core provides CAN FD support. CAN FD drames are
109de4d4adbSPavel Pisadelivered even to the host systems when SocketCAN interface is found
110de4d4adbSPavel PisaCAN FD capable.
111de4d4adbSPavel Pisa
112ac9574bcSStefan WeilThe PCIe board emulation is provided for now (the device identifier is
113ac9574bcSStefan Weilctucan_pci). The default build defines two CTU CAN FD cores
114de4d4adbSPavel Pisaon the board.
115de4d4adbSPavel Pisa
116de4d4adbSPavel PisaExample how to connect the canbus0-bus (virtual wire) to the host
117de4d4adbSPavel PisaLinux system (SocketCAN used) and to both CTU CAN FD cores emulated
118de4d4adbSPavel Pisaon the corresponding PCI card expects that host system CAN bus
11987f14eaaSLucas Ramageis setup according to the previous SJA1000 section::
120de4d4adbSPavel Pisa
121de4d4adbSPavel Pisa  qemu-system-x86_64 -enable-kvm -kernel /boot/vmlinuz-4.19.52+ \
122de4d4adbSPavel Pisa      -initrd ramdisk.cpio \
123de4d4adbSPavel Pisa      -virtfs local,path=shareddir,security_model=none,mount_tag=shareddir \
124de4d4adbSPavel Pisa      -vga cirrus \
125de4d4adbSPavel Pisa      -append "console=ttyS0" \
126de4d4adbSPavel Pisa      -object can-bus,id=canbus0-bus \
127de4d4adbSPavel Pisa      -object can-host-socketcan,if=can0,canbus=canbus0-bus,id=canbus0-socketcan \
128de4d4adbSPavel Pisa      -device ctucan_pci,canbus0=canbus0-bus,canbus1=canbus0-bus \
129de4d4adbSPavel Pisa      -nographic
130de4d4adbSPavel Pisa
13187f14eaaSLucas RamageSetup of CTU CAN FD controller in a guest Linux system::
132de4d4adbSPavel Pisa
133de4d4adbSPavel Pisa  insmod ctucanfd.ko || modprobe ctucanfd
134de4d4adbSPavel Pisa  insmod ctucanfd_pci.ko || modprobe ctucanfd_pci
135de4d4adbSPavel Pisa
136de4d4adbSPavel Pisa  for ifc in /sys/class/net/can* ; do
137de4d4adbSPavel Pisa    if [ -e  $ifc/device/vendor ] ; then
138de4d4adbSPavel Pisa      if ! grep -q 0x1760 $ifc/device/vendor ; then
139de4d4adbSPavel Pisa        continue;
140de4d4adbSPavel Pisa      fi
141de4d4adbSPavel Pisa    else
142de4d4adbSPavel Pisa      continue;
143de4d4adbSPavel Pisa    fi
144de4d4adbSPavel Pisa    if [ -e  $ifc/device/device ] ; then
145de4d4adbSPavel Pisa       if ! grep -q 0xff00 $ifc/device/device ; then
146de4d4adbSPavel Pisa         continue;
147de4d4adbSPavel Pisa       fi
148de4d4adbSPavel Pisa    else
149de4d4adbSPavel Pisa      continue;
150de4d4adbSPavel Pisa    fi
151de4d4adbSPavel Pisa    ifc=$(basename $ifc)
152de4d4adbSPavel Pisa    /bin/ip link set $ifc type can bitrate 1000000 dbitrate 10000000 fd on
153de4d4adbSPavel Pisa    /bin/ip link set $ifc up
154de4d4adbSPavel Pisa  done
155de4d4adbSPavel Pisa
15687f14eaaSLucas RamageThe test can run for example::
157de4d4adbSPavel Pisa
158de4d4adbSPavel Pisa  candump can1
159de4d4adbSPavel Pisa
16087f14eaaSLucas Ramagein the guest system and next commands in the host system for basic CAN::
161de4d4adbSPavel Pisa
162de4d4adbSPavel Pisa  cangen can0
163de4d4adbSPavel Pisa
16487f14eaaSLucas Ramagefor CAN FD without bitrate switch::
165de4d4adbSPavel Pisa
166de4d4adbSPavel Pisa  cangen can0 -f
167de4d4adbSPavel Pisa
16887f14eaaSLucas Ramageand with bitrate switch::
169de4d4adbSPavel Pisa
170de4d4adbSPavel Pisa  cangen can0 -b
171de4d4adbSPavel Pisa
1722cb40d44SStefan WeilThe test can also be run the other way around, generating messages in the
1732cb40d44SStefan Weilguest system and capturing them in the host system. Other combinations are
1742cb40d44SStefan Weilalso possible.
175de4d4adbSPavel Pisa
176e73e0948SPavel PisaLinks to other resources
17787f14eaaSLucas Ramage------------------------
178e73e0948SPavel Pisa
17987f14eaaSLucas Ramage (1) `CAN related projects at Czech Technical University, Faculty of Electrical Engineering <http://canbus.pages.fel.cvut.cz>`_
18087f14eaaSLucas Ramage (2) `Repository with development can-pci branch at Czech Technical University <https://gitlab.fel.cvut.cz/canbus/qemu-canbus>`_
18187f14eaaSLucas Ramage (3) `RTEMS page describing project <https://devel.rtems.org/wiki/Developer/Simulators/QEMU/CANEmulation>`_
18287f14eaaSLucas Ramage (4) `RTLWS 2015 article about the project and its use with CANopen emulation <http://cmp.felk.cvut.cz/~pisa/can/doc/rtlws-17-pisa-qemu-can.pdf>`_
18387f14eaaSLucas Ramage (5) `GNU/Linux, CAN and CANopen in Real-time Control Applications Slides from LinuxDays 2017 (include updated RTLWS 2015 content) <https://www.linuxdays.cz/2017/video/Pavel_Pisa-CAN_canopen.pdf>`_
18487f14eaaSLucas Ramage (6) `Linux SocketCAN utilities <https://github.com/linux-can/can-utils>`_
18587f14eaaSLucas Ramage (7) `CTU CAN FD project including core VHDL design, Linux driver, test utilities etc. <https://gitlab.fel.cvut.cz/canbus/ctucanfd_ip_core>`_
18680b952bbSPavel Pisa (8) `CTU CAN FD Core Datasheet Documentation <http://canbus.pages.fel.cvut.cz/ctucanfd_ip_core/doc/Datasheet.pdf>`_
18780b952bbSPavel Pisa (9) `CTU CAN FD Core System Architecture Documentation <http://canbus.pages.fel.cvut.cz/ctucanfd_ip_core/doc/System_Architecture.pdf>`_
18880b952bbSPavel Pisa (10) `CTU CAN FD Driver Documentation <https://canbus.pages.fel.cvut.cz/ctucanfd_ip_core/doc/linux_driver/build/ctucanfd-driver.html>`_
18987f14eaaSLucas Ramage (11) `Integration with PCIe interfacing for Intel/Altera Cyclone IV based board <https://gitlab.fel.cvut.cz/canbus/pcie-ctu_can_fd>`_
190