nicdrv.c File Reference

EtherCAT RAW socket driver. More...

#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <netpacket/packet.h>
#include <pthread.h>
#include <stdlib.h>
#include "ethercattype.h"
#include "nicdrv.h"
Include dependency graph for nicdrv.c:

Go to the source code of this file.

Data Structures

struct  ec_stackT
 pointer structure to Tx and Rx stacks More...

Defines

#define RX_PRIM   priMAC[1]
 second MAC word is used for identification
#define RX_SEC   secMAC[1]
 second MAC word is used for identification

Enumerations

enum  { ECT_RED_NONE, ECT_RED_DOUBLE }
 

Redundancy modes.

More...

Functions

int ec_setupnic (const char *ifname, int secondary)
 Basic setup to connect NIC to socket.
int ec_closenic (void)
 Close sockets used.
void ec_setupheader (void *p)
 Fill buffer with ethernet header structure.
uint8 ec_getindex (void)
 Get new frame identifier index and allocate corresponding rx buffer.
void ec_setbufstat (uint8 idx, int bufstat)
 Set rx buffer status.
int ec_outframe (uint8 idx, int stacknumber)
 Transmit buffer over socket (non blocking).
int ec_outframe_red (uint8 idx)
 Transmit buffer over socket (non blocking).
int ec_inframe (uint8 idx, int stacknumber)
 Non blocking receive frame function.
int ec_waitinframe (uint8 idx, int timeout)
 Blocking receive frame function.
int ec_srconfirm (uint8 idx, int timeout)
 Blocking send and recieve frame function.

Variables

ec_bufT ec_rxbuf [EC_MAXBUF]
 primary rx buffers
int ec_rxbufstat [EC_MAXBUF]
 primary rx buffer status
ec_bufT ec_txbuf [EC_MAXBUF]
 transmit buffers
int ec_txbuflength [EC_MAXBUF]
 transmit buffer lenghts
ec_bufT ec_txbuf2
 temporary tx buffer
int ec_txbuflength2
 temporary tx buffer length
int sockhandle = -1
 primary socket handle
int sockhandle2 = -1
 secondary socket handle
int ec_incnt
 global rx packet counter, counts only EtherCAT packets
int ec_errcnt
 global error packet counter, ie non EtherCAT packets
int ec_redstate
 current redundancy state
int hlp_txtime
 global helper var to count time used in tx socket
int hlp_rxtime
 global helper var ri count time used in rx socket
const uint16 priMAC [3] = { 0x0101, 0x0101, 0x0101 }
 Primary source MAC address used for EtherCAT.
const uint16 secMAC [3] = { 0x0404, 0x0404, 0x0404 }
 Secondary source MAC address used for EtherCAT.
pthread_mutex_t ec_getindex_mutex = PTHREAD_MUTEX_INITIALIZER
pthread_mutex_t ec_tx_mutex = PTHREAD_MUTEX_INITIALIZER
pthread_mutex_t ec_rx_mutex = PTHREAD_MUTEX_INITIALIZER

Detailed Description

EtherCAT RAW socket driver.

Low level interface functions to send and receive EtherCAT packets. EtherCAT has the property that packets are only send by the master, and the send packets allways return in the receive buffer. There can be multiple packets "on the wire" before they return. To combine the received packets with the original send packets a buffer system is installed. The identifier is put in the index item of the EtherCAT header. The index is stored and compared when a frame is recieved. If there is a match the packet can be combined with the transmit packet and returned to the higher level function.

The socket layer can exhibit a reversal in the packet order (rare). If the Tx order is A-B-C the return order could be A-C-B. The indexed buffer will reorder the packets automatically.

The "redundant" option will configure two sockets and two NIC interfaces. Slaves are connected to both interfaces, one on the IN port and one on the OUT port. Packets are send via both interfaces. Any one of the connections (also an interconnect) can be removed and the slaves are still serviced with packets. The software layer will detect the possible failure modes and compensate. If needed the packets from interface A are resend through interface B. This layer if fully transparent for the higher layers.

Definition in file nicdrv.c.


Define Documentation

#define RX_PRIM   priMAC[1]

second MAC word is used for identification

Definition at line 176 of file nicdrv.c.

#define RX_SEC   secMAC[1]

second MAC word is used for identification

Definition at line 178 of file nicdrv.c.


Enumeration Type Documentation

anonymous enum

Redundancy modes.

Enumerator:
ECT_RED_NONE 

No redundancy, single NIC mode.

ECT_RED_DOUBLE 

Double redundant NIC connecetion.

Definition at line 86 of file nicdrv.c.


Function Documentation

int ec_closenic ( void   ) 

Close sockets used.

Returns:
0

Definition at line 255 of file nicdrv.c.

Here is the caller graph for this function:

uint8 ec_getindex ( void   ) 

Get new frame identifier index and allocate corresponding rx buffer.

Returns:
new index.

Definition at line 284 of file nicdrv.c.

Here is the caller graph for this function:

int ec_inframe ( uint8  idx,
int  stacknumber 
)

Non blocking receive frame function.

Uses RX buffer and index to combine read frame with transmitted frame. To compensate for received frames that are out-of-order all frames are stored in their respective indexed buffer. If a frame was placed in the buffer previously, the function retreives it from that buffer index without calling ec_recvpkt. If the requested index is not already in the buffer it calls ec_recvpkt to fetch it. There are three options now, 1 no frame read, so exit. 2 frame read but other than requested index, store in buffer and exit. 3 frame read with matching index, store in buffer, set completed flag in buffer status and exit.

Parameters:
[in] idx = requested index of frame
[in] stacknumber = 0=primary 1=secondary stack
Returns:
Workcounter if a frame is found with corresponding index, otherwise EC_NOFRAME or EC_OTHERFRAME.

Definition at line 411 of file nicdrv.c.

int ec_outframe ( uint8  idx,
int  stacknumber 
)

Transmit buffer over socket (non blocking).

Parameters:
[in] idx = index in tx buffer array
[in] stacknumber = 0=Primary 1=Secondary stack
Returns:
socket send result

Definition at line 332 of file nicdrv.c.

Here is the caller graph for this function:

int ec_outframe_red ( uint8  idx  ) 

Transmit buffer over socket (non blocking).

Parameters:
[in] idx = index in tx buffer array
Returns:
socket send result

Definition at line 349 of file nicdrv.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void ec_setbufstat ( uint8  idx,
int  bufstat 
)

Set rx buffer status.

Parameters:
[in] idx = index in buffer array
[in] bufstat = status to set

Definition at line 321 of file nicdrv.c.

Here is the caller graph for this function:

void ec_setupheader ( void *  p  ) 

Fill buffer with ethernet header structure.

Destination MAC is allways broadcast. Ethertype is allways ETH_P_ECAT.

Parameters:
[out] p = buffer

Definition at line 268 of file nicdrv.c.

Here is the caller graph for this function:

int ec_setupnic ( const char *  ifname,
int  secondary 
)

Basic setup to connect NIC to socket.

Parameters:
[in] ifname = Name of NIC device, f.e. "eth0"
[in] secondary = if >0 then use secondary stack instead of primary
Returns:
>0 if succeeded

Definition at line 189 of file nicdrv.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int ec_srconfirm ( uint8  idx,
int  timeout 
)

Blocking send and recieve frame function.

Used for non processdata frames. A datagram is build into a frame and transmitted via this function. It waits for an answer and returns the workcounter. The function retries if time is left and the result is WKC=0 or no frame received.

The function calls ec_outframe_red() and ec_waitinframe_red().

Parameters:
[in] idx = index of frame
[in] timeout = timeout in us
Returns:
Workcounter or EC_NOFRAME

Definition at line 619 of file nicdrv.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int ec_waitinframe ( uint8  idx,
int  timeout 
)

Blocking receive frame function.

Calls ec_waitinframe_red().

Parameters:
[in] idx = requested index of frame
[in] timeout = timeout in us
Returns:
Workcounter if a frame is found with corresponding index, otherwise EC_NOFRAME.

Definition at line 589 of file nicdrv.c.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int ec_errcnt

global error packet counter, ie non EtherCAT packets

Definition at line 157 of file nicdrv.c.

pthread_mutex_t ec_getindex_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 180 of file nicdrv.c.

int ec_incnt

global rx packet counter, counts only EtherCAT packets

Definition at line 155 of file nicdrv.c.

current redundancy state

Definition at line 159 of file nicdrv.c.

pthread_mutex_t ec_rx_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 182 of file nicdrv.c.

ec_bufT ec_rxbuf[EC_MAXBUF]

primary rx buffers

Definition at line 114 of file nicdrv.c.

int ec_rxbufstat[EC_MAXBUF]

primary rx buffer status

Definition at line 116 of file nicdrv.c.

pthread_mutex_t ec_tx_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 181 of file nicdrv.c.

ec_bufT ec_txbuf[EC_MAXBUF]

transmit buffers

Definition at line 134 of file nicdrv.c.

temporary tx buffer

Definition at line 138 of file nicdrv.c.

int ec_txbuflength[EC_MAXBUF]

transmit buffer lenghts

Definition at line 136 of file nicdrv.c.

temporary tx buffer length

Definition at line 140 of file nicdrv.c.

global helper var ri count time used in rx socket

Definition at line 164 of file nicdrv.c.

global helper var to count time used in tx socket

Definition at line 162 of file nicdrv.c.

const uint16 priMAC[3] = { 0x0101, 0x0101, 0x0101 }

Primary source MAC address used for EtherCAT.

This address is not the MAC address used from the NIC. EtherCAT does not care about MAC addressing, but it is used here to differentiate the route the packet traverses through the EtherCAT segment. This is needed to find out the packet flow in redundant configurations.

Definition at line 171 of file nicdrv.c.

const uint16 secMAC[3] = { 0x0404, 0x0404, 0x0404 }

Secondary source MAC address used for EtherCAT.

Definition at line 173 of file nicdrv.c.

int sockhandle = -1

primary socket handle

Definition at line 143 of file nicdrv.c.

int sockhandle2 = -1

secondary socket handle

Definition at line 145 of file nicdrv.c.

Generated by  doxygen 1.6.3