SOEM源码解析——ec

编程入门 行业动态 更新时间:2024-10-26 06:29:00

SOEM<a href=https://www.elefans.com/category/jswz/34/1770099.html style=源码解析——ec"/>

SOEM源码解析——ec

0 工具准备

1.SOEM-master-1.4.0源码

1 ec_init总览

/** Initialise lib in single NIC mode:初始化库在单网卡模式* @param[in] ifname   = Dev name, f.e. "eth0" 设备名* @return >0 if OK* @see ecx_init*/
int ec_init(const char * ifname)
{return ecx_init(&ecx_context, ifname);
}
/** Initialise lib in single NIC mode:初始化库在单网卡模式* @param[in]  context = context struct SOEM句柄* @param[in] ifname   = Dev name, f.e. "eth0" 设备名* @return >0 if OK*/
int ecx_init(ecx_contextt *context, const char * ifname)
{return ecx_setupnic(context->port, ifname, FALSE);
}

当我们的主站工作模式为单网卡而非冗余模式时会使用ec_init函数初始化SOEM库,ec_init函数是主站初始化时第一个执行的函数。它真正调用的是ecx_setupnic函数,该函数具体操作如下:

/** Basic setup to connect NIC to socket.* @param[in] port        = port context struct* @param[in] ifname      = Name of NIC device, f.e. "eth0"* @param[in] secondary   = if >0 then use secondary stack instead of primary* @return >0 if succeeded*/
int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary) 
{int i;int *psock;if (secondary){/* secondary port stuct available? *//* 冗余端口结构体有效? */if (port->redport){/* when using secondary socket it is automatically a redundant setup *//* 当使用冗余套接字时,切换到冗余状态*/psock = &(port->redport->sockhandle);*psock = -1;port->redstate                   = ECT_RED_DOUBLE;port->redport->stack.sock        = &(port->redport->sockhandle);port->redport->stack.txbuf       = &(port->txbuf);port->redport->stack.txbuflength = &(port->txbuflength);port->redport->stack.tempbuf     = &(port->redport->tempinbuf);port->redport->stack.rxbuf       = &(port->redport->rxbuf);port->redport->stack.rxbufstat   = &(port->redport->rxbufstat);port->redport->stack.rxsa        = &(port->redport->rxsa);ecx_clear_rxbufstat(&(port->redport->rxbufstat[0]));}else{/* fail */return 0;}}else{port->sockhandle        = -1;port->lastidx           = 0;port->redstate          = ECT_RED_NONE;port->stack.sock        = &(port->sockhandle);port->stack.txbuf       = &(port->txbuf);port->stack.txbuflength = &(port->txbuflength);port->stack.tempbuf     = &(port->tempinbuf);port->stack.rxbuf       = &(port->rxbuf);port->stack.rxbufstat   = &(port->rxbufstat);port->stack.rxsa        = &(port->rxsa);ecx_clear_rxbufstat(&(port->rxbufstat[0]));psock = &(port->sockhandle);}  /* setup ethernet headers in tx buffers so we don't have to repeat it *//* 在tx buffers里设置以太网帧头,这样就不必重复设置了 */for (i = 0; i < EC_MAXBUF; i++){ec_setupheader(&(port->txbuf[i]));port->rxbufstat[i] = EC_BUF_EMPTY;}ec_setupheader(&(port->txbuf2));return 1;
}

该函数的功能可以分为2块:
(1)根据传进来的secondary分别设置传入句柄的普通端口或冗余端口,当secondary为FALSE也就是0时设置普通端口,当secondary为TRUE也就是1时设置冗余端口。
(2)设置发送缓冲区的以太网帧头,在后续的报文发送中就不必重复设置了。
上面的(1)、(2)步骤中涉及到ecx_clear_rxbufstat和ec_setupheader这2个函数,下面一起来看看它们的具体工作。

2 ecx_clear_rxbufstat函数解析

/*** @brief 将接收缓冲区状态设置为EC_BUF_EMPTY(空)* @param rxbufstat 接收缓冲区状态*/
static void ecx_clear_rxbufstat(int *rxbufstat)
{int i;for(i = 0; i < EC_MAXBUF; i++){rxbufstat[i] = EC_BUF_EMPTY;}
}

该函数主要工作就是将主站接收缓冲区所有buffer的状态设置为EC_BUF_EMPTY,也就是空,初始化接收缓冲区状态到正确的状态以便于后面的正常工作。

3 ec_setupheader函数解析

/** Fill buffer with ethernet header structure. 将以太网帧头填充到buffer* Destination MAC is allways broadcast. 目的mac地址是广播mac地址* Ethertype is allways ETH_P_ECAT. 帧类型是ETH_P_ECAT(0x88A4)* @param[out] p = buffer*/
void ec_setupheader(void *p) 
{ec_etherheadert *bp;bp = p;bp->da0 = oshw_htons(0xffff);bp->da1 = oshw_htons(0xffff);bp->da2 = oshw_htons(0xffff);bp->sa0 = oshw_htons(priMAC[0]);bp->sa1 = oshw_htons(priMAC[1]);bp->sa2 = oshw_htons(priMAC[2]);bp->etype = oshw_htons(ETH_P_ECAT);
}

前面的ecx_clear_rxbufstat函数是为了设置接收缓冲区到争取的状态,这个函数则是为了设置发送缓冲区。
我们都知道,EtherCAT报文组成框图如下:

其中目的地址、源地址都可以设置为固定值,SOEM主站将目的地址设置为ff:ff:ff:ff:ff:ff、源地址设置为01:01:01:01:01:01、帧类型规定为0x88A4。SOEM主站这样设置以后便不需要每次组帧时设置以太网帧头,可以提高组帧效率。
还有一个地方需要特别注意,就是oshw_htons函数,用于将主机序(小端)设置为网络序(大端)。

4 总结

(1)SOEM主站初始化以太网帧为广播帧,主站所连接的网络中所有设备都会接收到EtherCAT报文。
(2)SOEM主站支持冗余模式,设置时和单网卡主站不同。

更多推荐

SOEM源码解析——ec

本文发布于:2023-11-16 17:35:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源码   SOEM   ec

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!