123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /**
- * @file
- * Ethernet Interface for standalone applications (without RTOS)
- *
- */
- /*
- * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
- * All rights reserved.
- *
- * 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. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * This file is part of the lwIP TCP/IP stack.
- *
- * Author: Adam Dunkels <adam@sics.se>
- *
- */
- #include "lwip/mem.h"
- #include "netif/etharp.h"
- #include "ethernetif.h"
- #include "gd32f30x_enet.h"
- #include "main.h"
- #include <string.h>
- /* network interface name */
- #define IFNAME0 'G'
- #define IFNAME1 'D'
- /* ENET RxDMA/TxDMA descriptor */
- extern enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM], txdesc_tab[ENET_TXBUF_NUM];
- /* ENET receive buffer */
- extern uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE];
- /* ENET transmit buffer */
- extern uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE];
- /*global transmit and receive descriptors pointers */
- extern enet_descriptors_struct *dma_current_txdesc;
- extern enet_descriptors_struct *dma_current_rxdesc;
- /* preserve another ENET RxDMA/TxDMA ptp descriptor for normal mode */
- enet_descriptors_struct ptp_txstructure[ENET_TXBUF_NUM];
- enet_descriptors_struct ptp_rxstructure[ENET_RXBUF_NUM];
- /**
- * In this function, the hardware should be initialized.
- * Called from ethernetif_init().
- *
- * @param netif the already initialized lwip network interface structure
- * for this ethernetif
- */
- static void low_level_init(struct netif *netif)
- {
- #ifdef CHECKSUM_BY_HARDWARE
- int i;
- #endif /* CHECKSUM_BY_HARDWARE */
- /* set MAC hardware address length */
- netif->hwaddr_len = ETHARP_HWADDR_LEN;
- /* set MAC hardware address */
- netif->hwaddr[0] = MAC_ADDR0;
- netif->hwaddr[1] = MAC_ADDR1;
- netif->hwaddr[2] = MAC_ADDR2;
- netif->hwaddr[3] = MAC_ADDR3;
- netif->hwaddr[4] = MAC_ADDR4;
- netif->hwaddr[5] = MAC_ADDR5;
-
- /* initialize MAC address in ethernet MAC */
- enet_mac_address_set(ENET_MAC_ADDRESS0, netif->hwaddr);
- /* maximum transfer unit */
- netif->mtu = 1500;
- /* device capabilities */
- /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
- netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
- /* initialize descriptors list: chain/ring mode */
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- enet_ptp_enhanced_descriptors_chain_init(ENET_DMA_TX);
- enet_ptp_enhanced_descriptors_chain_init(ENET_DMA_RX);
- #else
- enet_descriptors_chain_init(ENET_DMA_TX);
- enet_descriptors_chain_init(ENET_DMA_RX);
-
- // enet_descriptors_ring_init(ENET_DMA_TX);
- // enet_descriptors_ring_init(ENET_DMA_RX);
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- /* enable ethernet Rx interrrupt */
- { int i;
- for(i=0; i<ENET_RXBUF_NUM; i++){
- enet_rx_desc_immediate_receive_complete_interrupt(&rxdesc_tab[i]);
- }
- }
- #ifdef CHECKSUM_BY_HARDWARE
- /* enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
- for(i=0; i < ENET_TXBUF_NUM; i++){
- enet_transmit_checksum_config(&txdesc_tab[i], ENET_CHECKSUM_TCPUDPICMP_FULL);
- }
- #endif /* CHECKSUM_BY_HARDWARE */
- /* note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
- /* enable MAC and DMA transmission and reception */
- enet_enable();
- }
- /**
- * This function should do the actual transmission of the packet. The packet is
- * contained in the pbuf that is passed to the function. This pbuf
- * might be chained.
- *
- * @param netif the lwip network interface structure for this ethernetif
- * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
- * @return ERR_OK if the packet could be sent
- * an err_t value if the packet couldn't be sent
- *
- * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
- * strange results. You might consider waiting for space in the DMA queue
- * to become availale since the stack doesn't retry to send a packet
- * dropped because of memory failure (except for the TCP timers).
- */
- static err_t low_level_output(struct netif *netif, struct pbuf *p)
- {
- struct pbuf *q;
- int framelength = 0;
- uint8_t *buffer;
- while((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- }
-
- buffer = (uint8_t *)(enet_desc_information_get(dma_current_txdesc, TXDESC_BUFFER_1_ADDR));
-
- /* copy frame from pbufs to driver buffers */
- for(q = p; q != NULL; q = q->next){
- memcpy((uint8_t *)&buffer[framelength], q->payload, q->len);
- framelength = framelength + q->len;
- }
-
- /* note: padding and CRC for transmitted frame
- are automatically inserted by DMA */
- /* transmit descriptors to give to DMA */
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- ENET_NOCOPY_PTPFRAME_TRANSMIT_ENHANCED_MODE(framelength, NULL);
-
- #else
-
- ENET_NOCOPY_FRAME_TRANSMIT(framelength);
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- return ERR_OK;
- }
- /**
- * Should allocate a pbuf and transfer the bytes of the incoming
- * packet from the interface into the pbuf.
- *
- * @param netif the lwip network interface structure for this ethernetif
- * @return a pbuf filled with the received packet (including MAC header)
- * NULL on memory error
- */
- static struct pbuf * low_level_input(struct netif *netif)
- {
- struct pbuf *p, *q;
- u16_t len;
- int l =0;
- uint8_t *buffer;
-
- p = NULL;
-
- /* obtain the size of the packet and put it into the "len" variable. */
- len = enet_desc_information_get(dma_current_rxdesc, RXDESC_FRAME_LENGTH);
- buffer = (uint8_t *)(enet_desc_information_get(dma_current_rxdesc, RXDESC_BUFFER_1_ADDR));
-
- /* we allocate a pbuf chain of pbufs from the Lwip buffer pool */
- p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
-
- /* copy received frame to pbuf chain */
- if (p != NULL){
- for (q = p; q != NULL; q = q->next){
- memcpy((uint8_t *)q->payload, (u8_t*)&buffer[l], q->len);
- l = l + q->len;
- }
- }
-
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- ENET_NOCOPY_PTPFRAME_RECEIVE_ENHANCED_MODE(NULL);
-
- #else
-
- ENET_NOCOPY_FRAME_RECEIVE();
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- return p;
- }
- /**
- * This function should be called when a packet is ready to be read
- * from the interface. It uses the function low_level_input() that
- * should handle the actual reception of bytes from the network
- * interface. Then the type of the received packet is determined and
- * the appropriate input function is called.
- *
- * @param netif the lwip network interface structure for this ethernetif
- */
- err_t ethernetif_input(struct netif *netif)
- {
- err_t err;
- struct pbuf *p;
- /* move received packet into a new pbuf */
- p = low_level_input(netif);
- /* no packet could be read, silently ignore this */
- if (p == NULL) return ERR_MEM;
- /* entry point to the LwIP stack */
- err = netif->input(p, netif);
-
- if (err != ERR_OK){
- LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
- pbuf_free(p);
- p = NULL;
- }
- return err;
- }
- /**
- * Should be called at the beginning of the program to set up the
- * network interface. It calls the function low_level_init() to do the
- * actual setup of the hardware.
- *
- * This function should be passed as a parameter to netif_add().
- *
- * @param netif the lwip network interface structure for this ethernetif
- * @return ERR_OK if the loopif is initialized
- * ERR_MEM if private data couldn't be allocated
- * any other err_t on error
- */
- err_t ethernetif_init(struct netif *netif)
- {
- LWIP_ASSERT("netif != NULL", (netif != NULL));
-
- #if LWIP_NETIF_HOSTNAME
- /* Initialize interface hostname */
- netif->hostname = "Gigadevice.COM_lwip";
- #endif /* LWIP_NETIF_HOSTNAME */
- netif->name[0] = IFNAME0;
- netif->name[1] = IFNAME1;
- /* We directly use etharp_output() here to save a function call.
- * You can instead declare your own function an call etharp_output()
- * from it if you have to do some checks before sending (e.g. if link
- * is available...) */
- netif->output = etharp_output;
- netif->linkoutput = low_level_output;
- /* initialize the hardware */
- low_level_init(netif);
- return ERR_OK;
- }
|