- Saved searches
- Use saved searches to filter your results more quickly
- License
- rkollataj/mcba_usb
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Linux SocketCAN Driver
- 2 A Virtual SocketCAN Driver
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Linux kernel driver for Microchip CAN BUS Analyzer Tool
License
rkollataj/mcba_usb
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Linux kernel driver for Microchip CAN BUS Analyzer Tool
The CAN BUS Analyzer Tool is a simple to use low cost CAN bus monitor which can be used to develop and debug a high speed CAN network. The tool supports CAN 2.0b and ISO11898-2 and a broad range of functions which allow it to be used across various market segments including automotive, industrial, medical and marine. The toolkit comes with all the hardware and software required to connect a CAN network to a PC. The Graphical User Interface makes it easy to quickly observe and interpret bus traffic.
Originally the tool is supported on Windows environment only. This project adds support for the tool to Linux Kernel (SocketCAN).
NOTE: Driver is a part of Linux Kernel starting from 4.12 version
git clone https://github.com/rkollataj/mcba_usb.git cd mcba_usb make sudo make modules_install sudo modprobe mcba_usb
To start SocketCAN interface:
sudo ip link set can0 type can bitrate 500000 sudo ip link set can0 up
cansend can0 001#DEADBEEF cansend can0 1000001#DEADBEEF
The tool works internally with 40Mhz clock. Following bus speed are supported by default:
- 20 Kbps
- 33.3 Kbps
- 50 Kbps
- 80 Kbps
- 83.3 Kbps
- 100 Kbps
- 125 Kbps
- 150 Kbps
- 175 Kbps
- 200 Kbps
- 225 Kbps
- 250 Kbps
- 275 Kbps
- 300 Kbps
- 500 Kbps
- 625 Kbps
- 800 Kbps
- 1000 Kbps
Note: Bittiming parameters are hardcoded inside device. Only speed can be configured using iproute2 utils.
The tool supports build in termination. It can be controlled by sysfs. To read current termination status:
cat /sys/class/net/can0/termination
echo 1 > /sys/class/net/can0/termination
echo 0 > /sys/class/net/can0/termination
Termination values are stored in device’s EEPROM (no need to set it again after device reconnection).
Official Microchip CAN BUS Analyzer firmware v2.3 contains bugs:
- Too low SPI sychro time (PIC_USB->PIC_CAN) causes CAN frame to be lost
- Sending remote frames do not work
About
Linux kernel driver for Microchip CAN BUS Analyzer Tool
Linux SocketCAN Driver
The Can bus has been supported in the Linux kernel since some years, and there are a lot of drivers for CAN bus controllers, traditional CAN drivers for Linux are based on the model of character deivces. Typically they only allow sending to and receiving from the CAN controller, a set of open source CAN drivers and a networking stack is contributed by Volkswagen Research, which known as SocketCAN. The following graph showed the typical CAN communication layers, with SocketCAN in left and conventional in right.
2 A Virtual SocketCAN Driver
As above graph showed, the CAN bus device exist as a kind of network device in the Linux kernel, so writing a CAN bus controller driver is very samilar to write a network card driver, the following is a virtual can driver.
#include #include #include #include #include #include #include #include #include #define VCAN_FIFO_DEPTH 4 struct vcan_priv < struct can_priv can; struct net_device *ndev; >; struct platform_device *vcan_dev; static bool echo; /* echo testing. Default: 0 (Off) */ module_param(echo, bool, S_IRUGO); MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)"); static void vcan_rx(struct sk_buff *skb, struct net_device *ndev) < struct canfd_frame *cfd = (struct canfd_frame *)skb->data; struct net_device_stats *stats = &ndev->stats; stats->rx_packets++; stats->rx_bytes += cfd->len; skb->pkt_type = PACKET_BROADCAST; skb->dev = ndev; skb->ip_summed = CHECKSUM_UNNECESSARY; netif_rx_ni(skb); > static netdev_tx_t vcan_start_xmit(struct sk_buff *skb, struct net_device *ndev) < struct canfd_frame *cfd = (struct canfd_frame *)skb->data; struct net_device_stats *stats = &ndev->stats; int loop; if (can_dropped_invalid_skb(ndev, skb)) return NETDEV_TX_OK; stats->tx_packets++; stats->tx_bytes += cfd->len; /* set flag whether this packet has to be looped back */ loop = skb->pkt_type == PACKET_LOOPBACK; if (!echo) < /* no echo handling available inside this driver */ if (loop) < /* * only count the packets here, because the * CAN core already did the echo for us */ stats->rx_packets++; stats->rx_bytes += cfd->len; > consume_skb(skb); return NETDEV_TX_OK; > /* perform standard echo handling for CAN network interfaces */ if (loop) < skb = can_create_echo_skb(skb); if (!skb) return NETDEV_TX_OK; /* receive with packet counting */ vcan_rx(skb, ndev); >else < /* no looped packets =>no counting */ consume_skb(skb); > return NETDEV_TX_OK; > static int vcan_change_mtu(struct net_device *ndev, int new_mtu) < /* Do not allow changing the MTU while running */ if (ndev->flags & IFF_UP) return -EBUSY; if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU) return -EINVAL; ndev->mtu = new_mtu; return 0; > static const struct net_device_ops vcan_netdev_ops = < .ndo_start_xmit = vcan_start_xmit, .ndo_change_mtu = vcan_change_mtu, >; static int vcan_probe(struct platform_device *pdev) < struct net_device *ndev; struct vcan_priv *priv; int err = -ENODEV; ndev = alloc_candev(sizeof(struct vcan_priv), VCAN_FIFO_DEPTH); if (!ndev) < dev_err(&pdev->dev, "alloc_candev() failed\n"); err = -ENOMEM; goto fail; > priv = netdev_priv(ndev); ndev->netdev_ops = &vcan_netdev_ops; ndev->flags |= IFF_ECHO; priv->ndev = ndev; platform_set_drvdata(pdev, ndev); SET_NETDEV_DEV(ndev, &pdev->dev); err = register_candev(ndev); if (err) < dev_err(&pdev->dev, "register_candev() failed, error %d\n", err); goto fail_candev; > dev_info(&pdev->dev, "device registered\n"); return 0; fail_candev: free_candev(ndev); fail: return err; > static int vcan_remove(struct platform_device *pdev) < struct net_device *ndev = platform_get_drvdata(pdev); unregister_candev(ndev); free_candev(ndev); dev_info(&pdev->dev, "device removed\n"); return 0; > static struct platform_driver vcan_driver = < .driver = < .name = "vcan", .owner = THIS_MODULE, >, .probe = vcan_probe, .remove = vcan_remove, >; static int __init vcan_init(void) < int retval; vcan_dev = platform_device_alloc("vcan", -1); if (!vcan_dev) return -ENOMEM; retval = platform_device_add(vcan_dev); if (retval < 0) < platform_device_put(vcan_dev); return retval; >retval = platform_driver_register(&vcan_driver); if (retval < 0) platform_device_unregister(vcan_dev); return retval; >static void __exit vcan_exit(void) < platform_driver_unregister(&vcan_driver); platform_device_unregister(vcan_dev); >module_init(vcan_init); module_exit(vcan_exit); MODULE_AUTHOR("Yannik Li"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Virtual CAN");
The Makefile as following:
ifneq ($(KERNELRELEASE),) obj-m := vcan.o else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: @rm -rf .tmp* .vcan* Module* modules* vcan.*o vcan.mod* endif
To install and test the virtual driver as following steps:
- modprobe can
- modprobe can-dev
- modprobe can-raw
- insmod vcan.ko
- ifconfig can0 up
- candump can0
- cansend can0 123#112233
Author: Yanqing Li(Yannik Li)
Created: 2016-04-19 Tue 22:59