| 1 | [[PageOutline]] |
| 2 | |
| 3 | = Controller Area Network (CAN) Bus = |
| 4 | Most boards in the Gateworks Ventana product family has one or more CAN bus interfaces. The CAN bus is popular in automotive, industrial, and marine applications where robust signaling is necessary in an electrically noisy environment. |
| 5 | |
| 6 | 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. |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | == Signalling and Termination == |
| 11 | 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: |
| 12 | * CAN_H |
| 13 | * CAN_L |
| 14 | |
| 15 | The voltage drop from CAN_H to CAN_L determines a 0 or a 1 bit. |
| 16 | |
| 17 | Typically a CAN bus network requires a single 120ohm termination resistor at each end of the bus (these are in parallel so a properly terminated bus should have a total resistance of 60 ohms). The maximum distance for any device from the bus is six meters. |
| 18 | |
| 19 | == Arbitration == |
| 20 | 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. |
| 21 | |
| 22 | == Framing == |
| 23 | There are four different types of frames for communicating info and the state of the bus: |
| 24 | * 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) |
| 25 | * Request frame (request data) |
| 26 | * Error frame (indicates an error) |
| 27 | * Overload frame (inserts a delay) |
| 28 | |
| 29 | == CAN FD spec == |
| 30 | 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). |
| 31 | |
| 32 | == Specifications and standards == |
| 33 | * [http://en.wikipedia.org/wiki/ISO_11898 ISO 11898-1 or ISOBUS] (2003) describes the CAN data link layer protocol |
| 34 | * [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. |
| 35 | * [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. |
| 36 | |
| 37 | === SAE J1939 === |
| 38 | While not currently part of the Gateworks BSP's, there are various sources for a source-code level software stack for SAE J1939: |
| 39 | * [https://gitorious.org/linux-can/linux-can-j1939/ linux-can-j1939] - GPLv2 open-source |
| 40 | * [http://www.simmasoftware.com/j1939-software-faq.html Simma Software] - available for purchase / license |
| 41 | * [https://esd.eu/en/products/j1939-protocol-stack ESD Electronic system design] - available for purchase / license |
| 42 | |
| 43 | == CAN ID == |
| 44 | 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). |
| 45 | |
| 46 | == Gateworks Hardware == |
| 47 | Gateworks uses the [http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en561070 MCP2562 Transceiver] chip from Microchip. |
| 48 | * Note: There is an EEPROM bit that must be set for CAN to be enabled. This is already done by default on most boards. Please contact Gateworks support for any further questions. |
| 49 | |
| 50 | Boards that feature CAN Bus: |
| 51 | * GW52xx |
| 52 | * GW53xx |
| 53 | * GW54xx |
| 54 | * GW551x (*No Transceiver on GW5510, use with GW16111) |
| 55 | To find which pins and which connectors have the CAN Bus signals, please refer to the [http://www.gateworks.com/usermanuals hardware user manuals] |
| 56 | |
| 57 | |
| 58 | Boards that do not have CAN Bus: |
| 59 | * GW552x |
| 60 | * GW51xx |
| 61 | * GW553x |
| 62 | * GW2xxx |
| 63 | |
| 64 | 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. |
| 65 | |
| 66 | === CAN Transceiver STBY === |
| 67 | The STBY signal is connected to a GPIO on Ventana baseboards: |
| 68 | * GW54xx/GW53xx: gpio-2 |
| 69 | * GW52xx: gpio-9 |
| 70 | * When using a 'device-tree' kernel such as the 3.10.x and beyond kernel under OpenWrt, you must manually enable the transceiver: |
| 71 | * For example on a GW53xx/GW54xx: |
| 72 | {{{ |
| 73 | echo 2 > /sys/class/gpio/export |
| 74 | echo out > /sys/class/gpio/gpio2/direction |
| 75 | echo 0 > /sys/class/gpio/gpio2/value |
| 76 | }}} |
| 77 | * For example on a GW52xx: |
| 78 | {{{ |
| 79 | echo 9 > /sys/class/gpio/export |
| 80 | echo out > /sys/class/gpio/gpio9/direction |
| 81 | echo 0 > /sys/class/gpio/gpio9/value |
| 82 | }}} |
| 83 | * This is done automatically when the transceiver interface is brought up in the 3.0.35 kernel used on Yocto v1.3 and Android BSP's |
| 84 | |
| 85 | == Software == |
| 86 | CAN bus is supported under Yocto, OpenWrt, and Android Board Support Packages for the Ventana product family. |
| 87 | |
| 88 | Software included in Gateworks BSP: |
| 89 | * kernel driver: flexcan (a SocketCAN driver) |
| 90 | * userspace: |
| 91 | * can-utils (gitorious) - a set of the major community tools that are usually used and references (does not require libsocketcan to compile or run) |
| 92 | |
| 93 | |
| 94 | Other Software: |
| 95 | * canutils (PTX) - five tools that are used and referenced in 'PTX dist' board support packages. These tools are not very featureful and no longer maintained. The 'canconfig' tool is used to configure the CAN interface as an alternative to the 'ip' tool and uses libsocketcan |
| 96 | * libsocketcan - a library for people that want to configure and control CAN interfaces from C code (instead of using the 'ip' tool from the iproute2 package |
| 97 | |
| 98 | |
| 99 | |
| 100 | === IMX6 Kernel drivers - SocketCAN === |
| 101 | SocketCAN is a set of open source CAN drivers and a networking stack contributed by Volkswagon Research to the Linux kernel and was formerly known as Low Level CAN Framework (LLCF). |
| 102 | |
| 103 | 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: |
| 104 | * [http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/flexcan.c mainline linux kernel] |
| 105 | * [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] |
| 106 | |
| 107 | General Notes: |
| 108 | * canbus is broadcast-only |
| 109 | * 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. |
| 110 | * the CAN-identifier (can_id) determines the priority of the messages on the bus |
| 111 | * there are two CAN protocols to choose from: CAN_RAW, and CAN_BCM (broadcast manager) |
| 112 | * the basic CAN frame is: |
| 113 | {{{ |
| 114 | struct can_frame { |
| 115 | canid_t can_id; /* 32bit CAN_ID + EFF/RTF/ERR flags */ |
| 116 | __u8 can_dlc; /* frame payload length in bytes (0..8) */ |
| 117 | __u8 data[8] __attribute__((aligned(8)))); /* aligned on 64bit boundary so payload can be used with own structs */ |
| 118 | }; |
| 119 | }}} |
| 120 | * 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. |
| 121 | |
| 122 | Reference: |
| 123 | * source: https://gitorious.org/linux-can |
| 124 | * http://en.wikipedia.org/wiki/SocketCAN - includes userspace configuration examples and code examples using sockets |
| 125 | * https://www.kernel.org/doc/Documentation/networking/can.txt - detailed socket API |
| 126 | |
| 127 | === can-utils === |
| 128 | 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. |
| 129 | |
| 130 | This is the package that exists on the latest Gateworks OpenWrt and Yocto v1.6 BSP's. |
| 131 | |
| 132 | Configure SocketCAN interface (using '''ip''' from the '''iproute2''' package) (for 250kbps in this example): |
| 133 | {{{ |
| 134 | ip link set can0 type can bitrate 250000 listen-only off |
| 135 | ip link set can0 up |
| 136 | }}} |
| 137 | |
| 138 | Send a frame (consisting of 4 hex bytes to 0x100): |
| 139 | {{{ |
| 140 | cansend can0 100#11.22.33.44 |
| 141 | }}} |
| 142 | |
| 143 | Receive frames: |
| 144 | {{{ |
| 145 | candump can0 |
| 146 | }}} |
| 147 | |
| 148 | Example: |
| 149 | {{{ |
| 150 | root@ventana:~# candump can0 |
| 151 | can0 1CFF0105 [7] 47 9F 10 00 00 00 00 |
| 152 | can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 |
| 153 | can0 1CFF0105 [5] 47 9F 20 00 00 |
| 154 | can0 1CFF0105 [7] 47 9F 10 00 00 00 00 |
| 155 | can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 |
| 156 | can0 1CFF0105 [5] 47 9F 20 00 00 |
| 157 | can0 1CFF0105 [7] 47 9F 10 00 00 00 00 |
| 158 | can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 |
| 159 | can0 1CFF0105 [5] 47 9F 20 00 00 |
| 160 | can0 1CFF0105 [7] 47 9F 10 00 00 00 00 |
| 161 | can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 |
| 162 | can0 1CFF0105 [5] 47 9F 20 00 00 |
| 163 | can0 1CFF0105 [7] 47 9F 10 00 00 00 00 |
| 164 | can0 1CFF0105 [7] 47 9F 00 FF FF 00 00 |
| 165 | |
| 166 | }}} |
| 167 | |
| 168 | |
| 169 | Notes: |
| 170 | * see #TransceiverSTBY above to enable the SocketCAN transceiver |
| 171 | |
| 172 | Reference: |
| 173 | * source: https://gitorious.org/linux-can/can-utils |
| 174 | |
| 175 | === PTX canutils and libsocketcan === |
| 176 | The PTX flavor of canutils (http://git.pengutronix.de/?p=tools/canutils.git) provides five tools that are commonly found referenced online which contains the 'canconfig' utility (which uses libsocketcan) to configure the CAN interface as an alternative to the 'ip' tool. |
| 177 | |
| 178 | Freescale still uses canutils and the canconfig utility thus you will find it in some of the IMX online documentation. Gateworks moved to the more popular full-featured and maintained can-utils configuration utilities for its OpenWrt BSP and its Yocto v1.6 BSP. If you are using an older version of OpenWrt or Yocto BSP and have the 'canconfig' utility you can use these configuration instructions. |
| 179 | |
| 180 | Configure the following on a Ventana board: |
| 181 | {{{ |
| 182 | canconfig can0 bitrate 125000 |
| 183 | ifconfig can0 up |
| 184 | }}} |
| 185 | |
| 186 | To send from the Ventana board command prompt type: |
| 187 | {{{ |
| 188 | cansend can0 -i0x100 0x11 0x22 0x33 0x44 |
| 189 | }}} |
| 190 | |
| 191 | To recieve (on another Ventana board, or from !CanAnalyzer USB Device) type the command: |
| 192 | {{{ |
| 193 | canecho can0 -v |
| 194 | }}} |
| 195 | |
| 196 | Notes: |
| 197 | * see #TransceiverSTBY above to enable the SocketCAN transceiver |
| 198 | |
| 199 | References: |
| 200 | * https://community.freescale.com/docs/DOC-1437 |
| 201 | |
| 202 | === Android BSP === |
| 203 | * Freescale has not released can-utils or userspace utilities for CAN in Android |
| 204 | * Android requires standard socket programming to access the CAN0 socket interface. |
| 205 | * To check for can0 in Android, use the command: |
| 206 | {{{ |
| 207 | root@ventana:/ # busybox ifconfig -a |
| 208 | can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 |
| 209 | NOARP MTU:16 Metric:1 |
| 210 | RX packets:0 errors:0 dropped:0 overruns:0 frame:0 |
| 211 | TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 |
| 212 | collisions:0 txqueuelen:10 |
| 213 | RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) |
| 214 | Interrupt:142 |
| 215 | }}} |
| 216 | |
| 217 | More notes in this thread here: [https://community.freescale.com/thread/307066] |
| 218 | |
| 219 | = CAN Bus Hardware = |
| 220 | |
| 221 | == NMEA 2000 Equipment == |
| 222 | Some examples of NMEA 2000 equipment we have tested our products with: |
| 223 | * [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 |
| 224 | * [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) |
| 225 | |
| 226 | == Chip-Level == |
| 227 | An example chip that would interface to the Ventana board would be: |
| 228 | - A CAN controller chip would be something like [http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010406 Microchip MCP2515] |
| 229 | |
| 230 | |
| 231 | |
| 232 | = CAN Bus Development Tools = |
| 233 | - Microchip APGDT002 Can Bus Analyzer: |
| 234 | - This product allows us to try CAN from a computer with a USB port. This is for 'testing and development' |
| 235 | - [[Image(http://www.microchip.com/stellent/groups/devtools_sg/documents/devtools/~export/en546534~2~en006096~PressReleaseLayout/124985-1.png)]] |
| 236 | - Product: [http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en546534] |
| 237 | - Manual: [http://ww1.microchip.com/downloads/en/DeviceDoc/51848B.pdf] |
| 238 | - Software: |
| 239 | * Windows software can be found on the [http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en546534 product page]. |
| 240 | * '''NOTE: When installing on Windows7+ be sure to run the installer as Administrator or you will run into errors''' |
| 241 | * '''Open the software and drop down the 'Tools' menu item to start a rolling trace or to transmit.''' |
| 242 | - [[Image(cananalyzer.jpg)]] |
| 243 | - Connection (look at silkscreen): D+ GND D- (Looking at top of board from edge) ('''see manual for Can Bus Analyzer tool if used''') |
| 244 | - [[Image(photo (5).JPG, 400px)]] |
| 245 | - [[Image(canpins.png)]] |
| 246 | |
| 247 | |
| 248 | = References = |
| 249 | * CAN Bus Wikipedia: [http://en.wikipedia.org/wiki/CAN_bus] |
| 250 | * SocketCAN API: https://www.kernel.org/doc/Documentation/networking/can.txt |
| 251 | * [http://www.computer-solutions.co.uk/info/Embedded_tutorials/can_tutorial.htm CAN tutorial] |
| 252 | * [http://www.simmasoftware.com/j1939-presentation.pdf SAE J1939 Presentation] |