From abf82013acf0ffb7de727b1487f51a455a3020cf Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Fri, 21 Oct 2022 10:33:27 +0200 Subject: [PATCH] drivers: can: add support for configuring CAN emulation in QEMU Add support for configuring CAN emulation support in QEMU. For now, the only supported CAN controller is the single-channel Kvaser PCIcan PCI card. Signed-off-by: Henrik Brix Andersen --- cmake/emu/qemu.cmake | 16 ++++++++++++++++ drivers/can/Kconfig | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/cmake/emu/qemu.cmake b/cmake/emu/qemu.cmake index b11b9d16bec..cad833d0032 100644 --- a/cmake/emu/qemu.cmake +++ b/cmake/emu/qemu.cmake @@ -269,6 +269,22 @@ elseif(QEMU_NET_STACK) endif() endif(QEMU_PIPE_STACK) +if(CONFIG_CAN) + # Add CAN bus 0 + list(APPEND QEMU_FLAGS -object can-bus,id=canbus0) + + if(NOT "${CONFIG_CAN_QEMU_IFACE_NAME}" STREQUAL "") + # Connect CAN bus 0 to host SocketCAN interface + list(APPEND QEMU_FLAGS + -object can-host-socketcan,id=canhost0,if=${CONFIG_CAN_QEMU_IFACE_NAME},canbus=canbus0) + endif() + + if(CONFIG_CAN_KVASER_PCI) + # Emulate a single-channel Kvaser PCIcan card connected to CAN bus 0 + list(APPEND QEMU_FLAGS -device kvaser_pci,canbus=canbus0) + endif() +endif() + if(CONFIG_X86_64 AND NOT CONFIG_QEMU_UEFI_BOOT) # QEMU doesn't like 64-bit ELF files. Since we don't use any >4GB # addresses, converting it to 32-bit is safe enough for emulation. diff --git a/drivers/can/Kconfig b/drivers/can/Kconfig index 2cfa3aa1afc..e2faa0d3c50 100644 --- a/drivers/can/Kconfig +++ b/drivers/can/Kconfig @@ -89,6 +89,15 @@ config CAN_AUTO_BUS_OFF_RECOVERY recessive bits). When this option is enabled, the recovery API is not available. +config CAN_QEMU_IFACE_NAME + string "SocketCAN interface name for QEMU" + default "" + depends on QEMU_TARGET + help + The SocketCAN interface name for QEMU. This value, if set, is given as "if" parameter to + the "-object can-host-socketcan" qemu command line option. The CAN interface must be + configured before starting QEMU. + source "drivers/can/Kconfig.sam" source "drivers/can/Kconfig.stm32" source "drivers/can/Kconfig.stm32fd"