ethernetif_eth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "ethernetif_eth.h"
  39. #include "lwip/def.h"
  40. #include "lwip/err.h"
  41. #include "lwip/mem.h"
  42. #include "lwip/opt.h"
  43. #include "lwip/pbuf.h"
  44. #include "lwip/timeouts.h"
  45. #include "lwip_init.h"
  46. #include "main.h"
  47. #include "netif/etharp.h"
  48. #include "stm32f4x7_eth.h"
  49. #include <string.h>
  50. #define ENET_RX_TASK_PRIO (4)
  51. #define ENET_RX_TASK_STK_SIZE (2048)
  52. CPU_STK enet_rx_task_stk[ENET_RX_TASK_STK_SIZE];
  53. /* Network interface name */
  54. #define IFNAME0 's'
  55. #define IFNAME1 't'
  56. /* Ethernet Rx & Tx DMA Descriptors */
  57. extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
  58. /* Ethernet Driver Receive buffers */
  59. extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
  60. /* Ethernet Driver Transmit buffers */
  61. extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
  62. /* Global pointers to track current transmit and receive descriptors */
  63. extern ETH_DMADESCTypeDef *DMATxDescToSet;
  64. extern ETH_DMADESCTypeDef *DMARxDescToGet;
  65. /* Global pointer for last received frame infos */
  66. extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
  67. static struct netif *low_netif = NULL;
  68. OS_EVENT *g_enet_rx_sem = NULL;
  69. static void ethernetif_input(void *pvParameters);
  70. /**
  71. * In this function, the hardware should be initialized.
  72. * Called from ethernetif_init().
  73. *
  74. * @param netif the already initialized lwip network interface structure
  75. * for this ethernetif
  76. */
  77. static void low_level_init(struct netif *netif)
  78. {
  79. #ifdef CHECKSUM_BY_HARDWARE
  80. int i;
  81. #endif
  82. /* set MAC hardware address length */
  83. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  84. /* set MAC hardware address */ /* 网卡的 MAC 修改2 */
  85. netif->hwaddr[0] = ethdev.mac[0];
  86. netif->hwaddr[1] = ethdev.mac[1];
  87. netif->hwaddr[2] = ethdev.mac[2];
  88. netif->hwaddr[3] = ethdev.mac[3];
  89. netif->hwaddr[4] = ethdev.mac[4];
  90. netif->hwaddr[5] = ethdev.mac[5];
  91. /* initialize MAC address in ethernet MAC */
  92. ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
  93. /* maximum transfer unit */
  94. netif->mtu = 1500;
  95. /* device capabilities */
  96. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  97. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  98. low_netif = netif;
  99. /* create binary semaphore used for informing ethernetif of frame reception */
  100. if (NULL == g_enet_rx_sem)
  101. {
  102. g_enet_rx_sem = OSSemCreate(0);
  103. }
  104. /* Initialize Tx Descriptors list: Chain Mode */
  105. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  106. /* Initialize Rx Descriptors list: Chain Mode */
  107. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  108. /* Enable Ethernet Rx interrrupt */
  109. for (i = 0; i < ETH_RXBUFNB; i++)
  110. {
  111. ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
  112. }
  113. #ifdef CHECKSUM_BY_HARDWARE
  114. /* Enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
  115. for (i = 0; i < ETH_TXBUFNB; i++)
  116. {
  117. ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
  118. }
  119. #endif
  120. /* Note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
  121. /* create the task that handles the ENET RX */
  122. OSTaskCreateExt((void (*)(void *))ethernetif_input,
  123. (void *)0,
  124. (OS_STK *)&enet_rx_task_stk[ENET_RX_TASK_STK_SIZE - 1],
  125. (INT8U)ENET_RX_TASK_PRIO,
  126. (INT16U)ENET_RX_TASK_PRIO,
  127. (OS_STK *)&enet_rx_task_stk[0],
  128. (INT32U)ENET_RX_TASK_STK_SIZE,
  129. (void *)0,
  130. (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
  131. /* Enable MAC and DMA transmission and reception */
  132. ETH_Start();
  133. }
  134. /**
  135. * This function should do the actual transmission of the packet. The packet is
  136. * contained in the pbuf that is passed to the function. This pbuf
  137. * might be chained.
  138. *
  139. * @param netif the lwip network interface structure for this ethernetif
  140. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  141. * @return ERR_OK if the packet could be sent
  142. * an err_t value if the packet couldn't be sent
  143. *
  144. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  145. * strange results. You might consider waiting for space in the DMA queue
  146. * to become availale since the stack doesn't retry to send a packet
  147. * dropped because of memory failure (except for the TCP timers).
  148. */
  149. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  150. {
  151. static OS_EVENT *p_enet_tx_sem = NULL;
  152. INT8U err;
  153. struct pbuf *q;
  154. u8 *buffer;
  155. __IO ETH_DMADESCTypeDef *DmaTxDesc;
  156. uint16_t framelength = 0;
  157. uint32_t bufferoffset = 0;
  158. uint32_t byteslefttocopy = 0;
  159. uint32_t payloadoffset = 0;
  160. SYS_ARCH_DECL_PROTECT(sr);
  161. if (NULL == p_enet_tx_sem)
  162. {
  163. p_enet_tx_sem = OSSemCreate(1);
  164. }
  165. OSSemPend(p_enet_tx_sem, 0, &err);
  166. SYS_ARCH_PROTECT(sr);
  167. DmaTxDesc = DMATxDescToSet;
  168. buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
  169. bufferoffset = 0;
  170. for (q = p; q != NULL; q = q->next)
  171. {
  172. if ((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  173. {
  174. goto error;
  175. }
  176. /* Get bytes in current lwIP buffer */
  177. byteslefttocopy = q->len;
  178. payloadoffset = 0;
  179. /* Check if the length of data to copy is bigger than Tx buffer size*/
  180. while ((byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE)
  181. {
  182. /* Copy data to Tx buffer*/
  183. memcpy((u8_t *)((u8_t *)buffer + bufferoffset), (u8_t *)((u8_t *)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset));
  184. /* Point to next descriptor */
  185. DmaTxDesc = (ETH_DMADESCTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  186. /* Check if the buffer is available */
  187. if ((DmaTxDesc->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  188. {
  189. goto error;
  190. }
  191. buffer = (u8 *)(DmaTxDesc->Buffer1Addr);
  192. byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  193. payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  194. framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  195. bufferoffset = 0;
  196. }
  197. /* Copy the remaining bytes */
  198. memcpy((u8_t *)((u8_t *)buffer + bufferoffset), (u8_t *)((u8_t *)q->payload + payloadoffset), byteslefttocopy);
  199. bufferoffset = bufferoffset + byteslefttocopy;
  200. framelength = framelength + byteslefttocopy;
  201. }
  202. /* Prepare transmit descriptors to give to DMA*/
  203. ETH_Prepare_Transmit_Descriptors(framelength);
  204. /* Give semaphore and exit */
  205. error:
  206. /* give semaphore and exit */
  207. OSSemPost(p_enet_tx_sem);
  208. SYS_ARCH_UNPROTECT(sr);
  209. return ERR_OK;
  210. }
  211. /**
  212. * Should allocate a pbuf and transfer the bytes of the incoming
  213. * packet from the interface into the pbuf.
  214. *
  215. * @param netif the lwip network interface structure for this ethernetif
  216. * @return a pbuf filled with the received packet (including MAC header)
  217. * NULL on memory error
  218. */
  219. static struct pbuf *low_level_input(struct netif *netif)
  220. {
  221. struct pbuf *p = NULL, *q;
  222. u32_t len;
  223. FrameTypeDef frame;
  224. u8 *buffer;
  225. __IO ETH_DMADESCTypeDef *DMARxDesc;
  226. uint32_t bufferoffset = 0;
  227. uint32_t payloadoffset = 0;
  228. uint32_t byteslefttocopy = 0;
  229. uint32_t i = 0;
  230. /* get received frame */
  231. frame = ETH_Get_Received_Frame_interrupt();
  232. /* Obtain the size of the packet and put it into the "len" variable. */
  233. len = frame.length;
  234. buffer = (u8 *)frame.buffer;
  235. if (len > 0)
  236. {
  237. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  238. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  239. }
  240. if (p != NULL)
  241. {
  242. DMARxDesc = frame.descriptor;
  243. bufferoffset = 0;
  244. for (q = p; q != NULL; q = q->next)
  245. {
  246. byteslefttocopy = q->len;
  247. payloadoffset = 0;
  248. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  249. while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
  250. {
  251. /* Copy data to pbuf*/
  252. memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  253. /* Point to next descriptor */
  254. DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  255. buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
  256. byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  257. payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  258. bufferoffset = 0;
  259. }
  260. /* Copy remaining data in pbuf */
  261. memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), byteslefttocopy);
  262. bufferoffset = bufferoffset + byteslefttocopy;
  263. }
  264. /* Release descriptors to DMA */
  265. DMARxDesc = frame.descriptor;
  266. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  267. for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++)
  268. {
  269. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  270. DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  271. }
  272. /* Clear Segment_Count */
  273. DMA_RX_FRAME_infos->Seg_Count = 0;
  274. /* added for test*/
  275. }
  276. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  277. if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
  278. {
  279. /* Clear RBUS ETHERNET DMA flag */
  280. ETH->DMASR = ETH_DMASR_RBUS;
  281. /* Resume DMA reception */
  282. ETH->DMARPDR = 0;
  283. }
  284. return p;
  285. }
  286. /**
  287. * This function should be called when a packet is ready to be read
  288. * from the interface. It uses the function low_level_input() that
  289. * should handle the actual reception of bytes from the network
  290. * interface. Then the type of the received packet is determined and
  291. * the appropriate input function is called.
  292. *
  293. * @param netif the lwip network interface structure for this ethernetif
  294. */
  295. void ethernetif_input(void *pvParameters)
  296. {
  297. struct pbuf *p;
  298. INT8U err;
  299. INT8U *pv;
  300. pv = pvParameters;
  301. SYS_ARCH_DECL_PROTECT(sr);
  302. for (;;)
  303. {
  304. OSSemPend(g_enet_rx_sem, 0, &err);
  305. TRY_GET_NEXT_FRAME:
  306. SYS_ARCH_PROTECT(sr);
  307. p = low_level_input(low_netif);
  308. SYS_ARCH_UNPROTECT(sr);
  309. if (p != NULL)
  310. {
  311. if (ERR_OK != low_netif->input(p, low_netif))
  312. {
  313. pbuf_free(p);
  314. }
  315. else
  316. {
  317. goto TRY_GET_NEXT_FRAME;
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Should be called at the beginning of the program to set up the
  324. * network interface. It calls the function low_level_init() to do the
  325. * actual setup of the hardware.
  326. *
  327. * This function should be passed as a parameter to netif_add().
  328. *
  329. * @param netif the lwip network interface structure for this ethernetif
  330. * @return ERR_OK if the loopif is initialized
  331. * ERR_MEM if private data couldn't be allocated
  332. * any other err_t on error
  333. */
  334. err_t ethernetif_eth_init(struct netif *netif)
  335. {
  336. LWIP_ASSERT("netif != NULL", (netif != NULL));
  337. #if LWIP_NETIF_HOSTNAME
  338. /* Initialize interface hostname */
  339. netif->hostname = "lwip";
  340. #endif /* LWIP_NETIF_HOSTNAME */
  341. netif->name[0] = IFNAME0;
  342. netif->name[1] = IFNAME1;
  343. netif->output = etharp_output;
  344. netif->linkoutput = low_level_output;
  345. /* initialize the hardware */
  346. low_level_init(netif);
  347. return ERR_OK;
  348. }