Ethercat 1.5 6129a5f715fb
Ethercat 1.5 6129a5f715fb
ii
6129a5f715fb, 2010/04/30
Contents
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The IgH EtherCAT Master 1.1 Feature Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Architecture 2.1 Master Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Master Phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Process Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Application Interface 3.1 Master Conguration . . . 3.1.1 Slave Conguration 3.2 Cyclic Operation . . . . . 3.3 VoE Handlers . . . . . . . 3.4 Concurrent Master Access 3.5 Distributed Clocks . . . . x 1 1 3 5 7 8 8 11 11 11 13 13 14 15 19 19 21 23 24 24 24 27 28 30 33 33 36 36 39 40
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
4 Ethernet Devices 4.1 Network Driver Basics . . . . . . 4.2 Native EtherCAT Device Drivers 4.3 Generic EtherCAT Device Driver 4.4 Providing Ethernet Devices . . . 4.5 EtherCAT Device Interface . . . . 4.6 Patching Native Network Drivers
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
5 State Machines 5.1 State Machine Theory . . . . . . . . . 5.2 The Masters State Model . . . . . . . 5.3 The Master State Machine . . . . . . . 5.4 The Slave Scan State Machine . . . . . 5.5 The Slave Conguration State Machine 5.6 The State Change State Machine . . . 5.7 The SII State Machine . . . . . . . . . 5.8 The PDO State Machines . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
6129a5f715fb, 2010/04/30
iii
6 Mailbox Protocol Implementations 6.1 Ethernet over EtherCAT (EoE) . . . 6.2 CANopen over EtherCAT (CoE) . . 6.3 Vendor specic over EtherCAT (VoE) 6.4 Servo Prole over EtherCAT (SoE) .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
45 45 47 49 49 51 51 51 52 52 53 53 54 54 55 56 56 57 58 58 59 60 61 62 64 65 65 66 66 66 67 67 68 68 69 69 70
7 Userspace Interfaces 7.1 Command-line Tool . . . . . . . . . . . . . . . . 7.1.1 Character Devices . . . . . . . . . . . . . 7.1.2 Setting Alias Addresses . . . . . . . . . . 7.1.3 Displaying the Bus Conguration . . . . 7.1.4 Output PDO information in C Language 7.1.5 Displaying Process Data . . . . . . . . . 7.1.6 Setting a Masters Debug Level . . . . . 7.1.7 Congured Domains . . . . . . . . . . . 7.1.8 SDO Access . . . . . . . . . . . . . . . . 7.1.9 EoE Statistics . . . . . . . . . . . . . . . 7.1.10 File-Access over EtherCAT . . . . . . . . 7.1.11 Creating Topology Graphs . . . . . . . . 7.1.12 Master and Ethernet Devices . . . . . . 7.1.13 Sync Managers, PDOs and PDO Entries 7.1.14 Register Access . . . . . . . . . . . . . . 7.1.15 SDO Dictionary . . . . . . . . . . . . . . 7.1.16 SII Access . . . . . . . . . . . . . . . . . 7.1.17 Slaves on the Bus . . . . . . . . . . . . . 7.1.18 SoE IDN Access . . . . . . . . . . . . . . 7.1.19 Requesting Application-Layer States . . 7.1.20 Displaying the Master Version . . . . . . 7.1.21 Generating Slave Description XML . . . 7.2 Userspace Library . . . . . . . . . . . . . . . . . 7.2.1 Using the Library . . . . . . . . . . . . . 7.2.2 Implementation . . . . . . . . . . . . . . 7.2.3 Timing . . . . . . . . . . . . . . . . . . . 7.3 System Integration . . . . . . . . . . . . . . . . 7.3.1 Init Script . . . . . . . . . . . . . . . . . 7.3.2 Syscong File . . . . . . . . . . . . . . . 7.3.3 Starting the Master as a Service . . . . . 7.4 Debug Interfaces . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Timing Aspects 73 8.0.1 Application Interface Proling . . . . . . . . . . . . . . . . . . . 73 8.0.2 Bus Cycle Measuring . . . . . . . . . . . . . . . . . . . . . . . . 74 9 Installation 77
iv
6129a5f715fb, 2010/04/30
Getting the Software . . . . . . . . . Building the Software . . . . . . . . . Building the Interface Documentation Installing the Software . . . . . . . . Automatic Device Node Creation . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
77 77 78 78 81 83 83
Bibliography Glossary
6129a5f715fb, 2010/04/30
vi
6129a5f715fb, 2010/04/30
List of Tables
3.1 5.1 7.1 8.1 9.1 Specifying a Slave Position . . . . . . . . . . . . . . . . . . . . . . . . . 12 A typical state transition table . . . . . . . . . . . . . . . . . . . . . . . 29 Application Interface Timing Comparison . . . . . . . . . . . . . . . . 68
6129a5f715fb, 2010/04/30
vii
viii
6129a5f715fb, 2010/04/30
List of Figures
2.1 2.2 2.3 2.4 3.1 3.2 3.3 3.4 4.1 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 6.1 6.2 Master Architecture . . . . . . . Multiple masters in one module Master phases and transitions . FMMU Conguration . . . . . . Master Conguration . . . . . . Slave Conguration Attachment Concurrent Master Access . . . Distributed Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 . 7 . 8 . 10 . . . . 12 13 14 15
Interrupt Operation versus Interrupt-less Operation . . . . . . . . . . . 22 A typical state transition diagram . . . . . . . . . . . . . . . . . . . Transition diagram of the master state machine . . . . . . . . . . . Transition diagram of the slave scan state machine . . . . . . . . . Transition diagram of the slave conguration state machine . . . . . Transition Diagram of the State Change State Machine . . . . . . . Transition Diagram of the SII State Machine . . . . . . . . . . . . . Transition Diagram of the PDO Reading State Machine . . . . . . . Transition Diagram of the PDO Entry Reading State Machine . . . Transition Diagram of the PDO Conguration State Machine . . . . Transition Diagram of the PDO Entry Conguration State Machine . . . . . . . . . . . . . . . . . . . . 29 34 35 37 38 39 41 41 42 43
Transition Diagram of the EoE State Machine . . . . . . . . . . . . . . 46 Transition diagram of the CoE download state machine . . . . . . . . . 48
6129a5f715fb, 2010/04/30
ix
Conventions
Conventions
The following typographic conventions are used: Italic face is used for newly introduced terms and le names. Typewriter face is used for code examples and command line output. Bold typewriter face is used for user input in command lines. Data values and addresses are usually specied as hexadecimal values. These are marked in the C programming language style with the prex 0x (example: 0x88A4). Unless otherwise noted, address values are specied as byte addresses. Function names are always printed with parentheses, but without parameters. So, if a function ecrt_request_master() has empty parentheses, this shall not imply that it has no parameters. If shell commands have to be entered, this is marked by a dollar prompt:
$
Further, if a shell command has to be entered as the superuser, the prompt is a mesh:
#
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
1 The IgH EtherCAT Master Bus monitoring during operation. Automatic reconguration of slaves (for example after power failure) during operation. Distributed Clocks support (see sec. 3.5). Conguration of the slaves DC parameters through the application interface. Synchronization (oset and drift compensation) of the distributed slave clocks to the reference clock. Optional synchronization of the reference clock to the master clock. CANopen over EtherCAT (CoE) SDO upload, download and information service. Slave conguration via SDOs. SDO access from userspace and from the application. Ethernet over EtherCAT (EoE) Transparent use of EoE slaves via virtual network interfaces. Natively supports either a switched or a routed EoE network architecture. Vendor-specic over EtherCAT (VoE) Communication with vendor-specic mailbox protocols via the API. File Access over EtherCAT (FoE) Loading and storing les via the command-line tool. Updating a slaves rmware can be done easily. Servo Prole over EtherCAT (SoE) Implemented according to IEC 61800-7 [15]. Storing IDN congurations, that are written to the slave during startup. Accessing IDNs via the command-line tool. Accessing IDNs at runtime via the the user-space library. Userspace command-line-tool ethercat (see sec. 7.1) Detailed information about master, slaves, domains and bus conguration. Setting the masters debug level. Reading/Writing alias addresses. Listing slave congurations. Viewing process data. SDO download/upload; listing SDO dictionaries.
6129a5f715fb, 2010/04/30
1.2 License Loading and storing les via FoE. SoE IDN access. Access to slave registers. Slave SII (EEPROM) access. Controlling application-layer states. Generation of slave description XML and C-code from existing slaves. Seamless system integration though LSB compliance. Master and network device conguration via syscong les. Init script for master control. Virtual read-only network interface for monitoring and debugging purposes.
1.2 License
The master code is released under the terms and conditions of the GNU General Public License (GPL [4]), version 2. Other developers, that want to use EtherCAT with Linux systems, are invited to use the master code or even participate on development. To allow static linking of userspace application against the masters application interface (see chap. 3), the userspace library (see sec. 7.2) is licensed under the terms and conditions of the GNU Lesser General Public License (LGPL [5]), version 2.1.
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
2 Architecture
The EtherCAT master is integrated into the Linux 2.6 kernel. This was an early design decision, which has been made for several reasons: Kernel code has signicantly better realtime characteristics, i. e. less latency than userspace code. It was foreseeable, that a eldbus master has a lot of cyclic work to do. Cyclic work is usually triggered by timer interrupts inside the kernel. The execution delay of a function that processes timer interrupts is less, when it resides in kernelspace, because there is no need of time-consuming context switches to a userspace process. It was also foreseeable, that the master code has to directly communicate with the Ethernet hardware. This has to be done in the kernel anyway (through network device drivers), which is one more reason for the master code being in kernelspace. Figure 2.1 gives a general overview of the master architecture. The components of the master environment are described below: Master Module Kernel module containing one or more EtherCAT master instances (see sec. 2.1), the Device Interface (see sec. 4.5) and the Application Interface (see chap. 3). Device Modules EtherCAT-capable Ethernet device driver modules, that oer their devices to the EtherCAT master via the device interface (see sec. 4.5). These modied network drivers can handle network devices used for EtherCAT operation and normal Ethernet devices in parallel. A master can accept a certain device and then is able to send and receive EtherCAT frames. Ethernet devices declined by the master module are connected to the kernels network stack as usual. Application A program that uses the EtherCAT master (usually for cyclic exchange of process data with EtherCAT slaves). These programs are not part of the EtherCAT master code1 , but have to be generated or written by the user. An application can request a master through the application interface (see chap. 3). If this succeeds, it has the control over the master: It can provide a bus conguration and exchange process data. Applications can be kernel modules (that use the kernel application interface directly) or userspace programs, that use the application interface via the EtherCAT library (see sec. 7.2).
1
6129a5f715fb, 2010/04/30
2 Architecture
ecrt_*()
libethercat
Application Interface
Userspace Application
'ethercat' Tool
Userspace Kernelspace
Character Device
Application Module
Master 1
Packet Socket
Task
Master 0
Generic Ethernet Device
netif_*()
net_device
net_device
net_device
Hardware
NIC
NIC
NIC
EtherCAT
Ethernet
EtherCAT
6129a5f715fb, 2010/04/30
Network Stack
ecrt_*()
Application Interface
The two masters can be addressed by their indices 0 and 1 respectively (see gure 2.2). The master index is needed for the ecrt_master_request() function of the application interface (see chap. 3) and the --master option of the ethercat command-line tool (see sec. 7.1), which defaults to 0.
Kernel space EtherCAT master module
master 0
master 1
Debug Level The master module also has a parameter debug level to set the initial debug level for all masters (see also 7.1.6). Init Script In most cases it is not necessary to load the master module and the Ethernet driver modules manually. There is an init script available, so the master can be started as a service (see sec. 7.3). Syslog The master module outputs information about its state and events to the kernel ring buer. These also end up in the system logs. The above module loading command should result in the messages below:
6129a5f715fb, 2010/04/30
2 Architecture
# dmesg | tail -2 EtherCAT : Master driver 1.5.0 EtherCAT : 2 masters waiting for devices . # tail -2 /var/log/messages Jul 4 10:22:45 ethercat kernel : EtherCAT : Master driver 1.5.0 Jul 4 10:22:45 ethercat kernel : EtherCAT : 2 masters waiting for devices .
All EtherCAT master output is prexed with EtherCAT which makes searching the logs easier.
Operation
Figure 2.3: Master phases and transitions Orphaned phase This mode takes eect, when the master still waits for its Ethernet device to connect. No bus communication is possible until then. Idle phase takes eect when the master has accepted an Ethernet device, but is not requested by any application yet. The master runs its state machine (see sec. 5.3), that automatically scans the bus for slaves and executes pending operations from the userspace interface (for example SDO access). The command-line tool can be used to access the bus, but there is no process data exchange because of the missing bus conguration. Operation phase The master is requested by an application that can provide a bus conguration and exchange process data.
6129a5f715fb, 2010/04/30
2.3 Process Data Process Data Image Slaves oer their inputs and outputs by presenting the master so-called Process Data Objects (PDOs). The available PDOs can be either determined by reading out the slaves TXPDO and RXPDO SII categories from the E2 PROM (in case of xed PDOs) or by reading out the appropriate CoE objects (see sec. 6.2), if available. The application can register the PDOs entries for exchange during cyclic operation. The sum of all registered PDO entries denes the process data image, which is exchanged via datagrams with logical memory access (like LWR, LRD or LRW) introduced in [2, sec. 5.4]. Process Data Domains The process data image can be easily managed by creating so-called domains, which allow grouped PDO exchange. They also take care of managing the datagram structures needed to exchange the PDOs. Domains are mandatory for process data exchange, so there has to be at least one. They were introduced for the following reasons: The maximum size of a datagram is limited due to the limited size of an Ethernet frame: The maximum data size is the Ethernet data eld size minus the EtherCAT frame header, EtherCAT datagram header and EtherCAT datagram footer: 1500 2 12 2 = 1484 octets. If the size of the process data image exceeds this limit, multiple frames have to be sent, and the image has to be partitioned for the use of multiple datagrams. A domain manages this automatically. Not every PDO has to be exchanged with the same frequency: The values of PDOs can vary slowly over time (for example temperature values), so exchanging them with a high frequency would just waste bus bandwidth. For this reason, multiple domains can be created, to group dierent PDOs and so allow separate exchange. There is no upper limit for the number of domains, but each domain occupies one FMMU in each slave involved, so the maximum number of domains is de facto limited by the slaves. FMMU Conguration An application can register PDO entries for exchange. Every PDO entry and its parent PDO is part of a memory area in the slaves physical memory, that is protected by a sync manager [2, sec. 6.7] for synchronized access. In order to make a sync manager react on a datagram accessing its memory, it is necessary to access the last byte covered by the sync manager. Otherwise the sync manager will not react on the datagram and no data will be exchanged. That is why the whole synchronized memory area has to be included into the process data image: For example, if a certain PDO entry of a slave is registered for exchange with a certain domain, one FMMU will be congured to map the complete sync-managerprotected memory, the PDO entry resides in. If a second PDO entry of the same slave is registered for process data exchange within the same domain, and it resides in the same sync-manager-protected memory as the rst one, the FMMU conguration is
6129a5f715fb, 2010/04/30
2 Architecture not altered, because the desired memory is already part of the domains process data image. If the second PDO entry would belong to another sync-manager-protected area, this complete area would also be included into the domains process data image. Figure 2.4 gives an overview, how FMMUs are congured to map physical memory to logical process data images.
Slave0 RAM SM0 SM1 Slave1 RAM SM3
FMMU0
FMMU1
FMMU2
FMMU0
Domain0 Image
Domain1 Image
10
6129a5f715fb, 2010/04/30
3 Application Interface
The application interface provides functions and data structures for applications to access an EtherCAT master. The complete documentation of the interface is included as Doxygen [12] comments in the header le include/ecrt.h. It can either be read directly from the le comments, or as a more comfortable HTML documentation. The HTML generation is described in sec. 9.3. The following sections cover a general description of the application interface. Every application should use the master in two steps: Conguration The master is requested and the conguration is applied. For example, domains are created, slaves are congured and PDO entries are registered (see sec. 3.1). Operation Cyclic code is run and process data are exchanged (see sec. 3.2). Example Applications There are a few example applications in the examples/ subdirectory of the master code. They are documented in the source code.
6129a5f715fb, 2010/04/30
11
3 Application Interface
Master Index n Domain
n Slave Configuration Alias Position Vendor ID Product Code n n Sync Manager Index Direction n PDO Index n
Figure 3.1: Master Conguration Slave Position The slave position has to be specied as a tuple of alias and position. This allows addressing slaves either via an absolute bus position, or a stored identier called alias, or a mixture of both. The alias is a 16-bit value stored in the slaves E2 PROM. It can be modied via the command-line tool (see sec. 7.1.2). Table 3.1 shows, how the values are interpreted. Table 3.1: Specifying a Slave Position Position Interpretation 0 0 65535 Position addressing. The position parameter is interpreted as the absolute ring position in the bus. 1 65535 0 65535 Alias addressing. The position parameter is interpreted as relative position after the rst slave with the given alias address. Alias Figure 3.2 shows an example of how slave congurations are attached. Some of the congurations were attached, while others remain detached. The below lists gives the reasons beginning with the top slave conguration. 1. A zero alias means to use simple position addressing. Slave 1 exists and vendor id and product code match the expected values. 2. Although the slave with position 0 is found, the product code does not match, so the conguration is not attached.
12
6129a5f715fb, 2010/04/30
Slave Configurations
Alias: Position: Vendor: Product: Alias: Position: Vendor: Product: Alias: Position: Vendor: Product: Alias: Position: Vendor: Product: Alias: Position: Vendor: Product: 0x0000 1 0x00000002 0x00000004 0x0000 0 0x00000001 0x00000002 0x2000 0 0x00000001 0x00000002 0x3000 0 0x00000001 0x00000002 0x2000 1 0x00000001 0x00000002
Figure 3.2: Slave Conguration Attachment 3. The alias is non-zero, so alias addressing is used. Slave 2 is the rst slave with alias 0x2000. Because the position value is zero, the same slave is used. 4. There is no slave with the given alias, so the conguration can not be attached. 5. Slave 2 is again the rst slave with the alias 0x2000, but position is now 1, so slave 3 is attached.
6129a5f715fb, 2010/04/30
13
3 Application Interface The VoE state machine can only process one operation at a time. As a result, either a read or write operation may be issued at a time1 . After the operation is initiated, the handler must be executed cyclically until it is nished. After that, the results of the operation can be retrieved. A VoE handler has an own datagram structure, that is marked for exchange after each execution step. So the application can decide, how many handlers to execute before sending the corresponding EtherCAT frame(s). For more information about the use of VoE handlers see the documentation of the application interface functions and the example applications provided in the examples/ directory.
Task
Master0
EoE
If simultaneous sending and receiving is desired, two VoE handlers can be created for the slave conguration.
14
6129a5f715fb, 2010/04/30
3.5 Distributed Clocks Figure 3.3 exemplary shows, how two processes share one master: The applications cyclic task uses the master for process data exchange, while the master-internal EoE process uses it to communicate with EoE-capable slaves. Both have to access the bus from time to time, but the EoE process does this by asking the application to do the bus access for it. In this way, the application can use the appropriate locking mechanism to avoid accessing the bus at the same time. See the application interface documentation (chap. 3) for how to use these callbacks.
Slave 1
Slave 2
Slave n
Reference Clock
Slave Clocks
Local Clocks Any EtherCAT slave that supports DC has a local clock register with nanosecond resolution. If the slave is powered, the clock starts from zero, meaning that when slaves are powered on at dierent times, their clocks will have dierent values. These osets have to be compensated by the distributed clocks mechanism. On the other hand, the clocks do not run exactly with the same speed, since the used quarts units have a natural frequency deviation. This deviation is usually very small, but over longer periods, the error would accumulate and the dierence between local clocks would grow. This clock drift has also to be compensated by the DC mechanism. Application Time The common time base for the bus has to be provided by the application. This application time tapp is used 1. to congure the slaves clock osets (see below), 2. to program the slaves start times for sync pulse generation (see below). 3. to synchronize the reference clock to the master clock (optional).
6129a5f715fb, 2010/04/30
15
3 Application Interface Oset Compensation For the oset compensation, each slave provides a System Time Oset register to , that is added to the internal clock value tint to get the System Time tsys :
(3.1)
The master reads the values of both registers to calculate a new system time oset in a way, that the resulting system time shall match the masters application time tapp :
!
(3.2)
(3.3)
The small time oset error resulting from the dierent times of reading and writing the registers will be compensated by the drift compensation. Drift Compensation The drift compensation is possible due to a special mechanism in each DC-capable slave: A write operation to the System time register will cause the internal time control loop to compare the written time (minus the programmed transmission delay, see below) to the current system time. The calculated time error will be used as an input to the time controller, that will tune the local clock speed to be a little faster or slower2 , according to the sign of the error. Transmission Delays The Ethernet frame needs a small amount of time to get from slave to slave. The resulting transmission delay times accumulate on the bus and can reach microsecond magnitude and thus have to be considered during the drift compensation. EtherCAT slaves supporting DC provide a mechanism to measure the transmission delays: For each of the four slave ports there is a receive time register. A write operation to the receive time register of port 0 starts the measuring and the current system time is latched and stored in a receive time register once the frame is received on the corresponding port. The master can read out the relative receive times, then calculate time delays between the slaves (using its knowledge of the bus topology), and nally calculate the time delays from the reference clock to each slave. These values are programmed into the slaves transmission delay registers. In this way, the drift compensation can reach nanosecond synchrony.
2
The local slave clock will be incremented either with 9 ns, 10 ns or 11 ns every 10 ns.
16
6129a5f715fb, 2010/04/30
3.5 Distributed Clocks Checking Synchrony DC-capable slaves provide the 32-bit System time dierence register at address 0x092c, where the system time dierence of the last drift compensation is stored in nanosecond resolution and in sign-and-magnitude coding3 . To check for bus synchrony, the system time dierence registers can also be cyclically read via the command-line-tool (see sec. 7.1.14):
$ watch -n0 "ethercat reg read -p4 -tsm32 0x92c"
Sync Signals Synchronous clocks are only the prerequisite for synchronous events on the bus. Each slave with DC support provides two sync signals, that can be programmed to create events, that will for example cause the slave application to latch its inputs on a certain time. A sync event can either be generated once or cyclically, depending on what makes sense for the slave application. Programming the sync signals is a matter of setting the so-called AssignActivate word and the sync signals cycle- and shift times. The AssignActivate word is slave-specic and has to be taken from the XML slave description (Device Dc), where also typical sync signal congurations OpModes can be found.
This allows broadcast-reading all system time dierence registers on the bus to get an upper approximation
6129a5f715fb, 2010/04/30
17
3 Application Interface
18
6129a5f715fb, 2010/04/30
4 Ethernet Devices
The EtherCAT protocol is based on the Ethernet standard, so a master relies on standard Ethernet hardware to communicate with the bus. The term device is used as a synonym for Ethernet network interface hardware. Native Ethernet Device Drivers There are native device driver modules (see sec. 4.2) that handle Ethernet hardware, which a master can use to connect to an EtherCAT bus. They oer their Ethernet hardware to the master module via the device interface (see sec. 4.5) and must be capable to prepare Ethernet devices either for EtherCAT (realtime) operation or for normal operation using the kernels network stack. The advantage of this approach is that the master can operate nearly directly on the hardware, which allows a high performance. The disadvantage is, that there has to be an EtherCAT-capable version of the original Ethernet driver. Generic Ethernet Device Driver From master version 1.5, there is a generic Ethernet device driver module (see sec. 4.3), that uses the lower layers of the network stack to connect to the hardware. The advantage is, that arbitrary Ethernet hardware can be used for EtherCAT operation, independently of the actual hardware driver (so all Linux Ethernet drivers are supported without modications). The disadvantage is, that this approach does not support realtime extensions like RTAI, because the Linux network stack is addressed. Moreover the performance is a little worse than the native approach, because the Ethernet frame data have to traverse the network stack.
6129a5f715fb, 2010/04/30
19
4 Ethernet Devices kernels networking stack and forwarding it to the hardware, that does the physical transmission. If data is received by the hardware respectively, the driver is notied (usually by means of an interrupt) and has to read the data from the hardware memory and forward it to the network stack. There are a few more tasks, a network device driver has to handle, including queue control, statistics and device dependent features. Driver Startup Usually, a driver searches for compatible devices on module loading. For PCI drivers, this is done by scanning the PCI bus and checking for known device IDs. If a device is found, data structures are allocated and the device is taken into operation. Interrupt Operation A network device usually provides a hardware interrupt that is used to notify the driver of received frames and success of transmission, or errors, respectively. The driver has to register an interrupt service routine (ISR), that is executed each time, the hardware signals such an event. If the interrupt was thrown by the own device (multiple devices can share one hardware interrupt), the reason for the interrupt has to be determined by reading the devices interrupt register. For example, if the ag for received frames is set, frame data has to be copied from hardware to kernel memory and passed to the network stack. The net_device Structure The driver registers a net_device structure for each device to communicate with the network stack and to create a network interface. In case of an Ethernet driver, this interface appears as ethX, where X is a number assigned by the kernel on registration. The net_device structure receives events (either from userspace or from the network stack) via several callbacks, which have to be set before registration. Not every callback is mandatory, but for reasonable operation the ones below are needed in any case:
open() This function is called when network communication has to be started, for example after a command ip link set ethX up from userspace. Frame reception
ture, which permanently has to be lled with frame statistics. This means, that every time a frame is received, sent, or an error happened, the appropriate counter in this structure has to be increased. The actual registration is done with the register_netdev() call, unregistering is done with unregister_netdev().
20
6129a5f715fb, 2010/04/30
4.2 Native EtherCAT Device Drivers The netif Interface All other communication in the direction interface network stack is done via the netif_*() calls. For example, on successful device opening, the network stack has to be notied, that it can now pass frames to the interface. This is done by calling netif_start_queue(). After this call, the hard_start_xmit() callback can be called by the network stack. Furthermore a network driver usually manages a frame transmission queue. If this gets lled up, the network stack has to be told to stop passing further frames for a while. This happens with a call to netif_stop_queue(). If some frames have been sent, and there is enough space again to queue new frames, this can be notied with netif_wake_queue(). Another important call is netif_receive_skb()1 : It passes a frame to the network stack, that was just received by the device. Frame data has to be included in a so-called socket buer for that (see below). Socket Buers Socket buers are the basic data type for the whole network stack. They serve as containers for network data and are able to quickly add data headers and footers, or strip them o again. Therefore a socket buer consists of an allocated buer and several pointers that mark beginning of the buer (head), beginning of data (data), end of data (tail) and end of buer (end). In addition, a socket buer holds network header information and (in case of received data) a pointer to the net_device, it was received on. There exist functions that create a socket buer (dev_alloc_skb()), add data either from front (skb_push()) or back (skb_put()), remove data from front (skb_pull()) or back (skb_trim()), or delete the buer (kfree_skb()). A socket buer is passed from layer to layer, and is freed by the layer that uses it the last time. In case of sending, freeing has to be done by the network driver.
This function is part of the NAPI (New API), that replaces the kernel 2.4 technique for interfacing to the network stack (with netif_rx()). NAPI is a technique to improve network performance on Linux. Read more in http://www.cyberus.ca/~hadi/usenix-paper.tgz.
6129a5f715fb, 2010/04/30
21
4 Ethernet Devices notify the driver about frame reception: The master can instead query the hardware for received frames, if it expects them to be already received. Figure 4.1 shows two workows for cyclic frame transmission and reception with and without interrupts.
Interrupt Operation ... Data Processing Frame Assembly Realtime Cycle Realtime Cycle Frame Sending Interruptless Operation ... ISR Frame Dissection Data Processing Frame Assembly Frame Sending
Time
ISR ...
Figure 4.1: Interrupt Operation versus Interrupt-less Operation In the left workow Interrupt Operation, the data from the last cycle is rst processed and a new frame is assembled with new datagrams, which is then sent. The cyclic work is done for now. Later, when the frame is received again by the hardware, an interrupt is triggered and the ISR is executed. The ISR will fetch the frame data from the hardware and initiate the frame dissection: The datagrams will be processed, so that the data is ready for processing in the next cycle. In the right workow Interrupt-less Operation, there is no hardware interrupt enabled. Instead, the hardware will be polled by the master by executing the ISR. If the frame has been received in the meantime, it will be dissected. The situation is now the same as at the beginning of the left workow: The received data is processed and a new frame is assembled and sent. There is nothing to do for the rest of the cycle. The interrupt-less operation is desirable, because hardware interrupts are not conducive in improving the drivers realtime behaviour: Their indeterministic incidences contribute to increasing the jitter. Besides, if a realtime extension (like RTAI) is used, some additional eort would have to be made to prioritize interrupts. Ethernet and EtherCAT Devices Another issue lies in the way Linux handles devices of the same type. For example, a PCI driver scans the PCI bus for devices it can handle. Then it registers itself as the responsible driver for all of the devices found. The problem is, that an unmodied driver can not be told to ignore a device because it will be used for EtherCAT later. There must be a way to handle multiple devices
22
6129a5f715fb, 2010/04/30
4.3 Generic EtherCAT Device Driver of the same type, where one is reserved for EtherCAT, while the other is treated as an ordinary Ethernet device. For all this reasons, the author decided that the only acceptable solution is to modify standard Ethernet drivers in a way that they keep their normal functionality, but gain the ability to treat one or more of the devices as EtherCAT-capable. Below are the advantages of this solution: No need to tell the standard drivers to ignore certain devices. One networking driver for EtherCAT and non-EtherCAT devices. No need to implement a network driver from scratch and running into issues, the former developers already solved. The chosen approach has the following disadvantages: The modied driver gets more complicated, as it must handle EtherCAT and non-EtherCAT devices. Many additional case dierentiations in the driver code. Changes and bug xes on the standard drivers have to be ported to the EtherCAT-capable versions from time to time.
6129a5f715fb, 2010/04/30
23
4 Ethernet Devices
24
6129a5f715fb, 2010/04/30
4.6 Patching Native Network Drivers 3. The master does not use a new socket buer for each send operation: Instead there is a x one allocated on master initialization. This socket buer is lled with an EtherCAT frame with every send operation and passed to the hard_start_xmit() callback. For that it is necessary, that the socket buer is not be freed by the network driver as usual. An Ethernet driver usually handles several Ethernet devices, each described by a net_device structure with a priv_data eld to attach driver-dependent data to the structure. To distinguish between normal Ethernet devices and the ones used by EtherCAT masters, the private data structure used by the driver could be extended by a pointer, that points to an ec_device_t object returned by ecdev_offer() (see sec. 4.5) if the device is used by a master and otherwise is zero. The RealTek RTL-8139 Fast Ethernet driver is a simple Ethernet driver and can be taken as an example to patch new drivers. The interesting sections can be found by searching the string ecdev in the le devices/8139too-2.6.24-ethercat.c.
6129a5f715fb, 2010/04/30
25
4 Ethernet Devices
26
6129a5f715fb, 2010/04/30
5 State Machines
Many parts of the EtherCAT master are implemented as nite state machines (FSMs). Though this leads to a higher grade of complexity in some aspects, is opens many new possibilities. The below short code example exemplary shows how to read all slave states and moreover illustrates the restrictions of sequential coding:
1 2 3
ec_datagram_brd ( datagram , 0 x0130 , 2); // prepare datagram if ( ec_mas te r _ s im p l e_ i o ( master , datagram )) return -1; slave_states = EC_READ_U8 ( datagram - > data ); // process datagram
The ec master simple io() function provides a simple interface for synchronously sending a single datagram and receiving the result1 . Internally, it queues the specied datagram, invokes the ec master send datagrams() function to send a frame with the queued datagram and then waits actively for its reception. This sequential approach is very simple, reecting in only three lines of code. The disadvantage is, that the master is blocked for the time it waits for datagram reception. There is no diculty when only one instance is using the master, but if more instances want to (synchronously2 ) use the master, it is inevitable to think about an alternative to the sequential model. Master access has to be sequentialized for more than one instance wanting to send and receive datagrams synchronously. With the present approach, this would result in having one phase of active waiting for each instance, which would be non-acceptable especially in realtime circumstances, because of the huge time overhead. A possible solution is, that all instances would be executed sequentially to queue their datagrams, then give the control to the next instance instead of waiting for the datagram reception. Finally, bus IO is done by a higher instance, which means that all queued datagrams are sent and received. The next step is to execute all instances again, which then process their received datagrams and issue new ones. This approach results in all instances having to retain their state, when giving the control back to the higher instance. It is quite obvious to use a nite state machine model in this case. Section 5.1 will introduce some of the theory used, while the
1
For all communication issues have been meanwhile sourced out into state machines, the function is deprecated and stopped existing. Nevertheless it is adequate for showing its own restrictions. 2 At this time, synchronous master access will be adequate to show the advantages of an FSM. The asynchronous approach will be discussed in sec. 6.1
6129a5f715fb, 2010/04/30
27
5 State Machines listings below show the basic approach by coding the example from above as a state machine:
1 2 3 4 5
// state 1 ec_datagram_brd ( datagram , 0 x0130 , 2); // prepare datagram ec_master_queue ( master , datagram ); // queue datagram next_state = state_2 ; // state processing finished
After all instances executed their current state and queued their datagrams, these are sent and received. Then the respective next states are executed:
1 2 3 4 5 6 7
// state 2
See sec. 5.2 for an introduction to the state machine programming concept used in the master code.
28
6129a5f715fb, 2010/04/30
The state diagram for the same example looks like the one in gure 5.1. The states are represented as circles or ellipses and the transitions are drawn as arrows between them. Close to a transition arrow can be the condition that must be fullled to allow the transition. The initial state is marked by a lled black circle with an arrow pointing to the respective state.
b a, b s0 a, b, s2 a s1
Deterministic and non-deterministic state machines A state machine can be deterministic, meaning that for one state and input, there is one (and only one) following state. In this case, the state machine has exactly one starting state. Non-deterministic state machines can have more than one transitions for a single state-input combination. There is a set of starting states in the latter case. Moore and Mealy machines There is a distinction between so-called Moore machines, and Mealy machines. Mathematically spoken, the distinction lies in the output function : If it only depends on the current state ( : S ), the machine corresponds to the Moore Model. Otherwise, if is a function of a state and the input alphabet ( : S ) the state machine corresponds to the Mealy model. Mealy machines are the more practical solution in most cases, because their design allows machines with a minimum number of states. In practice, a mixture of both models is often used. Misunderstandings about state machines There is a phenomenon called state explosion, that is often taken as a counter-argument against general use of state
6129a5f715fb, 2010/04/30
29
5 State Machines machines in complex environments. It has to be mentioned, that this point is misleading [9]. State explosions happen usually as a result of a bad state machine design: Common mistakes are storing the present values of all inputs in a state, or not dividing a complex state machine into simpler sub state machines. The EtherCAT master uses several state machines, that are executed hierarchically and so serve as sub state machines. These are also described below.
enum { STATE_1 , STATE_2 , STATE_3 }; int state = STATE_1 ; void sta te_mac hine_r un ( void * priv_data ) { switch ( state ) { case STATE_1 :
action_1 (); state = STATE_2 ; break ; case STATE_2 : action_2 () if ( some_condition ) state = STATE_1 ; else state = STATE_3 ; break ; case STATE_3 : action_3 (); state = STATE_1 ; break ; } }
For small state machines, this is an option. The disadvantage is, that with an increasing number of states the code soon gets complex and an additional case dierentiation is executed each run. Besides, lots of indentation is wasted. The method used in the master is to implement every state in an own function and to store the current state function with a function pointer:
1
30
6129a5f715fb, 2010/04/30
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
In the master code, state pointers of all state machines3 are gathered in a single object of the ec_fsm_master_t class. This is advantageous, because there is always one instance of every state machine available and can be started on demand. Mealy and Moore If a closer look is taken to the above listing, it can be seen that the actions executed (the outputs of the state machine) only depend on the current state. This accords to the Moore model introduced in sec. 5.1. As mentioned, the Mealy model oers a higher exibility, which can be seen in the listing below:
1 2 3 4 5 6 7 8 9 10
else {
action_7b (); state = state8 ;
} }
3
The state function executes the actions depending on the state transition, that is about to be done.
7
All except for the EoE state machine, because multiple EoE slaves have to be handled in parallel. For this reason each EoE handler object has its own state pointer.
6129a5f715fb, 2010/04/30
31
5 State Machines The most exible alternative is to execute certain actions depending on the state, followed by some actions dependent on the state transition:
1 2 3 4 5 6 7 8 9 10 11
This model is often used in the master. It combines the best aspects of both approaches. Using Sub State Machines To avoid having too much states, certain functions of the EtherCAT master state machine have been sourced out into sub state machines. This helps to encapsulate the related workows and moreover avoids the state explosion phenomenon described in sec. 5.1. If the master would instead use one big state machine, the number of states would be a multiple of the actual number. This would increase the level of complexity to a non-manageable grade. Executing Sub State Machines If a state machine starts to execute a sub state machine, it usually remains in one state until the sub state machine terminates. This is usually done like in the listing below, which is taken out of the slave conguration state machine code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
32
6129a5f715fb, 2010/04/30
. . . either until the state machine terminates with the error state . . . . . . or until the state machine terminates in the end state. Until then, the higher state machine remains in the current state and executes the sub state machine again in the next cycle.
State Machine Descriptions The below sections describe every state machine used in the EtherCAT master. The textual descriptions of the state machines contain references to the transitions in the corresponding state transition diagrams, that are marked with an arrow followed by the name of the successive state. Transitions caused by trivial error cases (i. e. no response from slave) are not described explicitly. These transitions are drawn as dashed arrows in the diagrams.
6129a5f715fb, 2010/04/30
33
5 State Machines
start
broadcast
clear_addresses
read_state
dc_measure_delays
acknowledge
scan_slave
configure_slave
sdo_dictionary
sdo_request
write_sii
reg_request
34
6129a5f715fb, 2010/04/30
start
address
state
base
dc_cap
DC not supported
datalink
sii_size
sii_data
pdos
end
6129a5f715fb, 2010/04/30
35
5 State Machines PREOP If the slave supports CoE, it is set to PREOP state using the State change FSM (see sec. 5.6) to enable mailbox communication and read the PDO conguration via CoE. PDOs The PDOs are read via CoE (if supported) using the PDO Reading FSM (see sec. 5.8). If this is successful, the PDO information from the SII (if any) is overwritten.
36
6129a5f715fb, 2010/04/30
init
clear_fmmus
No FMMUs
clear_sync
No SMs
dc_clear_assign
dc_read_offset
No DC support
dc_write_offset
mbox_sync
No mailboxes
Config detached
boot_preop
sdo_conf
No SDOs configured
No config attached
pdo_conf
pdo_sync
No PDO SMs
INIT requested
No FMMUs configured
fmmu
dc_cycle
No config attached
dc_start
DC not configured
dc_assign
safeop
op
SAFEOP requested
end
37
5 State Machines
start
check
Ack only
Success
38
6129a5f715fb, 2010/04/30
5.7 The SII State Machine Start The new application-layer state is requested via the AL Control Request register (see [3, sec. 5.3.1]). Check for Response Some slave need some time to respond to an AL state change command, and do not respond for some time. For this case, the command is issued again, until it is acknowledged. Check AL Status If the AL State change datagram was acknowledged, the AL Control Response register (see [3, sec. 5.3.2]) must be read out until the slave changes the AL state. AL Status Code If the slave refused the state change command, the reason can be read from the AL Status Code eld in the AL State Changed registers (see [3, sec. 5.3.3]). Acknowledge State If the state change was not successful, the master has to acknowledge the old state by writing to the AL Control request register again. Check Acknowledge After sending the acknowledge command, it has to read out the AL Control Response register again. The start ack state is a shortcut in the state machine for the case, that the master wants to acknowledge a spontaneous AL state change, that was not requested.
read_check
write_check
read_fetch
write_check2
error
end
Figure 5.6: Transition Diagram of the SII State Machine This is how the reading part of the state machine works: Start Reading The read request and the requested word address are written to the SII attribute.
6129a5f715fb, 2010/04/30
39
5 State Machines Check Read Command If the SII read request command has been acknowledged, a timer is started. A datagram is issued, that reads out the SII attribute for state and data. Fetch Data If the read operation is still busy (the SII is usually implemented as an E2 PROM), the state is read again. Otherwise the data are copied from the datagram. The writing part works nearly similar: Start Writing A write request, the target address and the data word are written to the SII attribute. Check Write Command If the SII write request command has been acknowledged, a timer is started. A datagram is issued, that reads out the SII attribute for the state of the write operation. Wait while Busy If the write operation is still busy (determined by a minimum wait time and the state of the busy ag), the state machine remains in this state to avoid that another write operation is issued too early.
40
6129a5f715fb, 2010/04/30
start First SM
pdo_count
end
No more PDOs
pdo_entries
start
count
Figure 5.8: Transition Diagram of the PDO Entry Reading State Machine
6129a5f715fb, 2010/04/30
41
5 State Machines
start First SM No config First PDO No more SMs end Unknown read_mapping
Assign ok No PDOs
No PDOs
Next PDO
mapping
42
6129a5f715fb, 2010/04/30
start
end
Figure 5.10: Transition Diagram of the PDO Entry Conguration State Machine
6129a5f715fb, 2010/04/30
43
5 State Machines
44
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
45
6 Mailbox Protocol Implementations EoE Handlers The virtual EoE interfaces and the related functionality is encapsulated in the ec_eoe_t class. An object of this class is called EoE handler. For example the master does not create the network interfaces directly: This is done inside the constructor of an EoE handler. An EoE handler additionally contains a frame queue. Each time, the kernel passes a new socket buer for sending via the interfaces hard_start_xmit() callback, the socket buer is queued for transmission by the EoE state machine (see below). If the queue gets lled up, the passing of new socket buers is suspended with a call to netif_stop_queue(). Creation of EoE Handlers During bus scanning (see sec. 5.4), the master determines the supported mailbox protocols foe each slave. This is done by examining the Supported Mailbox Protocols mask eld at word address 0x001C of the SII. If bit 1 is set, the slave supports the EoE protocol. In this case, an EoE handler is created for that slave. EoE State Machine Every EoE handler owns an EoE state machine, that is used to send frames to the corresponding slave and receive frames from the it via the EoE communication primitives. This state machine is showed in gure 6.1.
RX_START
RX_CHECK
RX_FETCH
TX_START
TX_SENT
Figure 6.1: Transition Diagram of the EoE State Machine RX START The beginning state of the EoE state machine. A mailbox check datagram is sent, to query the slaves mailbox for new frames. RX CHECK RX CHECK The mailbox check datagram is received. If the slaves mailbox did not contain data, a transmit cycle is started. TX START If there are new data in the mailbox, a datagram is sent to fetch the new data. RX FETCH RX FETCH The fetch datagram is received. If the mailbox data do not contain a EoE Fragment request command, the data are dropped and a transmit sequence is started. TX START If the received Ethernet frame fragment is the rst fragment, a new socket buer is allocated. In either case, the data are copied into the correct position of the socket buer.
46
6129a5f715fb, 2010/04/30
6.2 CANopen over EtherCAT (CoE) If the fragment is the last fragment, the socket buer is forwarded to the network stack and a transmit sequence is started. TX START Otherwise, a new receive sequence is started to fetch the next fragment. RX START TX START The beginning state of a transmit sequence. It is checked, if the transmission queue contains a frame to send. If not, a receive sequence is started. RX START If there is a frame to send, it is dequeued. If the queue was inactive before (because it was full), the queue is woken up with a call to netif wake queue(). The rst fragment of the frame is sent. TX SENT TX SENT It is checked, if the rst fragment was sent successfully. If the current frame consists of further fragments, the next one is sent. TX SENT If the last fragment was sent, a new receive sequence is started. RX START
EoE Processing To execute the EoE state machine of every active EoE handler, there must be a cyclic process. The easiest solution would be to execute the EoE state machines synchronously with the master state machine (see sec. 5.3). This approach has the following disadvantage: Only one EoE fragment could be sent or received every few cycles. This causes the data rate to be very low, because the EoE state machines are not executed in the time between the application cycles. Moreover, the data rate would be dependent on the period of the application task. To overcome this problem, an own cyclic process is needed to asynchronously execute the EoE state machines. For that, the master owns a kernel timer, that is executed each timer interrupt. This guarantees a constant bandwidth, but poses the new problem of concurrent access to the master. The locking mechanisms needed for this are introduced in sec. 3.4.
Automatic Conguration By default, slaves are left in PREOP state, if no conguration is applied. If an EoE interface link is set to up, the requested slaves application-layer state is automatically set to OP.
6129a5f715fb, 2010/04/30
47
6 Mailbox Protocol Implementations SDO Download State Machine The best time to apply SDO congurations is during the slaves PREOP state, because mailbox communication is already possible and slaves application will start with updating input data in the succeeding SAFEOP state. Therefore the SDO conguration has to be part of the slave conguration state machine (see sec. 5.5): It is implemented via an SDO download state machine, that is executed just before entering the slaves SAFEOP state. In this way, it is guaranteed that the SDO congurations are applied each time, the slave is recongured. The transition diagram of the SDO Download state machine can be seen in gure 6.2.
START
REQUEST
CHECK
RESPONSE
ERROR
END
START The beginning state of the CoE download state machine. The SDO Download Normal Request mailbox command is sent. REQUEST REQUEST It is checked, if the CoE download request has been received by the slave. After that, a mailbox check command is issued and a timer is started. CHECK CHECK If no mailbox data is available, the timer is checked. If it timed out, the SDO download is aborted. ERROR Otherwise, the mailbox is queried again. CHECK If the mailbox contains new data, the response is fetched. RESPONSE RESPONSE If the mailbox response could not be fetched, the data is invalid, the wrong protocol was received, or a Abort SDO Transfer Request was received, the SDO download is aborted. ERROR If a SDO Download Normal Response acknowledgement was received, the SDO download was successful. END END The SDO download was successful. ERROR The SDO download was aborted due to an error.
48
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
49
50
6129a5f715fb, 2010/04/30
7 Userspace Interfaces
For the master runs as a kernel module, accessing it is natively limited to analyzing Syslog messages and controlling using modutils. It was necessary to implement further interfaces, that make it easier to access the master from userspace and allow a ner inuence. It should be possible to view and to change special parameters at runtime. Bus visualization is another point: For development and debugging purposes it is necessary to show the connected slaves with a single command, for instance (see sec. 7.1). The application interface has to be available in userspace, to allow userspace programs to use EtherCAT master functionality. This was implemented via a character device and a userspace library (see sec. 7.2). Another aspect is automatic startup and conguration. The master must be able to automatically start up with a persistent conguration (see sec. 7.3). A last thing is monitoring EtherCAT communication. For debugging purposes, there had to be a way to analyze EtherCAT datagrams. The best way would be with a popular network analyzer, like Wireshark [7] (the former Ethereal) or others (see sec. 7.4). This chapter covers all these points and introduces the interfaces and tools to make all that possible.
Device Node Creation The character device nodes are automatically created, if the udev Package is installed. See sec. 9.5 for how to install and congure it.
6129a5f715fb, 2010/04/30
51
7 Userspace Interfaces
52
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
53
7 Userspace Interfaces
Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
The domain s base address for the logical datagram ( LRD / LWR / LRW ) is displayed followed by the domain s process data size in byte . The last values are the current datagram working counter sum and the expected working counter sum . If the values are equal , all PDOs were exchanged during the last cycle . If the -- verbose option is given , the participating slave configurations / FMMUs and the current process data are additionally displayed : Domain1 : LogBaseAddr 0 x00000006 , Size 6 , WorkingCounter 0/1 SlaveConfig 1001:0 , SM3 ( Input ) , LogAddr 0 x00000006 , Size 6 0 x00 0 x00 0 x00 0 x00 0 x00 0 x00 The process data are displayed as hexadecimal bytes . Command - specific options : -- domain -d < index > Positive numerical domain index .
54
6129a5f715fb, 2010/04/30
Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
6129a5f715fb, 2010/04/30
55
7 Userspace Interfaces
Read an SDO entry from a slave . This command requires a single slave to be selected . The data type of the SDO entry is taken from the SDO dictionary by default . It can be overridden with the -- type option . If the slave does not support the SDO information service or the SDO is not in the dictionary , the -- type option is mandatory . These are valid data types to use with the -- type option : bool , int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , uint64 , float , double string , octet_string , unicode_string . For sign - and - magnitude coding , use the following types : sm8 , sm16 , sm32 , sm64 Arguments : INDEX is the SDO index and must be an unsigned 16 bit number . SUBINDEX is the SDO entry subindex and must be an unsigned 8 bit number . Command - specific options : -- alias -a < alias > -- position -p <pos > Slave selection . See the help of the slaves command . -- type -t < type > SDO entry data type ( see above ). Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
56
6129a5f715fb, 2010/04/30
Arguments : SOURCEFILE is the name of the source file on the slave . Command - specific options : -- output - file -o < file >
Local target filename . If - ( default ) , data are printed to stdout . Slave selection . See the help of the slaves command .
-- alias -- position
Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base . ethercat foe_write [ OPTIONS ] < FILENAME > Store a file on a slave via FoE . This command requires a single slave to be selected . Arguments : FILENAME can either be a path to a file , or - . In the latter case , data are read from stdin and the -- output - file option has to be specified . Command - specific options : -- output - file -o < file >
Target filename on the slave . If the FILENAME argument is -, this is mandatory . Otherwise , the basename () of FILENAME is used by default . Slave selection . See the help of the slaves command .
-- alias -- position
Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
6129a5f715fb, 2010/04/30
57
7 Userspace Interfaces
58
6129a5f715fb, 2010/04/30
6129a5f715fb, 2010/04/30
59
7 Userspace Interfaces
either a path to a file with data to write , or -, which means , that data are read from stdin . If a datatype was specified , VALUE is interpreted respective to the given type . These are valid data types to use with the -- type option : bool , int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , uint64 , float , double string , octet_string , unicode_string . For sign - and - magnitude coding , use the following types : sm8 , sm16 , sm32 , sm64 Command - specific options : -- alias -a < alias > -- position -p <pos > Slave selection . See the help of the slaves command . -- type -t < type > Data type ( see above ). Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
60
6129a5f715fb, 2010/04/30
Reading out SII data is as easy as other commands. Though the data are in binary format, analysis is easier with a tool like hexdump:
$ ethercat sii read --position 3 | hexdump 0000000 0103 0000 0000 0000 0000 0000 0000 008 c
6129a5f715fb, 2010/04/30
61
7 Userspace Interfaces
0000010 0002 0000 3052 07 f0 0000 0000 0000 0000 0000020 0000 0000 0000 0000 0000 0000 0000 0000 ...
To download SII contents to a slave, writing access to the masters character device is necessary (see sec. 7.1.1).
ethercat sii_write [ OPTIONS ] < FILENAME > Write SII contents to a slave . This command requires a single slave to be selected . The file contents are checked for validity and integrity . These checks can be overridden with the -- force option . Arguments : FILENAME must be a path to a file that contains a positive number of words . If it is -, data are read from stdin . Command - specific options : -- alias -a < alias > -- position -p <pos > Slave selection . See the help of the slaves command . -- force -f Override validity checks . Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
The SII contents will be checked for validity and then sent to the slave. The write operation may take a few seconds.
62
6129a5f715fb, 2010/04/30
If the -- verbose option is given , a detailed ( multi - line ) description is output for each slave . Slave selection : Slaves for this and other commands can be selected with the -- alias and -- position parameters as follows : 1) If neither the -- alias nor the -- position option is given , all slaves are selected . 2) If only the -- position option is given , it is interpreted as an absolute ring position and a slave with this position is matched . 3) If only the -- alias option is given , all slaves with the given alias address and subsequent slaves before a slave with a different alias address match ( use - p0 if only the slaves with the given alias are desired , see 4)). 4) If both the -- alias and the -- position option are given , the latter is interpreted as relative position behind any slave with the given alias . Command - specific options : -- alias -a < alias > Slave alias ( see above ). -- position -p <pos > Slave position ( see above ). -- verbose -v Show detailed slave information . Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
6129a5f715fb, 2010/04/30
63
7 Userspace Interfaces
3 5555:2 PREOP + EL2004 4 K . Dig . Ausgang 24 V , 0 ,5 A
64
6129a5f715fb, 2010/04/30
VALUE
The VALUE argument is interpreted as the given data type ( - - type is mandatory ) and written to the selected slave . These are valid data types to use with the -- type option : bool , int8 , int16 , int32 , int64 , uint8 , uint16 , uint32 , uint64 , float , double string , octet_string , unicode_string . For sign - and - magnitude coding , use the following types : sm8 , sm16 , sm32 , sm64 Command - specific options : -- alias -a < alias > -- position -p <pos > Slave selection . See the help of the slaves command . -- type -t < type > Data type ( see above ). Numerical values can be specified either with decimal ( no prefix ) , octal ( prefix 0 ) or hexadecimal ( prefix 0x ) base .
6129a5f715fb, 2010/04/30
65
7 Userspace Interfaces
66
6129a5f715fb, 2010/04/30
return 0;
}
The program can be compiled and dynamically linked to the library with the below command:
gcc ethercat . c -o ectest -I / opt / etherlab / include \ -L / opt / etherlab / lib - lethercat \ -Wl , - - rpath -Wl ,/ opt / etherlab / lib
7.2.2 Implementation
Basically the kernel API was transferred into userspace via the master character device (see chap. 2, g. 2.1 and sec. 7.1.1). The function calls of the kernel API are mapped to the userspace via an ioctl() interface. The userspace API functions share a set of generic ioctl() calls. The kernel part of the interface calls the according API functions directly, what results in a minimum additional delay (see sec. 7.2.3). For performance reasons, the actual domain process data (see sec. 2.3) are not copied between kernel and user memory on every access: Instead, the data are memorymapped to the userspace application. Once the master is congured and activated, the master module creates one process data memory area spanning all domains and maps it to userspace, so that the application can directly access the process data. As a result, there is no additional delay when accessing process data from userspace. Kernel/User API Dierences Because of the memory-mapping of the process data, the memory is managed internally by the library functions. As a result, it is not possible to provide external memory for domains, like in the kernel API. The corresponding functions are only available in kernelspace. This is the only dierence when using the application interface in userspace.
7.2.3 Timing
An interesting aspect is the timing of the userspace library calls compared to those of the kernel API. Table 7.1 shows the call times and standard deviancies of typical (and time-critical) API functions measured on an Intel Pentium 4 M CPU with 2.2 GHz and a standard 2.6.26 kernel.
6129a5f715fb, 2010/04/30
67
7 Userspace Interfaces
Table 7.1: Application Interface Timing Comparison Kernelspace (t) (t) Function ecrt_master_receive() 1.1 s 0.3 s ecrt_domain_process() < 0.1 s < 0.1 s ecrt_domain_queue() < 0.1 s < 0.1 s 1.8 s 0.2 s ecrt_master_send() Userspace (t) (t) 2.2 s 0.5 s 1.0 s 0.2 s 1.0 s 0.1 s 2.5 s 0.5 s
The test results show, that for this conguration, the userspace API causes about 1 s additional delay for each function, compared to the kernel API.
#------------------------------------------------------------------------
68
6129a5f715fb, 2010/04/30
# # Master devices . # # The MASTER <X > _DEVICE variable specifies the Ethernet device for a master # with index X . # # Specify the MAC address ( hexadecimal with colons ) of the Ethernet device to # use . Example : "00:00:08:44: ab :66" # # The broadcast address " ff : ff : ff : ff : ff : ff " has a special meaning : It tells # the master to accept the first device offered by any Ethernet driver . # # The MASTER <X > _DEVICE variables also determine , how many masters will be # created : A non - empty variable M ASTER0_D EVICE will create one master , adding # a non - empty variable MAS TER1_DEV ICE will create a second master , and so on . # MAST ER0_DEVI CE ="" # MA STER1_DE VICE ="" # # Ethernet driver modules to use for EtherCAT operation . # # Specify a non - empty list of Ethernet drivers , that shall be used for EtherCAT # operation . # # Except for the generic Ethernet driver module , the init script will try to # unload the usual Ethernet driver modules in the list and replace them with # the EtherCAT - capable ones . If a certain ( EtherCAT - capable ) driver is not # found , a warning will appear . # # Possible values : 8139 too , e100 , e1000 , r8169 , generic . # Separate multiple drivers with spaces . # # Note : The e100 , e1000 , r8169 and generic drivers are not built by default . # Enable them with the -- enable - < driver > configure switches . # DEVI CE_MODUL ES ="" # # Flags for loading kernel modules . # # This can usually be left empty . Adjust this variable , if you have problems # with module loading . # # MO DPROBE_F LAGS =" - b " #------------------------------------------------------------------------------
6129a5f715fb, 2010/04/30
69
7 Userspace Interfaces dierent ways to mark a service for starting and stopping in certain runlevels. For example, SUSE Linux provides the insserv command:
# insserv ethercat
The init script can also be used for manually starting and stopping the EtherCAT master. It has to be executed with one of the parameters start, stop, restart or status.
# /etc/init.d/ethercat restart Shutting down EtherCAT master Starting EtherCAT master done done
While a debug interface is enabled, all frames sent or received to or from the physical device are additionally forwarded to the debug interface by the corresponding master. Network interfaces can be enabled with the below command:
70
6129a5f715fb, 2010/04/30
Please note, that the frame rate can be very high. With an application connected, the debug interface can produce thousands of frames per second. Attention The socket buers needed for the operation of debug interfaces have to be allocated dynamically. Some Linux realtime extensions (like RTAI) do not allow this in realtime context!
6129a5f715fb, 2010/04/30
71
7 Userspace Interfaces
72
6129a5f715fb, 2010/04/30
8 Timing Aspects
Although EtherCATs timing is highly deterministic and therefore timing issues are rare, there are a few aspects that can (and should be) dealt with.
Between each call of an interface function, the CPU timestamp counter is read. The counter dierences are converted to s with help of the cpu_khz variable, that contains the number of increments per ms. For the actual measuring, a system with a 2.0 GHz CPU was used, that ran the above code in an RTAI thread with a period of 100 s. The measuring was repeated n = 100 times and the results were averaged. These can be seen in table 8.1. Table 8.1: Proling of an Application Cycle on a 2.0 GHz Processor Element ecrt master receive() ecrt domain process() ecrt master run() ecrt master send() Complete Cycle Mean Duration [s] Standard Deviancy [s] 8.04 0.48 0.14 0.03 0.29 0.12 2.18 0.17 10.65 0.69
6129a5f715fb, 2010/04/30
73
8 Timing Aspects It is obvious, that the functions accessing hardware make up the lions share. The ec master receive() executes the ISR of the Ethernet device, analyzes datagrams and copies their contents into the memory of the datagram objects. The ec master send() assembles a frame out of dierent datagrams and copies it to the hardware buers. Interestingly, this makes up only a quarter of the receiving time. The functions that only operate on the masters internal data structures are very fast (t < 1 s). Interestingly the runtime of ec domain process() has a small standard deviancy relative to the mean value, while this ratio is about twice as big for ec master run(): This probably results from the latter function having to execute code depending on the current state and the dierent state functions are more or less complex. For a realtime cycle makes up about 10 s, the theoretical frequency can be up to 100 kHz. For two reasons, this frequency keeps being theoretical: 1. The processor must still be able to run the operating system between the realtime cycles. 2. The EtherCAT frame must be sent and received, before the next realtime cycle begins. The determination of the bus cycle time is dicult and covered in sec. 8.0.2.
To limit Syslog output, a mechanism has been implemented, that outputs a summarized notication at maximum once a second.
74
6129a5f715fb, 2010/04/30
same as in the last cycle, because it is not erased by the domain. When the domain datagrams are queued again, the master notices, that they are already queued (and marked as sent). The master will mark them as unsent again and output a warning, that datagrams were skipped. On the mentioned 2.0 GHz system, the possible cycle frequency can be up to 25 kHz without skipped frames. This value can surely be increased by choosing faster hardware. Especially the RealTek network hardware could be replaced by a faster one. Besides, implementing a dedicated ISR for EtherCAT devices would also contribute to increasing the latency. These are two points on the authors to-do list.
6129a5f715fb, 2010/04/30
75
8 Timing Aspects
76
6129a5f715fb, 2010/04/30
9 Installation
9.1 Getting the Software
There are several ways to get the master software: 1. An ocial release (for example 1.5.0), can be downloaded from the masters website1 at the EtherLab project [1] as a tarball. 2. The most recent development revision (and moreover any other revision) can be obtained via the Mercurial [13] repository on the masters project page on SourceForge.net2 . The whole repository can be cloned with the command
hg clone http :// etherlabmaster . hg . sourceforge . net / hgweb / etherlabmaster / etherlabmaster local-dir
3. Without a local Mercurial installation, tarballs of arbitrary revisions can be downloaded via the bz2 links in the browsable repository pages3 .
The software conguration is managed with Autoconf [14] so the released versions contain a configure shell script, that has to be executed for conguration (see below). Bootstrap When downloading or cloning directly from the repository, the configure script does not yet exist. It can be created via the bootstrap.sh script in the master sources. The autoconf and automake packages are required for this.
1 2
6129a5f715fb, 2010/04/30
77
9 Installation Conguration and Build The conguration and the build process follow the below commands:
$ ./configure $ make $ make modules
The interface documentation can be viewed by pointing a browser to the le doxygenoutput/html/index.html. The functions and data structures of the application interface a covered by an own module Application Interface.
If the target kernels modules directory is not under /lib/modules, a dierent destination directory can be specied with the DESTDIR make variable. For example:
# make DESTDIR=/vol/nfs/root modules install
This command will install the compiled kernel modules to /vol/nfs/root/lib/modules, prepended by the kernel release. If the EtherCAT master shall be run as a service4 (see sec. 7.3), the init script and the syscong le have to be copied (or linked) to the appropriate locations. The below example is suitable for SUSE Linux. It may vary for other distributions.
4
Even if the EtherCAT master shall not be loaded on system startup, the use of the init script is recommended for manual (un-)loading.
78
6129a5f715fb, 2010/04/30
Table 9.1: Conguration options Description --prefix Installation prex --with-linux-dir Linux kernel sources RTAI path (only for --with-rtai-dir RTAI example) --enable-tool Build the command-line tool ethercat (see sec. 7.1). --enable-userlib Build the userspace library. --enable-eoe Enable EoE support Use CPU timestamp --enable-cycles counter. Enable this on Intel architecture to get ner timing calculation. --enable-debug-if Create a debug interface for each master Create a debug ring to --enable-debug-ring record frames --enable-8139too Build the 8139too driver --with-8139too-kernel 8139too kernel --enable-e100 Build the e100 driver --with-e100-kernel e100 kernel --enable-e1000 Enable e1000 driver e1000 kernel --with-e1000-kernel --enable-r8169 Enable r8169 driver r8169 kernel --with-r8169-kernel --enable-generic Build the generic Ethernet driver (see sec. 4.3). Option/Switch Default /opt/etherlab Use running kernel
yes
yes yes no
no no yes no no no no
If this option is not specied, the kernel version to use is extracted from the Linux kernel sources.
6129a5f715fb, 2010/04/30
79
9 Installation
# # # #
Now the syscong le /etc/sysconfig/ethercat (see sec. 7.3.2) has to be customized. The minimal customization is to set the MASTER0_DEVICE variable to the MAC address of the Ethernet device to use (or ff:ff:ff:ff:ff:ff to use the rst device oered) and selecting the driver(s) to load via the DEVICE_MODULES variable. After the basic conguration is done, the master can be started with the below command:
# /etc/init.d/ethercat start
At this time, the operation of the master can be observed by viewing the Syslog messages, which should look like the ones below. If EtherCAT slaves are connected to the masters EtherCAT device, the activity indicators should begin to ash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
EtherCAT : Master driver 1.5.0 EtherCAT : 1 master waiting for devices . EtherCAT Intel ( R ) PRO /1000 Network Driver - version 6.0.60 - k2 Copyright ( c ) 1999 -2005 Intel Corporation . PCI : Found IRQ 12 for device 0000:01:01.0 PCI : Sharing IRQ 12 with 0000:00:1 d .2 PCI : Sharing IRQ 12 with 0000:00:1 f .1 EtherCAT : Accepting device 00:0 E :0 C : DA : A2 :20 for master 0. EtherCAT : Starting master thread . ec_e1000 : ec0 : e1000_probe : Intel ( R ) PRO /1000 Network Connection ec_e1000 : ec0 : e 1 0 00 _ w at c h do g _ ta s k : NIC Link is Up 100 Mbps Full Duplex EtherCAT : Link state changed to UP . EtherCAT : 7 slave ( s ) responding . EtherCAT : Slave states : PREOP . EtherCAT : Scanning bus . EtherCAT : Bus scanning completed in 431 ms .
The EtherCAT-capable e1000 driver is loading. The master accepts the device with the address 00:0E:0C:DA:A2:20.
8 16
The master goes to idle phase, starts its state machine and begins scanning the bus.
80
6129a5f715fb, 2010/04/30
After the udev rule le is created and the EtherCAT master is restarted with /etc /init.d/ethercat restart, the device node will be automatically created with the desired rights:
# ls -l /dev/EtherCAT0 crw - rw -r - - 1 root root 252 , 0 2008 -09 -03 16:19 / dev / EtherCAT0
Now, the ethercat tool can be used (see sec. 7.1) even as a non-root user. If non-root users shall have writing access, the following udev rule can be used instead:
KERNEL ==" EtherCAT [0 -9]*" , MODE ="0664" , GROUP =" users "
6129a5f715fb, 2010/04/30
81
9 Installation
82
6129a5f715fb, 2010/04/30
Bibliography
[1] Ingenieurgemeinschaft IgH: EtherLab Open Source Toolkit for rapid realtime code generation under Linux with Simulink/RTW and EtherCAT technology. http://etherlab.org/en, 2008. [2] IEC 61158-4-12: Data-link Protocol Specication. International Electrotechnical Commission (IEC), 2005. [3] IEC 61158-6-12: Application Layer Protocol Specication. International Electrotechnical Commission (IEC), 2005. [4] GNU General Public License, Version 2. http://www.gnu.org/licenses/ gpl-2.0.html. October 15, 2008. [5] GNU Lesser General Public License, Version 2.1. http://www.gnu.org/ licenses/old-licenses/lgpl-2.1.html. October 15, 2008. [6] Linux Standard Base. http://www.linuxfoundation.org/en/LSB. August 9, 2006. [7] Wireshark. http://www.wireshark.org. 2008. [8] Hopcroft, J. E. / Ullman, J. D.: Introduction to Automata Theory, Languages and Computation. Adison-Wesley, Reading, Mass. 1979. [9] Wagner, F. / Wolstenholme, P.: State machine misunderstandings. In: IEE journal Computing and Control Engineering, 2004. [10] RTAI. The RealTime Application Interface for Linux from DIAPM. https:// www.rtai.org, 2010. [11] RT PREEMPT HOWTO. PREEMPT_HOWTO, 2010. http://rt.wiki.kernel.org/index.php/RT_
[12] Doxygen. Source code documentation generator tool. http://www.stack.nl/ ~dimitri/doxygen, 2008. [13] Mercurial SCM. http://mercurial.selenic.com, 2010. [14] Autoconf GNU Project Free Software Foundation (FSF). http://www.gnu. org/software/autoconf, 2010. [15] IEC 61800-7-304: Adjustable speed electrical power drive systems - Part 7-300: Generic interface and use of proles for power drive systems - Mapping of proles to network technologies. International Electrotechnical Commission (IEC), 2007.
6129a5f715fb, 2010/04/30
83