ethernetif_eth.c 14 KB

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