[[PageOutline]] = Controller Area Network (CAN) Bus The CAN bus is popular in automotive, industrial, and marine applications where robust signaling is necessary in an electrically noisy environment. CAN bus is one of the five protocols used in the on-board diagnostics (OBD)-II vehicle diagnostics standard mandatory for all cards and light trucks sold in the US since 1996. CAN bus was originated in 1983 at Bosch and was released in 1986 at the Society of Automotive Engineers (SAE). The first CAN controller chips were produced by Intel and Philips and came to market in 1987. The CAN 2.0 spec was published in 1991. High-Speed/FD (Flexible Data-Rate) CAN is the most common CAN transceiver and is used in nearly every production automobile and is required in model year 2008 and later ODB-II vehicles sold in the USA. The High-Speed CAN is the fastest form of CAN which allows use of the full standard speed up to 1 Mbit/s. CAN is a form of multidrop serial communications using a differential signal making it suitable for noisy environments. As it is differential the bus is made of up two wires: * CAN_H * CAN_L The voltage drop from CAN_H to CAN_L determines a 0 or a 1 bit. The maximum distance for any device from the bus is six meters. == Signalling and Termination For high-speed/FD CAN, both ends of the pair of signal wires (CAN_H and CAN_L) must be terminated which is typically done with a 120ohm resistor across the pair. These are in parallel so a properly terminated bus should have a total resistance of 60 ohms. This is because communication flows both ways on the CAN bus. The termination resistors on a cable should match the nominal impedance of the cable. == Arbitration CAN bus arbitration happens when two or more ECUs (nodes) start to transmit at the same time. Every transmitter listens to the bus to ensure what it sent was what it received - if there is a difference than the CAN controller stops transmitting. The winner will always be the CAN messages with the lowest identifier. == Framing There are four different types of frames for communicating info and the state of the bus: * Data frame (send data) - uses a header (identifier) of 29bits (CAN 2.0B) or 11bits (CAN 2.0A) followed by data (0 to 8 bytes in length) * Request frame (request data) * Error frame (indicates an error) * Overload frame (inserts a delay) == CAN FD spec The CAN FD spec was published in 2012 and specified an improved CAN data link layer protocol which extends the data section of the message from 8 bytes to 64 bytes per frame (not all devices support this). == Specifications and standards * [http://en.wikipedia.org/wiki/ISO_11898 ISO 11898-1 or ISOBUS] (2003) describes the CAN data link layer protocol * [http://en.wikipedia.org/wiki/SAE_J1939 SAE J1939] is a standard maintained by Society of Automotive Engineers (SAE) defining how information is transferred across a network to allow ECUs to communication information (a software specification that rides on top of a CAN bus). It uses CAN 2.0B and replaces J1587 and J1708. It is used on commercial vehicles (tractor/trailers, cement mixers, military trucks, etc). Paramter Group Number (PGN) indicates the message being sent. * [http://en.wikipedia.org/wiki/NMEA_2000 NMEA 2000] (aka NMEA2k or N2K) is compatible with CAN Bus and is used for connecting marine sensors and display units within ships and boards. It uses SAE J1939, runs at 250kbps and specifies a standardised !DeviceNet Micro-C M12 5-pin screw connector. Raymarine, !SeaTalk 2, Raymarine !SeaTalk, Simrad Simnet, Furuno CAN are rebranded imlementations of NMEA 2000 though may not use the standardized connectors. === SAE J1939 While not currently part of the Gateworks BSP's, there are various sources for a source-code level software stack for SAE J1939: * [http://www.simmasoftware.com/j1939-software-faq.html Simma Software] - available for purchase / license * [https://esd.eu/en/products/j1939-protocol-stack ESD Electronic system design] - available for purchase / license == CAN ID Devices on a CAN bus have a unique CAN ID which also defines the message priority. In the early 1990's the choice of IDs was done simply on the basis of identifying the type of data and the sending node but this has now changes where IDs are determined based on the deadline of the message (lower the number, higher the priority). [=#gateworks] == Gateworks Hardware The following Gateworks products have CAN: ||= Family =||= Board =||= Device =||= Driver =||= STBY =||= Termination =||= Notes =|| || Newport || GW6404-B+ || [http://dev.gateworks.com/datasheets/MCP25625.pdf MCP25625] || mcp251x || N/A || R138 120ohms || ^^^1^^^ || || || GW6304-E+ || [http://dev.gateworks.com/datasheets/MCP25625.pdf MCP25625] || mcp251x || N/A || R151 120ohms || ^^^1^^^ || || || GW6204-B+ || [http://dev.gateworks.com/datasheets/MCP25625.pdf MCP25625] || mcp251x || N/A || R171 120ohms || ^^^1^^^ || || || || Ventana || GW551x+GW16111 || IMX6 || flexcan || N/A || N/A || ^^^2^^^ || || || GW54xx || IMX6 / [http://dev.gateworks.com/datasheets/MCP2562.pdf MCP2562] || flexcan || R123 (not loaded) || GPIO1_IO02 / gpio-2 || || || || GW53xx || IMX6 / [http://dev.gateworks.com/datasheets/MCP2562.pdf MCP2562] || flexcan || R50 (not loaded) || GPIO1_IO02 / gpio-2 || || || || GW52xx || IMX6 / [http://dev.gateworks.com/datasheets/MCP2562.pdf MCP2562] || flexcan || R168 (not loaded) || GPIO1_IO09 / gpio-9 || || 1. The Newport product family uses the MCP25625 CAN over SPI device thus the spi--thunderx (SPI_THUNDERX) driver is required in addition to the mcp251x (CAN_MCP251X) driver. Also, note the Thunderx SPI controller only supports half-duplex transactions requiring a patch to the mcp251x driver to make it operating as half-duplex: [https://github.com/Gateworks/linux-newport/commit/f284504b771b8741222ac54fd2d773d88e53de39 can: mcp251x: convert driver to half-duplex SPI] 2. The GW551x does not have a transceiver on board; CAN_RXD / CAN_TXD go to application connector Note the Termination resistor options in the table above and remember that a properly terminated CAN network will have 120ohm termination across CAN_L and CAN_H at each end of the bus for a total measured resistance of 60ohm. CAN Bus can be found on the [http://www.gateworks.com/product/item/ventana-gw16113-expansion-adapter GW16113 Expansion card], but note this card will need to be custom programmed and does not have a transceiver. [=#stby] === CAN Transceiver STBY For boards that have a CAN STBY signal connected to a GPIO, the signal must be driven high to enable CAN: * For example on a GW53xx/GW54xx which use GPIO1_I02: {{{ echo 2 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio2/direction echo 0 > /sys/class/gpio/gpio2/value }}} * For example on a GW52xx which use GPIO1_IO09: {{{ echo 9 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio9/direction echo 0 > /sys/class/gpio/gpio9/value }}} [=#software] == Software CAN bus is supported on the various BSP's supported by Gateworks. SocketCAN is a set of open source CAN drivers and a networking stack contributed to the Linux Kernel by Volkswagon Research to the Linux kernel and was formerly known as Low Level CAN Framework (LLCF). The [https://github.com/linux-can/can-utils can-utils] project is a set of userspace community tools that are most commonly used with SocketCAN drivers for CAN over Linux. [=#newport] === Newport CAN Kernel drivers The Newport product family uses the [http://dev.gateworks.com/datasheets/MCP25625.pdf MCP25625] CAN over SPI device. This is both a CAN controller as well as a CAN transceiver. Because this operates over the SPI bus, the '''spi-thunderx''' (SPI_THUNDERX) kernel driver is required as well as the '''mcp251x''' (CAN_MCP251X) driver. Because the Thunderx SPI controller only supports half-duplex transactions a patch to the mcp251x driver is needed (which Gateworks has in its [https://github.com/Gateworks/linux-newport linux-newport] kernel repository): [https://github.com/Gateworks/linux-newport/commit/f284504b771b8741222ac54fd2d773d88e53de39 can: mcp251x: convert driver to half-duplex SPI]. You can use this patch as inspiration if you need to convert any other device drivers from full-duplex to half-duplex. Basically you need to make sure read and write operations are done in separate SPI transactions. [=#ventana] === Ventana IMX6 Kernel drivers The Freescale IMX6 FlexCAN module is a full implementation of the CAN protocol specification version 2.0 B which supports both standard and extended message frames. The '''flexcan''' driver source can be found at: * [http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/flexcan.c mainline linux kernel] * [http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/tree/drivers/net/can/flexcan.c?h=imx_3.10.17_1.0.0_ga Vendor kernel] General Notes: * canbus is broadcast-only * no MAC-layer addressing like ethernet - the CAN-identifier (can_id) is used for arbitration on the CAN-bus therefore must be chosen uniquely on the bus. * the CAN-identifier (can_id) determines the priority of the messages on the bus * there are two CAN protocols to choose from: CAN_RAW, and CAN_BCM (broadcast manager) * the basic CAN frame is: {{{ struct can_frame { canid_t can_id; /* 32bit CAN_ID + EFF/RTF/ERR flags */ __u8 can_dlc; /* frame payload length in bytes (0..8) */ __u8 data[8] __attribute__((aligned(8)))); /* aligned on 64bit boundary so payload can be used with own structs */ }; }}} * CAN FD (flexible data rate) support: for new CAN FD capable CAN controllers that support two different bitrates for the arbitration phase and the payload phase of the CAN FD frame and up to 64 bytes of payload. Reference: * http://en.wikipedia.org/wiki/SocketCAN - includes userspace configuration examples and code examples using sockets * https://www.kernel.org/doc/Documentation/networking/can.txt - detailed socket API [=#canutils] === can-utils The can-utils package is a sub-project of the SocketCAN project and is the more popular and maintained set of userspace configuration tools for SocketCAN interfaces. This is the package that exists on the latest Gateworks OpenWrt and Yocto v1.6 BSP's. Configure SocketCAN interface (using '''ip''' from the '''iproute2''' package) (for 250kbps in this example): {{{#!bash ip link set can0 type can bitrate 250000 listen-only off ip link set can0 up }}} Send a frame (consisting of 4 hex bytes to 0x100): {{{#!bash cansend can0 100#11.22.33.44 }}} Receive frames: {{{#!bash candump can0 }}} Example: {{{#!bash root@ventana:~# candump can0 can0 1CFF0105 [7] 47 9F 10 00 00 00 00 can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 can0 1CFF0105 [5] 47 9F 20 00 00 can0 1CFF0105 [7] 47 9F 10 00 00 00 00 can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 can0 1CFF0105 [5] 47 9F 20 00 00 can0 1CFF0105 [7] 47 9F 10 00 00 00 00 can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 can0 1CFF0105 [5] 47 9F 20 00 00 can0 1CFF0105 [7] 47 9F 10 00 00 00 00 can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 can0 1CFF0105 [5] 47 9F 20 00 00 can0 1CFF0105 [7] 47 9F 10 00 00 00 00 can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 }}} Notes: * see [#STBY STBY] above to enable the SocketCAN transceiver [=#android] === Android BSP * Freescale has not released can-utils or userspace utilities for CAN in Android * Android requires standard socket programming to access the CAN0 socket interface. * To check for can0 in Android, use the command: {{{ root@ventana:/ # busybox ifconfig -a can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 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) Interrupt:142 }}} More notes in this thread here: [https://community.freescale.com/thread/307066] [=#hardware] = CAN Bus Hardware [=#nmea2000] == NMEA 2000 Equipment Some examples of NMEA 2000 equipment we have tested our products with: * [http://www.amazon.com/Lowrance-NMEA-2000-Network-Starter/dp/B002BBFE98/ref=sr_1_4?ie=UTF8&qid=1413221333&sr=8-4&keywords=lowrance+point+1 Lowrance NMEA 2000 Network Starter Kit] - Includes 2 terminatoring resistors, 1 2ft extension cable, 1 15ft extension cable, 2 T-connectors, and a power node connector * [http://www.lowrance.com/en-US/Products/NMEA-2000/Point-1-en-us.aspx Lowrance POINT-1 GPS] - NMEA 2000 GPS (12V powered 250kbps bitrate) == Chip-Level An example chip that would interface to the Ventana board would be: - A CAN controller chip would be something like [http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010406 Microchip MCP2515] [=#apgdt002] = CAN Bus Development Tools - Microchip APGDT002 Can Bus Analyzer: - This product allows us to try CAN from a computer with a USB port. This is for 'testing and development' - [https://www.digikey.com/catalog/en/partgroup/apgdt002/12743?mpart=APGDT002&vendor=150 Digikey APGDT002] - [http://ww1.microchip.com/downloads/en/DeviceDoc/51848B.pdf Manual] - Software: * Windows software can be found on the [http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en546534 product page]. * '''NOTE: When installing on Windows7+ be sure to run the installer as Administrator or you will run into errors''' * '''Open the software and drop down the 'Tools' menu item to start a rolling trace or to transmit.''' - [[Image(cananalyzer.jpg)]] - Connection (look at silkscreen): D+ GND D- (Looking at top of board from edge) ('''see manual for Can Bus Analyzer tool if used''') - [[Image(canpins.png)]] = References * CAN Bus Wikipedia: [http://en.wikipedia.org/wiki/CAN_bus] * SocketCAN API: https://www.kernel.org/doc/Documentation/networking/can.txt * [http://www.computer-solutions.co.uk/info/Embedded_tutorials/can_tutorial.htm CAN tutorial] * [http://www.simmasoftware.com/j1939-presentation.pdf SAE J1939 Presentation]