Add apps/system/mdio from Lazlo

This commit is contained in:
Gregory Nutt 2014-06-27 10:02:52 -06:00
parent 721dc008e8
commit 77572a522a
4 changed files with 372 additions and 0 deletions

11
system/mdio/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

14
system/mdio/Kconfig Normal file
View File

@ -0,0 +1,14 @@
#
# For a description of the syntax of this configuration file,
# see misc/tools/kconfig-language.txt.
#
config SYSTEM_MDIO
bool "MDIO tool"
default n
depends on NETDEV_PHY_IOCTL
---help---
Enable the MDIO tool
if SYSTEM_MDIO
endif

109
system/mdio/Makefile Normal file
View File

@ -0,0 +1,109 @@
############################################################################
# apps/examples/mdio/Makefile
#
# Copyright (C) 2008, 2010-2013 Gregory Nutt. All rights reserved.
# 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.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# MDIO tool built-in application info
APPNAME = mdio
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# MDIO tool
ASRCS =
CSRCS = mdio_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
BIN = ..\..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
BIN = ..\\..\\libapps$(LIBEXT)
else
BIN = ../../libapps$(LIBEXT)
endif
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
$(call ARCHIVE, $(BIN), $(OBJS))
@touch .built
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
else
context:
endif
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
$(call DELFILE, .built)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
-include Make.dep

238
system/mdio/mdio_main.c Normal file
View File

@ -0,0 +1,238 @@
/****************************************************************************
* examples/mdio/mdio_main.c
*
* Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
* 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>
/****************************************************************************
* Definitions
****************************************************************************/
#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))
{
// printf("call to %s() failed (fd = %d)!\n", "socket", g_listen_fd);
return -1;
} else {
// printf("got socket with fd %d\n", g_listen_fd);
}
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)
{
printf("failed! (value %d, errno %d)\n", ret, errno);
return -1;
}
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)
{
*val = ifr.ifr_mii_val_out;
}
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
****************************************************************************/
int mdio_main(int argc, char *argv[])
{
int ret;
int i;
/* test related */
uint16_t phy_id;
uint16_t reg_num;
uint16_t val_in;
uint16_t val_out;
/*--- SETUP --------------------------------------------------------------*/
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;
}
initialize_socket();
if (argc == 4) /* Write to register */
{
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);
}
else if (argc == 3) /* Read from register */
{
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);
}
else if (argc == 2)
{
phy_id = strtol(argv[1], NULL, 16);
for(i = 0; i < 32; i++)
{
ret = get_phy_reg(phy_id, i, &val_out);
printf("phy[%d][%d] = 0x%4x\n", phy_id, i, val_out);
}
}
else
{
/* Read the PHY address */
phy_id = get_phy_id();
if(phy_id < 1)
{
/* failed (can not be negative) */
printf("getting phy id failed\n");
} else {
printf("phy_id = %d\n", phy_id);
}
}
return 0;
}