ethernetif.c 13 KB

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