RK3568平台移植EtherCAT Master

【前言】EtherCAT全称EtherNet Control Automation Technology,是由德国倍福(Beckhoff)公司提出的一种实时以太网技术,用于工业自动化的现场总线通信协议。EtherCAT是一种开放但不开源的技术,意味着您可以任意使用这项技术,但若要进行相关设备的开发,则需要向倍福公司获取相关授权。

        IGH EtherCAT是一种EtherCAT协议开源实现,是一个完全符合EtherCAT标准的库,能够在实时操作系统上运行。与传统总线相比,EtherCAT的优点在于减少了总线通信的延迟时间,从而提高了实时性。IGH EtherCAT实现了这一协议,并对其进行了优化和改进。

1、环境说明

1.1、硬件平台

        M3568-4GF16GLI-T(RK3568)

1.2、Linux内核版本

        Linux4.19.193

2、EtherCAT Master源码下载 

 源码网站:EtherLab / EtherCAT Master · GitLab

 在Ubuntu中输入如下命令下载EtherCAT源码:

git clone https://gitlab.com/etherlab.org/ethercat.git

 3、安装编译igh所需的依赖

博主使用的编译平台是Ubuntu,因此输入如下命令安装依赖包。

apt-get install autoconf autogen libtool -y

4、生成配置文件

进入ethercat目录下,输入如下命令生成配置文件。

./bootstrap

5、配置编译

输入如下命令配置编译。

./configure --prefix=/home/vmuser/workspace/EtherCAT/EtherCAT-Master/ethercat/output --with-linux-dir=/home/vmuser/workspace/new-RT-rk3568/RK3568_SDK/RK3568_SDK/kernel --enable-8139too=no --enable-e100=no --enable-e1000=no --enable-e1000e=no --enable-r8169=no --enable-generic=yes --enable-ccat=no --enable-igb=yes --enable-igc=no CC=aarch64-buildroot-linux-gnu-gcc --host=aarch64-buildroot-linux-gnu

注意:

(1)网卡驱动根据实际硬件环境编译对应的驱动,博主的硬件环境需要使用到igb和generic这两个驱动。

(2) --prefix是指定你下面make install时的安装目录,--with-linux-dir是指定你的linux内核目录。此处需要将交叉编译工具链设置到环境变量中,如果使用交叉编译链的绝对路径的话,配置会编译不过,会报如下错误: (原因未知,恳请大佬留言指导。)

host system type... Invalid configuration `/opt/zlg/m3568-sdk-v1.0.0-ga/host/bin/aarch64-buildroot-linux-gnu': machine `/opt/zlg/m3568-sdk-v1.0.0-ga/host/bin/aarch64' not recognized
configure: error: /bin/bash autoconf/config.sub /opt/zlg/m3568-sdk-v1.0.0-ga/host/bin/aarch64-buildroot-linux-gnu failed

特别说明:

--prefix是指定的路径建议为空,这样在下面make install后,所生成的文件的配置路径均为根目录(/),不这样做的话会导致在开发板上启动ethercat时,报如下错误:

 博主这块的配置重新改为如下所示情况:

./configure --prefix= --with-linux-dir=/home/vmuser/workspace/new-RT-rk3568/RK3568_SDK/RK3568_SDK/kernel --enable-8139too=no --enable-e100=no --enable-e1000=no --enable-e1000e=no --enable-r8169=no --enable-generic=yes --enable-ccat=no --enable-igb=yes --enable-igc=no CC=aarch64-buildroot-linux-gnu-gcc --host=aarch64-buildroot-linux-gnu

此时make install后的所有生成文件均保存在了当前编译平台的根目录下。可以参考如下脚本将所有生成文件移动到ethercat/output/目录下,同时省去第9步的操作。(所有操作需要获取root权限)

#!/bin/bash

function print_info() {
    echo ""
    echo "Usage: $0 <ethercat-dir>"
    echo "params: $#"
    echo ""
    exit 1
}

if [ $# -ne 1 ]; then
    print_info
fi

if [ ! -d $1 ]; then
    echo ""
    echo "'$1' is not a directory!"
    print_info
fi

ethercat=$1

mkdir -p $ethercat/output/
mkdir -p $ethercat/output/bin/
mkdir -p $ethercat/output/etc/
mkdir -p $ethercat/output/etc/init.d/
mkdir -p $ethercat/output/etc/sysconfig/
mkdir -p $ethercat/output/lib/
mkdir -p $ethercat/output/sbin/
mkdir -p $ethercat/output/share/

mv /bin/ethercat			$ethercat/output/bin/

mv /etc/ethercat.conf		$ethercat/output/etc/
mv /etc/init.d/ethercat		$ethercat/output/etc/init.d/
mv /etc/sysconfig/ethercat	$ethercat/output/etc/sysconfig/

mv /include/				$ethercat/output/

mv /lib/libethercat*		$ethercat/output/lib/
mv /lib/cmake/				$ethercat/output/lib/
mv /lib/pkgconfig/			$ethercat/output/lib/

mv /sbin/ethercatctl		$ethercat/output/sbin/

mv /share/bash-completion/	$ethercat/output/share/

mkdir -p $ethercat/output/modules
cp $ethercat/devices/ec_8139too.ko      $ethercat/output/modules/
cp $ethercat/devices/ec_e100.ko         $ethercat/output/modules/
cp $ethercat/devices/e1000/ec_e1000.ko $ethercat/output/modules/
cp $ethercat/devices/ec_r8169.ko        $ethercat/output/modules/
cp $ethercat/devices/e1000e/ec_e1000e.ko $ethercat/output/modules/
cp $ethercat/devices/ec_generic.ko      $ethercat/output/modules/
cp $ethercat/devices/ccat/ec_ccat.ko    $ethercat/output/modules/
cp $ethercat/devices/igb/ec_igb.ko      $ethercat/output/modules/
cp $ethercat/devices/igc/ec_igc.ko      $ethercat/output/modules/
cp $ethercat/master/ec_master.ko        $ethercat/output/modules/
echo "Copy successfully!"

6、编译源码

make

7、编译modules模块

指定交叉编译工具,编译modules

make ARCH=arm64 CROSS_COMPILE=aarch64-buildroot-linux-gnu- modules

注意:编译通过会对应生devices/ec_generic.ko、devices/ec_igb.ko和master/ec_master.ko。

8、安装

此处先需使用su,获取root权限,不能直接使用sudo,否则会报如下错误:

同时你也需要在~/.bashrc文件中添加交叉工具链的路径,以使其在任何root权限的路径下使用工具链。

su 和 sudo 的区别:  

        1.共同点:都具有root用户权限;  

        2.不同点:su仅仅取得root权限,工作环境不变;sudo是完全取得root的权限和root的工作环境。

使用如下命令安装ethercat:

make install

 注意:make install 后,在前面指定output目录下会有编译生成的各种用户空间的文件。

9、创建modules文件

在//home/vmuser/workspace/EtherCAT/EtherCAT-Master/ethercat/output目录下创建modules文件夹,并复制ec_generic.ko、ec_igb.ko和ec_master.ko到modules下。

mkdir -p output/modules
cp devices/ec_generic.ko output/modules/
cp devices/igb/ec_igb.ko output/modules/
cp master/ec_master.ko   output/modules/

10、打包编译出的文件

将output文件夹打包,并下载到开发板系统中。

tar -cvzf output.tar.gz output/

--------------------------至此IGH交叉编译完成,接下来是在对应arm目标板上的操作---------------------------

ARM开发板所需的所有内容均在output.tar.gz文件内。

11、开发板上解压编译包

tar -zxvf output.tar.gz

12、拷贝文件到对应目录下

将output目录下各文件目录的内容复制到ARM开发板系统的根目录下对应路径下,例如:cp bin/ethercat  /bin/

13、拷贝ko到内核版本目录下

拷贝ec_master.ko到/lib/modules/内核版本号/ 。

cp output/modules/ec_master.ko  /lib/modules/4.19.193/   

然后执行:depmod

注意:内核版本可以通过uname -r 查看,博主的内核版本为4.19.193。

14、配置rules,创建设备号

echo KERNEL=="EtherCAT[0-9]*", MODE="0664" > /etc/udev/rules.d/99-EtherCAT.rules

15、获取开发板的MAC地址

以网口1为例: (ifconfig命令查看)

-------------------------------------------------至此开发板上的配置结束--------------------------------------------------

 通过网线直连EtherCAT主从站,从站启动完成后,打开主站电源。

16、配置主站的MAC地址

modprobe ec_master main_devices=B6:6A:CD:F3:EA:FB

注意:此处的MAC地址就是第15点获取的MAC地址。

17、启动ethercat

/etc/init.d/ethercat start

出现如下提示,说明ethercat启动成功。 

 如果在启动过程中,出现如下错误:

解决方案一:尝试把 /sbin/ethercatctl文件中的“#!/bin/bash"改为“#!/bin/sh”。

解决方案二:向ARM平台移植bash。移植步骤见:ARM平台移植bash-CSDN博客

18、加载网卡驱动

 insmod /modules/ec_generic.ko
 insmod /modules/ec_igb.ko

19、查看ethercat帮助信息

ethercat    //输入ethercat得到帮助菜单

20、 查看master信息

ethercat master

如果出现如下信息,则说明你编译的目标文件正常。

21、连接从机情况

博主的环境为:主站连接了两个从站,输入指令后会输出如下信息:

从站设备信息如下:

连接了从机的情况下,在你的主站系统中输入对应指令能获取上述信息的话,那恭喜你的IGH移植成功了。

 ------------------------------------------------至此EtherCAT移植结束----------------------------------------------------

22、参考文献

IgH(Igh EtherCAT Master for Linux)移植到搭载Linux系统的开发板_igh移植-CSDN博客

EtherCAT IGH 的下载和编译_igh编译-CSDN博客

IMX6Q开发板平台移植ethercat通讯协议 - 天嵌IMX6Q开发板技术支持专版 天嵌 ARM开发社区 (armbbs.net)

结束语】因技术能力有限,文章如有不妥之处,恳请各位技术大佬留言指正!  

猜你喜欢

转载自blog.csdn.net/m0_56121792/article/details/135255452