ethernetif.c 9.5 KB

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