ethernetif.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <[email protected]>
  36. *
  37. */
  38. #include "lwip/opt.h"
  39. #include "lwip/mem.h"
  40. #include "netif/etharp.h"
  41. #include "ethernetif.h"
  42. #include "stm32f4x7_eth.h"
  43. #include "main.h"
  44. #include <string.h>
  45. /* Network interface name */
  46. #define IFNAME0 's'
  47. #define IFNAME1 't'
  48. /* Ethernet Rx & Tx DMA Descriptors */
  49. extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
  50. /* Ethernet Driver Receive buffers */
  51. extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
  52. /* Ethernet Driver Transmit buffers */
  53. extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
  54. /* Global pointers to track current transmit and receive descriptors */
  55. extern ETH_DMADESCTypeDef *DMATxDescToSet;
  56. extern ETH_DMADESCTypeDef *DMARxDescToGet;
  57. /* Global pointer for last received frame infos */
  58. extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
  59. /**
  60. * In this function, the hardware should be initialized.
  61. * Called from ethernetif_init().
  62. *
  63. * @param netif the already initialized lwip network interface structure
  64. * for this ethernetif
  65. */
  66. static void low_level_init(struct netif *netif)
  67. {
  68. #ifdef CHECKSUM_BY_HARDWARE
  69. int i;
  70. #endif
  71. /* set MAC hardware address length */
  72. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  73. /* set MAC hardware address */
  74. netif->hwaddr[0] = MAC_ADDR0;
  75. netif->hwaddr[1] = MAC_ADDR1;
  76. netif->hwaddr[2] = MAC_ADDR2;
  77. netif->hwaddr[3] = MAC_ADDR3;
  78. netif->hwaddr[4] = MAC_ADDR4;
  79. netif->hwaddr[5] = MAC_ADDR5;
  80. /* initialize MAC address in ethernet MAC */
  81. ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
  82. /* maximum transfer unit */
  83. netif->mtu = 1500;
  84. /* device capabilities */
  85. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  86. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  87. /* Initialize Tx Descriptors list: Chain Mode */
  88. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  89. /* Initialize Rx Descriptors list: Chain Mode */
  90. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  91. #ifdef CHECKSUM_BY_HARDWARE
  92. /* Enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
  93. for(i=0; i<ETH_TXBUFNB; i++)
  94. {
  95. ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
  96. }
  97. #endif
  98. /* Note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
  99. /* Enable MAC and DMA transmission and reception */
  100. ETH_Start();
  101. }
  102. /**
  103. * This function should do the actual transmission of the packet. The packet is
  104. * contained in the pbuf that is passed to the function. This pbuf
  105. * might be chained.
  106. *
  107. * @param netif the lwip network interface structure for this ethernetif
  108. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  109. * @return ERR_OK if the packet could be sent
  110. * an err_t value if the packet couldn't be sent
  111. *
  112. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  113. * strange results. You might consider waiting for space in the DMA queue
  114. * to become availale since the stack doesn't retry to send a packet
  115. * dropped because of memory failure (except for the TCP timers).
  116. */
  117. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  118. {
  119. err_t errval;
  120. struct pbuf *q;
  121. u8 *buffer = (u8 *)(DMATxDescToSet->Buffer1Addr);
  122. __IO ETH_DMADESCTypeDef *DmaTxDesc;
  123. uint16_t framelength = 0;
  124. uint32_t bufferoffset = 0;
  125. uint32_t byteslefttocopy = 0;
  126. uint32_t payloadoffset = 0;
  127. DmaTxDesc = DMATxDescToSet;
  128. bufferoffset = 0;
  129. /* copy frame from pbufs to driver buffers */
  130. for(q = p; q != NULL; q = q->next)
  131. {
  132. /* Is this buffer available? If not, goto error */
  133. if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  134. {
  135. errval = ERR_BUF;
  136. goto error;
  137. }
  138. /* Get bytes in current lwIP buffer */
  139. byteslefttocopy = q->len;
  140. payloadoffset = 0;
  141. /* Check if the length of data to copy is bigger than Tx buffer size*/
  142. while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
  143. {
  144. /* Copy data to Tx buffer*/
  145. memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
  146. /* Point to next descriptor */
  147. DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  148. /* Check if the buffer is available */
  149. if((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  150. {
  151. errval = ERR_USE;
  152. goto error;
  153. }
  154. buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
  155. byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  156. payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  157. framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  158. bufferoffset = 0;
  159. }
  160. /* Copy the remaining bytes */
  161. memcpy( (u8_t*)((u8_t*)buffer + bufferoffset), (u8_t*)((u8_t*)q->payload + payloadoffset), byteslefttocopy );
  162. bufferoffset = bufferoffset + byteslefttocopy;
  163. framelength = framelength + byteslefttocopy;
  164. }
  165. /* Note: padding and CRC for transmitted frame
  166. are automatically inserted by DMA */
  167. /* Prepare transmit descriptors to give to DMA*/
  168. ETH_Prepare_Transmit_Descriptors(framelength);
  169. errval = ERR_OK;
  170. error:
  171. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  172. if ((ETH->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
  173. {
  174. /* Clear TUS ETHERNET DMA flag */
  175. ETH->DMASR = ETH_DMASR_TUS;
  176. /* Resume DMA transmission*/
  177. ETH->DMATPDR = 0;
  178. }
  179. return errval;
  180. }
  181. /**
  182. * Should allocate a pbuf and transfer the bytes of the incoming
  183. * packet from the interface into the pbuf.
  184. *
  185. * @param netif the lwip network interface structure for this ethernetif
  186. * @return a pbuf filled with the received packet (including MAC header)
  187. * NULL on memory error
  188. */
  189. static struct pbuf * low_level_input(struct netif *netif)
  190. {
  191. struct pbuf *p, *q;
  192. uint32_t len;
  193. FrameTypeDef frame;
  194. u8 *buffer;
  195. __IO ETH_DMADESCTypeDef *DMARxDesc;
  196. uint32_t bufferoffset = 0;
  197. uint32_t payloadoffset = 0;
  198. uint32_t byteslefttocopy = 0;
  199. uint32_t i=0;
  200. /* get received frame */
  201. frame = ETH_Get_Received_Frame();
  202. /* Obtain the size of the packet and put it into the "len" variable. */
  203. len = frame.length;
  204. buffer = (u8 *)frame.buffer;
  205. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  206. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  207. if (p != NULL)
  208. {
  209. DMARxDesc = frame.descriptor;
  210. bufferoffset = 0;
  211. for(q = p; q != NULL; q = q->next)
  212. {
  213. byteslefttocopy = q->len;
  214. payloadoffset = 0;
  215. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  216. while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
  217. {
  218. /* Copy data to pbuf*/
  219. memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  220. /* Point to next descriptor */
  221. DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  222. buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
  223. byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  224. payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  225. bufferoffset = 0;
  226. }
  227. /* Copy remaining data in pbuf */
  228. memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy);
  229. bufferoffset = bufferoffset + byteslefttocopy;
  230. }
  231. }
  232. /* Release descriptors to DMA */
  233. DMARxDesc =frame.descriptor;
  234. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  235. for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
  236. {
  237. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  238. DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  239. }
  240. /* Clear Segment_Count */
  241. DMA_RX_FRAME_infos->Seg_Count =0;
  242. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  243. if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
  244. {
  245. /* Clear RBUS ETHERNET DMA flag */
  246. ETH->DMASR = ETH_DMASR_RBUS;
  247. /* Resume DMA reception */
  248. ETH->DMARPDR = 0;
  249. }
  250. return p;
  251. }
  252. /**
  253. * This function should be called when a packet is ready to be read
  254. * from the interface. It uses the function low_level_input() that
  255. * should handle the actual reception of bytes from the network
  256. * interface. Then the type of the received packet is determined and
  257. * the appropriate input function is called.
  258. *
  259. * @param netif the lwip network interface structure for this ethernetif
  260. */
  261. err_t ethernetif_input(struct netif *netif)
  262. {
  263. err_t err;
  264. struct pbuf *p;
  265. /* move received packet into a new pbuf */
  266. p = low_level_input(netif);
  267. /* no packet could be read, silently ignore this */
  268. if (p == NULL) return ERR_MEM;
  269. /* entry point to the LwIP stack */
  270. err = netif->input(p, netif);
  271. if (err != ERR_OK)
  272. {
  273. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  274. pbuf_free(p);
  275. }
  276. return err;
  277. }
  278. /**
  279. * Should be called at the beginning of the program to set up the
  280. * network interface. It calls the function low_level_init() to do the
  281. * actual setup of the hardware.
  282. *
  283. * This function should be passed as a parameter to netif_add().
  284. *
  285. * @param netif the lwip network interface structure for this ethernetif
  286. * @return ERR_OK if the loopif is initialized
  287. * ERR_MEM if private data couldn't be allocated
  288. * any other err_t on error
  289. */
  290. err_t ethernetif_init(struct netif *netif)
  291. {
  292. LWIP_ASSERT("netif != NULL", (netif != NULL));
  293. #if LWIP_NETIF_HOSTNAME
  294. /* Initialize interface hostname */
  295. netif->hostname = "lwip";
  296. #endif /* LWIP_NETIF_HOSTNAME */
  297. netif->name[0] = IFNAME0;
  298. netif->name[1] = IFNAME1;
  299. /* We directly use etharp_output() here to save a function call.
  300. * You can instead declare your own function an call etharp_output()
  301. * from it if you have to do some checks before sending (e.g. if link
  302. * is available...) */
  303. netif->output = etharp_output;
  304. netif->linkoutput = low_level_output;
  305. /* initialize the hardware */
  306. low_level_init(netif);
  307. return ERR_OK;
  308. }