当前位置:   article > 正文

linux:嵌入式以太网_mdio-rw

mdio-rw

以太网开机初始化

        嵌入式设备在部署时,通常需要为各个设备设置mac和ip地址。通常linux系统下会有专门的以太网配置文件。
        此处以petalinux系统为例,配置文件路径为:/etc/network/interfaces

  1. #开机后使能eth0,dhcp分配ip地址
  2. auto eth0
  3. iface eth0 inet dhcp
  1. #开机使能eth0,并设置为静态ip
  2. auto eth0
  3. iface eth0 inet static
  4. address 192.168.1.2
  5. netmask 255.255.255.0
  6. gateway 192.168.1.1
  1. #开机后,设置eth0 mac地址
  2. auto eth0
  3. hwaddress ether 00:11:22:33:44:56
  4. iface eth0 inet dhcp

vlan设置

  1. ip link add link eth0 name eth0.33 type vlan id 33
  2. ifconfig eth0.33 hw ether 02:04:00:00:00:22
  3. ifconfig eth0.33 172.16.33.123 netmask 255.255.255.0

以太网phy寄存器设置

        我们可以在应用程序中,读写phy寄存器,来让phy工作在我们想要的模式,而无需添加或修改对应phy驱动,也不用担心应用中的操作与驱动中读写phy相冲突,因为驱动中操作完phy后会将选页等寄存器恢复原状。读写方法如下:

景略phy JL2121灯初始化(系统下没有对应phy驱动时,将使用通用phy驱动,此时灯状态不对)

  1. ./mdio_rw eth0 1 0x1f 0xd04
  2. ./mdio_rw eth0 1 0x10 0xAE01
  3. ./mdio_rw eth0 1 0x1f 0x1000
  4. ./mdio_rw eth0 1 0x10 0x0704

景略phy JL2121 dly调整 (以太网存在丢包情况,有很大机率是rgmii接口,dly需要调整)

  1. ifconfig eth0 up
  2. ./mdio_rw eth0 1 0x1f 0xab
  3. ./mdio_rw eth0 1 0x10
  4. ./mdio_rw eth0 1 0x10 0x07b4
  5. ./mdio_rw eth0 1 0x11
  6. ./mdio_rw eth0 1 0x11 0x6002

  mdio_rw代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <linux/mii.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <net/if.h>
  9. #include <linux/sockios.h>
  10. #include <linux/types.h>
  11. #include <netinet/in.h>
  12. #include <unistd.h>
  13. int main(int argc, char *argv[])
  14. {
  15. int sockfd = -1;
  16. struct mii_ioctl_data *mii = NULL;
  17. struct ifreq ifr;
  18. memset(&ifr, 0, sizeof(ifr));
  19. if(argc == 4)
  20. {
  21. strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
  22. sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
  23. ioctl(sockfd, SIOCGMIIPHY, &ifr);
  24. mii = (struct mii_ioctl_data*)&ifr.ifr_data;
  25. mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 16);
  26. ioctl(sockfd, SIOCGMIIREG, &ifr);
  27. printf("read:id=%d reg=0x%08x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out);
  28. }
  29. else if(argc == 5)
  30. {
  31. strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
  32. sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
  33. ioctl(sockfd, SIOCGMIIPHY, &ifr);
  34. mii = (struct mii_ioctl_data*)&ifr.ifr_data;
  35. mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 16);
  36. mii->val_in = (uint16_t)strtoul(argv[4], NULL, 16);
  37. ioctl(sockfd, SIOCSMIIREG, &ifr);
  38. printf("write:id=%d reg=0x%08x value=0x%x\n", mii->phy_id, mii->reg_num, mii->val_in);
  39. }else{
  40. printf("mdio ethX phyId reg value\n");
  41. }
  42. if(sockfd > 0) {
  43. close(sockfd);
  44. }
  45. return 0;
  46. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/868444
推荐阅读
相关标签
  

闽ICP备14008679号