2014-06-27 18:02:52 +02:00
|
|
|
/****************************************************************************
|
2016-05-19 03:22:47 +02:00
|
|
|
* system/mdio/mdio_main.c
|
2014-06-27 18:02:52 +02:00
|
|
|
*
|
2014-08-17 17:51:26 +02:00
|
|
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
2014-06-27 18:02:52 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
|
|
|
|
|
|
|
ifr.ifr_mii_phy_id = phy_id;
|
|
|
|
ifr.ifr_mii_reg_num = reg_num;
|
|
|
|
|
|
|
|
ret = ioctl(g_listen_fd, SIOCGMIIREG, (unsigned long) &ifr);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/*--- SETUP --------------------------------------------------------------*/
|
|
|
|
|
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;
|
|
|
|
}
|