| |
| //mdio eth0 1 读取phy寄存器1的数值 |
| //mdio eth0 0 0x1120 将0x1120写入 phy寄存器1 |
| |
| #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> |
| #include <string.h> |
| |
| |
| #define reteck(ret) \ |
| if(ret < 0){ \ |
| printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \ |
| goto lab; \ |
| } |
| |
| #define help() \ |
| printf("For example:\n"); \ |
| printf("mbtk_mdio_demo eth0\n"); \ |
| exit(0); |
| |
| int sockfd; |
| |
| int main(int argc, char *argv[]) |
| { |
| int err, value; |
| int cmdIdx = 0; |
| int ret = 0; |
| int i = 0; |
| char operator[10]; |
| int opt = 0; |
| |
| struct mii_ioctl_data *mii = NULL; |
| struct ifreq ifr; |
| |
| if(argc == 1 || !strcmp(argv[1], "-h")){ |
| help(); |
| } |
| |
| memset(&ifr, 0, sizeof(ifr)); |
| strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1); |
| |
| sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0); |
| reteck(sockfd); |
| |
| //get phy address in smi bus |
| ret = ioctl(sockfd, SIOCGMIIPHY, &ifr); |
| reteck(ret); |
| |
| mii = (struct mii_ioctl_data*)&ifr.ifr_data; |
| |
| while(1) |
| { |
| |
| printf("=========EX:Jl3103========\n" |
| "\t 0: Set the slave mode\n" |
| "\t 1: Set the master mode\n" |
| "\t 2: indicates setting SQI value view mode\n" |
| "\t 3: Set the VCT value view mode\n" |
| "\t 4: Get slave/master mode\n" |
| "\t 5 EXIT \n" |
| "=========================\n"); |
| |
| fflush(stdin); |
| fgets(operator, sizeof(operator), stdin); |
| opt = atoi(operator); |
| |
| switch(opt) |
| { |
| case 0://"Set the slave mode" |
| { |
| int val = 0; |
| mii->reg_num = 0x0834; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| val = mii->val_out; |
| |
| mii->reg_num = 0x0834; |
| mii->val_in = val & 0xBFFF;// set bit[14] = 0 |
| |
| ret = ioctl(sockfd, SIOCSMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_in); |
| |
| break; |
| } |
| case 1://"Set the master mode" |
| { |
| int val = 0; |
| mii->reg_num = 0x0834; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| val = mii->val_out; |
| |
| mii->reg_num = 0x0834; |
| mii->val_in = val | 0x4000;//set bit[14] = 1 |
| |
| ret = ioctl(sockfd, SIOCSMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_in); |
| break; |
| } |
| case 2://"indicates setting SQI value view mode\" |
| { |
| mii->reg_num = 0x8B10; |
| |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| printf("[Jl3103] SQI is 0x%x\n", mii->val_out); |
| break; |
| } |
| case 3://"Set the VCT value view mode" |
| { |
| mii->reg_num = 0x8B00; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| |
| //--TDR Enable |
| mii->reg_num = 0x8B00; |
| mii->val_in = 0x4000; |
| |
| ret = ioctl(sockfd, SIOCSMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_in); |
| usleep(200000); |
| //--TDR Start |
| mii->reg_num = 0x8B00; |
| mii->val_in = 0x5000; |
| |
| ret = ioctl(sockfd, SIOCSMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_in); |
| usleep(20000); |
| //--Read VCT |
| mii->reg_num = 0x8B02; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| printf("[Jl3103] Open status: %s - Short status: %s\n", (mii->val_out & 0x0002) ? "Open" : "Normal", (mii->val_out & 0x0001) ? "Short" : "Normal"); |
| |
| mii->reg_num = 0x8B01; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| printf("[Jl3103] Distance status is 0x%x\n", mii->val_out % 0x200);//bits[9:0] |
| //--TDR Disable |
| mii->reg_num = 0x8B00; |
| mii->val_in = 0; |
| |
| ret = ioctl(sockfd, SIOCSMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_in); |
| break; |
| } |
| case 4://4: Get slave/master mode |
| { |
| int val = 0; |
| mii->reg_num = 0x0834; |
| ret = ioctl(sockfd, SIOCGMIIREG, &ifr); |
| reteck(ret); |
| |
| printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out); |
| printf("[Jl3103] mode: %s\n", (mii->val_out & 0x4000) ? "master" : "slave"); |
| break; |
| } |
| default://EXIT |
| { |
| printf("break\n"); |
| close(sockfd); |
| return 0; |
| } |
| } |
| } |
| lab: |
| close(sockfd); |
| return 0; |
| } |