ethernetif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.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 "main.h"
  46. #include "netif/etharp.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. low_netif = netif;
  98. /* create binary semaphore used for informing ethernetif of frame reception */
  99. if (NULL == g_enet_rx_sem)
  100. {
  101. g_enet_rx_sem = OSSemCreate(0);
  102. }
  103. /* Initialize Tx Descriptors list: Chain Mode */
  104. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  105. /* Initialize Rx Descriptors list: Chain Mode */
  106. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  107. // // /* enable ethernet Rx interrrupt */
  108. // {
  109. // int i;
  110. // for (i = 0; i < ETH_RXBUFNB; i++)
  111. // {
  112. // ETH_DMARxDescTransmitITConfig(&DMARxDscrTab[i], ENABLE);
  113. // }
  114. // }
  115. #ifdef CHECKSUM_BY_HARDWARE
  116. /* Enable the TCP, UDP and ICMP checksum insertion for the Tx frames */
  117. for (i = 0; i < ETH_TXBUFNB; i++)
  118. {
  119. ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
  120. }
  121. #endif
  122. /* Note: TCP, UDP, ICMP checksum checking for received frame are enabled in DMA config */
  123. /* create the task that handles the ENET RX */
  124. OSTaskCreateExt((void (*)(void *))ethernetif_input,
  125. (void *)0,
  126. (OS_STK *)&enet_rx_task_stk[ENET_RX_TASK_STK_SIZE - 1],
  127. (INT8U)ENET_RX_TASK_PRIO,
  128. (INT16U)ENET_RX_TASK_PRIO,
  129. (OS_STK *)&enet_rx_task_stk[0],
  130. (INT32U)ENET_RX_TASK_STK_SIZE,
  131. (void *)0,
  132. (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
  133. /* Enable MAC and DMA transmission and reception */
  134. ETH_Start();
  135. }
  136. /**
  137. * This function should do the actual transmission of the packet. The packet is
  138. * contained in the pbuf that is passed to the function. This pbuf
  139. * might be chained.
  140. *
  141. * @param netif the lwip network interface structure for this ethernetif
  142. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  143. * @return ERR_OK if the packet could be sent
  144. * an err_t value if the packet couldn't be sent
  145. *
  146. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  147. * strange results. You might consider waiting for space in the DMA queue
  148. * to become availale since the stack doesn't retry to send a packet
  149. * dropped because of memory failure (except for the TCP timers).
  150. */
  151. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  152. {
  153. static OS_EVENT *p_enet_tx_sem = NULL;
  154. INT8U err;
  155. struct pbuf *q;
  156. uint8_t *buffer;
  157. FrameTypeDef frame;
  158. uint16_t framelength = 0;
  159. ErrorStatus reval = ERROR;
  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. while ((uint32_t)RESET != (DMATxDescToSet->Status & ETH_DMATxDesc_OWN))
  168. {
  169. }
  170. /* get received frame */
  171. frame = ETH_Get_Received_Frame();
  172. /* Obtain the size of the packet and put it into the "len" variable. */
  173. buffer = (u8 *)frame.buffer;
  174. for (q = p; q != NULL; q = q->next)
  175. {
  176. memcpy((uint8_t *)&buffer[framelength], q->payload, q->len);
  177. framelength = framelength + q->len;
  178. }
  179. /* Prepare transmit descriptors to give to DMA*/
  180. reval = ETH_Prepare_Transmit_Descriptors(framelength);
  181. SYS_ARCH_UNPROTECT(sr);
  182. /* give semaphore and exit */
  183. OSSemPost(p_enet_tx_sem);
  184. if (SUCCESS == reval)
  185. {
  186. return ERR_OK;
  187. }
  188. else
  189. {
  190. while (1)
  191. {
  192. }
  193. }
  194. }
  195. /**
  196. * Should allocate a pbuf and transfer the bytes of the incoming
  197. * packet from the interface into the pbuf.
  198. *
  199. * @param netif the lwip network interface structure for this ethernetif
  200. * @return a pbuf filled with the received packet (including MAC header)
  201. * NULL on memory error
  202. */
  203. static struct pbuf *low_level_input(struct netif *netif)
  204. {
  205. struct pbuf *p = NULL, *q;
  206. uint32_t l = 0;
  207. u16_t len;
  208. FrameTypeDef frame;
  209. uint8_t *buffer;
  210. /* get received frame */
  211. frame = ETH_Get_Received_Frame();
  212. /* Obtain the size of the packet and put it into the "len" variable. */
  213. len = frame.length;
  214. buffer = (u8 *)frame.buffer;
  215. if (len > 0)
  216. {
  217. /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  218. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  219. }
  220. if (p != NULL)
  221. {
  222. for (q = p; q != NULL; q = q->next)
  223. {
  224. memcpy((uint8_t *)q->payload, (u8_t *)&buffer[l], q->len);
  225. l = l + q->len;
  226. }
  227. }
  228. ETH_Get_Received_Frame_interrupt();
  229. return p;
  230. }
  231. // static struct pbuf *low_level_input(struct netif *netif)
  232. // {
  233. // struct pbuf *p, *q;
  234. // uint32_t len;
  235. // FrameTypeDef frame;
  236. // u8 *buffer;
  237. // __IO ETH_DMADESCTypeDef *DMARxDesc;
  238. // uint32_t bufferoffset = 0;
  239. // uint32_t payloadoffset = 0;
  240. // uint32_t byteslefttocopy = 0;
  241. // uint32_t i = 0;
  242. // /* get received frame */
  243. // frame = ETH_Get_Received_Frame();
  244. // /* Obtain the size of the packet and put it into the "len" variable. */
  245. // len = frame.length;
  246. // buffer = (u8 *)frame.buffer;
  247. // /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  248. // p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  249. // if (p != NULL)
  250. // {
  251. // DMARxDesc = frame.descriptor;
  252. // bufferoffset = 0;
  253. // for (q = p; q != NULL; q = q->next)
  254. // {
  255. // byteslefttocopy = q->len;
  256. // payloadoffset = 0;
  257. // /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
  258. // while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
  259. // {
  260. // /* Copy data to pbuf*/
  261. // memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  262. // /* Point to next descriptor */
  263. // DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  264. // buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
  265. // byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  266. // payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  267. // bufferoffset = 0;
  268. // }
  269. // /* Copy remaining data in pbuf */
  270. // memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), byteslefttocopy);
  271. // bufferoffset = bufferoffset + byteslefttocopy;
  272. // }
  273. // }
  274. // /* Release descriptors to DMA */
  275. // DMARxDesc = frame.descriptor;
  276. // /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  277. // for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++)
  278. // {
  279. // DMARxDesc->Status = ETH_DMARxDesc_OWN;
  280. // DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
  281. // }
  282. // /* Clear Segment_Count */
  283. // DMA_RX_FRAME_infos->Seg_Count = 0;
  284. // /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  285. // if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
  286. // {
  287. // /* Clear RBUS ETHERNET DMA flag */
  288. // ETH->DMASR = ETH_DMASR_RBUS;
  289. // /* Resume DMA reception */
  290. // ETH->DMARPDR = 0;
  291. // }
  292. // return p;
  293. // }
  294. /**
  295. * This function should be called when a packet is ready to be read
  296. * from the interface. It uses the function low_level_input() that
  297. * should handle the actual reception of bytes from the network
  298. * interface. Then the type of the received packet is determined and
  299. * the appropriate input function is called.
  300. *
  301. * @param netif the lwip network interface structure for this ethernetif
  302. */
  303. void ethernetif_input(void *pvParameters)
  304. {
  305. struct pbuf *p;
  306. INT8U err;
  307. SYS_ARCH_DECL_PROTECT(sr);
  308. for (;;)
  309. {
  310. OSSemPend(g_enet_rx_sem, 0, &err);
  311. TRY_GET_NEXT_FRAME:
  312. SYS_ARCH_PROTECT(sr);
  313. p = low_level_input(low_netif);
  314. SYS_ARCH_UNPROTECT(sr);
  315. if (p != NULL)
  316. {
  317. if (ERR_OK != low_netif->input(p, low_netif))
  318. {
  319. pbuf_free(p);
  320. }
  321. else
  322. {
  323. goto TRY_GET_NEXT_FRAME;
  324. }
  325. }
  326. }
  327. }
  328. // err_t ethernetif_input(struct netif *netif)
  329. // {
  330. // err_t err;
  331. // struct pbuf *p;
  332. // /* move received packet into a new pbuf */
  333. // p = low_level_input(netif);
  334. // /* no packet could be read, silently ignore this */
  335. // if (p == NULL)
  336. // return ERR_MEM;
  337. // /* entry point to the LwIP stack */
  338. // err = netif->input(p, netif);
  339. // if (err != ERR_OK)
  340. // {
  341. // LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  342. // pbuf_free(p);
  343. // }
  344. // return err;
  345. // }
  346. /**
  347. * Should be called at the beginning of the program to set up the
  348. * network interface. It calls the function low_level_init() to do the
  349. * actual setup of the hardware.
  350. *
  351. * This function should be passed as a parameter to netif_add().
  352. *
  353. * @param netif the lwip network interface structure for this ethernetif
  354. * @return ERR_OK if the loopif is initialized
  355. * ERR_MEM if private data couldn't be allocated
  356. * any other err_t on error
  357. */
  358. err_t ethernetif_init(struct netif *netif)
  359. {
  360. LWIP_ASSERT("netif != NULL", (netif != NULL));
  361. #if LWIP_NETIF_HOSTNAME
  362. /* Initialize interface hostname */
  363. netif->hostname = "lwip";
  364. #endif /* LWIP_NETIF_HOSTNAME */
  365. netif->name[0] = IFNAME0;
  366. netif->name[1] = IFNAME1;
  367. netif->output = etharp_output;
  368. netif->linkoutput = low_level_output;
  369. /* initialize the hardware */
  370. low_level_init(netif);
  371. return ERR_OK;
  372. }