documentation/ethercat_doc.tex
changeset 2589 2b9c78543663
parent 2229 97c7c202fa78
child 2646 0c56c67072a6
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
     3 %  IgH EtherCAT Master Documentation
     3 %  IgH EtherCAT Master Documentation
     4 %
     4 %
     5 %  $Id$
     5 %  $Id$
     6 %
     6 %
     7 %  vi: spell spelllang=en tw=78
     7 %  vi: spell spelllang=en tw=78
     8 % 
     8 %
     9 %------------------------------------------------------------------------------
     9 %------------------------------------------------------------------------------
    10 
    10 
    11 \documentclass[a4paper,12pt,BCOR6mm,bibtotoc,idxtotoc]{scrbook}
    11 \documentclass[a4paper,12pt,BCOR6mm,bibtotoc,idxtotoc]{scrbook}
    12 
    12 
    13 \usepackage[latin1]{inputenc}
    13 \usepackage[latin1]{inputenc}
    34 {
    34 {
    35   <5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10
    35   <5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10
    36 }{}
    36 }{}
    37 
    37 
    38 \lstset{basicstyle=\ttfamily\small,numberstyle=\tiny,aboveskip=4mm,
    38 \lstset{basicstyle=\ttfamily\small,numberstyle=\tiny,aboveskip=4mm,
    39   belowskip=2mm,escapechar=`}
    39   belowskip=2mm,escapechar=`,breaklines=true}
    40 \renewcommand\lstlistlistingname{List of Listings}
    40 \renewcommand\lstlistlistingname{List of Listings}
    41 
    41 
    42 % Workaround for lstlistoflistings bug
    42 % Workaround for lstlistoflistings bug
    43 \makeatletter% --> De-TeX-FAQ
    43 \makeatletter% --> De-TeX-FAQ
    44 \renewcommand*{\lstlistoflistings}{%
    44 \renewcommand*{\lstlistoflistings}{%
    63 \newcommand{\IgH}{\raisebox{-0.7667ex}
    63 \newcommand{\IgH}{\raisebox{-0.7667ex}
    64   {\includegraphics[height=2.2ex]{images/ighsign}}}
    64   {\includegraphics[height=2.2ex]{images/ighsign}}}
    65 
    65 
    66 \rcsInfo $RCSId$
    66 \rcsInfo $RCSId$
    67 
    67 
    68 \newcommand{\masterversion}{1.5.0}
    68 \newcommand{\masterversion}{1.5.2}
    69 \newcommand{\linenum}[1]{\normalfont\textcircled{\tiny #1}}
    69 \newcommand{\linenum}[1]{\normalfont\textcircled{\tiny #1}}
    70 
    70 
    71 \makeindex
    71 \makeindex
    72 \makenomenclature
    72 \makenomenclature
    73 
    73 
   172 
   172 
   173 The list below gives a short summary of the master features.
   173 The list below gives a short summary of the master features.
   174 
   174 
   175 \begin{itemize}
   175 \begin{itemize}
   176 
   176 
   177 \item Designed as a kernel module for Linux 2.6.
   177 \item Designed as a kernel module for Linux 2.6 / 3.x.
   178 
   178 
   179 \item Implemented according to IEC 61158-12 \cite{dlspec} \cite{alspec}.
   179 \item Implemented according to IEC 61158-12 \cite{dlspec} \cite{alspec}.
   180 
   180 
   181 \item Comes with EtherCAT-capable native drivers for several common Ethernet
   181 \item Comes with EtherCAT-capable native drivers for several common Ethernet
   182 chips, as well as a generic driver for all chips supported by the Linux
   182 chips, as well as a generic driver for all chips supported by the Linux
   185   \begin{itemize}
   185   \begin{itemize}
   186 
   186 
   187   \item The native drivers operate the hardware without interrupts.
   187   \item The native drivers operate the hardware without interrupts.
   188 
   188 
   189   \item Native drivers for additional Ethernet hardware can easily be
   189   \item Native drivers for additional Ethernet hardware can easily be
   190   implemented using the common device interface (see sec.~\ref{sec:ecdev})
   190   implemented using the common device interface (see~\autoref{sec:ecdev})
   191   provided by the master module.
   191   provided by the master module.
   192 
   192 
   193   \item For any other hardware, the generic driver can be used. It uses the
   193   \item For any other hardware, the generic driver can be used. It uses the
   194   lower layers of the Linux network stack.
   194   lower layers of the Linux network stack.
   195 
   195 
   201 \item The master code supports any Linux realtime extension through its
   201 \item The master code supports any Linux realtime extension through its
   202 independent architecture.
   202 independent architecture.
   203 
   203 
   204   \begin{itemize}
   204   \begin{itemize}
   205 
   205 
   206   \item RTAI\nomenclature{RTAI}{Realtime Application Interface} \cite{rtai},
   206   \item RTAI\nomenclature{RTAI}{Realtime Application Interface} \cite{rtai}
   207   ADEOS\nomenclature{ADEOS}{Adaptive Domain Environment for Operating
   207   (including LXRT via RTDM), ADEOS\nomenclature{ADEOS}{Adaptive Domain
   208   Systems}, RT-Preempt \cite{rt-preempt}, etc.
   208   Environment for Operating Systems}, RT-Preempt \cite{rt-preempt}, Xenomai
       
   209   (including RTDM), etc.
   209 
   210 
   210   \item It runs well even without realtime extensions.
   211   \item It runs well even without realtime extensions.
   211 
   212 
   212   \end{itemize}
   213   \end{itemize}
   213 
   214 
   214 \item Common ``Application Interface'' for applications, that want to use
   215 \item Common ``Application Interface'' for applications, that want to use
   215 EtherCAT functionality (see chap.~\ref{chap:api}).
   216 EtherCAT functionality (see \autoref{chap:api}).
   216 
   217 
   217 \item \textit{Domains} are introduced, to allow grouping of process
   218 \item \textit{Domains} are introduced, to allow grouping of process
   218   data transfers with different slave groups and task periods.
   219   data transfers with different slave groups and task periods.
   219 
   220 
   220   \begin{itemize}
   221   \begin{itemize}
   237   \item Automatic reconfiguration of slaves (for example after power failure)
   238   \item Automatic reconfiguration of slaves (for example after power failure)
   238   during operation.
   239   during operation.
   239 
   240 
   240   \end{itemize}
   241   \end{itemize}
   241 
   242 
   242 \item Distributed Clocks support (see sec.~\ref{sec:dc}).
   243 \item Distributed Clocks support (see \autoref{sec:dc}).
   243 
   244 
   244   \begin{itemize}
   245   \begin{itemize}
   245 
   246 
   246   \item Configuration of the slave's DC parameters through the application
   247   \item Configuration of the slave's DC parameters through the application
   247   interface.
   248   interface.
   248 
   249 
   249   \item Synchronization (offset and drift compensation) of the distributed
   250   \item Synchronization (offset and drift compensation) of the distributed
   250   slave clocks to the reference clock.
   251   slave clocks to the reference clock.
   251 
   252 
   252   \item Optional synchronization of the reference clock to the master clock.
   253   \item Optional synchronization of the reference clock to the master clock or
       
   254   the other way round.
   253 
   255 
   254   \end{itemize}
   256   \end{itemize}
   255 
   257 
   256 \item CANopen over EtherCAT (CoE)
   258 \item CANopen over EtherCAT (CoE)
   257 
   259 
   307 
   309 
   308   \item Accessing IDNs at runtime via the the user-space library.
   310   \item Accessing IDNs at runtime via the the user-space library.
   309 
   311 
   310   \end{itemize}
   312   \end{itemize}
   311 
   313 
   312 \item Userspace command-line-tool ``ethercat'' (see sec.~\ref{sec:tool})
   314 \item Userspace command-line-tool ``ethercat'' (see \autoref{sec:tool})
   313 
   315 
   314   \begin{itemize}
   316   \begin{itemize}
   315 
   317 
   316   \item Detailed information about master, slaves, domains and bus
   318   \item Detailed information about master, slaves, domains and bus
   317   configuration.
   319   configuration.
   336 
   338 
   337   \item Master and network device configuration via sysconfig files.
   339   \item Master and network device configuration via sysconfig files.
   338 
   340 
   339   \item Init script for master control.
   341   \item Init script for master control.
   340 
   342 
       
   343   \item Service file for systemd.
       
   344 
   341   \end{itemize}
   345   \end{itemize}
   342 
   346 
   343 \item Virtual read-only network interface for monitoring and debugging
   347 \item Virtual read-only network interface for monitoring and debugging
   344   purposes.
   348   purposes.
   345 
   349 
   354 Public License (GPL \cite{gpl})\index{GPL}, version 2. Other developers, that
   358 Public License (GPL \cite{gpl})\index{GPL}, version 2. Other developers, that
   355 want to use EtherCAT with Linux systems, are invited to use the master code or
   359 want to use EtherCAT with Linux systems, are invited to use the master code or
   356 even participate on development.
   360 even participate on development.
   357 
   361 
   358 To allow static linking of userspace application against the master's
   362 To allow static linking of userspace application against the master's
   359 application interface (see chap.~\ref{chap:api}), the userspace library (see
   363 application interface (see \autoref{chap:api}), the userspace library (see
   360 sec.~\ref{sec:userlib}) is licensed under the terms and conditions of the GNU
   364 \autoref{sec:userlib}) is licensed under the terms and conditions of the GNU
   361 Lesser General Public License (LGPL \cite{lgpl})\index{LGPL}, version 2.1.
   365 Lesser General Public License (LGPL \cite{lgpl})\index{LGPL}, version 2.1.
   362 
   366 
   363 %------------------------------------------------------------------------------
   367 %------------------------------------------------------------------------------
   364 
   368 
   365 \chapter{Architecture}
   369 \chapter{Architecture}
   366 \label{chap:arch}
   370 \label{chap:arch}
   367 \index{Master!Architecture}
   371 \index{Master!Architecture}
   368 
   372 
   369 The EtherCAT master is integrated into the Linux 2.6 kernel. This was
   373 The EtherCAT master is integrated into the Linux kernel. This was an early
   370 an early design decision, which has been made for several reasons:
   374 design decision, which has been made for several reasons:
   371 
   375 
   372 \begin{itemize}
   376 \begin{itemize}
   373 
   377 
   374 \item Kernel code has significantly better realtime characteristics, i.\,e.\
   378 \item Kernel code has significantly better realtime characteristics, i.\,e.\
   375 less latency than userspace code. It was foreseeable, that a fieldbus master
   379 less latency than userspace code. It was foreseeable, that a fieldbus master
   383 anyway (through network device drivers), which is one more reason for the
   387 anyway (through network device drivers), which is one more reason for the
   384 master code being in kernelspace.
   388 master code being in kernelspace.
   385 
   389 
   386 \end{itemize}
   390 \end{itemize}
   387 
   391 
   388 Figure~\ref{fig:arch} gives a general overview of the master architecture.
   392 \autoref{fig:arch} gives a general overview of the master architecture.
   389 
   393 
   390 \begin{figure}[htbp]
   394 \begin{figure}[htbp]
   391   \centering
   395   \centering
   392   \includegraphics[width=\textwidth]{images/architecture}
   396   \includegraphics[width=\textwidth]{images/architecture}
   393   \caption{Master Architecture}
   397   \caption{Master Architecture}
   397 The components of the master environment are described below:
   401 The components of the master environment are described below:
   398 
   402 
   399 \begin{description}
   403 \begin{description}
   400 
   404 
   401 \item[Master Module]\index{Master Module} Kernel module containing one or more
   405 \item[Master Module]\index{Master Module} Kernel module containing one or more
   402 EtherCAT master instances (see sec.~\ref{sec:mastermod}), the ``Device
   406 EtherCAT master instances (see \autoref{sec:mastermod}), the ``Device
   403 Interface'' (see sec.~\ref{sec:ecdev}) and the ``Application Interface'' (see
   407 Interface'' (see \autoref{sec:ecdev}) and the ``Application Interface'' (see
   404 chap.~\ref{chap:api}).
   408 \autoref{chap:api}).
   405 
   409 
   406 \item[Device Modules]\index{Device modules} EtherCAT-capable Ethernet device
   410 \item[Device Modules]\index{Device modules} EtherCAT-capable Ethernet device
   407 driver modules\index{Device modules}, that offer their devices to the EtherCAT
   411 driver modules\index{Device modules}, that offer their devices to the EtherCAT
   408 master via the device interface (see sec.~\ref{sec:ecdev}). These modified
   412 master via the device interface (see \autoref{sec:ecdev}). These modified
   409 network drivers can handle network devices used for EtherCAT operation and
   413 network drivers can handle network devices used for EtherCAT operation and
   410 ``normal'' Ethernet devices in parallel. A master can accept a certain device
   414 ``normal'' Ethernet devices in parallel. A master can accept a certain device
   411 and then is able to send and receive EtherCAT frames. Ethernet devices
   415 and then is able to send and receive EtherCAT frames. Ethernet devices
   412 declined by the master module are connected to the kernel's network stack as
   416 declined by the master module are connected to the kernel's network stack as
   413 usual.
   417 usual.
   414 
   418 
   415 \item[Application]\index{Application} A program that uses the EtherCAT master
   419 \item[Application]\index{Application} A program that uses the EtherCAT master
   416 (usually for cyclic exchange of process data with EtherCAT slaves). These
   420 (usually for cyclic exchange of process data with EtherCAT slaves). These
   417 programs are not part of the EtherCAT master code\footnote{Although there are
   421 programs are not part of the EtherCAT master code\footnote{Although there are
   418 some examples provided in the \textit{examples/} directory.}, but have to be
   422 some examples provided in the \textit{examples/} directory.}, but have to be
   419 generated or written by the user. An application can ``request'' a master
   423 generated or written by the user. An application can request a master through
   420 through the application interface (see chap.~\ref{chap:api}). If this
   424 the application interface (see \autoref{chap:api}). If this succeeds, it has
   421 succeeds, it has the control over the master: It can provide a bus
   425 the control over the master: It can provide a bus configuration and exchange
   422 configuration and exchange process data.  Applications can be kernel modules
   426 process data.  Applications can be kernel modules (that use the kernel
   423 (that use the kernel application interface directly) or userspace programs,
   427 application interface directly) or userspace programs, that use the
   424 that use the application interface via the EtherCAT library (see
   428 application interface via the EtherCAT library (see \autoref{sec:userlib}), or
   425 sec.~\ref{sec:userlib}).
   429 the RTDM library (see~\autoref{sec:rtdm}).
   426 
   430 
   427 \end{description}
   431 \end{description}
   428 
   432 
   429 %------------------------------------------------------------------------------
   433 %------------------------------------------------------------------------------
   430 
   434 
   431 \section{Master Module}
   435 \section{Master Module}
   432 \label{sec:mastermod}
   436 \label{sec:mastermod}
   433 \index{Master module}
   437 \index{Master module}
   434 
   438 
   435 The EtherCAT master kernel module \textit{ec\_master} can contain multiple
   439 The EtherCAT master kernel module \textit{ec\_master} can contain multiple
   436 master instances. Each master waits for a certain Ethernet device identified
   440 master instances. Each master waits for certain Ethernet device(s) identified
   437 by its MAC address\index{MAC address}. These addresses have to be specified on
   441 by its MAC address(es)\index{MAC address}. These addresses have to be
   438 module loading via the \textit{main\_devices} module parameter. The number of
   442 specified on module loading via the \textit{main\_devices} (and optional:
   439 master instances to initialize is taken from the number of MAC addresses
   443 \textit{backup\_devices}) module parameter.  The number of master instances to
   440 given.
   444 initialize is taken from the number of MAC addresses given.
   441 
   445 
   442 The below command loads the master module with a single master instance that
   446 The below command loads the master module with a single master instance that
   443 waits for the Ethernet device with the MAC address
   447 waits for one Ethernet device with the MAC address
   444 \lstinline+00:0E:0C:DA:A2:20+. The master will be accessible via index $0$.
   448 \lstinline+00:0E:0C:DA:A2:20+. The master will be accessible via index $0$.
   445 
   449 
   446 \begin{lstlisting}
   450 \begin{lstlisting}
   447 # `\textbf{modprobe ec\_master main\_devices=00:0E:0C:DA:A2:20}`
   451 # `\textbf{modprobe ec\_master main\_devices=00:0E:0C:DA:A2:20}`
   448 \end{lstlisting}
   452 \end{lstlisting}
   452 \begin{lstlisting}
   456 \begin{lstlisting}
   453 # `\textbf{modprobe ec\_master main\_devices=00:0E:0C:DA:A2:20,00:e0:81:71:d5:1c}`
   457 # `\textbf{modprobe ec\_master main\_devices=00:0E:0C:DA:A2:20,00:e0:81:71:d5:1c}`
   454 \end{lstlisting}
   458 \end{lstlisting}
   455 
   459 
   456 The two masters can be addressed by their indices 0 and 1 respectively (see
   460 The two masters can be addressed by their indices 0 and 1 respectively (see
   457 figure~\ref{fig:masters}). The master index is needed for the
   461 \autoref{fig:masters}). The master index is needed for the
   458 \lstinline+ecrt_master_request()+ function of the application interface (see
   462 \lstinline+ecrt_master_request()+ function of the application interface (see
   459 chap.~\ref{chap:api}) and the \lstinline+--master+ option of the
   463 \autoref{chap:api}) and the \lstinline+--master+ option of the
   460 \textit{ethercat} command-line tool (see sec.~\ref{sec:tool}), which defaults
   464 \textit{ethercat} command-line tool (see \autoref{sec:tool}), which defaults
   461 to $0$.
   465 to $0$.
   462 
   466 
   463 \begin{figure}[htbp]
   467 \begin{figure}[htbp]
   464   \centering
   468   \centering
   465   \includegraphics[width=.5\textwidth]{images/masters}
   469   \includegraphics[width=.5\textwidth]{images/masters}
   467   \label{fig:masters}
   471   \label{fig:masters}
   468 \end{figure}
   472 \end{figure}
   469 
   473 
   470 \paragraph{Debug Level} The master module also has a parameter
   474 \paragraph{Debug Level} The master module also has a parameter
   471 \textit{debug\_level} to set the initial debug level for all masters (see
   475 \textit{debug\_level} to set the initial debug level for all masters (see
   472 also~\ref{sec:ethercat-debug}).
   476 also~\autoref{sec:ethercat-debug}).
   473 
   477 
   474 \paragraph{Init Script}
   478 \paragraph{Init Script}
   475 \index{Init script}
   479 \index{Init script}
   476 
   480 
   477 In most cases it is not necessary to load the master module and the Ethernet
   481 In most cases it is not necessary to load the master module and the Ethernet
   478 driver modules manually. There is an init script available, so the master can
   482 driver modules manually. There is an init script available, so the master can
   479 be started as a service (see sec.~\ref{sec:system}).
   483 be started as a service (see \autoref{sec:system}). For systems that are
       
   484 managed by systemd \cite{systemd}, there is also a service file available.
   480 
   485 
   481 \paragraph{Syslog}
   486 \paragraph{Syslog}
   482 
   487 
   483 The master module outputs information about its state and events to the kernel
   488 The master module outputs information about its state and events to the kernel
   484 ring buffer. These also end up in the system logs.  The above module loading
   489 ring buffer. These also end up in the system logs. The above module loading
   485 command should result in the messages below:
   490 command should result in the messages below:
   486 
   491 
   487 \begin{lstlisting}
   492 \begin{lstlisting}
   488 # `\textbf{dmesg | tail -2}`
   493 # `\textbf{dmesg | tail -2}`
   489 EtherCAT: Master driver `\masterversion`
   494 EtherCAT: Master driver `\masterversion`
   493 Jul  4 10:22:45 ethercat kernel: EtherCAT: Master driver `\masterversion`
   498 Jul  4 10:22:45 ethercat kernel: EtherCAT: Master driver `\masterversion`
   494 Jul  4 10:22:45 ethercat kernel: EtherCAT: 2 masters waiting
   499 Jul  4 10:22:45 ethercat kernel: EtherCAT: 2 masters waiting
   495                                  for devices.
   500                                  for devices.
   496 \end{lstlisting}
   501 \end{lstlisting}
   497 
   502 
   498 All EtherCAT master output is prefixed with \lstinline+EtherCAT+ which makes
   503 Master output is prefixed with \lstinline+EtherCAT+ which makes searching the
   499 searching the logs easier.
   504 logs easier.
   500 
   505 
   501 %------------------------------------------------------------------------------
   506 %------------------------------------------------------------------------------
   502 
   507 
   503 \section{Master Phases}
   508 \section{Master Phases}
   504 \index{Master phases}
   509 \index{Master phases}
   505 
   510 
   506 Every EtherCAT master provided by the master module (see
   511 Every EtherCAT master provided by the master module (see
   507 sec.~\ref{sec:mastermod}) runs through several phases (see
   512 \autoref{sec:mastermod}) runs through several phases (see
   508 fig.~\ref{fig:phases}):
   513 \autoref{fig:phases}):
   509 
   514 
   510 \begin{figure}[htbp]
   515 \begin{figure}[htbp]
   511   \centering
   516   \centering
   512   \includegraphics[width=.9\textwidth]{images/phases}
   517   \includegraphics[width=.9\textwidth]{images/phases}
   513   \caption{Master phases and transitions}
   518   \caption{Master phases and transitions}
   515 \end{figure}
   520 \end{figure}
   516 
   521 
   517 \begin{description}
   522 \begin{description}
   518 
   523 
   519 \item[Orphaned phase]\index{Orphaned phase} This mode takes effect, when the
   524 \item[Orphaned phase]\index{Orphaned phase} This mode takes effect, when the
   520 master still waits for its Ethernet device to connect. No bus communication is
   525 master still waits for its Ethernet device(s) to connect. No bus communication
   521 possible until then.
   526 is possible until then.
   522 
   527 
   523 \item[Idle phase]\index{Idle phase} takes effect when the master has accepted
   528 \item[Idle phase]\index{Idle phase} takes effect when the master has accepted
   524 an Ethernet device, but is not requested by any application yet. The master
   529 all required Ethernet devices, but is not requested by any application yet.
   525 runs its state machine (see sec.~\ref{sec:fsm-master}), that automatically
   530 The master runs its state machine (see \autoref{sec:fsm-master}), that
   526 scans the bus for slaves and executes pending operations from the userspace
   531 automatically scans the bus for slaves and executes pending operations from
   527 interface (for example SDO access). The command-line tool can be used to
   532 the userspace interface (for example SDO access). The command-line tool can be
   528 access the bus, but there is no process data exchange because of the missing
   533 used to access the bus, but there is no process data exchange because of the
   529 bus configuration.
   534 missing bus configuration.
   530 
   535 
   531 \item[Operation phase]\index{Operation phase} The master is requested by an
   536 \item[Operation phase]\index{Operation phase} The master is requested by an
   532 application that can provide a bus configuration and exchange process data.
   537 application that can provide a bus configuration and exchange process data.
   533 
   538 
   534 \end{description}
   539 \end{description}
   544 \paragraph{Process Data Image}
   549 \paragraph{Process Data Image}
   545 \index{Process data}
   550 \index{Process data}
   546 
   551 
   547 Slaves offer their inputs and outputs by presenting the master so-called
   552 Slaves offer their inputs and outputs by presenting the master so-called
   548 ``Process Data Objects'' (PDOs\index{PDO}). The available PDOs can be either
   553 ``Process Data Objects'' (PDOs\index{PDO}). The available PDOs can be either
   549 determined by reading out the slave's TXPDO and RXPDO SII categories from the
   554 determined by reading out the slave's TxPDO and RxPDO SII categories from the
   550 E$^2$PROM (in case of fixed PDOs) or by reading out the appropriate CoE
   555 E$^2$PROM (in case of fixed PDOs) or by reading out the appropriate CoE
   551 objects (see sec.~\ref{sec:coe}), if available.  The application can register
   556 objects (see \autoref{sec:coe}), if available.  The application can register
   552 the PDOs' entries for exchange during cyclic operation. The sum of all
   557 the PDOs' entries for exchange during cyclic operation. The sum of all
   553 registered PDO entries defines the ``process data image'', which is exchanged
   558 registered PDO entries defines the ``process data image'', which is exchanged
   554 via datagrams with ``logical'' memory access (like LWR, LRD or LRW) introduced
   559 via datagrams with ``logical'' memory access (like LWR, LRD or LRW) introduced
   555 in~\cite[sec.~5.4]{dlspec}.
   560 in~\cite[sec.~5.4]{dlspec}.
   556 
   561 
   603 memory as the first one, the FMMU configuration is not altered, because the
   608 memory as the first one, the FMMU configuration is not altered, because the
   604 desired memory is already part of the domain's process data image. If the
   609 desired memory is already part of the domain's process data image. If the
   605 second PDO entry would belong to another sync-manager-protected area, this
   610 second PDO entry would belong to another sync-manager-protected area, this
   606 complete area would also be included into the domains process data image.
   611 complete area would also be included into the domains process data image.
   607 
   612 
   608 Figure~\ref{fig:fmmus} gives an overview, how FMMUs are configured to map
   613 \autoref{fig:fmmus} gives an overview, how FMMUs are configured to map
   609 physical memory to logical process data images.
   614 physical memory to logical process data images.
   610 
   615 
   611 \begin{figure}[htbp]
   616 \begin{figure}[htbp]
   612   \centering
   617   \centering
   613   \includegraphics[width=\textwidth]{images/fmmus}
   618   \includegraphics[width=\textwidth]{images/fmmus}
   637 The application interface provides functions and data structures for
   642 The application interface provides functions and data structures for
   638 applications to access an EtherCAT master. The complete documentation of the
   643 applications to access an EtherCAT master. The complete documentation of the
   639 interface is included as Doxygen~\cite{doxygen} comments in the header file
   644 interface is included as Doxygen~\cite{doxygen} comments in the header file
   640 \textit{include/ecrt.h}. It can either be read directly from the file
   645 \textit{include/ecrt.h}. It can either be read directly from the file
   641 comments, or as a more comfortable HTML documentation. The HTML generation is
   646 comments, or as a more comfortable HTML documentation. The HTML generation is
   642 described in sec.~\ref{sec:gendoc}.
   647 described in \autoref{sec:gendoc}.
   643 
   648 
   644 The following sections cover a general description of the application
   649 The following sections cover a general description of the application
   645 interface.
   650 interface.
   646 
   651 
   647 Every application should use the master in two steps:
   652 Every application should use the master in two steps:
   648 
   653 
   649 \begin{description}
   654 \begin{description}
   650 
   655 
   651 \item[Configuration] The master is requested and the configuration is applied.
   656 \item[Configuration] The master is requested and the configuration is applied.
   652 For example, domains are created, slaves are configured and PDO entries are
   657 For example, domains are created, slaves are configured and PDO entries are
   653 registered (see sec.~\ref{sec:masterconfig}).
   658 registered (see \autoref{sec:masterconfig}).
   654 
   659 
   655 \item[Operation] Cyclic code is run and process data are exchanged (see
   660 \item[Operation] Cyclic code is run and process data are exchanged (see
   656 sec.~\ref{sec:cyclic}).
   661 \autoref{sec:cyclic}).
   657 
   662 
   658 \end{description}
   663 \end{description}
   659 
   664 
   660 \paragraph{Example Applications}\index{Example Applications} There are a few
   665 \paragraph{Example Applications}\index{Example Applications} There are a few
   661 example applications in the \textit{examples/} subdirectory of the master
   666 example applications in the \textit{examples/} subdirectory of the master
   665 
   670 
   666 \section{Master Configuration}
   671 \section{Master Configuration}
   667 \label{sec:masterconfig}
   672 \label{sec:masterconfig}
   668 
   673 
   669 The bus configuration is supplied via the application interface.
   674 The bus configuration is supplied via the application interface.
   670 Figure~\ref{fig:app-config} gives an overview of the objects, that can be
   675 \autoref{fig:app-config} gives an overview of the objects, that can be
   671 configured by the application.
   676 configured by the application.
   672 
   677 
   673 \begin{figure}[htbp]
   678 \begin{figure}[htbp]
   674   \centering
   679   \centering
   675   \includegraphics[width=.8\textwidth]{images/app-config}
   680   \includegraphics[width=.8\textwidth]{images/app-config}
   688 with the given vendor id and product code at the given position. If this is
   693 with the given vendor id and product code at the given position. If this is
   689 the case, the slave configuration is ``attached'' to the real slave on the bus
   694 the case, the slave configuration is ``attached'' to the real slave on the bus
   690 and the slave is configured according to the settings provided by the
   695 and the slave is configured according to the settings provided by the
   691 application. The state of a slave configuration can either be queried via the
   696 application. The state of a slave configuration can either be queried via the
   692 application interface or via the command-line tool (see
   697 application interface or via the command-line tool (see
   693 sec.~\ref{sec:ethercat-config}).
   698 \autoref{sec:ethercat-config}).
   694 
   699 
   695 \paragraph{Slave Position} The slave position has to be specified as a tuple
   700 \paragraph{Slave Position} The slave position has to be specified as a tuple
   696 of ``alias'' and ``position''. This allows addressing slaves either via an
   701 of ``alias'' and ``position''. This allows addressing slaves either via an
   697 absolute bus position, or a stored identifier called ``alias'', or a mixture
   702 absolute bus position, or a stored identifier called ``alias'', or a mixture
   698 of both. The alias is a 16-bit value stored in the slave's E$^2$PROM. It can
   703 of both. The alias is a 16-bit value stored in the slave's E$^2$PROM. It can
   699 be modified via the command-line tool (see sec.~\ref{sec:ethercat-alias}).
   704 be modified via the command-line tool (see \autoref{sec:ethercat-alias}).
   700 Table~\ref{tab:slaveposition} shows, how the values are interpreted.
   705 \autoref{tab:slaveposition} shows, how the values are interpreted.
   701 
   706 
   702 \begin{table}[htbp]
   707 \begin{table}[htbp]
   703   \centering
   708   \centering
   704   \caption{Specifying a Slave Position}
   709   \caption{Specifying a Slave Position}
   705   \label{tab:slaveposition}
   710   \label{tab:slaveposition}
   719     position after the first slave with the given alias address. \\ \hline
   724     position after the first slave with the given alias address. \\ \hline
   720 
   725 
   721   \end{tabular}
   726   \end{tabular}
   722 \end{table}
   727 \end{table}
   723 
   728 
   724 Figure~\ref{fig:attach} shows an example of how slave configurations are
   729 \autoref{fig:attach} shows an example of how slave configurations are
   725 attached. Some of the configurations were attached, while others remain
   730 attached. Some of the configurations were attached, while others remain
   726 detached. The below lists gives the reasons beginning with the top slave
   731 detached. The below lists gives the reasons beginning with the top slave
   727 configuration.
   732 configuration.
   728 
   733 
   729 \begin{figure}[htbp]
   734 \begin{figure}[htbp]
   751 \item Slave 2 is again the first slave with the alias \lstinline+0x2000+, but
   756 \item Slave 2 is again the first slave with the alias \lstinline+0x2000+, but
   752 position is now 1, so slave 3 is attached.
   757 position is now 1, so slave 3 is attached.
   753 
   758 
   754 \end{enumerate}
   759 \end{enumerate}
   755 
   760 
       
   761 If the master sources are configured with \lstinline+--enable-wildcards+, then
       
   762 \lstinline+0xffffffff+ matches every vendor ID and/or product code.
       
   763 
   756 %------------------------------------------------------------------------------
   764 %------------------------------------------------------------------------------
   757 
   765 
   758 \section{Cyclic Operation}
   766 \section{Cyclic Operation}
   759 \label{sec:cyclic}
   767 \label{sec:cyclic}
   760 
   768 
   761 
   769 
   762 To enter cyclic operation mode, the master has to be ``activated'' to
   770 To enter cyclic operation mode, the master has to be ``activated'' to
   763 calculate the process data image and apply the bus configuration for the first
   771 calculate the process data image and apply the bus configuration for the first
   764 time. After activation, the application is in charge to send and receive
   772 time. After activation, the application is in charge to send and receive
   765 frames.
   773 frames. The configuration can not be changed after activation.
   766 
   774 
   767 % TODO
   775 % TODO
   768 %
   776 %
   769 % PDO endianess
   777 % PDO endianess
   770 % Datagram injection
   778 % Datagram injection
   773 
   781 
   774 \section{VoE Handlers}
   782 \section{VoE Handlers}
   775 \label{sec:api-voe}
   783 \label{sec:api-voe}
   776 
   784 
   777 During the configuration phase, the application can create handlers for the
   785 During the configuration phase, the application can create handlers for the
   778 VoE mailbox protocol described in sec.~\ref{sec:voe}. One VoE handler always
   786 VoE mailbox protocol described in \autoref{sec:voe}. One VoE handler always
   779 belongs to a certain slave configuration, so the creation function is a method
   787 belongs to a certain slave configuration, so the creation function is a method
   780 of the slave configuration.
   788 of the slave configuration.
   781 
   789 
   782 A VoE handler manages the VoE data and the datagram used to transmit and
   790 A VoE handler manages the VoE data and the datagram used to transmit and
   783 receive VoE messages. Is contains the state machine necessary to transfer VoE
   791 receive VoE messages. Is contains the state machine necessary to transfer VoE
   805 \index{Concurrency}
   813 \index{Concurrency}
   806 
   814 
   807 In some cases, one master is used by several instances, for example when an
   815 In some cases, one master is used by several instances, for example when an
   808 application does cyclic process data exchange, and there are EoE-capable
   816 application does cyclic process data exchange, and there are EoE-capable
   809 slaves that require to exchange Ethernet data with the kernel (see
   817 slaves that require to exchange Ethernet data with the kernel (see
   810 sec.~\ref{sec:eoe}). For this reason, the master is a shared resource, and
   818 \autoref{sec:eoe}). For this reason, the master is a shared resource, and
   811 access to it has to be sequentialized. This is usually done by locking with
   819 access to it has to be sequentialized. This is usually done by locking with
   812 semaphores, or other methods to protect critical sections.
   820 semaphores, or other methods to protect critical sections.
   813 
   821 
   814 The master itself can not provide locking mechanisms, because it has no chance
   822 The master itself can not provide locking mechanisms, because it has no chance
   815 to know the appropriate kind of lock. For example if the application is in
   823 to know the appropriate kind of lock. For example if the application is in
   827   \includegraphics[width=.6\textwidth]{images/master-locks}
   835   \includegraphics[width=.6\textwidth]{images/master-locks}
   828   \caption{Concurrent Master Access}
   836   \caption{Concurrent Master Access}
   829   \label{fig:locks}
   837   \label{fig:locks}
   830 \end{figure}
   838 \end{figure}
   831 
   839 
   832 Figure~\ref{fig:locks} exemplary shows, how two processes share one master:
   840 \autoref{fig:locks} exemplary shows, how two processes share one master:
   833 The application's cyclic task uses the master for process data exchange, while
   841 The application's cyclic task uses the master for process data exchange, while
   834 the master-internal EoE process uses it to communicate with EoE-capable
   842 the master-internal EoE process uses it to communicate with EoE-capable
   835 slaves. Both have to access the bus from time to time, but the EoE process
   843 slaves. Both have to access the bus from time to time, but the EoE process
   836 does this by ``asking'' the application to do the bus access for it. In this
   844 does this by ``asking'' the application to do the bus access for it. In this
   837 way, the application can use the appropriate locking mechanism to avoid
   845 way, the application can use the appropriate locking mechanism to avoid
   838 accessing the bus at the same time. See the application interface
   846 accessing the bus at the same time. See the application interface
   839 documentation (chap.~\ref{chap:api}) for how to use these callbacks.
   847 documentation (\autoref{chap:api}) for how to use these callbacks.
   840 
   848 
   841 %------------------------------------------------------------------------------
   849 %------------------------------------------------------------------------------
   842 
   850 
   843 \section{Distributed Clocks}
   851 \section{Distributed Clocks}
   844 \label{sec:dc}
   852 \label{sec:dc}
   847 From version 1.5, the master supports EtherCAT's ``Distributed Clocks''
   855 From version 1.5, the master supports EtherCAT's ``Distributed Clocks''
   848 feature. It is possible to synchronize the slave clocks on the bus to the
   856 feature. It is possible to synchronize the slave clocks on the bus to the
   849 ``reference clock'' (which is the local clock of the first slave with DC
   857 ``reference clock'' (which is the local clock of the first slave with DC
   850 support) and to synchronize the reference clock to the ``master clock'' (which
   858 support) and to synchronize the reference clock to the ``master clock'' (which
   851 is the local clock of the master). All other clocks on the bus (after the
   859 is the local clock of the master). All other clocks on the bus (after the
   852 reference clock) are considered as ``slave clocks'' (see fig.~\ref{fig:dc}).
   860 reference clock) are considered as ``slave clocks'' (see \autoref{fig:dc}).
   853 
   861 
   854 \begin{figure}[htbp]
   862 \begin{figure}[htbp]
   855   \centering
   863   \centering
   856   \includegraphics[width=.8\textwidth]{images/dc}
   864   \includegraphics[width=.8\textwidth]{images/dc}
   857   \caption{Distributed Clocks}
   865   \caption{Distributed Clocks}
   924 latched and stored in a receive time register once the frame is received on
   932 latched and stored in a receive time register once the frame is received on
   925 the corresponding port. The master can read out the relative receive times,
   933 the corresponding port. The master can read out the relative receive times,
   926 then calculate time delays between the slaves (using its knowledge of the bus
   934 then calculate time delays between the slaves (using its knowledge of the bus
   927 topology), and finally calculate the time delays from the reference clock to
   935 topology), and finally calculate the time delays from the reference clock to
   928 each slave. These values are programmed into the slaves' transmission delay
   936 each slave. These values are programmed into the slaves' transmission delay
   929 registers. In this way, the drift compensation can reach nanosecond synchrony. 
   937 registers. In this way, the drift compensation can reach nanosecond synchrony.
   930 
   938 
   931 \paragraph{Checking Synchrony} DC-capable slaves provide the 32-bit ``System
   939 \paragraph{Checking Synchrony} DC-capable slaves provide the 32-bit ``System
   932 time difference'' register at address \lstinline+0x092c+, where the system
   940 time difference'' register at address \lstinline+0x092c+, where the system
   933 time difference of the last drift compensation is stored in nanosecond
   941 time difference of the last drift compensation is stored in nanosecond
   934 resolution and in sign-and-magnitude coding\footnote{This allows
   942 resolution and in sign-and-magnitude coding\footnote{This allows
   935 broadcast-reading all system time difference registers on the bus to get an
   943 broadcast-reading all system time difference registers on the bus to get an
   936 upper approximation}. To check for bus synchrony, the system time difference
   944 upper approximation}. To check for bus synchrony, the system time difference
   937 registers can also be cyclically read via the command-line-tool (see
   945 registers can also be cyclically read via the command-line-tool (see
   938 sec.~\ref{sec:regaccess}):
   946 \autoref{sec:regaccess}):
   939 
   947 
   940 \begin{lstlisting}
   948 \begin{lstlisting}
   941 $ `\textbf{watch -n0 "ethercat reg\_read -p4 -tsm32 0x92c"}`
   949 $ `\textbf{watch -n0 "ethercat reg\_read -p4 -tsm32 0x92c"}`
   942 \end{lstlisting}
   950 \end{lstlisting}
   943 
   951 
   963 
   971 
   964 The term \textit{device} is used as a synonym for Ethernet network interface
   972 The term \textit{device} is used as a synonym for Ethernet network interface
   965 hardware.
   973 hardware.
   966 
   974 
   967 \paragraph{Native Ethernet Device Drivers} There are native device driver
   975 \paragraph{Native Ethernet Device Drivers} There are native device driver
   968 modules (see sec.~\ref{sec:native-drivers}) that handle Ethernet hardware,
   976 modules (see \autoref{sec:native-drivers}) that handle Ethernet hardware,
   969 which a master can use to connect to an EtherCAT bus. They offer their
   977 which a master can use to connect to an EtherCAT bus. They offer their
   970 Ethernet hardware to the master module via the device interface (see
   978 Ethernet hardware to the master module via the device interface (see
   971 sec.~\ref{sec:ecdev}) and must be capable to prepare Ethernet devices either
   979 \autoref{sec:ecdev}) and must be capable to prepare Ethernet devices either
   972 for EtherCAT (realtime) operation or for ``normal'' operation using the
   980 for EtherCAT (realtime) operation or for ``normal'' operation using the
   973 kernel's network stack. The advantage of this approach is that the master can
   981 kernel's network stack. The advantage of this approach is that the master can
   974 operate nearly directly on the hardware, which allows a high performance. The
   982 operate nearly directly on the hardware, which allows a high performance. The
   975 disadvantage is, that there has to be an EtherCAT-capable version of the
   983 disadvantage is, that there has to be an EtherCAT-capable version of the
   976 original Ethernet driver.
   984 original Ethernet driver.
   977 
   985 
   978 \paragraph{Generic Ethernet Device Driver} From master version 1.5, there is a
   986 \paragraph{Generic Ethernet Device Driver} From master version 1.5, there is a
   979 generic Ethernet device driver module (see sec.~\ref{sec:generic-driver}),
   987 generic Ethernet device driver module (see \autoref{sec:generic-driver}),
   980 that uses the lower layers of the network stack to connect to the hardware.
   988 that uses the lower layers of the network stack to connect to the hardware.
   981 The advantage is, that arbitrary Ethernet hardware can be used for EtherCAT
   989 The advantage is, that arbitrary Ethernet hardware can be used for EtherCAT
   982 operation, independently of the actual hardware driver (so all Linux Ethernet
   990 operation, independently of the actual hardware driver (so all Linux Ethernet
   983 drivers are supported without modifications). The disadvantage is, that this
   991 drivers are supported without modifications). The disadvantage is, that this
   984 approach does not support realtime extensions like RTAI, because the Linux
   992 approach does not support realtime extensions like RTAI, because the Linux
  1124 Communication is highly deterministic: A frame is sent and will be received
  1132 Communication is highly deterministic: A frame is sent and will be received
  1125 again after a constant time, so there is no need to notify the driver about
  1133 again after a constant time, so there is no need to notify the driver about
  1126 frame reception: The master can instead query the hardware for received
  1134 frame reception: The master can instead query the hardware for received
  1127 frames, if it expects them to be already received.
  1135 frames, if it expects them to be already received.
  1128 
  1136 
  1129 Figure~\ref{fig:interrupt} shows two workflows for cyclic frame transmission
  1137 \autoref{fig:interrupt} shows two workflows for cyclic frame transmission
  1130 and reception with and without interrupts.
  1138 and reception with and without interrupts.
  1131 
  1139 
  1132 \begin{figure}[htbp]
  1140 \begin{figure}[htbp]
  1133   \centering
  1141   \centering
  1134   \includegraphics[width=.9\textwidth]{images/interrupt}
  1142   \includegraphics[width=.9\textwidth]{images/interrupt}
  1197 \label{sec:generic-driver}
  1205 \label{sec:generic-driver}
  1198 
  1206 
  1199 Since there are approaches to enable the complete Linux kernel for realtime
  1207 Since there are approaches to enable the complete Linux kernel for realtime
  1200 operation \cite{rt-preempt}, it is possible to operate without native
  1208 operation \cite{rt-preempt}, it is possible to operate without native
  1201 implementations of EtherCAT-capable Ethernet device drivers and use the Linux
  1209 implementations of EtherCAT-capable Ethernet device drivers and use the Linux
  1202 network stack instead. Fig.~\ref{fig:arch} shows the ``Generic Ethernet Driver
  1210 network stack instead. \autoref{fig:arch} shows the ``Generic Ethernet Driver
  1203 Module'', that connects to local Ethernet devices via the network stack. The
  1211 Module'', that connects to local Ethernet devices via the network stack. The
  1204 kernel module is named \lstinline+ec_generic+ and can be loaded after the
  1212 kernel module is named \lstinline+ec_generic+ and can be loaded after the
  1205 master module like a native EtherCAT-capable Ethernet driver.
  1213 master module like a native EtherCAT-capable Ethernet driver.
  1206 
  1214 
  1207 The generic device driver scans the network stack for interfaces, that have
  1215 The generic device driver scans the network stack for interfaces, that have
  1208 been registered by Ethernet device drivers. It offers all possible devices to
  1216 been registered by Ethernet device drivers. It offers all possible devices to
  1209 the EtherCAT master. If the master accepts a device, the generic driver
  1217 the EtherCAT master. If the master accepts a device, the generic driver
  1210 creates a packet socket (see \lstinline+man 7 packet+) with
  1218 creates a packet socket (see \lstinline+man 7 packet+) with
  1211 \lstinline+socket_type+ set to \lstinline+SOCK_RAW+, bound to that device. All
  1219 \lstinline+socket_type+ set to \lstinline+SOCK_RAW+, bound to that device. All
  1212 functions of the device interface (see sec.~\ref{sec:ecdev}) will then operate
  1220 functions of the device interface (see \autoref{sec:ecdev}) will then operate
  1213 on that socket.
  1221 on that socket.
  1214 
  1222 
  1215 Below are the advantages of this solution:
  1223 Below are the advantages of this solution:
  1216 
  1224 
  1217 \begin{itemize}
  1225 \begin{itemize}
  1229 the generic driver, because the network stack code uses dynamic memory
  1237 the generic driver, because the network stack code uses dynamic memory
  1230 allocations and other things, that could cause the system to freeze in
  1238 allocations and other things, that could cause the system to freeze in
  1231 realtime context.
  1239 realtime context.
  1232 \end{itemize}
  1240 \end{itemize}
  1233 
  1241 
       
  1242 \paragraph{Device Activation} In order to send and receive frames through a
       
  1243 socket, the Ethernet device linked to that socket has to be activated,
       
  1244 otherwise all frames will be rejected. Activation has to take place before the
       
  1245 master module is loaded and can happen in several ways:
       
  1246 
       
  1247 \begin{itemize}
       
  1248 
       
  1249 \item Ad-hoc, using the command \lstinline+ip link set dev ethX up+ (or the
       
  1250 older \lstinline+ifconfig ethX up+),
       
  1251 
       
  1252 \item Configured, depending on the distribution, for example using
       
  1253 \lstinline+ifcfg+ files (\lstinline+/etc/sysconfig/network/ifcfg-ethX+) in
       
  1254 openSUSE and others. This is the better choice, if the EtherCAT master shall
       
  1255 start at system boot time. Since the Ethernet device shall only be activated,
       
  1256 but no IP address etc.\ shall be assigned, it is enough to use
       
  1257 \lstinline+STARTMODE=auto+ as configuration.
       
  1258 
       
  1259 \end{itemize}
       
  1260 
  1234 %------------------------------------------------------------------------------
  1261 %------------------------------------------------------------------------------
  1235 
  1262 
  1236 \section{Providing Ethernet Devices}
  1263 \section{Providing Ethernet Devices}
  1237 \label{sec:providing-devices}
  1264 \label{sec:providing-devices}
  1238 
  1265 
  1239 After loading the master module, additional module(s) have to be loaded to
  1266 After loading the master module, additional module(s) have to be loaded to
  1240 offer devices to the master(s) (see sec.~\ref{sec:ecdev}). The master module
  1267 offer devices to the master(s) (see \autoref{sec:ecdev}). The master module
  1241 knows the devices to choose from the module parameters (see
  1268 knows the devices to choose from the module parameters (see
  1242 sec.~\ref{sec:mastermod}). If the init script is used to start the master, the
  1269 \autoref{sec:mastermod}). If the init script is used to start the master, the
  1243 drivers and devices to use can be specified in the sysconfig file (see
  1270 drivers and devices to use can be specified in the sysconfig file (see
  1244 sec.~\ref{sec:sysconfig}).
  1271 \autoref{sec:sysconfig}).
  1245 
  1272 
  1246 Modules offering Ethernet devices can be
  1273 Modules offering Ethernet devices can be
  1247 
  1274 
  1248 \begin{itemize}
  1275 \begin{itemize}
  1249 \item native EtherCAT-capable network driver modules (see
  1276 \item native EtherCAT-capable network driver modules (see
  1250 sec.~\ref{sec:native-drivers}) or
  1277 \autoref{sec:native-drivers}) or
  1251 \item the generic EtherCAT device driver module (see
  1278 \item the generic EtherCAT device driver module (see
  1252 sec.~\ref{sec:generic-driver}).
  1279 \autoref{sec:generic-driver}).
  1253 \end{itemize}
  1280 \end{itemize}
       
  1281 
       
  1282 %------------------------------------------------------------------------------
       
  1283 
       
  1284 \section{Redundancy}
       
  1285 \label{sec:redundancy}
       
  1286 \index{Redundancy}
       
  1287 
       
  1288 Redundant bus operation means, that there is more than one Ethernet connection
       
  1289 from the master to the slaves. Process data exchange datagrams are sent out on
       
  1290 every master link, so that the exchange is still complete, even if the bus is
       
  1291 disconnected somewhere in between.
       
  1292 
       
  1293 Prerequisite for fully redundant bus operation is, that every slave can be
       
  1294 reached by at least one master link. In this case a single connection failure
       
  1295 (i.\,e.~cable break) will never lead to incomplete process data. Double-faults
       
  1296 can not be handled with two Ethernet devices.
       
  1297 
       
  1298 Redundancy is configured with the \lstinline+--with-devices+ switch at
       
  1299 configure time (see \autoref{sec:installation}) and using the
       
  1300 \lstinline+backup_devices+ parameter of the \lstinline+ec_master+ kernel
       
  1301 module (see \autoref{sec:mastermod}) or the appropriate variable
       
  1302 \lstinline+MASTERx_BACKUP+ in the (sys-)config file (see
       
  1303 \autoref{sec:sysconfig}).
       
  1304 
       
  1305 Bus scanning is done after a topology change on any Ethernet link. The
       
  1306 application interface (see \autoref{chap:api}) and the command-line tool (see
       
  1307 \autoref{sec:tool}) both have methods to query the status of the redundant
       
  1308 operation.
  1254 
  1309 
  1255 %------------------------------------------------------------------------------
  1310 %------------------------------------------------------------------------------
  1256 
  1311 
  1257 \section{EtherCAT Device Interface}
  1312 \section{EtherCAT Device Interface}
  1258 \label{sec:ecdev}
  1313 \label{sec:ecdev}
  1259 \index{Device interface}
  1314 \index{Device interface}
  1260 
  1315 
  1261 An anticipation to the section about the master module
  1316 An anticipation to the section about the master module
  1262 (sec.~\ref{sec:mastermod}) has to be made in order to understand the way, a
  1317 (\autoref{sec:mastermod}) has to be made in order to understand the way, a
  1263 network device driver module can connect a device to a specific EtherCAT
  1318 network device driver module can connect a device to a specific EtherCAT
  1264 master.
  1319 master.
  1265 
  1320 
  1266 The master module provides a ``device interface'' for network device drivers.
  1321 The master module provides a ``device interface'' for network device drivers.
  1267 To use this interface, a network device driver module must include the header
  1322 To use this interface, a network device driver module must include the header
  1270 devices. All functions of the device interface are named with the prefix
  1325 devices. All functions of the device interface are named with the prefix
  1271 \lstinline+ecdev+.
  1326 \lstinline+ecdev+.
  1272 
  1327 
  1273 The documentation of the device interface can be found in the header file or
  1328 The documentation of the device interface can be found in the header file or
  1274 in the appropriate module of the interface documentation (see
  1329 in the appropriate module of the interface documentation (see
  1275 sec.~\ref{sec:gendoc} for generation instructions).
  1330 \autoref{sec:gendoc} for generation instructions).
  1276 
  1331 
  1277 % TODO general description of the device interface
  1332 % TODO general description of the device interface
  1278 
  1333 
  1279 %------------------------------------------------------------------------------
  1334 %------------------------------------------------------------------------------
  1280 
  1335 
  1282 \label{sec:patching}
  1337 \label{sec:patching}
  1283 \index{Network drivers}
  1338 \index{Network drivers}
  1284 
  1339 
  1285 This section will describe, how to make a standard Ethernet driver
  1340 This section will describe, how to make a standard Ethernet driver
  1286 EtherCAT-capable, using the native approach (see
  1341 EtherCAT-capable, using the native approach (see
  1287 sec.~\ref{sec:native-drivers}). Unfortunately, there is no standard procedure
  1342 \autoref{sec:native-drivers}). Unfortunately, there is no standard procedure
  1288 to enable an Ethernet driver for use with the EtherCAT master, but there are a
  1343 to enable an Ethernet driver for use with the EtherCAT master, but there are a
  1289 few common techniques.
  1344 few common techniques.
  1290 
  1345 
  1291 \begin{enumerate}
  1346 \begin{enumerate}
  1292 
  1347 
  1311 a \lstinline+net_device+ structure with a \lstinline+priv_data+ field to
  1366 a \lstinline+net_device+ structure with a \lstinline+priv_data+ field to
  1312 attach driver-dependent data to the structure. To distinguish between normal
  1367 attach driver-dependent data to the structure. To distinguish between normal
  1313 Ethernet devices and the ones used by EtherCAT masters, the private data
  1368 Ethernet devices and the ones used by EtherCAT masters, the private data
  1314 structure used by the driver could be extended by a pointer, that points to an
  1369 structure used by the driver could be extended by a pointer, that points to an
  1315 \lstinline+ec_device_t+ object returned by \lstinline+ecdev_offer()+ (see
  1370 \lstinline+ec_device_t+ object returned by \lstinline+ecdev_offer()+ (see
  1316 sec.~\ref{sec:ecdev}) if the device is used by a master and otherwise is zero.
  1371 \autoref{sec:ecdev}) if the device is used by a master and otherwise is zero.
  1317 
  1372 
  1318 The RealTek RTL-8139 Fast Ethernet driver is a ``simple'' Ethernet driver and
  1373 The RealTek RTL-8139 Fast Ethernet driver is a ``simple'' Ethernet driver and
  1319 can be taken as an example to patch new drivers. The interesting sections can
  1374 can be taken as an example to patch new drivers. The interesting sections can
  1320 be found by searching the string ``ecdev" in the file
  1375 be found by searching the string ``ecdev" in the file
  1321 \textit{devices/8139too-2.6.24-ethercat.c}.
  1376 \textit{devices/8139too-2.6.24-ethercat.c}.
  1352 This sequential approach is very simple, reflecting in only three lines of
  1407 This sequential approach is very simple, reflecting in only three lines of
  1353 code. The disadvantage is, that the master is blocked for the time it waits
  1408 code. The disadvantage is, that the master is blocked for the time it waits
  1354 for datagram reception. There is no difficulty when only one instance is using
  1409 for datagram reception. There is no difficulty when only one instance is using
  1355 the master, but if more instances want to (synchronously\footnote{At this
  1410 the master, but if more instances want to (synchronously\footnote{At this
  1356 time, synchronous master access will be adequate to show the advantages of an
  1411 time, synchronous master access will be adequate to show the advantages of an
  1357 FSM. The asynchronous approach will be discussed in sec.~\ref{sec:eoe}}) use
  1412 FSM. The asynchronous approach will be discussed in \autoref{sec:eoe}}) use
  1358 the master, it is inevitable to think about an alternative to the sequential
  1413 the master, it is inevitable to think about an alternative to the sequential
  1359 model.
  1414 model.
  1360 
  1415 
  1361 Master access has to be sequentialized for more than one instance
  1416 Master access has to be sequentialized for more than one instance
  1362 wanting to send and receive datagrams synchronously. With the present
  1417 wanting to send and receive datagrams synchronously. With the present
  1373 new ones.
  1428 new ones.
  1374 
  1429 
  1375 This approach results in all instances having to retain their state,
  1430 This approach results in all instances having to retain their state,
  1376 when giving the control back to the higher instance. It is quite
  1431 when giving the control back to the higher instance. It is quite
  1377 obvious to use a \textit{finite state machine} model in this case.
  1432 obvious to use a \textit{finite state machine} model in this case.
  1378 Section~\ref{sec:fsmtheory} will introduce some of the theory used,
  1433 \autoref{sec:fsmtheory} will introduce some of the theory used,
  1379 while the listings below show the basic approach by coding the example
  1434 while the listings below show the basic approach by coding the example
  1380 from above as a state machine:
  1435 from above as a state machine:
  1381 
  1436 
  1382 \begin{lstlisting}[gobble=2,language=C,numbers=left]
  1437 \begin{lstlisting}[gobble=2,language=C,numbers=left]
  1383   // state 1
  1438   // state 1
  1399   }
  1454   }
  1400   slave_states = EC_READ_U8(datagram->data); // process datagram
  1455   slave_states = EC_READ_U8(datagram->data); // process datagram
  1401   // state processing finished.
  1456   // state processing finished.
  1402 \end{lstlisting}
  1457 \end{lstlisting}
  1403 
  1458 
  1404 See sec.~\ref{sec:statemodel} for an introduction to the state machine
  1459 See \autoref{sec:statemodel} for an introduction to the state machine
  1405 programming concept used in the master code.
  1460 programming concept used in the master code.
  1406 
  1461 
  1407 %------------------------------------------------------------------------------
  1462 %------------------------------------------------------------------------------
  1408 
  1463 
  1409 \section{State Machine Theory}
  1464 \section{State Machine Theory}
  1430 \end{itemize}
  1485 \end{itemize}
  1431 
  1486 
  1432 The state transition function $\delta$ is often specified by a
  1487 The state transition function $\delta$ is often specified by a
  1433 \textit{state transition table}, or by a \textit{state transition
  1488 \textit{state transition table}, or by a \textit{state transition
  1434   diagram}. The transition table offers a matrix view of the state
  1489   diagram}. The transition table offers a matrix view of the state
  1435 machine behavior (see table~\ref{tab:statetrans}). The matrix rows
  1490 machine behavior (see \autoref{tab:statetrans}). The matrix rows
  1436 correspond to the states ($S = \{s_0, s_1, s_2\}$) and the columns
  1491 correspond to the states ($S = \{s_0, s_1, s_2\}$) and the columns
  1437 correspond to the input symbols ($\Gamma = \{a, b, \varepsilon\}$).
  1492 correspond to the input symbols ($\Gamma = \{a, b, \varepsilon\}$).
  1438 The table contents in a certain row $i$ and column $j$ then represent
  1493 The table contents in a certain row $i$ and column $j$ then represent
  1439 the next state (and possibly the output) for the case, that a certain
  1494 the next state (and possibly the output) for the case, that a certain
  1440 input symbol $\sigma_j$ is read in the state $s_i$.
  1495 input symbol $\sigma_j$ is read in the state $s_i$.
  1451     $s_2$ & $s_0$ & $s_0$ & $s_0$\\ \hline
  1506     $s_2$ & $s_0$ & $s_0$ & $s_0$\\ \hline
  1452   \end{tabular}
  1507   \end{tabular}
  1453 \end{table}
  1508 \end{table}
  1454 
  1509 
  1455 The state diagram for the same example looks like the one in
  1510 The state diagram for the same example looks like the one in
  1456 figure~\ref{fig:statetrans}. The states are represented as circles or
  1511 \autoref{fig:statetrans}. The states are represented as circles or
  1457 ellipses and the transitions are drawn as arrows between them. Close
  1512 ellipses and the transitions are drawn as arrows between them. Close
  1458 to a transition arrow can be the condition that must be fulfilled to
  1513 to a transition arrow can be the condition that must be fulfilled to
  1459 allow the transition. The initial state is marked by a filled black
  1514 allow the transition. The initial state is marked by a filled black
  1460 circle with an arrow pointing to the respective state.
  1515 circle with an arrow pointing to the respective state.
  1461 
  1516 
  1578 \paragraph{Mealy and Moore}
  1633 \paragraph{Mealy and Moore}
  1579 
  1634 
  1580 If a closer look is taken to the above listing, it can be seen that the
  1635 If a closer look is taken to the above listing, it can be seen that the
  1581 actions executed (the ``outputs'' of the state machine) only depend on the
  1636 actions executed (the ``outputs'' of the state machine) only depend on the
  1582 current state. This accords to the ``Moore'' model introduced in
  1637 current state. This accords to the ``Moore'' model introduced in
  1583 sec.~\ref{sec:fsmtheory}. As mentioned, the ``Mealy'' model offers a higher
  1638 \autoref{sec:fsmtheory}. As mentioned, the ``Mealy'' model offers a higher
  1584 flexibility, which can be seen in the listing below:
  1639 flexibility, which can be seen in the listing below:
  1585 
  1640 
  1586 \begin{lstlisting}[gobble=2,language=C,numbers=left]
  1641 \begin{lstlisting}[gobble=2,language=C,numbers=left]
  1587   void state7(void *priv_data) {
  1642   void state7(void *priv_data) {
  1588           if (some_condition) {
  1643           if (some_condition) {
  1627 \paragraph{Using Sub State Machines}
  1682 \paragraph{Using Sub State Machines}
  1628 
  1683 
  1629 To avoid having too much states, certain functions of the EtherCAT master
  1684 To avoid having too much states, certain functions of the EtherCAT master
  1630 state machine have been sourced out into sub state machines.  This helps to
  1685 state machine have been sourced out into sub state machines.  This helps to
  1631 encapsulate the related workflows and moreover avoids the ``state explosion''
  1686 encapsulate the related workflows and moreover avoids the ``state explosion''
  1632 phenomenon described in sec.~\ref{sec:fsmtheory}. If the master would instead
  1687 phenomenon described in \autoref{sec:fsmtheory}. If the master would instead
  1633 use one big state machine, the number of states would be a multiple of the
  1688 use one big state machine, the number of states would be a multiple of the
  1634 actual number. This would increase the level of complexity to a non-manageable
  1689 actual number. This would increase the level of complexity to a non-manageable
  1635 grade.
  1690 grade.
  1636 
  1691 
  1637 \paragraph{Executing Sub State Machines}
  1692 \paragraph{Executing Sub State Machines}
  1687 \section{The Master State Machine}
  1742 \section{The Master State Machine}
  1688 \label{sec:fsm-master}
  1743 \label{sec:fsm-master}
  1689 \index{FSM!Master}
  1744 \index{FSM!Master}
  1690 
  1745 
  1691 The master state machine is executed in the context of the master thread.
  1746 The master state machine is executed in the context of the master thread.
  1692 Figure~\ref{fig:fsm-master} shows its transition diagram. Its purposes are:
  1747 \autoref{fig:fsm-master} shows its transition diagram. Its purposes are:
  1693 
  1748 
  1694 \begin{figure}[htbp]
  1749 \begin{figure}[htbp]
  1695   \centering
  1750   \centering
  1696   \includegraphics[width=\textwidth]{graphs/fsm_master}
  1751   \includegraphics[width=\textwidth]{graphs/fsm_master}
  1697   \caption{Transition diagram of the master state machine}
  1752   \caption{Transition diagram of the master state machine}
  1718 \section{The Slave Scan State Machine}
  1773 \section{The Slave Scan State Machine}
  1719 \label{sec:fsm-scan}
  1774 \label{sec:fsm-scan}
  1720 \index{FSM!Slave Scan}
  1775 \index{FSM!Slave Scan}
  1721 
  1776 
  1722 The slave scan state machine, which can be seen in
  1777 The slave scan state machine, which can be seen in
  1723 figure~\ref{fig:fsm-slavescan}, leads through the process of reading desired
  1778 \autoref{fig:fsm-slavescan}, leads through the process of reading desired
  1724 slave information.
  1779 slave information.
  1725 
  1780 
  1726 \begin{figure}[htbp]
  1781 \begin{figure}[htbp]
  1727   \centering
  1782   \centering
  1728   \includegraphics[height=.8\textheight]{graphs/fsm_slave_scan}
  1783   \includegraphics[height=.8\textheight]{graphs/fsm_slave_scan}
  1748 image memory.
  1803 image memory.
  1749 
  1804 
  1750 \item[SII Data] The SII contents are read into the master's image.
  1805 \item[SII Data] The SII contents are read into the master's image.
  1751 
  1806 
  1752 \item[PREOP] If the slave supports CoE, it is set to PREOP state using the
  1807 \item[PREOP] If the slave supports CoE, it is set to PREOP state using the
  1753 State change FSM (see sec.~\ref{sec:fsm-change}) to enable mailbox
  1808 State change FSM (see \autoref{sec:fsm-change}) to enable mailbox
  1754 communication and read the PDO configuration via CoE.
  1809 communication and read the PDO configuration via CoE.
  1755 
  1810 
  1756 \item[PDOs] The PDOs are read via CoE (if supported) using the PDO Reading FSM
  1811 \item[PDOs] The PDOs are read via CoE (if supported) using the PDO Reading FSM
  1757 (see sec.~\ref{sec:fsm-pdo}). If this is successful, the PDO information from
  1812 (see \autoref{sec:fsm-pdo}). If this is successful, the PDO information from
  1758 the SII (if any) is overwritten.
  1813 the SII (if any) is overwritten.
  1759 
  1814 
  1760 \end{description}
  1815 \end{description}
  1761 
  1816 
  1762 %------------------------------------------------------------------------------
  1817 %------------------------------------------------------------------------------
  1764 \section{The Slave Configuration State Machine}
  1819 \section{The Slave Configuration State Machine}
  1765 \label{sec:fsm-conf}
  1820 \label{sec:fsm-conf}
  1766 \index{FSM!Slave Configuration}
  1821 \index{FSM!Slave Configuration}
  1767 
  1822 
  1768 The slave configuration state machine, which can be seen in
  1823 The slave configuration state machine, which can be seen in
  1769 figure~\ref{fig:fsm-slaveconf}, leads through the process of configuring a
  1824 \autoref{fig:fsm-slaveconf}, leads through the process of configuring a
  1770 slave and bringing it to a certain application-layer state.
  1825 slave and bringing it to a certain application-layer state.
  1771 
  1826 
  1772 \begin{figure}[htbp]
  1827 \begin{figure}[htbp]
  1773   \centering
  1828   \centering
  1774   \includegraphics[height=\textheight]{graphs/fsm_slave_conf}
  1829   \includegraphics[height=\textheight]{graphs/fsm_slave_conf}
  1792 
  1847 
  1793 \item[PREOP] The state change FSM is used to bring the slave to PREOP state.
  1848 \item[PREOP] The state change FSM is used to bring the slave to PREOP state.
  1794 If this is the requested state, the state machine is finished.
  1849 If this is the requested state, the state machine is finished.
  1795 
  1850 
  1796 \item[SDO Configuration] If there is a slave configuration attached (see
  1851 \item[SDO Configuration] If there is a slave configuration attached (see
  1797 sec.~\ref{sec:masterconfig}), and there are any SDO configurations are
  1852 \autoref{sec:masterconfig}), and there are any SDO configurations are
  1798 provided by the application, these are sent to the slave.
  1853 provided by the application, these are sent to the slave.
  1799 
  1854 
  1800 \item[PDO Configuration] The PDO configuration state machine is executed to
  1855 \item[PDO Configuration] The PDO configuration state machine is executed to
  1801 apply all necessary PDO configurations.
  1856 apply all necessary PDO configurations.
  1802 
  1857 
  1803 \item[PDO Sync Manager Configuration] If any PDO sync managers exist, they are
  1858 \item[PDO Sync Manager Configuration] If any PDO sync managers exist, they are
  1804 configured.
  1859 configured.
  1805 
  1860 
  1806 \item[FMMU Configuration] If there are FMMUs configurations supplied by the
  1861 \item[FMMU Configuration] If there are FMMUs configurations supplied by the
  1807 application (i.\,e.\ if the application registered PDO entries), they are
  1862 application (i.\,e.\ if the application registered PDO entries), they are
  1808 applied. 
  1863 applied.
  1809 
  1864 
  1810 \item[SAFEOP] The state change FSM is used to bring the slave to SAFEOP state.
  1865 \item[SAFEOP] The state change FSM is used to bring the slave to SAFEOP state.
  1811 If this is the requested state, the state machine is finished.
  1866 If this is the requested state, the state machine is finished.
  1812 
  1867 
  1813 \item[OP] The state change FSM is used to bring the slave to OP state.
  1868 \item[OP] The state change FSM is used to bring the slave to OP state.
  1820 \section{The State Change State Machine}
  1875 \section{The State Change State Machine}
  1821 \label{sec:fsm-change}
  1876 \label{sec:fsm-change}
  1822 \index{FSM!State Change}
  1877 \index{FSM!State Change}
  1823 
  1878 
  1824 The state change state machine, which can be seen in
  1879 The state change state machine, which can be seen in
  1825 figure~\ref{fig:fsm-change}, leads through the process of changing a slave's
  1880 \autoref{fig:fsm-change}, leads through the process of changing a slave's
  1826 application-layer state. This implements the states and transitions described
  1881 application-layer state. This implements the states and transitions described
  1827 in \cite[sec.~6.4.1]{alspec}.
  1882 in \cite[sec.~6.4.1]{alspec}.
  1828 
  1883 
  1829 \begin{figure}[htbp]
  1884 \begin{figure}[htbp]
  1830   \centering
  1885   \centering
  1867 
  1922 
  1868 \section{The SII State Machine}
  1923 \section{The SII State Machine}
  1869 \label{sec:fsm-sii}
  1924 \label{sec:fsm-sii}
  1870 \index{FSM!SII}
  1925 \index{FSM!SII}
  1871 
  1926 
  1872 The SII\index{SII} state machine (shown in figure~\ref{fig:fsm-sii})
  1927 The SII\index{SII} state machine (shown in \autoref{fig:fsm-sii})
  1873 implements the process of reading or writing SII data via the Slave
  1928 implements the process of reading or writing SII data via the Slave
  1874 Information Interface described in \cite[sec.~6.4]{dlspec}.
  1929 Information Interface described in \cite[sec.~6.4]{dlspec}.
  1875 
  1930 
  1876 \begin{figure}[htbp]
  1931 \begin{figure}[htbp]
  1877   \centering
  1932   \centering
  1921 \index{FSM!PDO}
  1976 \index{FSM!PDO}
  1922 
  1977 
  1923 The PDO state machines are a set of state machines that read or write the PDO
  1978 The PDO state machines are a set of state machines that read or write the PDO
  1924 assignment and the PDO mapping via the ``CoE Communication Area'' described in
  1979 assignment and the PDO mapping via the ``CoE Communication Area'' described in
  1925 \cite[sec. 5.6.7.4]{alspec}. For the object access, the CANopen over EtherCAT
  1980 \cite[sec. 5.6.7.4]{alspec}. For the object access, the CANopen over EtherCAT
  1926 access primitives are used (see sec.~\ref{sec:coe}), so the slave must support
  1981 access primitives are used (see \autoref{sec:coe}), so the slave must support
  1927 the CoE mailbox protocol.
  1982 the CoE mailbox protocol.
  1928 
  1983 
  1929 \paragraph{PDO Reading FSM} This state machine (fig.~\ref{fig:fsm-pdo-read})
  1984 \paragraph{PDO Reading FSM} This state machine (\autoref{fig:fsm-pdo-read})
  1930 has the purpose to read the complete PDO configuration of a slave. It reads
  1985 has the purpose to read the complete PDO configuration of a slave. It reads
  1931 the PDO assignment for each Sync Manager and uses the PDO Entry Reading FSM
  1986 the PDO assignment for each Sync Manager and uses the PDO Entry Reading FSM
  1932 (fig.~\ref{fig:fsm-pdo-entry-read}) to read the mapping for each assigned PDO.
  1987 (\autoref{fig:fsm-pdo-entry-read}) to read the mapping for each assigned PDO.
  1933 
  1988 
  1934 \begin{figure}[htbp]
  1989 \begin{figure}[htbp]
  1935   \centering
  1990   \centering
  1936   \includegraphics[width=.4\textwidth]{graphs/fsm_pdo_read}
  1991   \includegraphics[width=.4\textwidth]{graphs/fsm_pdo_read}
  1937   \caption{Transition Diagram of the PDO Reading State Machine}
  1992   \caption{Transition Diagram of the PDO Reading State Machine}
  1943 PDOs for this sync manager and then reads out the subindices of the SDO to get
  1998 PDOs for this sync manager and then reads out the subindices of the SDO to get
  1944 the assigned PDO's indices. When a PDO index is read, the PDO Entry Reading
  1999 the assigned PDO's indices. When a PDO index is read, the PDO Entry Reading
  1945 FSM is executed to read the PDO's mapped PDO entries.
  2000 FSM is executed to read the PDO's mapped PDO entries.
  1946 
  2001 
  1947 \paragraph{PDO Entry Reading FSM} This state machine
  2002 \paragraph{PDO Entry Reading FSM} This state machine
  1948 (fig.~\ref{fig:fsm-pdo-entry-read}) reads the PDO mapping (the PDO entries) of
  2003 (\autoref{fig:fsm-pdo-entry-read}) reads the PDO mapping (the PDO entries) of
  1949 a PDO. It reads the respective mapping SDO (\lstinline+0x1600+ --
  2004 a PDO. It reads the respective mapping SDO (\lstinline+0x1600+ --
  1950 \lstinline+0x17ff+, or \lstinline+0x1a00+ -- \lstinline+0x1bff+) for the given
  2005 \lstinline+0x17ff+, or \lstinline+0x1a00+ -- \lstinline+0x1bff+) for the given
  1951 PDO by reading first the subindex zero (number of elements) to determine the
  2006 PDO by reading first the subindex zero (number of elements) to determine the
  1952 number of mapped PDO entries. After that, each subindex is read to get the
  2007 number of mapped PDO entries. After that, each subindex is read to get the
  1953 mapped PDO entry index, subindex and bit size.
  2008 mapped PDO entry index, subindex and bit size.
  2001 slave. These interfaces are called either
  2056 slave. These interfaces are called either
  2002 
  2057 
  2003 \begin{description}
  2058 \begin{description}
  2004 
  2059 
  2005 \item[eoeXsY] for a slave without an alias address (see
  2060 \item[eoeXsY] for a slave without an alias address (see
  2006 sec.~\ref{sec:ethercat-alias}), where X is the master index and Y is the
  2061 \autoref{sec:ethercat-alias}), where X is the master index and Y is the
  2007 slave's ring position, or
  2062 slave's ring position, or
  2008 
  2063 
  2009 \item[eoeXaY] for a slave with a non-zero alias address, where X is the master
  2064 \item[eoeXaY] for a slave with a non-zero alias address, where X is the master
  2010 index and Y is the decimal alias address.
  2065 index and Y is the decimal alias address.
  2011 
  2066 
  2048 up, the passing of new socket buffers is suspended with a call to
  2103 up, the passing of new socket buffers is suspended with a call to
  2049 \lstinline+netif_stop_queue()+.
  2104 \lstinline+netif_stop_queue()+.
  2050 
  2105 
  2051 \paragraph{Creation of EoE Handlers}
  2106 \paragraph{Creation of EoE Handlers}
  2052 
  2107 
  2053 During bus scanning (see sec.~\ref{sec:fsm-scan}), the master determines the
  2108 During bus scanning (see \autoref{sec:fsm-scan}), the master determines the
  2054 supported mailbox protocols foe each slave. This is done by examining the
  2109 supported mailbox protocols foe each slave. This is done by examining the
  2055 ``Supported Mailbox Protocols'' mask field at word address 0x001C of the
  2110 ``Supported Mailbox Protocols'' mask field at word address 0x001C of the
  2056 SII\index{SII}. If bit 1 is set, the slave supports the EoE protocol. In this
  2111 SII\index{SII}. If bit 1 is set, the slave supports the EoE protocol. In this
  2057 case, an EoE handler is created for that slave.
  2112 case, an EoE handler is created for that slave.
  2058 
  2113 
  2060 \index{FSM!EoE}
  2115 \index{FSM!EoE}
  2061 
  2116 
  2062 Every EoE handler owns an EoE state machine, that is used to send frames to
  2117 Every EoE handler owns an EoE state machine, that is used to send frames to
  2063 the corresponding slave and receive frames from the it via the EoE
  2118 the corresponding slave and receive frames from the it via the EoE
  2064 communication primitives. This state machine is showed in
  2119 communication primitives. This state machine is showed in
  2065 figure~\ref{fig:fsm-eoe}.
  2120 \autoref{fig:fsm-eoe}.
  2066 
  2121 
  2067 \begin{figure}[htbp]
  2122 \begin{figure}[htbp]
  2068   \centering
  2123   \centering
  2069   \includegraphics[width=.7\textwidth]{images/fsm-eoe} % FIXME
  2124   \includegraphics[width=.7\textwidth]{images/fsm-eoe} % FIXME
  2070   \caption{Transition Diagram of the EoE State Machine}
  2125   \caption{Transition Diagram of the EoE State Machine}
  2121 \paragraph{EoE Processing}
  2176 \paragraph{EoE Processing}
  2122 
  2177 
  2123 To execute the EoE state machine of every active EoE handler, there must be a
  2178 To execute the EoE state machine of every active EoE handler, there must be a
  2124 cyclic process. The easiest solution would be to execute the EoE state
  2179 cyclic process. The easiest solution would be to execute the EoE state
  2125 machines synchronously with the master state machine (see
  2180 machines synchronously with the master state machine (see
  2126 sec.~\ref{sec:fsm-master}). This approach has the following disadvantage:
  2181 \autoref{sec:fsm-master}). This approach has the following disadvantage:
  2127 
  2182 
  2128 Only one EoE fragment could be sent or received every few cycles. This
  2183 Only one EoE fragment could be sent or received every few cycles. This
  2129 causes the data rate to be very low, because the EoE state machines are not
  2184 causes the data rate to be very low, because the EoE state machines are not
  2130 executed in the time between the application cycles. Moreover, the data rate
  2185 executed in the time between the application cycles. Moreover, the data rate
  2131 would be dependent on the period of the application task.
  2186 would be dependent on the period of the application task.
  2132 
  2187 
  2133 To overcome this problem, an own cyclic process is needed to asynchronously
  2188 To overcome this problem, an own cyclic process is needed to asynchronously
  2134 execute the EoE state machines. For that, the master owns a kernel timer, that
  2189 execute the EoE state machines. For that, the master owns a kernel timer, that
  2135 is executed each timer interrupt. This guarantees a constant bandwidth, but
  2190 is executed each timer interrupt. This guarantees a constant bandwidth, but
  2136 poses the new problem of concurrent access to the master. The locking
  2191 poses the new problem of concurrent access to the master. The locking
  2137 mechanisms needed for this are introduced in sec.~\ref{sec:concurr}.
  2192 mechanisms needed for this are introduced in \autoref{sec:concurr}.
  2138 
  2193 
  2139 \paragraph{Automatic Configuration}
  2194 \paragraph{Automatic Configuration}
  2140 
  2195 
  2141 By default, slaves are left in PREOP state, if no configuration is applied. If
  2196 By default, slaves are left in PREOP state, if no configuration is applied. If
  2142 an EoE interface link is set to ``up'', the requested slave's
  2197 an EoE interface link is set to ``up'', the requested slave's
  2164 
  2219 
  2165 The best time to apply SDO configurations is during the slave's PREOP state,
  2220 The best time to apply SDO configurations is during the slave's PREOP state,
  2166 because mailbox communication is already possible and slave's application will
  2221 because mailbox communication is already possible and slave's application will
  2167 start with updating input data in the succeeding SAFEOP state. Therefore the
  2222 start with updating input data in the succeeding SAFEOP state. Therefore the
  2168 SDO configuration has to be part of the slave configuration state machine (see
  2223 SDO configuration has to be part of the slave configuration state machine (see
  2169 sec.~\ref{sec:fsm-conf}): It is implemented via an SDO download state machine,
  2224 \autoref{sec:fsm-conf}): It is implemented via an SDO download state machine,
  2170 that is executed just before entering the slave's SAFEOP state. In this way,
  2225 that is executed just before entering the slave's SAFEOP state. In this way,
  2171 it is guaranteed that the SDO configurations are applied each time, the slave
  2226 it is guaranteed that the SDO configurations are applied each time, the slave
  2172 is reconfigured.
  2227 is reconfigured.
  2173 
  2228 
  2174 The transition diagram of the SDO Download state machine can be seen
  2229 The transition diagram of the SDO Download state machine can be seen
  2175 in figure~\ref{fig:fsm-coedown}.
  2230 in \autoref{fig:fsm-coedown}.
  2176 
  2231 
  2177 \begin{figure}[htbp]
  2232 \begin{figure}[htbp]
  2178   \centering
  2233   \centering
  2179   \includegraphics[width=.9\textwidth]{images/fsm-coedown} % FIXME
  2234   \includegraphics[width=.9\textwidth]{images/fsm-coedown} % FIXME
  2180   \caption{Transition diagram of the CoE download state machine}
  2235   \caption{Transition diagram of the CoE download state machine}
  2227 communication protocol. VoE mailbox messages are prepended by a VoE header
  2282 communication protocol. VoE mailbox messages are prepended by a VoE header
  2228 containing a 32-bit vendor ID and a 16-bit vendor-type. There are no more
  2283 containing a 32-bit vendor ID and a 16-bit vendor-type. There are no more
  2229 constraints regarding this protocol.
  2284 constraints regarding this protocol.
  2230 
  2285 
  2231 The EtherCAT master allows to create multiple VoE handlers per slave
  2286 The EtherCAT master allows to create multiple VoE handlers per slave
  2232 configuration via the application interface (see chap.~\ref{chap:api}). These
  2287 configuration via the application interface (see \autoref{chap:api}). These
  2233 handlers contain the state machine necessary for the communication via VoE.
  2288 handlers contain the state machine necessary for the communication via VoE.
  2234 
  2289 
  2235 For more information about using VoE handlers, see sec.~\ref{sec:api-voe} or
  2290 For more information about using VoE handlers, see \autoref{sec:api-voe} or
  2236 the example applications provided in the \textit{examples/} subdirectory.
  2291 the example applications provided in the \textit{examples/} subdirectory.
  2237 
  2292 
  2238 %------------------------------------------------------------------------------
  2293 %------------------------------------------------------------------------------
  2239 
  2294 
  2240 \section{Servo Profile over EtherCAT (SoE)}
  2295 \section{Servo Profile over EtherCAT (SoE)}
  2243 
  2298 
  2244 The SoE protocol implements the Service Channel layer, specified in IEC
  2299 The SoE protocol implements the Service Channel layer, specified in IEC
  2245 61800-7 \cite{soespec} via EtherCAT mailboxes.
  2300 61800-7 \cite{soespec} via EtherCAT mailboxes.
  2246 
  2301 
  2247 The SoE protocol is quite similar to the CoE protocol (see
  2302 The SoE protocol is quite similar to the CoE protocol (see
  2248 sec.~\ref{sec:coe}). Instead of SDO indices and subindices, so-called
  2303 \autoref{sec:coe}). Instead of SDO indices and subindices, so-called
  2249 identification numbers (IDNs) identify parameters.
  2304 identification numbers (IDNs) identify parameters.
  2250 
  2305 
  2251 The implementation covers the ``SCC Read'' and ``SCC Write'' primitives, each
  2306 The implementation covers the ``SCC Read'' and ``SCC Write'' primitives, each
  2252 with the ability to fragment data.
  2307 with the ability to fragment data.
  2253 
  2308 
  2254 There are several ways to use the SoE implementation:
  2309 There are several ways to use the SoE implementation:
  2255 
  2310 
  2256 \begin{itemize}
  2311 \begin{itemize}
  2257 
  2312 
  2258 \item Reading and writing IDNs via the command-line tool (see
  2313 \item Reading and writing IDNs via the command-line tool (see
  2259 sec.~\ref{sec:soeaccess}).
  2314 \autoref{sec:soeaccess}).
  2260 
  2315 
  2261 \item Storing configurations for arbitrary IDNs via the application interface
  2316 \item Storing configurations for arbitrary IDNs via the application interface
  2262 (see chap.~\ref{chap:api}, i.\,e.~\lstinline+ecrt_slave_config_idn()+). These
  2317 (see \autoref{chap:api}, i.\,e.~\lstinline+ecrt_slave_config_idn()+). These
  2263 configurations are written to the slave during configuration in PREOP state,
  2318 configurations are written to the slave during configuration in PREOP state,
  2264 before going to SAFEOP.
  2319 before going to SAFEOP.
  2265 
  2320 
  2266 \item The user-space library (see sec.~\ref{sec:userlib}), offers functions to
  2321 \item The user-space library (see \autoref{sec:userlib}), offers functions to
  2267 read/write IDNs in blocking mode (\lstinline+ecrt_master_read_idn()+,
  2322 read/write IDNs in blocking mode (\lstinline+ecrt_master_read_idn()+,
  2268 \lstinline+ecrt_master_write_idn()+).
  2323 \lstinline+ecrt_master_write_idn()+).
  2269 
  2324 
  2270 \end{itemize}
  2325 \end{itemize}
  2271 
  2326 
  2282 the master from userspace and allow a finer influence. It should be possible
  2337 the master from userspace and allow a finer influence. It should be possible
  2283 to view and to change special parameters at runtime.
  2338 to view and to change special parameters at runtime.
  2284 
  2339 
  2285 Bus visualization is another point: For development and debugging purposes it
  2340 Bus visualization is another point: For development and debugging purposes it
  2286 is necessary to show the connected slaves with a single command, for instance
  2341 is necessary to show the connected slaves with a single command, for instance
  2287 (see sec.~\ref{sec:tool}).
  2342 (see \autoref{sec:tool}).
  2288 
  2343 
  2289 The application interface has to be available in userspace, to allow userspace
  2344 The application interface has to be available in userspace, to allow userspace
  2290 programs to use EtherCAT master functionality. This was implemented via a
  2345 programs to use EtherCAT master functionality. This was implemented via a
  2291 character device and a userspace library (see sec.~\ref{sec:userlib}).
  2346 character device and a userspace library (see \autoref{sec:userlib}).
  2292 
  2347 
  2293 Another aspect is automatic startup and configuration. The master must be able
  2348 Another aspect is automatic startup and configuration. The master must be able
  2294 to automatically start up with a persistent configuration (see
  2349 to automatically start up with a persistent configuration (see
  2295 sec.~\ref{sec:system}).
  2350 \autoref{sec:system}).
  2296 
  2351 
  2297 A last thing is monitoring EtherCAT communication. For debugging purposes,
  2352 A last thing is monitoring EtherCAT communication. For debugging purposes,
  2298 there had to be a way to analyze EtherCAT datagrams. The best way would be
  2353 there had to be a way to analyze EtherCAT datagrams. The best way would be
  2299 with a popular network analyzer, like Wireshark \cite{wireshark} (the former
  2354 with a popular network analyzer, like Wireshark \cite{wireshark} or others
  2300 Ethereal) or others (see sec.~\ref{sec:debug}).
  2355 (see \autoref{sec:debug}).
  2301 
  2356 
  2302 This chapter covers all these points and introduces the interfaces and tools
  2357 This chapter covers all these points and introduces the interfaces and tools
  2303 to make all that possible.
  2358 to make all that possible.
  2304 
  2359 
  2305 %------------------------------------------------------------------------------
  2360 %------------------------------------------------------------------------------
  2316 The devices are named \textit{/dev/EtherCATx}, where $x \in \{0 \ldots n\}$ is
  2371 The devices are named \textit{/dev/EtherCATx}, where $x \in \{0 \ldots n\}$ is
  2317 the index of the master.
  2372 the index of the master.
  2318 
  2373 
  2319 \paragraph{Device Node Creation} The character device nodes are automatically
  2374 \paragraph{Device Node Creation} The character device nodes are automatically
  2320 created, if the \lstinline+udev+ Package is installed. See
  2375 created, if the \lstinline+udev+ Package is installed. See
  2321 sec.~\ref{sec:autonode} for how to install and configure it.
  2376 \autoref{sec:autonode} for how to install and configure it.
  2322 
  2377 
  2323 %------------------------------------------------------------------------------
  2378 %------------------------------------------------------------------------------
  2324 
  2379 
  2325 \subsection{Setting Alias Addresses}
  2380 \subsection{Setting Alias Addresses}
  2326 \label{sec:ethercat-alias}
  2381 \label{sec:ethercat-alias}
  2456 \begin{lstlisting}
  2511 \begin{lstlisting}
  2457 $ `\textbf{ethercat sii\_read --position 3 > sii-of-slave3.bin}`
  2512 $ `\textbf{ethercat sii\_read --position 3 > sii-of-slave3.bin}`
  2458 \end{lstlisting}
  2513 \end{lstlisting}
  2459 
  2514 
  2460 To download SII contents to a slave, writing access to the master's character
  2515 To download SII contents to a slave, writing access to the master's character
  2461 device is necessary (see sec.~\ref{sec:cdev}).
  2516 device is necessary (see \autoref{sec:cdev}).
  2462 
  2517 
  2463 \lstinputlisting[basicstyle=\ttfamily\footnotesize]{external/ethercat_sii_write}
  2518 \lstinputlisting[basicstyle=\ttfamily\footnotesize]{external/ethercat_sii_write}
  2464 
  2519 
  2465 \begin{lstlisting}
  2520 \begin{lstlisting}
  2466 # `\textbf{ethercat sii\_write --position 3 sii-of-slave3.bin}`
  2521 # `\textbf{ethercat sii\_write --position 3 sii-of-slave3.bin}`
  2517 %------------------------------------------------------------------------------
  2572 %------------------------------------------------------------------------------
  2518 
  2573 
  2519 \section{Userspace Library}
  2574 \section{Userspace Library}
  2520 \label{sec:userlib}
  2575 \label{sec:userlib}
  2521 
  2576 
  2522 The native application interface (see chap.~\ref{chap:api}) resides in
  2577 The native application interface (see \autoref{chap:api}) resides in
  2523 kernelspace and hence is only accessible from inside the kernel. To make the
  2578 kernelspace and hence is only accessible from inside the kernel. To make the
  2524 application interface available from userspace programs, a userspace library
  2579 application interface available from userspace programs, a userspace library
  2525 has been created, that can be linked to programs under the terms and
  2580 has been created, that can be linked to programs under the terms and
  2526 conditions of the LGPL, version 2 \cite{lgpl}.
  2581 conditions of the LGPL, version 2 \cite{lgpl}.
  2527 
  2582 
  2557 \end{lstlisting}
  2612 \end{lstlisting}
  2558 
  2613 
  2559 The program can be compiled and dynamically linked to the library with the
  2614 The program can be compiled and dynamically linked to the library with the
  2560 below command:
  2615 below command:
  2561 
  2616 
  2562 \begin{lstlisting}
  2617 \begin{lstlisting}[caption=Linker command for using the userspace library,
       
  2618 label=lst:linker-user]
  2563 gcc ethercat.c -o ectest -I/opt/etherlab/include \
  2619 gcc ethercat.c -o ectest -I/opt/etherlab/include \
  2564     -L/opt/etherlab/lib -lethercat \
  2620     -L/opt/etherlab/lib -lethercat \
  2565     -Wl,--rpath -Wl,/opt/etherlab/lib
  2621     -Wl,--rpath -Wl,/opt/etherlab/lib
  2566 \end{lstlisting}
  2622 \end{lstlisting}
  2567 
  2623 
  2574 
  2630 
  2575 \subsection{Implementation}
  2631 \subsection{Implementation}
  2576 \label{sec:userimp}
  2632 \label{sec:userimp}
  2577 
  2633 
  2578 Basically the kernel API was transferred into userspace via the master
  2634 Basically the kernel API was transferred into userspace via the master
  2579 character device (see chap.~\ref{chap:arch}, fig.~\ref{fig:arch} and
  2635 character device (see \autoref{chap:arch}, \autoref{fig:arch} and
  2580 sec.~\ref{sec:cdev}).
  2636 \autoref{sec:cdev}).
  2581 
  2637 
  2582 The function calls of the kernel API are mapped to the userspace via an
  2638 The function calls of the kernel API are mapped to the userspace via an
  2583 \lstinline+ioctl()+ interface. The userspace API functions share a set of
  2639 \lstinline+ioctl()+ interface. The userspace API functions share a set of
  2584 generic \lstinline+ioctl()+ calls. The kernel part of the interface calls the
  2640 generic \lstinline+ioctl()+ calls. The kernel part of the interface calls the
  2585 according API functions directly, what results in a minimum additional delay
  2641 according API functions directly, what results in a minimum additional delay
  2586 (see sec.~\ref{sec:usertiming}).
  2642 (see \autoref{sec:usertiming}).
  2587 
  2643 
  2588 For performance reasons, the actual domain process data (see
  2644 For performance reasons, the actual domain process data (see
  2589 sec.~\ref{sec:processdata}) are not copied between kernel and user memory on
  2645 \autoref{sec:processdata}) are not copied between kernel and user memory on
  2590 every access: Instead, the data are memory-mapped to the userspace
  2646 every access: Instead, the data are memory-mapped to the userspace
  2591 application. Once the master is configured and activated, the master module
  2647 application. Once the master is configured and activated, the master module
  2592 creates one process data memory area spanning all domains and maps it to
  2648 creates one process data memory area spanning all domains and maps it to
  2593 userspace, so that the application can directly access the process data. As a
  2649 userspace, so that the application can directly access the process data. As a
  2594 result, there is no additional delay when accessing process data from
  2650 result, there is no additional delay when accessing process data from
  2602 
  2658 
  2603 \subsection{Timing}
  2659 \subsection{Timing}
  2604 \label{sec:usertiming}
  2660 \label{sec:usertiming}
  2605 
  2661 
  2606 An interesting aspect is the timing of the userspace library calls compared to
  2662 An interesting aspect is the timing of the userspace library calls compared to
  2607 those of the kernel API. Table~\ref{tab:usertiming} shows the call times and
  2663 those of the kernel API. \autoref{tab:usertiming} shows the call times and
  2608 standard deviancies of typical (and time-critical) API functions measured on
  2664 standard deviancies of typical (and time-critical) API functions measured on
  2609 an Intel Pentium 4 M CPU with \unit{2.2}{\giga\hertz} and a standard 2.6.26
  2665 an Intel Pentium 4 M CPU with \unit{2.2}{\giga\hertz} and a standard 2.6.26
  2610 kernel.
  2666 kernel.
  2611 
  2667 
  2612 \begin{table}[htbp]
  2668 \begin{table}[htbp]
  2658 about \unit{1}{\micro\second} additional delay for each function, compared to
  2714 about \unit{1}{\micro\second} additional delay for each function, compared to
  2659 the kernel API.
  2715 the kernel API.
  2660 
  2716 
  2661 %------------------------------------------------------------------------------
  2717 %------------------------------------------------------------------------------
  2662 
  2718 
       
  2719 \section{RTDM Interface}
       
  2720 \label{sec:rtdm}
       
  2721 
       
  2722 When using the userspace interfaces of realtime extensions like Xenomai or
       
  2723 RTAI, the use of \textit{ioctl()} is not recommended, because it may disturb
       
  2724 realtime operation. To accomplish this, the Real-Time Device Model (RTDM)
       
  2725 \cite{rtdm} has been developed.  The master module provides an RTDM interface
       
  2726 (see \autoref{fig:arch}) in addition to the normal character device, if the
       
  2727 master sources were configured with \lstinline+--enable-rtdm+ (see
       
  2728 \autoref{sec:installation}).
       
  2729 
       
  2730 To force an application to use the RTDM interface instead of the normal
       
  2731 character device, it has to be linked with the \textit{libethercat\_rtdm}
       
  2732 library instead of \textit{libethercat}. The use of the
       
  2733 \textit{libethercat\_rtdm} is transparent, so the EtherCAT header
       
  2734 \textit{ecrt.h} with the complete API can be used as usual.
       
  2735 
       
  2736 To make the example in \autoref{lst:linker-user} use the RTDM library, the
       
  2737 linker command has to be altered as follows:
       
  2738 
       
  2739 \begin{lstlisting}
       
  2740 gcc ethercat-with-rtdm.c -o ectest -I/opt/etherlab/include \
       
  2741     -L/opt/etherlab/lib -lethercat_rtdm \
       
  2742     -Wl,--rpath -Wl,/opt/etherlab/lib
       
  2743 \end{lstlisting}
       
  2744 
       
  2745 %------------------------------------------------------------------------------
       
  2746 
  2663 \section{System Integration}
  2747 \section{System Integration}
  2664 \label{sec:system}
  2748 \label{sec:system}
  2665 
  2749 
  2666 To integrate the EtherCAT master as a service into a running system, it comes
  2750 To integrate the EtherCAT master as a service into a running system, it comes
  2667 with an init script and a sysconfig file, that are described below.
  2751 with an init script and a sysconfig file, that are described below. Modern
       
  2752 systems may be managed by systemd \cite{systemd}. Integration of the master
       
  2753 with systemd is described in \autoref{sec:systemd}.
  2668 
  2754 
  2669 \subsection{Init Script}
  2755 \subsection{Init Script}
  2670 \label{sec:init}
  2756 \label{sec:init}
  2671 \index{Init script}
  2757 \index{Init script}
  2672 
  2758 
  2673 The EtherCAT master init script conforms to the requirements of the ``Linux
  2759 The EtherCAT master init script conforms to the requirements of the ``Linux
  2674 Standard Base'' (LSB\index{LSB}, \cite{lsb}). The script is installed to
  2760 Standard Base'' (LSB\index{LSB}, \cite{lsb}). The script is installed to
  2675 \textit{etc/init.d/ethercat} below the installation prefix and has to be
  2761 \textit{etc/init.d/ethercat} below the installation prefix and has to be
  2676 copied (or better: linked) to the appropriate location (see
  2762 copied (or better: linked) to the appropriate location (see
  2677 sec.~\ref{sec:installation}), before the master can be inserted as a service.
  2763 \autoref{sec:installation}), before the master can be inserted as a service.
  2678 Please note, that the init script depends on the sysconfig file described
  2764 Please note, that the init script depends on the sysconfig file described
  2679 below.
  2765 below.
  2680 
  2766 
  2681 To provide service dependencies (i.\,e.\ which services have to be started
  2767 To provide service dependencies (i.\,e.\ which services have to be started
  2682 before others) inside the init script code, LSB defines a special comment
  2768 before others) inside the init script code, LSB defines a special comment
  2697 the file and included below:
  2783 the file and included below:
  2698 
  2784 
  2699 \lstinputlisting[numbers=left,firstline=9,basicstyle=\ttfamily\scriptsize]
  2785 \lstinputlisting[numbers=left,firstline=9,basicstyle=\ttfamily\scriptsize]
  2700     {../script/sysconfig/ethercat}
  2786     {../script/sysconfig/ethercat}
  2701 
  2787 
       
  2788 For systems managed by systemd (see \autoref{sec:systemd}), the sysconfig file
       
  2789 has moved to \lstinline+/etc/ethercat.conf+. Both versions are part of the
       
  2790 master sources and are meant to used alternatively.
       
  2791 
  2702 \subsection{Starting the Master as a Service}
  2792 \subsection{Starting the Master as a Service}
  2703 \label{sec:service}
  2793 \label{sec:service}
  2704 \index{Service}
  2794 \index{Service}
  2705 
  2795 
  2706 After the init script and the sysconfig file are placed into the right
  2796 After the init script and the sysconfig file are placed into the right
  2721   # `\textbf{/etc/init.d/ethercat restart}`
  2811   # `\textbf{/etc/init.d/ethercat restart}`
  2722   Shutting down EtherCAT master                done
  2812   Shutting down EtherCAT master                done
  2723   Starting EtherCAT master                     done
  2813   Starting EtherCAT master                     done
  2724 \end{lstlisting}
  2814 \end{lstlisting}
  2725 
  2815 
       
  2816 \subsection{Integration with systemd}
       
  2817 \label{sec:systemd}
       
  2818 \index{systemd}
       
  2819 
       
  2820 Distributions using \textit{systemd} instead of the SysV init system are using service files to describe how a service is to be maintained. \autoref{lst:service} lists the master's service file:
       
  2821 
       
  2822 \lstinputlisting[basicstyle=\ttfamily\footnotesize,caption=Service file,
       
  2823 label=lst:service]{../script/ethercat.service}
       
  2824 
       
  2825 The \textit{ethercatctl} command is used to load and unload the master and
       
  2826 network driver modules in a similar way to the former init script
       
  2827 (\autoref{sec:init}). Because it is installed into the \textit{sbin/}
       
  2828 directory, it can also be used separately:
       
  2829 
       
  2830 \begin{lstlisting}[gobble=2]
       
  2831   # `\textbf{ethercatctl start}`
       
  2832 \end{lstlisting}
       
  2833 
       
  2834 When using systemd and/or the \textit{ethercatctl} command, the master
       
  2835 configuration must be in \texttt{/etc/ethercat.conf} instead of
       
  2836 \texttt{/etc/sysconfig/ethercat}! The latter is ignored. The configuration
       
  2837 options are exactly the same.
       
  2838 
  2726 %------------------------------------------------------------------------------
  2839 %------------------------------------------------------------------------------
  2727 
  2840 
  2728 \section{Debug Interfaces}
  2841 \section{Debug Interfaces}
  2729 \label{sec:debug}
  2842 \label{sec:debug}
  2730 \index{Debug Interfaces}
  2843 \index{Debug Interfaces}
  2731 
  2844 
  2732 EtherCAT buses can always be monitored by inserting a switch between master
  2845 EtherCAT buses can always be monitored by inserting a switch between master
  2733 and slaves. This allows to connect another PC with a network monitor like
  2846 and slaves. This allows to connect another PC with a network monitor like
  2734 Wireshark~\cite{wireshark}, for example. It is also possible to listen to
  2847 Wireshark~\cite{wireshark}, for example. It is also possible to listen to
  2735 local network interfaces on the machine running the EtherCAT master directly.
  2848 local network interfaces on the machine running the EtherCAT master directly.
  2736 If the generic Ethernet driver (see sec.~\ref{sec:generic-driver}) is used,
  2849 If the generic Ethernet driver (see \autoref{sec:generic-driver}) is used,
  2737 the network monitor can directly listen on the network interface connected to
  2850 the network monitor can directly listen on the network interface connected to
  2738 the EtherCAT bus.
  2851 the EtherCAT bus.
  2739 
  2852 
  2740 When using native Ethernet drivers (see sec.~\ref{sec:native-drivers}), there
  2853 When using native Ethernet drivers (see \autoref{sec:native-drivers}), there
  2741 are no local network interfaces to listen to, because the Ethernet devices
  2854 are no local network interfaces to listen to, because the Ethernet devices
  2742 used for EtherCAT are not registered at the network stack. For that case,
  2855 used for EtherCAT are not registered at the network stack. For that case,
  2743 so-called ``debug interfaces'' are supported, which are virtual network
  2856 so-called ``debug interfaces'' are supported, which are virtual network
  2744 interfaces allowing to capture EtherCAT traffic with a network monitor (like
  2857 interfaces allowing to capture EtherCAT traffic with a network monitor (like
  2745 Wireshark or tcpdump) running on the master machine without using external
  2858 Wireshark or tcpdump) running on the master machine without using external
  2746 hardware. To use this functionality, the master sources have to be configured
  2859 hardware. To use this functionality, the master sources have to be configured
  2747 with the \lstinline+--enable-debug-if+ switch (see
  2860 with the \lstinline+--enable-debug-if+ switch (see
  2748 sec.~\ref{sec:installation}).
  2861 \autoref{sec:installation}).
  2749 
  2862 
  2750 Every EtherCAT master registers a read-only network interface per attached
  2863 Every EtherCAT master registers a read-only network interface per attached
  2751 physical Ethernet device. The network interfaces are named \textit{ecdbgmX}
  2864 physical Ethernet device. The network interfaces are named \textit{ecdbgmX}
  2752 for the main device, and \textit{ecdbgbX} for the backup device (for future
  2865 for the main device, and \textit{ecdbgbX} for the backup device, where X is
  2753 use), where X is the master index. The below listing shows a debug interface
  2866 the master index. The below listing shows a debug interface among some
  2754 among some standard network interfaces:
  2867 standard network interfaces:
  2755 
  2868 
  2756 \begin{lstlisting}
  2869 \begin{lstlisting}
  2757 # `\textbf{ip link}`
  2870 # `\textbf{ip link}`
  2758 1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
  2871 1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
  2759     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  2872     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  2818 \milli\second.
  2931 \milli\second.
  2819 
  2932 
  2820 For the actual measuring, a system with a \unit{2.0}{\giga\hertz} CPU was used,
  2933 For the actual measuring, a system with a \unit{2.0}{\giga\hertz} CPU was used,
  2821 that ran the above code in an RTAI thread with a period of
  2934 that ran the above code in an RTAI thread with a period of
  2822 \unit{100}{\micro\second}. The measuring was repeated $n = 100$ times and the
  2935 \unit{100}{\micro\second}. The measuring was repeated $n = 100$ times and the
  2823 results were averaged. These can be seen in table~\ref{tab:profile}.
  2936 results were averaged. These can be seen in \autoref{tab:profile}.
  2824 
  2937 
  2825 \begin{table}[htpb]
  2938 \begin{table}[htpb]
  2826   \centering
  2939   \centering
  2827   \caption{Profiling of an Application Cycle on a \unit{2.0}{\giga\hertz}
  2940   \caption{Profiling of an Application Cycle on a \unit{2.0}{\giga\hertz}
  2828   Processor}
  2941   Processor}
  2864 \item The processor must still be able to run the operating system between the
  2977 \item The processor must still be able to run the operating system between the
  2865 realtime cycles.
  2978 realtime cycles.
  2866 
  2979 
  2867 \item The EtherCAT frame must be sent and received, before the next realtime
  2980 \item The EtherCAT frame must be sent and received, before the next realtime
  2868 cycle begins. The determination of the bus cycle time is difficult and covered
  2981 cycle begins. The determination of the bus cycle time is difficult and covered
  2869 in sec.~\ref{sec:timing-bus}.
  2982 in \autoref{sec:timing-bus}.
  2870 
  2983 
  2871 \end{enumerate}
  2984 \end{enumerate}
  2872 
  2985 
  2873 %------------------------------------------------------------------------------
  2986 %------------------------------------------------------------------------------
  2874 
  2987 
  2955 \end{enumerate}
  3068 \end{enumerate}
  2956 
  3069 
  2957 \section{Building the Software}
  3070 \section{Building the Software}
  2958 
  3071 
  2959 After downloading a tarball or cloning the repository as described in
  3072 After downloading a tarball or cloning the repository as described in
  2960 sec.~\ref{sec:getting}, the sources have to be prepared and configured for the
  3073 \autoref{sec:getting}, the sources have to be prepared and configured for the
  2961 build process.
  3074 build process.
  2962 
  3075 
  2963 When a tarball was downloaded, it has to be extracted with the following
  3076 When a tarball was downloaded, it has to be extracted with the following
  2964 commands:
  3077 commands:
  2965 
  3078 
  2984   $ `\textbf{./configure}`
  3097   $ `\textbf{./configure}`
  2985   $ `\textbf{make}`
  3098   $ `\textbf{make}`
  2986   $ `\textbf{make modules}`
  3099   $ `\textbf{make modules}`
  2987 \end{lstlisting}
  3100 \end{lstlisting}
  2988 
  3101 
  2989 Table~\ref{tab:config} lists important configuration switches and options.
  3102 \autoref{tab:config} lists important configuration switches and options.
  2990 
  3103 
  2991 \begin{longtable}{l|p{.4\textwidth}|l}
  3104 \begin{longtable}{l|p{.4\textwidth}|l}
  2992   \caption{Configuration options}\rule[-5ex]{0mm}{0mm}
  3105   \caption{Configuration options}\rule[-5ex]{0mm}{0mm}
  2993   \label{tab:config}\\
  3106   \label{tab:config}\\
  2994 
  3107 
  3006 the EtherCAT kernel modules shall be installed. & \textit{ethercat}\\
  3119 the EtherCAT kernel modules shall be installed. & \textit{ethercat}\\
  3007 
  3120 
  3008 \hline
  3121 \hline
  3009 
  3122 
  3010 \lstinline+--enable-generic+ & Build the generic Ethernet driver (see
  3123 \lstinline+--enable-generic+ & Build the generic Ethernet driver (see
  3011 sec.~\ref{sec:generic-driver}). & yes\\
  3124 \autoref{sec:generic-driver}). & yes\\
  3012 
  3125 
  3013 \lstinline+--enable-8139too+ & Build the 8139too driver & yes\\
  3126 \lstinline+--enable-8139too+ & Build the 8139too driver & yes\\
  3014 
  3127 
  3015 \lstinline+--with-8139too-kernel+ & 8139too kernel & $\dagger$\\
  3128 \lstinline+--with-8139too-kernel+ & 8139too kernel & $\dagger$\\
  3016 
  3129 
  3030 
  3143 
  3031 \lstinline+--with-r8169-kernel+ & r8169 kernel & $\dagger$\\
  3144 \lstinline+--with-r8169-kernel+ & r8169 kernel & $\dagger$\\
  3032 
  3145 
  3033 \hline
  3146 \hline
  3034 
  3147 
  3035 \lstinline+--with-rtai-dir+ & RTAI path (only for RTAI example) & \\
  3148 \lstinline+--enable-rtdm+ & Create the RTDM interface (RTAI or Xenomai
       
  3149 directory needed, see below) & no\\
       
  3150 
       
  3151 \lstinline+--with-rtai-dir+ & RTAI path (for RTAI examples and RTDM interface)
       
  3152 & \\
       
  3153 
       
  3154 \lstinline+--with-xenomai-dir+ & Xenomai path (for Xenomai examples and RTDM
       
  3155 interface) & \\
       
  3156 
       
  3157 \lstinline+--with-devices+ & Number of Ethernet devices for redundant
       
  3158 operation ($>1$ switches redundancy on) & 1\\
  3036 
  3159 
  3037 \lstinline+--enable-debug-if+ & Create a debug interface for each master & no\\
  3160 \lstinline+--enable-debug-if+ & Create a debug interface for each master & no\\
  3038 
  3161 
  3039 \lstinline+--enable-debug-ring+ & Create a debug ring to record frames & no\\
  3162 \lstinline+--enable-debug-ring+ & Create a debug ring to record frames & no\\
  3040 
  3163 
  3047 state machine sleep between sending frames. & no\\
  3170 state machine sleep between sending frames. & no\\
  3048 
  3171 
  3049 \lstinline+--enable-regalias+ & Read alias address from register. & no\\
  3172 \lstinline+--enable-regalias+ & Read alias address from register. & no\\
  3050 
  3173 
  3051 \lstinline+--enable-tool+ & Build the command-line tool ``ethercat'' (see
  3174 \lstinline+--enable-tool+ & Build the command-line tool ``ethercat'' (see
  3052 sec.~\ref{sec:tool}). & yes\\
  3175 \autoref{sec:tool}). & yes\\
  3053 
  3176 
  3054 \lstinline+--enable-userlib+ & Build the userspace library. & yes\\
  3177 \lstinline+--enable-userlib+ & Build the userspace library. & yes\\
  3055 
  3178 
  3056 \lstinline+--enable-tty+ & Build the TTY driver. & no\\
  3179 \lstinline+--enable-tty+ & Build the TTY driver. & no\\
  3057 
  3180 
  3058 \lstinline+--enable-wildcards+ & Enable \textit{0xffffffff} to be wildcards
  3181 \lstinline+--enable-wildcards+ & Enable \textit{0xffffffff} to be wildcards
  3059 for vendor ID and product code. & no\\
  3182 for vendor ID and product code. & no\\
       
  3183 
       
  3184 \lstinline+--enable-sii-assign+ & Enable assigning SII access to the PDI layer
       
  3185 during slave configuration. & no\\
  3060 
  3186 
  3061 \hline
  3187 \hline
  3062 
  3188 
  3063 \end{longtable}
  3189 \end{longtable}
  3064 
  3190 
  3090 The below commands have to be entered as \textit{root}: The first one will
  3216 The below commands have to be entered as \textit{root}: The first one will
  3091 install the EtherCAT header, init script, sysconfig file and the userspace
  3217 install the EtherCAT header, init script, sysconfig file and the userspace
  3092 tool to the prefix path. The second one will install the kernel modules to the
  3218 tool to the prefix path. The second one will install the kernel modules to the
  3093 kernel's modules directory. The final \lstinline+depmod+ call is necessary to
  3219 kernel's modules directory. The final \lstinline+depmod+ call is necessary to
  3094 include the kernel modules into the \textit{modules.dep} file to make it
  3220 include the kernel modules into the \textit{modules.dep} file to make it
  3095 available to the \lstinline+modprobe+ command, used in the init script. 
  3221 available to the \lstinline+modprobe+ command, used in the init script.
  3096 
  3222 
  3097 \begin{lstlisting}
  3223 \begin{lstlisting}
  3098 # `\textbf{make install}`
  3224 # `\textbf{make install}`
  3099 # `\textbf{make modules\_install}`
  3225 # `\textbf{make modules\_install}`
  3100 # `\textbf{depmod}`
       
  3101 \end{lstlisting}
  3226 \end{lstlisting}
  3102 
  3227 
  3103 If the target kernel's modules directory is not under \textit{/lib/modules}, a
  3228 If the target kernel's modules directory is not under \textit{/lib/modules}, a
  3104 different destination directory can be specified with the \lstinline+DESTDIR+
  3229 different destination directory can be specified with the \lstinline+DESTDIR+
  3105 make variable. For example:
  3230 make variable. For example:
  3111 This command will install the compiled kernel modules to
  3236 This command will install the compiled kernel modules to
  3112 \textit{/vol/nfs/root/lib/modules}, prepended by the kernel release.
  3237 \textit{/vol/nfs/root/lib/modules}, prepended by the kernel release.
  3113 
  3238 
  3114 If the EtherCAT master shall be run as a service\footnote{Even if the EtherCAT
  3239 If the EtherCAT master shall be run as a service\footnote{Even if the EtherCAT
  3115 master shall not be loaded on system startup, the use of the init script is
  3240 master shall not be loaded on system startup, the use of the init script is
  3116 recommended for manual (un-)loading.} (see sec.~\ref{sec:system}), the init
  3241 recommended for manual (un-)loading.} (see \autoref{sec:system}), the init
  3117 script and the sysconfig file have to be copied (or linked) to the appropriate
  3242 script and the sysconfig file (or the systemd service file, respectively) have
  3118 locations. The below example is suitable for SUSE Linux. It may vary for other
  3243 to be copied (or linked) to the appropriate locations. The below example is
  3119 distributions.
  3244 suitable for SUSE Linux. It may vary for other distributions.
  3120 
  3245 
  3121 % FIXME relative ln -s?
  3246 % FIXME relative ln -s?
  3122 \begin{lstlisting}
  3247 \begin{lstlisting}
  3123 # `\textbf{cd /opt/etherlab}`
  3248 # `\textbf{cd /opt/etherlab}`
  3124 # `\textbf{cp etc/sysconfig/ethercat /etc/sysconfig/}`
  3249 # `\textbf{cp etc/sysconfig/ethercat /etc/sysconfig/}`
  3125 # `\textbf{ln -s etc/init.d/ethercat /etc/init.d/}`
  3250 # `\textbf{ln -s etc/init.d/ethercat /etc/init.d/}`
  3126 # `\textbf{insserv ethercat}`
  3251 # `\textbf{insserv ethercat}`
  3127 \end{lstlisting}
  3252 \end{lstlisting}
  3128 
  3253 
  3129 Now the sysconfig file \texttt{/etc/sysconfig/ethercat} (see
  3254 Now the sysconfig file \texttt{/etc/sysconfig/ethercat} (see
  3130 sec.~\ref{sec:sysconfig}) has to be customized. The minimal customization is
  3255 \autoref{sec:sysconfig}), or the configuration file
  3131 to set the \lstinline+MASTER0_DEVICE+ variable to the MAC address of the
  3256 \textit{/etc/ethercat.conf}, if using systemd, has to be customized. The
  3132 Ethernet device to use (or \lstinline+ff:ff:ff:ff:ff:ff+ to use the first
  3257 minimal customization is to set the \lstinline+MASTER0_DEVICE+ variable to the
  3133 device offered) and selecting the driver(s) to load via the
  3258 MAC address of the Ethernet device to use (or \lstinline+ff:ff:ff:ff:ff:ff+ to
       
  3259 use the first device offered) and selecting the driver(s) to load via the
  3134 \lstinline+DEVICE_MODULES+ variable.
  3260 \lstinline+DEVICE_MODULES+ variable.
  3135 
  3261 
  3136 After the basic configuration is done, the master can be started with the
  3262 After the basic configuration is done, the master can be started with the
  3137 below command:
  3263 below command:
  3138 
  3264 
  3139 \begin{lstlisting}
  3265 \begin{lstlisting}
  3140 # `\textbf{/etc/init.d/ethercat start}`
  3266 # `\textbf{/etc/init.d/ethercat start}`
       
  3267 \end{lstlisting}
       
  3268 
       
  3269 When using systemd, the following command can be used alternatively:
       
  3270 
       
  3271 \begin{lstlisting}
       
  3272 # `\textbf{ethercatctl start}`
  3141 \end{lstlisting}
  3273 \end{lstlisting}
  3142 
  3274 
  3143 At this time, the operation of the master can be observed by viewing the
  3275 At this time, the operation of the master can be observed by viewing the
  3144 Syslog\index{Syslog} messages, which should look like the ones below. If
  3276 Syslog\index{Syslog} messages, which should look like the ones below. If
  3145 EtherCAT slaves are connected to the master's EtherCAT device, the activity
  3277 EtherCAT slaves are connected to the master's EtherCAT device, the activity
  3181 \end{description}
  3313 \end{description}
  3182 
  3314 
  3183 \section{Automatic Device Node Creation}
  3315 \section{Automatic Device Node Creation}
  3184 \label{sec:autonode}
  3316 \label{sec:autonode}
  3185 
  3317 
  3186 The \lstinline+ethercat+ command-line tool (see sec.~\ref{sec:tool})
  3318 The \lstinline+ethercat+ command-line tool (see \autoref{sec:tool})
  3187 communicates with the master via a character device. The corresponding device
  3319 communicates with the master via a character device. The corresponding device
  3188 nodes are created automatically, if the udev daemon is running.  Note, that on
  3320 nodes are created automatically, if the udev daemon is running.  Note, that on
  3189 some distributions, the \lstinline+udev+ package is not installed by default.
  3321 some distributions, the \lstinline+udev+ package is not installed by default.
  3190 
  3322 
  3191 The device nodes will be created with mode \lstinline+0660+ and group
  3323 The device nodes will be created with mode \lstinline+0660+ and group
  3204 \begin{lstlisting}
  3336 \begin{lstlisting}
  3205 # `\textbf{ls -l /dev/EtherCAT0}`
  3337 # `\textbf{ls -l /dev/EtherCAT0}`
  3206 crw-rw-r--  1 root root 252, 0 2008-09-03 16:19 /dev/EtherCAT0
  3338 crw-rw-r--  1 root root 252, 0 2008-09-03 16:19 /dev/EtherCAT0
  3207 \end{lstlisting}
  3339 \end{lstlisting}
  3208 
  3340 
  3209 Now, the \lstinline+ethercat+ tool can be used (see sec.~\ref{sec:tool}) even
  3341 Now, the \lstinline+ethercat+ tool can be used (see \autoref{sec:tool}) even
  3210 as a non-root user.
  3342 as a non-root user.
  3211 
  3343 
  3212 If non-root users shall have writing access, the following udev rule can be
  3344 If non-root users shall have writing access, the following udev rule can be
  3213 used instead:
  3345 used instead:
  3214 
  3346 
  3237 \url{http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html}. October~15,
  3369 \url{http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html}. October~15,
  3238 2008.
  3370 2008.
  3239 
  3371 
  3240 \bibitem{lsb} Linux Standard Base.
  3372 \bibitem{lsb} Linux Standard Base.
  3241 \url{http://www.linuxfoundation.org/en/LSB}. August~9, 2006.
  3373 \url{http://www.linuxfoundation.org/en/LSB}. August~9, 2006.
       
  3374 
       
  3375 \bibitem{systemd} systemd System and Service Manager
       
  3376 \url{http://freedesktop.org/wiki/Software/systemd}. January~18, 2013.
  3242 
  3377 
  3243 \bibitem{wireshark} Wireshark. \url{http://www.wireshark.org}. 2008.
  3378 \bibitem{wireshark} Wireshark. \url{http://www.wireshark.org}. 2008.
  3244 
  3379 
  3245 \bibitem{automata} {\it Hopcroft, J.\,E.\ / Ullman, J.\,D.}: Introduction to
  3380 \bibitem{automata} {\it Hopcroft, J.\,E.\ / Ullman, J.\,D.}: Introduction to
  3246 Automata Theory, Languages and Computation. Adison-Wesley, Reading,
  3381 Automata Theory, Languages and Computation. Adison-Wesley, Reading,
  3267 \bibitem{soespec} IEC 61800-7-304: Adjustable speed electrical power drive
  3402 \bibitem{soespec} IEC 61800-7-304: Adjustable speed electrical power drive
  3268 systems - Part 7-300: Generic interface and use of profiles for power drive
  3403 systems - Part 7-300: Generic interface and use of profiles for power drive
  3269 systems - Mapping of profiles to network technologies. International
  3404 systems - Mapping of profiles to network technologies. International
  3270 Electrotechnical Commission (IEC), 2007.
  3405 Electrotechnical Commission (IEC), 2007.
  3271 
  3406 
       
  3407 \bibitem{rtdm} {\it J. Kiszka}: The Real-Time Driver Model and First
       
  3408 Applications.
       
  3409 \url{http://svn.gna.org/svn/xenomai/tags/v2.4.0/doc/nodist/pdf/RTDM-and-Applications.pdf},
       
  3410 2013.
       
  3411 
  3272 \end{thebibliography}
  3412 \end{thebibliography}
  3273 
  3413 
  3274 \printnomenclature
  3414 \printnomenclature
  3275 \addcontentsline{toc}{chapter}{\nomname}
  3415 \addcontentsline{toc}{chapter}{\nomname}
  3276 \markleft{\nomname}
  3416 \markleft{\nomname}