2014-06-27 18:02:52 +02:00
|
|
|
/****************************************************************************
|
2021-06-10 18:40:35 +02:00
|
|
|
* apps/system/mdio/mdio_main.c
|
2014-06-27 18:02:52 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-06-27 18:02:52 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-27 18:02:52 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-06-27 18:02:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2014-06-27 18:02:52 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define CONFIG_HELLO_IPADDR 0xc0a8eacd
|
|
|
|
#define CONFIG_HELLO_PORT 81
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Socket related variables */
|
|
|
|
|
|
|
|
static int g_listen_fd;
|
|
|
|
|
|
|
|
static struct sockaddr_in g_sa;
|
|
|
|
|
|
|
|
int initialize_socket(void)
|
|
|
|
{
|
|
|
|
/* Prepare listen address */
|
|
|
|
|
|
|
|
g_sa.sin_family = AF_INET;
|
|
|
|
g_sa.sin_port = HTONS(CONFIG_HELLO_PORT);
|
|
|
|
g_sa.sin_addr.s_addr = HTONL(CONFIG_HELLO_IPADDR);
|
|
|
|
|
|
|
|
/* Open listening socket */
|
|
|
|
|
|
|
|
g_listen_fd = socket(g_sa.sin_family, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
/* Verify socket creation */
|
|
|
|
|
|
|
|
if (!(g_listen_fd > 0))
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_phy_id(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint16_t phy_id;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
/* Prepare ifreq */
|
|
|
|
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
ret = ioctl(g_listen_fd, SIOCGMIIPHY, (unsigned long) &ifr);
|
|
|
|
|
|
|
|
printf("call to %s() ", "ioctl");
|
|
|
|
|
|
|
|
if (ret != 0)
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
printf("failed! (value %d, errno %d)\n", ret, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
phy_id = ifr.ifr_mii_phy_id;
|
|
|
|
|
|
|
|
return phy_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
ifr.ifr_mii_phy_id = phy_id;
|
|
|
|
ifr.ifr_mii_reg_num = reg_num;
|
|
|
|
|
|
|
|
ret = ioctl(g_listen_fd, SIOCGMIIREG, (unsigned long) &ifr);
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
if (ret == OK)
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
*val = ifr.ifr_mii_val_out;
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
ifr.ifr_mii_phy_id = phy_id;
|
|
|
|
ifr.ifr_mii_reg_num = reg_num;
|
|
|
|
ifr.ifr_mii_val_in = val;
|
|
|
|
|
|
|
|
ret = ioctl(g_listen_fd, SIOCSMIIREG, (unsigned long) &ifr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* mdio_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-06-27 18:02:52 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* test related */
|
|
|
|
|
|
|
|
uint16_t phy_id;
|
|
|
|
uint16_t reg_num;
|
|
|
|
uint16_t val_in;
|
|
|
|
uint16_t val_out;
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
/* -- SETUP ------------------------------------------------------------- */
|
2014-06-27 18:02:52 +02:00
|
|
|
|
2014-06-27 18:13:23 +02:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
printf("usage:\n");
|
|
|
|
printf("\n");
|
|
|
|
printf(" %s phy_id reg_no -- read register\n", argv[0]);
|
|
|
|
printf(" %s phy_id reg_no value -- write register\n", argv[0]);
|
|
|
|
printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
|
|
|
|
initialize_socket();
|
|
|
|
|
|
|
|
if (argc == 4) /* Write to register */
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
phy_id = strtol(argv[1], NULL, 16);
|
|
|
|
reg_num = strtol(argv[2], NULL, 16);
|
|
|
|
val_in = strtol(argv[3], NULL, 16);
|
|
|
|
|
|
|
|
ret = set_phy_reg(phy_id, reg_num, val_in);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("%s() returned %d\n", "set_phy_reg", ret);
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
else if (argc == 3) /* Read from register */
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
phy_id = strtol(argv[1], NULL, 16);
|
|
|
|
reg_num = strtol(argv[2], NULL, 16);
|
|
|
|
|
|
|
|
ret = get_phy_reg(phy_id, reg_num, &val_out);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("%s() returned %d\n", "get_phy_reg", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("0x%4x\n", val_out);
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
else if (argc == 2)
|
|
|
|
{
|
2014-06-27 18:13:23 +02:00
|
|
|
phy_id = strtol(argv[1], NULL, 16);
|
2014-06-27 18:02:52 +02:00
|
|
|
|
2014-06-27 18:13:23 +02:00
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
get_phy_reg(phy_id, i, &val_out);
|
2014-06-27 18:13:23 +02:00
|
|
|
printf("phy[%d][%d] = 0x%4x\n", phy_id, i, val_out);
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
}
|
|
|
|
else
|
2014-06-27 18:13:23 +02:00
|
|
|
{
|
|
|
|
/* Read the PHY address */
|
2014-06-27 18:02:52 +02:00
|
|
|
|
2014-06-27 18:13:23 +02:00
|
|
|
phy_id = get_phy_id();
|
2014-06-27 18:02:52 +02:00
|
|
|
|
2014-06-27 18:13:23 +02:00
|
|
|
if (phy_id < 1)
|
|
|
|
{
|
|
|
|
/* failed (can not be negative) */
|
|
|
|
|
|
|
|
printf("getting phy id failed\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("phy_id = %d\n", phy_id);
|
|
|
|
}
|
2014-06-27 18:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|