ethernetif.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @file
  3. * Ethernet Interface for standalone applications (without RTOS)
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. #include "lwip/mem.h"
  38. #include "netif/etharp.h"
  39. #include "ethernetif.h"
  40. #include "gd32f30x_enet.h"
  41. #include "main.h"
  42. #include <string.h>
  43. /* network interface name */
  44. #define IFNAME0 'G'
  45. #define IFNAME1 'D'
  46. /* ENET RxDMA/TxDMA descriptor */
  47. extern enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM], txdesc_tab[ENET_TXBUF_NUM];
  48. /* ENET receive buffer */
  49. extern uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE];
  50. /* ENET transmit buffer */
  51. extern uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE];
  52. /*global transmit and receive descriptors pointers */
  53. extern enet_descriptors_struct *dma_current_txdesc;
  54. extern enet_descriptors_struct *dma_current_rxdesc;
  55. /* preserve another ENET RxDMA/TxDMA ptp descriptor for normal mode */
  56. enet_descriptors_struct ptp_txstructure[ENET_TXBUF_NUM];
  57. enet_descriptors_struct ptp_rxstructure[ENET_RXBUF_NUM];
  58. /**
  59. * In this function, the hardware should be initialized.
  60. * Called from ethernetif_init().
  61. *
  62. * @param netif the already initialized lwip network interface structure
  63. * for this ethernetif
  64. */
  65. static void low_level_init(struct netif *netif)
  66. {
  67. #ifdef CHECKSUM_BY_HARDWARE
  68. int i;
  69. #endif /* CHECKSUM_BY_HARDWARE */
  70. /* set MAC hardware address length */
  71. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  72. /* set MAC hardware address */
  73. netif->hwaddr[0] = MAC_ADDR0;
  74. netif->hwaddr[1] = MAC_ADDR1;
  75. netif->hwaddr[2] = MAC_ADDR2;
  76. netif->hwaddr[3] = MAC_ADDR3;
  77. netif->hwaddr[4] = MAC_ADDR4;
  78. netif->hwaddr[5] = MAC_ADDR5;
  79. /* initialize MAC address in ethernet MAC */
  80. enet_mac_address_set(ENET_MAC_ADDRESS0, netif->hwaddr);
  81. /* maximum transfer unit */
  82. netif->mtu = 1500;
  83. /* device capabilities */
  84. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  85. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  86. /* initialize descriptors list: chain/ring mode */
  87. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  88. enet_ptp_enhanced_descriptors_chain_init(ENET_DMA_TX);
  89. enet_ptp_enhanced_descriptors_chain_init(ENET_DMA_RX);
  90. #else
  91. enet_descriptors_chain_init(ENET_DMA_TX);
  92. enet_descriptors_chain_init(ENET_DMA_RX);
  93. // enet_descriptors_ring_init(ENET_DMA_TX);
  94. // enet_descriptors_ring_init(ENET_DMA_RX);
  95. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  96. /* enable ethernet Rx interrrupt */
  97. { int i;
  98. for(i=0; i<ENET_RXBUF_NUM; i++){
  99. enet_rx_desc_immediate_receive_complete_interrupt(&rxdesc_tab[i]);
  100. }
  101. }
  102. #ifdef CHECKSUM_BY_HARDWARE
  103. /* enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
  104. for(i=0; i < ENET_TXBUF_NUM; i++){
  105. enet_transmit_checksum_config(&txdesc_tab[i], ENET_CHECKSUM_TCPUDPICMP_FULL);
  106. }
  107. #endif /* CHECKSUM_BY_HARDWARE */
  108. /* note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
  109. /* enable MAC and DMA transmission and reception */
  110. enet_enable();
  111. }
  112. /**
  113. * This function should do the actual transmission of the packet. The packet is
  114. * contained in the pbuf that is passed to the function. This pbuf
  115. * might be chained.
  116. *
  117. * @param netif the lwip network interface structure for this ethernetif
  118. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  119. * @return ERR_OK if the packet could be sent
  120. * an err_t value if the packet couldn't be sent
  121. *
  122. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  123. * strange results. You might consider waiting for space in the DMA queue
  124. * to become availale since the stack doesn't retry to send a packet
  125. * dropped because of memory failure (except for the TCP timers).
  126. */
  127. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  128. {
  129. struct pbuf *q;
  130. int framelength = 0;
  131. uint8_t *buffer;
  132. while((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
  133. }
  134. buffer = (uint8_t *)(enet_desc_information_get(dma_current_txdesc, TXDESC_BUFFER_1_ADDR));
  135. /* copy frame from pbufs to driver buffers */
  136. for(q = p; q != NULL; q = q->next){
  137. memcpy((uint8_t *)&buffer[framelength], q->payload, q->len);
  138. framelength = framelength + q->len;
  139. }
  140. /* note: padding and CRC for transmitted frame
  141. are automatically inserted by DMA */
  142. /* transmit descriptors to give to DMA */
  143. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  144. ENET_NOCOPY_PTPFRAME_TRANSMIT_ENHANCED_MODE(framelength, NULL);
  145. #else
  146. ENET_NOCOPY_FRAME_TRANSMIT(framelength);
  147. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  148. return ERR_OK;
  149. }
  150. /**
  151. * Should allocate a pbuf and transfer the bytes of the incoming
  152. * packet from the interface into the pbuf.
  153. *
  154. * @param netif the lwip network interface structure for this ethernetif
  155. * @return a pbuf filled with the received packet (including MAC header)
  156. * NULL on memory error
  157. */
  158. static struct pbuf * low_level_input(struct netif *netif)
  159. {
  160. struct pbuf *p, *q;
  161. u16_t len;
  162. int l =0;
  163. uint8_t *buffer;
  164. p = NULL;
  165. /* obtain the size of the packet and put it into the "len" variable. */
  166. len = enet_desc_information_get(dma_current_rxdesc, RXDESC_FRAME_LENGTH);
  167. buffer = (uint8_t *)(enet_desc_information_get(dma_current_rxdesc, RXDESC_BUFFER_1_ADDR));
  168. /* we allocate a pbuf chain of pbufs from the Lwip buffer pool */
  169. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  170. /* copy received frame to pbuf chain */
  171. if (p != NULL){
  172. for (q = p; q != NULL; q = q->next){
  173. memcpy((uint8_t *)q->payload, (u8_t*)&buffer[l], q->len);
  174. l = l + q->len;
  175. }
  176. }
  177. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  178. ENET_NOCOPY_PTPFRAME_RECEIVE_ENHANCED_MODE(NULL);
  179. #else
  180. ENET_NOCOPY_FRAME_RECEIVE();
  181. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  182. return p;
  183. }
  184. /**
  185. * This function should be called when a packet is ready to be read
  186. * from the interface. It uses the function low_level_input() that
  187. * should handle the actual reception of bytes from the network
  188. * interface. Then the type of the received packet is determined and
  189. * the appropriate input function is called.
  190. *
  191. * @param netif the lwip network interface structure for this ethernetif
  192. */
  193. err_t ethernetif_input(struct netif *netif)
  194. {
  195. err_t err;
  196. struct pbuf *p;
  197. /* move received packet into a new pbuf */
  198. p = low_level_input(netif);
  199. /* no packet could be read, silently ignore this */
  200. if (p == NULL) return ERR_MEM;
  201. /* entry point to the LwIP stack */
  202. err = netif->input(p, netif);
  203. if (err != ERR_OK){
  204. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  205. pbuf_free(p);
  206. p = NULL;
  207. }
  208. return err;
  209. }
  210. /**
  211. * Should be called at the beginning of the program to set up the
  212. * network interface. It calls the function low_level_init() to do the
  213. * actual setup of the hardware.
  214. *
  215. * This function should be passed as a parameter to netif_add().
  216. *
  217. * @param netif the lwip network interface structure for this ethernetif
  218. * @return ERR_OK if the loopif is initialized
  219. * ERR_MEM if private data couldn't be allocated
  220. * any other err_t on error
  221. */
  222. err_t ethernetif_init(struct netif *netif)
  223. {
  224. LWIP_ASSERT("netif != NULL", (netif != NULL));
  225. #if LWIP_NETIF_HOSTNAME
  226. /* Initialize interface hostname */
  227. netif->hostname = "Gigadevice.COM_lwip";
  228. #endif /* LWIP_NETIF_HOSTNAME */
  229. netif->name[0] = IFNAME0;
  230. netif->name[1] = IFNAME1;
  231. /* We directly use etharp_output() here to save a function call.
  232. * You can instead declare your own function an call etharp_output()
  233. * from it if you have to do some checks before sending (e.g. if link
  234. * is available...) */
  235. netif->output = etharp_output;
  236. netif->linkoutput = low_level_output;
  237. /* initialize the hardware */
  238. low_level_init(netif);
  239. return ERR_OK;
  240. }