Difference between revisions of "EMAC CAN Interface"

From wiki.emacinc.com
Jump to: navigation, search
Line 28: Line 28:
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:using | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
 
{{:Templateimpl:using | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
The CAN bus is able to be accessed from the command line, or using a C API on EMAC products (with CAN support).
+
The CAN bus is able to be accessed from the command line, or using a C library on EMAC products (with CAN support).
 
===CAN Bus Setup===
 
===CAN Bus Setup===
Before using the CAN interface on an EMAC device, two commands must be run. These commands could be scripted to be run at startup.
+
Before using the CAN interface on an EMAC device, two commands must be run. These commands could be scripted to be run at startup, see [[EMAC_OE_Boot_Process_Customization|this page]] for details.
 
{{note|The settings in the first command may need to be changed according to the application in question.}}
 
{{note|The settings in the first command may need to be changed according to the application in question.}}
  
Line 50: Line 50:
 
{{cli | cansend can0 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
{{cli | cansend can0 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
The received message, on a board running <code>candump</code>, will look like the following:
 
The received message, on a board running <code>candump</code>, will look like the following:
{{clop}}<0x001> [4] be 42 ef 9a {{closp}}
+
{{clo}}
 +
<0x001> [4] be 42 ef 9a
 +
{{clos}}
  
 
<code>cansend</code> uses, by default, an identifier of <code>0x01</code>. If an identifier other than default, the <code>-i 0x<IDENT></code> option must be passed on the command line. In the following example, the identity <code>0x89</code> is used.  
 
<code>cansend</code> uses, by default, an identifier of <code>0x01</code>. If an identifier other than default, the <code>-i 0x<IDENT></code> option must be passed on the command line. In the following example, the identity <code>0x89</code> is used.  
Line 56: Line 58:
 
{{cli | cansend can0 -i 0x89 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
{{cli | cansend can0 -i 0x89 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
The received message, on a board running <code>candump</code>, will look like the following:
 
The received message, on a board running <code>candump</code>, will look like the following:
{{clop}}<0x089> [4] be 42 ef 9a {{closp}}
+
{{clo}}
 +
<0x089> [4] be 42 ef 9a
 +
{{clos}}
  
 
Using the standard identity, the length of the identity is limited to a maximum hexadecimal value of <code>0x7FF</code>. If a larger identity is needed, the <code>-e</code> option can be passed, which allows an extended CAN frame with a maximum hexadecimal value of <code>0x1FFFFFFF</code>. The following example shows a message with an extended frame.
 
Using the standard identity, the length of the identity is limited to a maximum hexadecimal value of <code>0x7FF</code>. If a larger identity is needed, the <code>-e</code> option can be passed, which allows an extended CAN frame with a maximum hexadecimal value of <code>0x1FFFFFFF</code>. The following example shows a message with an extended frame.
Line 63: Line 67:
 
{{cli | cansend can0 -e -i 0x8008 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
{{cli | cansend can0 -e -i 0x8008 0xBE 0x42 0xEF 0x9A | hostname=ipac9x25}}
 
The received message, on a board running <code>candump</code>, will look like the following:
 
The received message, on a board running <code>candump</code>, will look like the following:
{{clop}}<0x00008008> [4] be 42 ef 9a{{closp}}
+
{{clo}}
 +
<0x00008008> [4] be 42 ef 9a
 +
{{clos}}
 
====candump====
 
====candump====
 +
<code>candump</code> is used to receive messages over CAN. These messages can be output to <code>stdout</code> or directly to a file. <code>candump</code> will run until it is killed, either with <code>Ctrl-C</code> or a <code>kill</code> command. The following command shows how to receive any message over CAN and output it to <code>stdout</code>. A command of <code>cansend can0 0xBE 0x42 0xEF 0x9A</code> was given on a connected board.
 +
{{clo}}
 +
{{clio | candump can0 | hostname=ipac9x25}}
 +
interface = can0, family = 29, type = 3, proto = 1
 +
 +
<0x001> [4] be 42 ef 9a
 +
{{clos}}
 +
To receive any message over CAN and output it to a file, the following command is used. A command of <code>cansend can0 0xBE 0x42 0xEF 0x9A</code> was given on a connected board.
 +
{{clo}}
 +
{{clio | candump can0 -o /tmp/candump.txt | hostname=ipac9x25}}
 +
interface = can0, family = 29, type = 3, proto = 1
 +
 +
 +
{{clos}}
 +
Since <code>candump</code> will continue running, a <code>Ctrl-C</code> should be given before viewing the contents of the file. To view the file, <code>cat</code> can be used.
 +
{{clo}}
 +
{{clio | cat /tmp/candump.txt | hostname=ipac9x25}}
 +
<0x001> [4] be 42 ef 9a
 +
{{clos}}
 +
<code>candump</code> can also filter its output according to two values: filter and mask. The filter <code>[can_id]:[can_mask]</code> matches when <code>[received_can_id] & [can_mask] == [can_id] & [mask]</code>. For example, to only receive messages from the identity <code>0x088</code>, use the following command.
 +
{{cli|candump can0 --filter 0x088:0x7FF|hostname=ipac9x25}}
 +
 +
===C Programming CAN===
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*****************************************      Examples        *****************************************/ -->
 
<!-- /*****************************************      Examples        *****************************************/ -->
Line 74: Line 103:
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:conclusion | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
 
{{:Templateimpl:conclusion | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
 
+
CAN is a useful protocol for communicating quickly with sensors and other controllers. EMAC boards with CAN should be able to quickly integrate with existing CAN networks with little trouble.
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /******************************************  More Information  *****************************************/ -->
 
<!-- /******************************************  More Information  *****************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:moreinfo | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
 
{{:Templateimpl:moreinfo | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
*  
+
* [[Getting_Started_with_the_EMAC_OE_SDK|Getting started with the EMAC SDK]]
  
 
{{:Templateimpl:whatnext | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
 
{{:Templateimpl:whatnext | initials=KY | title=EMAC CAN Interface | desc=This page describes how to use the CAN interface on EMAC boards. | project=OE 5.0 }}
*
+
* [[EMAC_HW|EMAC Hardware]]

Revision as of 13:01, 19 January 2016

TODO: {{#todo: InProgress (01.18.2016-10:45->KY+)|Klint Youngmeyer|OE 5.0,KY,InProgress}}

The CAN interface is a vehicle bus standard that allows communication between a controller and various sensors. EMAC produces several boards capable of communicating on a CAN bus. For more information about which products support the CAN bus, see the hardware page.

Background

CAN (controller area network) is a multi-master serial bus for communicating between "nodes" over a two-wire interface. The wires are generally a 120 ohm twisted pair. Each CAN node has the ability to communicate with every other node, one at a time.

EMAC CAN Interface

The CAN bus is able to be accessed from the command line, or using a C library on EMAC products (with CAN support).

CAN Bus Setup

Before using the CAN interface on an EMAC device, two commands must be run. These commands could be scripted to be run at startup, see this page for details.



NOTE
The settings in the first command may need to be changed according to the application in question.


root@ipac9x25:~# ip link set can0 type can bitrate 125000 triple-sampling on


root@ipac9x25:~# ip link set can0 up

Command Line CAN

The command line tools included with EMAC OE 5 are part of the canutils package. The tools included in this package are:

  • canconfig
  • candump
  • canecho
  • cansend
  • cansequence

Of these five, the most easily used are cansend and candump.

cansend

cansend, as the name implies, is used for sending CAN messages over the bus. Up to eight bytes at a time can be sent using cansend, these bytes are formatted at a space separated list of hexadecimal octets e.g. "0xBE 0x42 0xEF 0x9A." The command for sending the previous hexadecimal digits on the can0 device is:

root@ipac9x25:~# cansend can0 0xBE 0x42 0xEF 0x9A

The received message, on a board running candump, will look like the following:

<0x001> [4] be 42 ef 9a

cansend uses, by default, an identifier of 0x01. If an identifier other than default, the -i 0x<IDENT> option must be passed on the command line. In the following example, the identity 0x89 is used.

root@ipac9x25:~# cansend can0 -i 0x89 0xBE 0x42 0xEF 0x9A

The received message, on a board running candump, will look like the following:

<0x089> [4] be 42 ef 9a

Using the standard identity, the length of the identity is limited to a maximum hexadecimal value of 0x7FF. If a larger identity is needed, the -e option can be passed, which allows an extended CAN frame with a maximum hexadecimal value of 0x1FFFFFFF. The following example shows a message with an extended frame.


root@ipac9x25:~# cansend can0 -e -i 0x8008 0xBE 0x42 0xEF 0x9A

The received message, on a board running candump, will look like the following:

<0x00008008> [4] be 42 ef 9a

candump

candump is used to receive messages over CAN. These messages can be output to stdout or directly to a file. candump will run until it is killed, either with Ctrl-C or a kill command. The following command shows how to receive any message over CAN and output it to stdout. A command of cansend can0 0xBE 0x42 0xEF 0x9A was given on a connected board.

root@ipac9x25:~# candump can0

interface = can0, family = 29, type = 3, proto = 1

<0x001> [4] be 42 ef 9a

To receive any message over CAN and output it to a file, the following command is used. A command of cansend can0 0xBE 0x42 0xEF 0x9A was given on a connected board.

root@ipac9x25:~# candump can0 -o /tmp/candump.txt

interface = can0, family = 29, type = 3, proto = 1


Since candump will continue running, a Ctrl-C should be given before viewing the contents of the file. To view the file, cat can be used.

root@ipac9x25:~# cat /tmp/candump.txt

<0x001> [4] be 42 ef 9a

candump can also filter its output according to two values: filter and mask. The filter [can_id]:[can_mask] matches when [received_can_id] & [can_mask] == [can_id] & [mask]. For example, to only receive messages from the identity 0x088, use the following command.

root@ipac9x25:~# candump can0 --filter 0x088:0x7FF

C Programming CAN

Conclusion

CAN is a useful protocol for communicating quickly with sensors and other controllers. EMAC boards with CAN should be able to quickly integrate with existing CAN networks with little trouble.

Further Information

Where to Go Next
Pages with Related Content