Created
November 9, 2022 03:52
-
-
Save KunYi/b430294d4d057a598ef66a98c4b01f03 to your computer and use it in GitHub Desktop.
using MDIO to access PHY registers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#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> | |
#define reteck(ret) \ | |
if (ret < 0) {\ | |
printf("%m! \"%s\" : line: %d\n", __func__, __LINE__);\ | |
goto lab; \ | |
} | |
#define help() \ | |
printf("mdio_reg:\n");\ | |
printf("read operation: mdio_reg reg_addr\n");\ | |
printf("write operation: mdio_reg reg_addr value\n");\ | |
printf("For example:\n");\ | |
printf("mdio_reg eth0 1\n");\ | |
printf("mdio_reg eth0 0 0x12\n\n");\ | |
exit(0) | |
int sockfd; | |
int main(int argc, char *argv[]) | |
{ | |
if (argc == 1 || !strcmp(argv[1], "-h")) | |
{ | |
help(); | |
} | |
struct mii_ioctl_data *mii = NULL; | |
struct ifreq ifr; | |
int ret; | |
memset(&ifr, 0, sizeof(ifr)); | |
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1); | |
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0); | |
reteck(sockfd); | |
ret = ioctl(sockfd, SIOCGMIIPHY, &ifr); | |
reteck(ret); | |
mii = (struct mii_ioctl_data*)&ifr.ifr_data; | |
if (argc == 3) { | |
mii->reg_num = (uint16_t) strtoul(argv[2], NULL, 0); | |
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); | |
} | |
else if (argc == 4) { | |
mii->reg_num = (uint16_t) strtoul(argv[2], NULL, 0); | |
mii->val_in = (uint16_t) strtoul(argv[3], NULL, 0); | |
ret = ioctl(sockfd, SIOCGMIIREG, &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); | |
} | |
lab: | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment