Classes | Defines | Enumerations | Functions | Variables
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 <youbot_driver/soem/ethercattype.h>
#include <youbot_driver/soem/nicdrv.h>
Include dependency graph for nicdrv.c:

Go to the source code of this file.

Classes

struct  ec_stackT

Defines

#define RX_PRIM   priMAC[1]
#define RX_SEC   secMAC[1]

Enumerations

enum  { ECT_RED_NONE, ECT_RED_DOUBLE }

Functions

int ec_closenic (void)
uint8 ec_getindex (void)
int ec_inframe (uint8 idx, int stacknumber)
int ec_outframe (uint8 idx, int stacknumber)
int ec_outframe_red (uint8 idx)
static int ec_recvpkt (int stacknumber)
void ec_setbufstat (uint8 idx, int bufstat)
void ec_setupheader (void *p)
int ec_setupnic (const char *ifname, int secondary)
int ec_srconfirm (uint8 idx, int timeout)
int ec_waitinframe (uint8 idx, int timeout)
static int ec_waitinframe_red (uint8 idx, struct timeval tvs)

Variables

int ec_errcnt
pthread_mutex_t ec_getindex_mutex = PTHREAD_MUTEX_INITIALIZER
int ec_incnt
static uint8 ec_lastidx
int ec_redstate
pthread_mutex_t ec_rx_mutex = PTHREAD_MUTEX_INITIALIZER
ec_bufT ec_rxbuf [EC_MAXBUF]
static ec_bufT ec_rxbuf2 [EC_MAXBUF]
int ec_rxbufstat [EC_MAXBUF]
static int ec_rxbufstat2 [EC_MAXBUF]
static int ec_rxsa [EC_MAXBUF]
static int ec_rxsa2 [EC_MAXBUF]
static ec_stackT ec_stack [2]
static ec_bufT ec_tempinbuf
static ec_bufT ec_tempinbuf2
static int ec_tempinbufs
pthread_mutex_t ec_tx_mutex = PTHREAD_MUTEX_INITIALIZER
ec_bufT ec_txbuf [EC_MAXBUF]
ec_bufT ec_txbuf2
int ec_txbuflength [EC_MAXBUF]
int ec_txbuflength2
int hlp_rxtime
int hlp_txtime
const uint16 priMAC [3] = {0x0101, 0x0101, 0x0101}
const uint16 secMAC [3] = {0x0404, 0x0404, 0x0404}
int sockhandle = -1
int sockhandle2 = -1

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 177 of file nicdrv.c.

#define RX_SEC   secMAC[1]

second MAC word is used for identification

Definition at line 179 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 257 of file nicdrv.c.

uint8 ec_getindex ( void  )

Get new frame identifier index and allocate corresponding rx buffer.

Returns:
new index.

Definition at line 288 of file nicdrv.c.

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 415 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 336 of file nicdrv.c.

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 353 of file nicdrv.c.

static int ec_recvpkt ( int  stacknumber) [static]

Non blocking read of socket. Put frame in temporary buffer.

Parameters:
[in]stacknumber= 0=primary 1=secondary stack
Returns:
>0 if frame is available and read

Definition at line 387 of file nicdrv.c.

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 325 of file nicdrv.c.

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 272 of file nicdrv.c.

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 190 of file nicdrv.c.

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 625 of file nicdrv.c.

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 595 of file nicdrv.c.

static int ec_waitinframe_red ( uint8  idx,
struct timeval  tvs 
) [static]

Blocking redundant receive frame function. If redundant mode is not active then it skips the secondary stack and redundancy functions. In redundant mode it waits for both (primary and secondary) frames to come in. The result goes in an decision tree that decides, depending on the route of the packet and its possible missing arrival, how to reroute the original packet to get the data in an other try.

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

Definition at line 506 of file nicdrv.c.


Variable Documentation

int ec_errcnt

global error packet counter, ie non EtherCAT packets

Definition at line 158 of file nicdrv.c.

pthread_mutex_t ec_getindex_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 181 of file nicdrv.c.

int ec_incnt

global rx packet counter, counts only EtherCAT packets

Definition at line 156 of file nicdrv.c.

uint8 ec_lastidx [static]

last used frame index

Definition at line 154 of file nicdrv.c.

current redundancy state

Definition at line 160 of file nicdrv.c.

pthread_mutex_t ec_rx_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 183 of file nicdrv.c.

primary rx buffers

Definition at line 114 of file nicdrv.c.

secondary rx buffers

Definition at line 125 of file nicdrv.c.

primary rx buffer status

Definition at line 116 of file nicdrv.c.

int ec_rxbufstat2[EC_MAXBUF] [static]

secondary rx buffer status

Definition at line 127 of file nicdrv.c.

int ec_rxsa[EC_MAXBUF] [static]

primary rx MAC source address

Definition at line 118 of file nicdrv.c.

int ec_rxsa2[EC_MAXBUF] [static]

secondary rx MAC source address

Definition at line 129 of file nicdrv.c.

ec_stackT ec_stack[2] [static]
Initial value:

primary and secondary tx/rx stack pointers

Definition at line 148 of file nicdrv.c.

primary temporary rx buffer

Definition at line 120 of file nicdrv.c.

secondary temporary rx buffer

Definition at line 131 of file nicdrv.c.

int ec_tempinbufs [static]

primary temporary rx buffer status

Definition at line 122 of file nicdrv.c.

pthread_mutex_t ec_tx_mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 182 of file nicdrv.c.

transmit buffers

Definition at line 134 of file nicdrv.c.

temporary tx buffer

Definition at line 138 of file nicdrv.c.

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 165 of file nicdrv.c.

global helper var to count time used in tx socket

Definition at line 163 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 172 of file nicdrv.c.

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

Secondary source MAC address used for EtherCAT.

Definition at line 174 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.



youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Oct 6 2014 09:08:03