From 4dfb4b92ea9606156f3823d39bae410321ea9dbf Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Tue, 23 Nov 2021 22:00:29 +0000 Subject: [PATCH] - refactored network - fixed gpio_clr() git-svn-id: http://moon:8086/svn/mips@206 a8ebac50-d88d-4704-bea3-6648445a41b3 --- src/Makefile | 15 ++- src/common/libsys/gpio.c | 2 +- src/common/libsys/mips_gfx.c | 2 + src/common/network/arp.c | 3 +- src/common/network/dhcp.c | 2 +- src/common/network/dhcp.h | 2 +- src/common/network/ethernet.c | 4 +- src/common/network/ethernet.h | 3 +- src/{ => common/network}/fifo.c | 2 +- src/{ => common/network}/fifo.h | 0 src/common/network/icmp.c | 4 +- src/common/network/icmp.h | 2 +- src/common/network/ipv4.c | 4 +- src/common/network/ipv4.h | 2 +- src/common/network/marvell_88e1111.c | 4 +- src/common/network/mii-bitbang.c | 169 +++++++++++++-------------- src/common/network/mii-bitbang.h | 19 +-- src/common/network/udp.h | 2 +- src/test_emac.c | 121 ++++++++++--------- 19 files changed, 194 insertions(+), 168 deletions(-) rename src/{ => common/network}/fifo.c (92%) rename src/{ => common/network}/fifo.h (100%) diff --git a/src/Makefile b/src/Makefile index 89867a8..0608549 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,10 +3,12 @@ BOARD ?=ml402 include $(MIPS_HOME)/make/mips_app.mk -PROG-ml402 = $(addprefix $(BUILD_DIR)/, hello.elf testbench.elf test_irq.elf dhry.elf queens.elf stanford.elf paranoia.elf rmd160_test.elf Bessel.elf whet.elf phrasen.elf dttl.elf richards_benchmark.elf test_exception.elf life.elf r3.elf gunzip.elf basic_math.elf jman_patches.elf jman_patchmeshes.elf jman_polys.elf test_hpi.elf test_vga.elf test_fft.elf flashtest.elf test_emac_sim.elf) +PROG-ml402 = $(addprefix $(BUILD_DIR)/, hello.elf testbench.elf test_irq.elf dhry.elf queens.elf stanford.elf paranoia.elf rmd160_test.elf Bessel.elf whet.elf phrasen.elf dttl.elf richards_benchmark.elf test_exception.elf life.elf r3.elf gunzip.elf basic_math.elf jman_patches.elf jman_patchmeshes.elf jman_polys.elf test_hpi.elf test_vga.elf test_fft.elf flashtest.elf test_emac.elf) PROG-denano = $(addprefix $(BUILD_DIR)/, hello.elf dhry.elf queens.elf stanford.elf paranoia.elf rmd160_test.elf Bessel.elf whet.elf phrasen.elf dttl.elf richards_benchmark.elf test_exception.elf testbench_denano.elf) PROG-sim = $(addprefix $(BUILD_DIR)/, hello.elf test_exception_sim.elf test_emac_sim.elf) - + +PROG-ml402 = $(addprefix $(BUILD_DIR)/, test_emac.elf) +SRCS-network = $(addprefix common/network/, arp.c dhcp.c emac.c ethernet.c icmp.c ipv4.c marvell_88e1111.c mii-bitbang.c udp.c fifo.c) all: $(PROG-$(BOARD)) $(BUILD_DIR): @@ -451,6 +453,15 @@ $(BUILD_DIR)/test_emac_sim.elf: $(BUILD_DIR) test_emac_sim.c common/network/emac cat $@.srec | $(PACKHEX) > $@.pack $(OBJCOPY) -O ihex -I binary $@.flash.bin $@.hex +$(BUILD_DIR)/test_emac.elf: $(BUILD_DIR) test_emac.c $(network-SRCS) + $(CC) $(CFLAGS) -o $@ -Os test_emac.c $(SRCS-network) $(LIBS) >$@.map + $(OBJDUMP) -d $@ > $@.dis + $(OBJCOPY) $@ -O binary $@.bin + $(OBJCOPY) -O srec $@ $@.srec + $(FLASHGEN) $@.bin $(ENDIAN_FLAGS) + cat $@.srec | $(PACKHEX) > $@.pack + $(OBJCOPY) -O ihex -I binary $@.flash.bin $@.hex + $(BUILD_DIR)/mips_io.elf: $(BUILD_DIR) mips_io.c $(CC) $(CFLAGS) -o $@ mips_io.c $(LIBS) >$@.map $(OBJDUMP) -d $@ > $@.dis diff --git a/src/common/libsys/gpio.c b/src/common/libsys/gpio.c index 1db863b..f979a81 100644 --- a/src/common/libsys/gpio.c +++ b/src/common/libsys/gpio.c @@ -28,7 +28,7 @@ void gpio_set(gpio_if_t *pObj, uint32_t value) void gpio_clr(gpio_if_t *pObj, uint32_t value) { - *pObj->pBase &= ~((value | pObj->mask) << pObj->shift); + *pObj->pBase &= ~((value & pObj->mask) << pObj->shift); } // --------------------------------------------------------- diff --git a/src/common/libsys/mips_gfx.c b/src/common/libsys/mips_gfx.c index f0b9cc6..84fcc5e 100644 --- a/src/common/libsys/mips_gfx.c +++ b/src/common/libsys/mips_gfx.c @@ -3,6 +3,8 @@ #include #include "libsys.h" #include "mips_gfx.h" +#include "screen.h" +#include "cop0.h" #define USE_HW_BLITTER diff --git a/src/common/network/arp.c b/src/common/network/arp.c index 535fe72..3b0013b 100644 --- a/src/common/network/arp.c +++ b/src/common/network/arp.c @@ -11,7 +11,8 @@ #include #include #include -#include "libsys.h" +#include + #include "ethernet.h" #include "arp.h" diff --git a/src/common/network/dhcp.c b/src/common/network/dhcp.c index 43fe14b..ce5a42d 100644 --- a/src/common/network/dhcp.c +++ b/src/common/network/dhcp.c @@ -4,7 +4,7 @@ // 21.12.2003, J. Ahrensfeld // // ------------------------------------------------------------------------- -#include "libsys.h" +#include #include "ethernet.h" #include "ipv4.h" #include "udp.h" diff --git a/src/common/network/dhcp.h b/src/common/network/dhcp.h index 5100f68..fe01d93 100644 --- a/src/common/network/dhcp.h +++ b/src/common/network/dhcp.h @@ -7,7 +7,7 @@ #ifndef DHCP_H #define DHCP_H -#include "libsys.h" +#include #include "ethernet.h" // ------------------------------------------------------------------------- diff --git a/src/common/network/ethernet.c b/src/common/network/ethernet.c index 4466a3b..473f950 100644 --- a/src/common/network/ethernet.c +++ b/src/common/network/ethernet.c @@ -11,7 +11,9 @@ #include #include #include -#include "libsys.h" + +#include +#include "emac.h" #include "ethernet.h" // ------------------------------------------------------------------------- diff --git a/src/common/network/ethernet.h b/src/common/network/ethernet.h index 458cb5d..7f1a550 100644 --- a/src/common/network/ethernet.h +++ b/src/common/network/ethernet.h @@ -14,7 +14,8 @@ #include #include #include -#include "libsys.h" + +#include #define ETH_PACKET_MIN_SIZE 64 #define ETH_PACKET_MAX_SIZE 1500 diff --git a/src/fifo.c b/src/common/network/fifo.c similarity index 92% rename from src/fifo.c rename to src/common/network/fifo.c index c29af85..a959879 100644 --- a/src/fifo.c +++ b/src/common/network/fifo.c @@ -1,5 +1,5 @@ #include -#include "libsys.h" +#include #include "fifo.h" uint32_t fifo_alloc(fifo_t *pObj, uint32_t size) diff --git a/src/fifo.h b/src/common/network/fifo.h similarity index 100% rename from src/fifo.h rename to src/common/network/fifo.h diff --git a/src/common/network/icmp.c b/src/common/network/icmp.c index f1b220c..a59acd8 100644 --- a/src/common/network/icmp.c +++ b/src/common/network/icmp.c @@ -4,11 +4,11 @@ // 21.12.2003, J. Ahrensfeld // // ------------------------------------------------------------------------- -#include "libsys.h" +#include +#include #include "ethernet.h" #include "ipv4.h" #include "icmp.h" -#include "string.h" // ------------------------------------------------------------------------- // Testing diff --git a/src/common/network/icmp.h b/src/common/network/icmp.h index 58c383a..e5ee97e 100644 --- a/src/common/network/icmp.h +++ b/src/common/network/icmp.h @@ -7,7 +7,7 @@ #ifndef ICMP_H #define ICMP_H -#include "libsys.h" +#include #include "ethernet.h" // ------------------------------------------------------------------------- diff --git a/src/common/network/ipv4.c b/src/common/network/ipv4.c index 197ca5f..e33dd96 100644 --- a/src/common/network/ipv4.c +++ b/src/common/network/ipv4.c @@ -4,10 +4,10 @@ // 21.12.2003, J. Ahrensfeld // // ------------------------------------------------------------------------- -#include "libsys.h" +#include +#include #include "ethernet.h" #include "ipv4.h" -#include "string.h" #define IPV4_HEADER_SIZE 20 diff --git a/src/common/network/ipv4.h b/src/common/network/ipv4.h index d327e70..3b8c8e3 100644 --- a/src/common/network/ipv4.h +++ b/src/common/network/ipv4.h @@ -7,7 +7,7 @@ #ifndef IPV4_H #define IPV4_H -#include "libsys.h" +#include #include "ethernet.h" // ------------------------------------------------------------------------- diff --git a/src/common/network/marvell_88e1111.c b/src/common/network/marvell_88e1111.c index 4145af4..0ea551b 100644 --- a/src/common/network/marvell_88e1111.c +++ b/src/common/network/marvell_88e1111.c @@ -1,7 +1,7 @@ /** ******************************************************************************* * * Project: Robotics library for the Autonomous Robotics Development Platform -* Port by: Jorge Sánchez de Nova jssdn (mail)_(at) kth.se +* Port by: Jorge S�nchez de Nova jssdn (mail)_(at) kth.se * * NOTE: This code is based on the Xilinx XAPP1042. * @@ -34,7 +34,7 @@ #include #include #include -#include "libsys.h" +#include //#include "errno.h" #include "marvell_88e1111.h" diff --git a/src/common/network/mii-bitbang.c b/src/common/network/mii-bitbang.c index 83bb8a4..e06dab8 100644 --- a/src/common/network/mii-bitbang.c +++ b/src/common/network/mii-bitbang.c @@ -1,7 +1,7 @@ /** ******************************************************************************* * * Project: Robotics library for the Autonomous Robotics Development Platform -* Port by: Jorge Sánchez de Nova jssdn (mail)_(at) kth.se +* Port by: Jorge S�nchez de Nova jssdn (mail)_(at) kth.se * * NOTE: This code is based on the Xilinx XAPP1042 which, as noted by Xilinx, is based in Linux Kernel's * drivers/net/fs_enet by Vitaly Bordug and Pantelis Antoniou @@ -35,8 +35,9 @@ #include #include -#include "libsys.h" -#include "gpio.h" +#include +#include + #include "mii-bitbang.h" /* @@ -56,42 +57,27 @@ static void mii_delay (void) * set GPIO to OUTPUT */ -static void mdio_mode_output(gpio_if_t *mii_gpio) +static void mdio_mode_output(Mii_t *pObj) { - uint32_t dir; - - gpio_get_dir(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, &dir); - dir |= GPIO_0_MDIO_MDIO; - - /* Set the direction for the MDIO signal to be output */ - gpio_set_dir(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, dir); + gpio_set(&pObj->gpio_mdio_dir, GPIO_0_MDIO_MDIO); } /* * mdio_mode_input: * set GPIO to INPUT */ -static void mdio_mode_input(gpio_if_t *mii_gpio) +static void mdio_mode_input(Mii_t *pObj) { - uint32_t dir; - - gpio_get_dir(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, &dir); - dir &= ~GPIO_0_MDIO_MDIO; -// printf("MDIO dir: 0x%x\n", dir); - /* Set the direction for the MDIO signal to be input */ - gpio_set_dir(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, dir); -} + gpio_clr(&pObj->gpio_mdio_dir, GPIO_0_MDIO_MDIO); + } /* * mdio_read: * Read the value presently driven by the PHY on the MDIO GPIO */ -static int mdio_read(gpio_if_t *mii_gpio) +static int mdio_read(Mii_t *pObj) { - uint32_t data; - - gpio_read_data(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, &data); -// printf("MDIO read: 0x%x\n", data); + uint32_t data = gpio_read(&pObj->gpio_mdio_data); return (data & GPIO_0_MDIO_MDIO) != 0; } @@ -99,80 +85,88 @@ static int mdio_read(gpio_if_t *mii_gpio) * mdio_drive_bit: * set the GPIO to drive the appropriate bit value on the MDIO pin */ -static void mdio_drive_bit(gpio_if_t *mii_gpio, uint32_t value) +static void mdio_drive_bit(Mii_t *pObj, uint32_t value) { if (value) - gpio_write(mii_gpio, GPIO_0_MDIO_MDIO, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, ~0); + { + gpio_set(&pObj->gpio_mdio_data, GPIO_0_MDIO_MDIO); + } else - gpio_write(mii_gpio, GPIO_0_MDIO_MDIO, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, 0); + { + gpio_clr(&pObj->gpio_mdio_data, GPIO_0_MDIO_MDIO); + } } /* * mdc_drive_bit: * Toggle the MDC GPIO to the appropriate bit. */ -static void mdc_drive_bit(gpio_if_t *mii_gpio, uint32_t value) +static void mdc_drive_bit(Mii_t *pObj, uint32_t value) { if (value) - gpio_write(mii_gpio, GPIO_0_MDIO_MDC, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, ~0); + { + gpio_set(&pObj->gpio_mdio_data, GPIO_0_MDIO_MDC); + } else - gpio_write(mii_gpio, GPIO_0_MDIO_MDC, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, 0); + { + gpio_clr(&pObj->gpio_mdio_data, GPIO_0_MDIO_MDC); + } } /* * mdc_clk_1_0: * Drive the MII Data Clock 1->0 */ -static void mdc_clk_1_0 (gpio_if_t *mii_gpio) +static void mdc_clk_1_0 (Mii_t *pObj) { mii_delay(); - mdc_drive_bit(mii_gpio, 1); + mdc_drive_bit(pObj, 1); mii_delay(); - mdc_drive_bit(mii_gpio, 0); + mdc_drive_bit(pObj, 0); } /* * mii_send_address: * Transmit the preamble, phy address, and phy register number on the bus. */ -static void mii_send_address (gpio_if_t *mii_gpio, int read, uint8_t addr, uint8_t reg) +static void mii_send_address (Mii_t *pObj, int read, uint8_t addr, uint8_t reg) { int i; /* * Send a 32 bit preamble of 1's */ - mdio_mode_output(mii_gpio); - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ + mdio_mode_output(pObj); + mdio_drive_bit(pObj, 1); /* <<<<< */ for (i = 0; i < MII_PREABLE_BITS; i++) { - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); } /* * send the start bit (01) */ - mdio_drive_bit(mii_gpio, 0); /* <<<<< */ - mdc_clk_1_0(mii_gpio); - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ - mdc_clk_1_0(mii_gpio); + mdio_drive_bit(pObj, 0); /* <<<<< */ + mdc_clk_1_0(pObj); + mdio_drive_bit(pObj, 1); /* <<<<< */ + mdc_clk_1_0(pObj); /* * send the opcode: read (10) write (10) */ - mdio_drive_bit(mii_gpio, read); /* <<<<< */ - mdc_clk_1_0(mii_gpio); - mdio_drive_bit(mii_gpio, !read); /* <<<<< */ - mdc_clk_1_0(mii_gpio); + mdio_drive_bit(pObj, read); /* <<<<< */ + mdc_clk_1_0(pObj); + mdio_drive_bit(pObj, !read); /* <<<<< */ + mdc_clk_1_0(pObj); /* * send the PHY address */ for (i = 0; i < MII_5ADDRESS_BITS; i++) { if (addr & FIFTH_BIT_0x10) { - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ + mdio_drive_bit(pObj, 1); /* <<<<< */ } else { - mdio_drive_bit(mii_gpio, 0); /* <<<<< */ + mdio_drive_bit(pObj, 0); /* <<<<< */ } - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); addr <<= 1; } @@ -182,11 +176,11 @@ static void mii_send_address (gpio_if_t *mii_gpio, int read, uint8_t addr, uint8 */ for (i = 0; i < MII_5ADDRESS_BITS; i++) { if (reg & FIFTH_BIT_0x10) { - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ + mdio_drive_bit(pObj, 1); /* <<<<< */ } else { - mdio_drive_bit(mii_gpio, 0); /* <<<<< */ + mdio_drive_bit(pObj, 0); /* <<<<< */ } - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); reg <<= 1; } @@ -196,7 +190,7 @@ static void mii_send_address (gpio_if_t *mii_gpio, int read, uint8_t addr, uint8 * MiiGpio_PhyRead: * Read from an MII PHY register */ -uint32_t MiiGpio_PhyRead (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t RegisterNum) +uint32_t MiiGpio_PhyRead (Mii_t *pObj, uint32_t PhyAddress, uint32_t RegisterNum) { uint16_t regval; uint16_t i; @@ -204,18 +198,18 @@ uint32_t MiiGpio_PhyRead (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t Reg /* * Bang the PHY address and PHY register on the bus. */ - mii_send_address(mii_gpio, MII_READ, PhyAddress, RegisterNum); + mii_send_address(pObj, MII_READ, PhyAddress, RegisterNum); /* * Set GPIO mode to read */ - mdio_mode_input(mii_gpio); - mdc_clk_1_0(mii_gpio); + mdio_mode_input(pObj); + mdc_clk_1_0(pObj); /* * check the turnaround bit */ - if (mdio_read(mii_gpio) != 0) + if (mdio_read(pObj) != 0) { printf("ERROR: PHY not driving turnaround bit low.\n\r"); return -1; @@ -227,13 +221,13 @@ uint32_t MiiGpio_PhyRead (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t Reg regval = 0; for (i = 0; i < MII_16REGISTER_BITS; i++) { - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); regval <<= 1; - regval |= mdio_read(mii_gpio); + regval |= mdio_read(pObj); } - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); return regval; } @@ -242,20 +236,20 @@ uint32_t MiiGpio_PhyRead (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t Reg * MiiGpio_PhyWrite: * Write to an MII PHY register */ -void MiiGpio_PhyWrite (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t RegisterNum, uint16_t PhyData) +void MiiGpio_PhyWrite (Mii_t *pObj, uint32_t PhyAddress, uint32_t RegisterNum, uint16_t PhyData) { int i; /* * Bang the PHY address and PHY register on the bus. */ - mii_send_address(mii_gpio, MII_WRITE, PhyAddress, RegisterNum); + mii_send_address(pObj, MII_WRITE, PhyAddress, RegisterNum); /* send the turnaround (10) */ - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ - mdc_clk_1_0(mii_gpio); - mdio_drive_bit(mii_gpio, 0); /* <<<<< */ - mdc_clk_1_0(mii_gpio); + mdio_drive_bit(pObj, 1); /* <<<<< */ + mdc_clk_1_0(pObj); + mdio_drive_bit(pObj, 0); /* <<<<< */ + mdc_clk_1_0(pObj); /* * write 16 bits of register data, MSB first @@ -264,19 +258,19 @@ void MiiGpio_PhyWrite (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t Regist { if (PhyData & MSB_16BITS_0x8000) { - mdio_drive_bit(mii_gpio, 1); /* <<<<< */ + mdio_drive_bit(pObj, 1); /* <<<<< */ } else { - mdio_drive_bit(mii_gpio, 0); /* <<<<< */ + mdio_drive_bit(pObj, 0); /* <<<<< */ } - mdc_clk_1_0(mii_gpio); + mdc_clk_1_0(pObj); PhyData <<= 1; } - mdio_mode_input(mii_gpio); - mdc_clk_1_0(mii_gpio); + mdio_mode_input(pObj); + mdc_clk_1_0(pObj); } @@ -285,29 +279,26 @@ void MiiGpio_PhyWrite (gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t Regist * MiiGpio_Init: * Initialize GPIOs connected to PHY MDC and MDIO pins */ -int MiiGpio_Init(gpio_if_t *mii_gpio) +void MiiGpio_Init(Mii_t *pObj) { - uint32_t Data; - int err; - - if (mii_gpio == NULL) { - return -ENODEV; - } - /* * Initialize the GPIO component */ + // Init GPIO MDIO + gpio_init(&pObj->gpio_mdio_data, SYS_GPIO_0_DATA, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO); + gpio_init(&pObj->gpio_mdio_dir, SYS_GPIO_0_DIR, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO); - //TODO: CHANGE! - err = gpio_init(mii_gpio, SYS_GPIO_0_BASE); - - if( err < 0 ) - return err; - - /* + /* * Set the direction for MDC signals to be output */ -// GPIO_SetDataDirection(mii_gpio, MDC_MDIO_GPIO_CHANNEL, MDC_MDIO_MDIO_BIT); - gpio_write(mii_gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, GPIO_DIR_OFFSET, GPIO_0_MDIO_RST | GPIO_0_MDIO_MDC); - return 0; + gpio_write(&pObj->gpio_mdio_dir, GPIO_0_MDIO_RST | GPIO_0_MDIO_MDC); +} + +void MiiGpio_Reset(Mii_t *pObj) +{ + // Reset PHY + gpio_set(&pObj->gpio_mdio_data, GPIO_0_MDIO_RST); + sleep(1); + gpio_clr(&pObj->gpio_mdio_data, GPIO_0_MDIO_RST); + sleep(100); } diff --git a/src/common/network/mii-bitbang.h b/src/common/network/mii-bitbang.h index 0affa0c..a5f4add 100644 --- a/src/common/network/mii-bitbang.h +++ b/src/common/network/mii-bitbang.h @@ -4,7 +4,7 @@ /** ******************************************************************************* * * Project: Robotics library for the Autonomous Robotics Development Platform -* Port by: Jorge Sánchez de Nova jssdn (mail)_(at) kth.se +* Port by: Jorge S�nchez de Nova jssdn (mail)_(at) kth.se * * NOTE: This code is based on the Xilinx XAPP1042 which, as noted by Xilinx, is based in Linux Kernel's * drivers/net/fs_enet by Vitaly Bordug and Pantelis Antoniou @@ -67,15 +67,20 @@ #define FIFTH_BIT_0x10 0x10 #define MSB_16BITS_0x8000 0x8000 +typedef struct _sMii_t +{ + gpio_if_t gpio_mdio_data; + gpio_if_t gpio_mdio_dir; + +} Mii_t; #include #include #include -#include "gpio.h" +#include -uint32_t MiiGpio_PhyRead(gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t RegisterNum); - -void MiiGpio_PhyWrite(gpio_if_t *mii_gpio, uint32_t PhyAddress, uint32_t RegisterNum, uint16_t PhyData); - -int MiiGpio_Init(gpio_if_t *mii_gpio); +uint32_t MiiGpio_PhyRead(Mii_t *pObj, uint32_t PhyAddress, uint32_t RegisterNum); +void MiiGpio_PhyWrite(Mii_t *pObj, uint32_t PhyAddress, uint32_t RegisterNum, uint16_t PhyData); +void MiiGpio_Init(Mii_t *pObj); +void MiiGpio_Reset(Mii_t *pObj); #endif diff --git a/src/common/network/udp.h b/src/common/network/udp.h index 977ed38..53c3cc4 100644 --- a/src/common/network/udp.h +++ b/src/common/network/udp.h @@ -7,7 +7,7 @@ #ifndef UDP_H #define UDP_H -#include "libsys.h" +#include #include "ethernet.h" // ------------------------------------------------------------------------- typedef struct _sUDP diff --git a/src/test_emac.c b/src/test_emac.c index 3646813..3fa4c18 100644 --- a/src/test_emac.c +++ b/src/test_emac.c @@ -5,21 +5,33 @@ #include #include #include -#include "libsys.h" -#include "fifo.h" -#include "emac.h" -#include "ethernet.h" -#include "arp.h" -#include "icmp.h" -#include "ipv4.h" -#include "udp.h" -#include "dhcp.h" -#include "gpio.h" -#include "mii-bitbang.h" -#include "marvell_88e1111.h" -gpio_if_t gpio; -gpio_if_t mii_gpio; +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "board.h" + +gpio_if_t gpio_led_data; +gpio_if_t gpio_led_dir; + +gpio_if_t gpio_btn_data; +gpio_if_t gpio_btn_dir; + +gpio_if_t gpio_mdio_data; + +Mii_t mii; #define BUFFER_SIZE (32*1024*1024) @@ -40,8 +52,6 @@ typedef struct _srx_descr_t #define PHY_IDO_REG 2 #define PHY_ID1_REG 3 -#define NOT_SIM 1 - void Time_print(void) { time_t curr_date; @@ -91,7 +101,7 @@ void handler6(void) } } -void mii_loopback_enable(gpio_if_t *pObj, uint16_t phy_id, uint32_t speed) +void mii_loopback_enable(Mii_t *pMii, uint16_t phy_id, uint32_t speed) { uint16_t val; @@ -99,17 +109,17 @@ void mii_loopback_enable(gpio_if_t *pObj, uint16_t phy_id, uint32_t speed) { case MPHY_CONTROL_SPD_SEL_10: val = MPHY_CONTROL_DUPLEX; - MiiGpio_PhyWrite(pObj, phy_id, MPHY_CONTROL_REG, val); + MiiGpio_PhyWrite(pMii, phy_id, MPHY_CONTROL_REG, val); break; case MPHY_CONTROL_SPD_SEL_100: val = MPHY_CONTROL_SPD_SEL_LSB | MPHY_CONTROL_DUPLEX; - MiiGpio_PhyWrite(pObj, phy_id, MPHY_CONTROL_REG, val); + MiiGpio_PhyWrite(pMii, phy_id, MPHY_CONTROL_REG, val); break; case MPHY_CONTROL_SPD_SEL_1000: val = MPHY_CONTROL_SPD_SEL_MSB | MPHY_CONTROL_DUPLEX; - MiiGpio_PhyWrite(pObj, phy_id, MPHY_CONTROL_REG, val); + MiiGpio_PhyWrite(pMii, phy_id, MPHY_CONTROL_REG, val); break; default: @@ -117,17 +127,17 @@ void mii_loopback_enable(gpio_if_t *pObj, uint16_t phy_id, uint32_t speed) } - val = MiiGpio_PhyRead(&mii_gpio, phy_id, MPHY_CONTROL_REG); + val = MiiGpio_PhyRead(pMii, phy_id, MPHY_CONTROL_REG); val |= MPHY_CONTROL_RESET; - MiiGpio_PhyWrite(&mii_gpio, phy_id, MPHY_CONTROL_REG, val); + MiiGpio_PhyWrite(pMii, phy_id, MPHY_CONTROL_REG, val); - val = MiiGpio_PhyRead(&mii_gpio, phy_id, MPHY_CONTROL_REG); + val = MiiGpio_PhyRead(pMii, phy_id, MPHY_CONTROL_REG); val |= MPHY_CONTROL_LOOPBACK; - MiiGpio_PhyWrite(&mii_gpio, phy_id, MPHY_CONTROL_REG, val); + MiiGpio_PhyWrite(pMii, phy_id, MPHY_CONTROL_REG, val); } -void Phy_print_regs(gpio_if_t *pObj, uint16_t phy_id) +void Phy_print_regs(Mii_t *pMii, uint16_t phy_id) { uint16_t i, j, val; @@ -136,7 +146,7 @@ void Phy_print_regs(gpio_if_t *pObj, uint16_t phy_id) { for (j=0; j < 8; j++) { - printf("%02X: %04X ", i+j, MiiGpio_PhyRead(&mii_gpio, phy_id, i+j)); + printf("%02X: %04X ", i+j, MiiGpio_PhyRead(pMii, phy_id, i+j)); } printf("\n"); } @@ -211,7 +221,15 @@ int main() setbuf(stdout, NULL); - gpio_init(&gpio, SYS_GPIO_0_BASE); + // Init GPIO BTN + gpio_init(&gpio_btn_data, SYS_GPIO_0_DATA, GPIO_0_MASK_BTN, GPIO_0_ALIGN_BTN); + gpio_init(&gpio_btn_dir, SYS_GPIO_0_DIR, GPIO_0_MASK_BTN, GPIO_0_ALIGN_BTN); + gpio_clr(&gpio_btn_dir, GPIO_0_MASK_BTN); + + // Init GPIO LED + gpio_init(&gpio_led_data, SYS_GPIO_0_DATA, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED); + gpio_init(&gpio_led_dir, SYS_GPIO_0_DIR, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED); + gpio_set(&gpio_led_dir, GPIO_0_MASK_LED); g_buf_start = (uint32_t*)malloc(BUFFER_SIZE); g_pBuf_end = g_buf_start + BUFFER_SIZE/sizeof(uint32_t); @@ -220,57 +238,52 @@ int main() fifo_alloc(&rx_descr_fifo, sizeof(rx_descr_t)*BUFFER_SIZE/64); // LED - gpio_write(&gpio, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED, GPIO_DIR_OFFSET, GPIO_0_MASK_LED); - gpio_write(&gpio, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED, GPIO_DATA_OFFSET, 0); - - // MDIO - gpio_write(&gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, GPIO_DIR_OFFSET, GPIO_0_MDIO_RST | GPIO_0_MDIO_MDC); + gpio_write(&gpio_led_data, 0); + // Create PHY interface + MiiGpio_Init(&mii); + // Reset PHY - gpio_write(&gpio, GPIO_0_MDIO_RST, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, 1); -#if NOT_SIM - sleep(1); - gpio_write(&gpio, GPIO_0_MDIO_RST, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, 0); - sleep(100); - - gpio_read(&gpio, ~0, 0, GPIO_DIR_OFFSET, &res); - + MiiGpio_Reset(&mii); + + res = gpio_read(&gpio_led_dir); printf("Dir = %08X\n", res); + // while(1) { - gpio_read(&gpio, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED, GPIO_DATA_OFFSET, &res); + res = gpio_read(&gpio_led_data); printf("LED-Data = %08X\n", res); - gpio_write(&gpio, GPIO_0_MASK_LED, GPIO_0_ALIGN_LED, GPIO_DATA_OFFSET, res + 1); - gpio_read(&gpio, GPIO_0_MASK_BTN, GPIO_0_ALIGN_BTN, GPIO_DATA_OFFSET, &res); + gpio_write(&gpio_led_data, res + 1); + + res = gpio_read(&gpio_btn_data); printf("Btn-Data = %08X\n", res); - gpio_read(&gpio, GPIO_0_MASK_MDIO, GPIO_0_ALIGN_MDIO, GPIO_DATA_OFFSET, &res); + + res = gpio_read(&gpio_mdio_data); printf("MDIO-Data = %08X\n", res); sleep(100); } -#endif - MiiGpio_Init(&mii_gpio); -// Phy_loopback_enable(&mii_gpio, PHY_ADDR, MPHY_CONTROL_SPD_SEL_100); +// Phy_loopback_enable(&mii, PHY_ADDR, MPHY_CONTROL_SPD_SEL_100); // printf("Set loopback mode.\n\r"); /* set 100mbps and reset PHY */ phy_reg = 0x3300; - MiiGpio_PhyWrite(&mii_gpio, PHY_ADDR, 0, phy_reg | 0x8000); + MiiGpio_PhyWrite(&mii, PHY_ADDR, 0, phy_reg | 0x8000); /* wait for reset to complete */ - while (MiiGpio_PhyRead(&mii_gpio, PHY_ADDR, 0) & 0x8000); + while (MiiGpio_PhyRead(&mii, PHY_ADDR, 0) & 0x8000); /* set auto negotiation */ - phy_reg = MiiGpio_PhyRead(&mii_gpio, PHY_ADDR, 0); - MiiGpio_PhyWrite(&mii_gpio, PHY_ADDR, 0, phy_reg | 0x0200); + phy_reg = MiiGpio_PhyRead(&mii, PHY_ADDR, 0); + MiiGpio_PhyWrite(&mii, PHY_ADDR, 0, phy_reg | 0x0200); /* wait for auto negotiation to complete */ - while (!(MiiGpio_PhyRead(&mii_gpio, PHY_ADDR, 1) & 0x0024)) + while (!(MiiGpio_PhyRead(&mii, PHY_ADDR, 1) & 0x0024)) { usleep(10000); } - Phy_print_regs(&mii_gpio, PHY_ADDR); + Phy_print_regs(&mii, PHY_ADDR); EMAC_RX_reset(); EMAC_TX_reset(); @@ -335,7 +348,7 @@ int main() printf("\n"); } - gpio_read(&gpio, GPIO_0_MASK_BTN, GPIO_0_ALIGN_BTN, GPIO_DATA_OFFSET, &res); + res = gpio_read(&gpio_btn_data); if (!res) continue;