新聞中心
隨著物聯(lián)網(wǎng)技術的發(fā)展,越來越多的設備需要連接到網(wǎng)絡上進行互聯(lián)。在以太網(wǎng)通信中,物理層(PHY)驅動是網(wǎng)絡通信的重要組成部分。Petalinux是一個開源的Linux操作系統(tǒng),專門用于嵌入式系統(tǒng)的開發(fā)。本文將介紹在Petalinux中進行PHY驅動開發(fā)的過程及應用。

創(chuàng)新互聯(lián)是專業(yè)的三水網(wǎng)站建設公司,三水接單;提供網(wǎng)站建設、成都網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行三水網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
一、Petalinux概述
Petalinux是一個由Xilinx公司開發(fā)的嵌入式Linux操作系統(tǒng),用于嵌入式系統(tǒng)的開發(fā)。它是基于Yocto Project構建的,支持多個開發(fā)板和處理器架構。Petalinux有許多優(yōu)點,如易于使用、可擴展性強、可定制化等。
二、PHY驅動概述
PHY驅動是在Ethernet MAC和物理層之間進行通信的軟件。它是將數(shù)字信號轉換為模擬信號的接口。PHY芯片與MAC芯片之間通過Media Independent Interface(MII)進行通信。PHY驅動負責處理MII通信,并控制PHY芯片的工作。
三、Petalinux中PHY驅動開發(fā)
1. 創(chuàng)建Petalinux項目
在Petalinux中,可以使用PetaWizard工具創(chuàng)建項目。首先需要打開終端窗口,然后執(zhí)行以下命令:
petalinux-create -t project -n myproject –template zynq
這將創(chuàng)建一個名為“myproject”的Petalinux項目。
2. 配置設備樹
設備樹是一種機器可讀的、描述硬件組件的文本格式。在Petalinux中,可以使用Device Tree Generator(DTG)工具生成設備樹文件。需在Petalinux中的工程文件夾內的project-spec/meta-user/recipes-bsp/device-tree/files/下增加一個.dts文件,內容如下:
/include/ “system-conf.dtsi”
/ {
model = “Avnet Zynq UltraScale+ MPSoC ZCU102 Evaluation Kit”;
compatible = “xlnx,zcu102-rev1.0″,”xlnx,zcu102-rev1.1″,”xlnx,zcu102-rev1.2”;
chosen {
bootargs = “console=ttyPS0,115200 earlyprintk “;
linux,stdout-path = “/amba/serial@e0001000”;
stdout-path = “/amba/serial@e0001000”;
stdout-path1 = “/amba/serial@e0001001”;
};
};
3. 創(chuàng)建PHY驅動
在Petalinux中,可以使用驅動模塊(module)方式創(chuàng)建PHY驅動。驅動模塊是一種特殊的程序,可以被Linux內核動態(tài)加載和卸載。使用以下命令創(chuàng)建PHY驅動模塊:
petalinux-create -t module -n mydriver –enable
創(chuàng)建完畢后會在項目的工程文件夾內生成一個名為“mydriver”的驅動模塊。
4. 編寫PHY驅動代碼
PHY驅動的實現(xiàn)一般包括以下幾個部分:初始化、讀取、寫入、復位等。下面是一個簡單的示例代碼:
#include
#include
#include
#include
static struct resource mydriver_resource[] = {
{
.start = 0x43C00000,
.end = 0x43C0FFFF,
.flags = IORESOURCE_MEM,
.name = “mydriver-mem”,
}
};
static struct platform_device mydriver_device = {
.name = “mydriver”,
.id = -1,
.num_resources = ARRAY_SIZE(mydriver_resource),
.resource = mydriver_resource,
};
static int __init mydriver_init(void)
{
printk(KERN_ALERT “mydriver: initializing…\n”);
platform_device_register(&mydriver_device);
return 0;
}
static void __exit mydriver_exit(void)
{
printk(KERN_ALERT “mydriver: exiting…\n”);
platform_device_unregister(&mydriver_device);
}
module_init(mydriver_init);
module_exit(mydriver_exit);
5. 編譯驅動程序及其應用
在Petalinux項目的根目錄中,使用以下命令編譯驅動程序:
petalinux-build -c mydriver
之后在應用程序代碼中使用驅動文件即可,如下:
/* open the device */
mydriver_fd = open(“/dev/mydriver”, O_RDWR);
if (mydriver_fd
perror(“open /dev/mydriver”);
return -1;
}
/* read from the device */
read(mydriver_fd, &data, sizeof(data));
/* write to the device */
write(mydriver_fd, &data, sizeof(data));
/* close the device */
close(mydriver_fd);
四、應用案例
1. petalinux phy驅動的應用
使用Petalinux中的PHY驅動進行網(wǎng)絡通信,可以使用以下示例代碼:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int mn()
{
int skfd, err;
struct ifreq ifr;
struct ethtool_cmd ecmd;
struct mii_ioctl_data *mii;
int i;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd
perror(“socket”);
return 1;
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, “eth0”, sizeof(ifr.ifr_name)-1);
err = ioctl(skfd, SIOCGIFCLASS, &ifr);
if (err) {
perror(“ioctl(SIOCGIFCLASS)”);
return 1;
}
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
fprintf(stderr, “Interface %s not Ethernet\n”, ifr.ifr_name);
return 1;
}
err = ioctl(skfd, SIOCGMIIPHY, &ifr);
if (err) {
perror(“ioctl(SIOCGMIIPHY)”);
return 1;
}
mii = (struct mii_ioctl_data *)&ifr.ifr_data;
ecmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = (char *)&ecmd;
err = ioctl(skfd, SIOCETHTOOL, &ifr);
if (err) {
perror(“ioctl(SIOCETHTOOL, ETHTOOL_GSET)”);
return 1;
}
close(skfd);
printf(“PHY ID: %04x:%04x\n”, mii->phy_id & 0xffff, mii->phy_id >> 16);
printf(“Link mode: “);
for (i=0; i
switch (ecmd.link_mode[i] & ethtool_link_mode_masks[i].mask) {
case 0:
printf(“None “);
break;
case 1:
printf(“%s “, ethtool_link_mode_masks[i].name);
break;
default:
printf(“Multiple “);
break;
}
}
printf(“\n”);
return 0;
}
該示例代碼可以打印出PHY ID和Link mode。在運行該代碼之前,需要確認網(wǎng)卡的設備名稱為“eth0”。
2. 自動協(xié)商模式的應用
程序員可以通過PHY驅動在Petalinux中實現(xiàn)自動協(xié)商模式,以實現(xiàn)網(wǎng)卡與交換機之間的自動工作模式匹配。
在PHY驅動中,可以通過ioctl接口對網(wǎng)卡的工作模式進行配置。在Petalinux中,可以使用以下示例代碼實現(xiàn)自動協(xié)商模式:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int mn(int argc, char *argv[])
{
struct ifreq ifr;
int fd, err;
struct mii_ioctl_data *mii;
struct ethtool_cmd ecmd;
if (argc != 2) {
fprintf(stderr, “Usage: %s [interface name]\n”, argv[0]);
exit(1);
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd
perror(“socket”);
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name)-1);
err = ioctl(fd, SIOCGIFCLASS, &ifr);
if (err) {
perror(“ioctl(SIOCGIFCLASS)”);
exit(1);
}
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
fprintf(stderr, “Interface %s not Ethernet\n”, argv[1]);
exit(1);
}
err = ioctl(fd, SIOCGMIIPHY, &ifr);
if (err) {
perror(“ioctl(SIOCGMIIPHY)”);
exit(1);
}
mii = (struct mii_ioctl_data *)&ifr.ifr_data;
err = ioctl(fd, SIOCETHTOOL, &ifr);
if (err) {
perror(“ioctl(SIOCETHTOOL, ETHTOOL_GSET)”);
exit(1);
}
ecmd.cmd = ETHTOOL_GLINKSETTINGS;
ifr.ifr_data = (char *)&ecmd;
err = ioctl(fd, SIOCETHTOOL, &ifr);
if (err) {
perror(“ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)”);
exit(1);
}
ecmd.autoneg = AUTONEG_ENABLE;
ifr.ifr_data = (char *)&ecmd;
err = ioctl(fd, SIOCETHTOOL, &ifr);
if (err) {
perror(“ioctl(SIOCETHTOOL, ETHTOOL_SSET)”);
exit(1);
}
printf(“Auto negotiation enabled.\n”);
return 0;
}
該示例代碼使用ioctl接口配置自動協(xié)商模式,并打印出信息以確認配置是否生效。
成都網(wǎng)站建設公司-創(chuàng)新互聯(lián),建站經(jīng)驗豐富以策略為先導10多年以來專注數(shù)字化網(wǎng)站建設,提供企業(yè)網(wǎng)站建設,高端網(wǎng)站設計,響應式網(wǎng)站制作,設計師量身打造品牌風格,熱線:028-86922220使用Petalinux準備U-Boot并從網(wǎng)絡啟動
如果想在FSBL里叢運面加載bitstream的話,必須關閉FPGA Manager!
GPIO操作,MIO從0-53,EMIO從檔橡54開始
petalinux-build -c fl
petalinux-build -c u-boot
petalinux-package –boot –u-boot –format BIN
然后把BOOT.BIN寫入到SD卡中即可啟動U-Boot
這里就使用網(wǎng)線直連的方式來啟動,也可以使用dhcp命令來自動獲取ip
記得saveenv,然行鄭旁后ping一下之前的NFS服務器ip,就可以使用了
bdinfo查看到DRAM bank
petalinux-build -c device-tree
petalinux-build -c kernel
nfs.168.123.196:/home/imcort/Documents/nfs/zImage
nfs 0 192.168.123.196:/home/imcort/Documents/nfs/system.dtb
bootz
u-boot只需要保留最基本的設備驅動即可
setenv bootcmd ‘dhcp; nfs.168.123.196:/home/imcort/Documents/nfs/zImage; nfs 0 192.168.123.196:/home/imcort/Documents/nfs/system.dtb; bootz’
saveenv
petalinux phy驅動的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于petalinux phy驅動,Petalinux中PHY驅動開發(fā)與應用,使用Petalinux準備U-Boot并從網(wǎng)絡啟動的信息別忘了在本站進行查找喔。
成都創(chuàng)新互聯(lián)科技公司主營:網(wǎng)站設計、網(wǎng)站建設、小程序制作、成都軟件開發(fā)、網(wǎng)頁設計、微信開發(fā)、成都小程序開發(fā)、網(wǎng)站制作、網(wǎng)站開發(fā)等業(yè)務,是專業(yè)的成都做小程序公司、成都網(wǎng)站建設公司、成都做網(wǎng)站的公司。創(chuàng)新互聯(lián)公司集小程序制作創(chuàng)意,網(wǎng)站制作策劃,畫冊、網(wǎng)頁、VI設計,網(wǎng)站、軟件、微信、小程序開發(fā)于一體。
名稱欄目:Petalinux中PHY驅動開發(fā)與應用(petalinuxphy驅動)
文章位置:http://fisionsoft.com.cn/article/djdighp.html


咨詢
建站咨詢
