以太网开机初始化
嵌入式设备在部署时,通常需要为各个设备设置mac和ip地址。通常linux系统下会有专门的以太网配置文件。
此处以petalinux系统为例,配置文件路径为:/etc/network/interfaces
#开机后使能eth0,dhcp分配ip地址
auto eth0
iface eth0 inet dhcp
#开机使能eth0,并设置为静态ip
auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
#开机后,设置eth0 mac地址
auto eth0
hwaddress ether 00:11:22:33:44:56
iface eth0 inet dhcp
vlan设置
ip link add link eth0 name eth0.33 type vlan id 33
ifconfig eth0.33 hw ether 02:04:00:00:00:22
ifconfig eth0.33 172.16.33.123 netmask 255.255.255.0
以太网phy寄存器设置
我们可以在应用程序中,读写phy寄存器,来让phy工作在我们想要的模式,而无需添加或修改对应phy驱动,也不用担心应用中的操作与驱动中读写phy相冲突,因为驱动中操作完phy后会将选页等寄存器恢复原状。读写方法如下:
景略phy JL2121灯初始化(系统下没有对应phy驱动时,将使用通用phy驱动,此时灯状态不对)
./mdio_rw eth0 1 0x1f 0xd04
./mdio_rw eth0 1 0x10 0xAE01
./mdio_rw eth0 1 0x1f 0x1000
./mdio_rw eth0 1 0x10 0x0704
景略phy JL2121 dly调整 (以太网存在丢包情况,有很大机率是rgmii接口,dly需要调整)
ifconfig eth0 up
./mdio_rw eth0 1 0x1f 0xab
./mdio_rw eth0 1 0x10
./mdio_rw eth0 1 0x10 0x07b4
./mdio_rw eth0 1 0x11
./mdio_rw eth0 1 0x11 0x6002
mdio_rw代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/mii.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <netinet/in.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int sockfd = -1;
struct mii_ioctl_data *mii = NULL;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
if(argc == 4)
{
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
ioctl(sockfd, SIOCGMIIPHY, &ifr);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 16);
ioctl(sockfd, SIOCGMIIREG, &ifr);
printf("read:id=%d reg=0x%08x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out);
}
else if(argc == 5)
{
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
ioctl(sockfd, SIOCGMIIPHY, &ifr);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 16);
mii->val_in = (uint16_t)strtoul(argv[4], NULL, 16);
ioctl(sockfd, SIOCSMIIREG, &ifr);
printf("write:id=%d reg=0x%08x value=0x%x\n", mii->phy_id, mii->reg_num, mii->val_in);
}else{
printf("mdio ethX phyId reg value\n");
}
if(sockfd > 0) {
close(sockfd);
}
return 0;
}