ethernetif.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * @file
  3. * Ethernet Interface Skeleton
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. /*
  38. * This file is a skeleton for developing Ethernet network interface
  39. * drivers for lwIP. Add code to the low_level functions and do a
  40. * search-and-replace for the word "ethernetif" to replace it with
  41. * something that better describes your network interface.
  42. */
  43. #include "lwip/opt.h"
  44. #include "lwip/def.h"
  45. #include "lwip/mem.h"
  46. #include "lwip/pbuf.h"
  47. #include "lwip/sys.h"
  48. #include "netif/etharp.h"
  49. #include "err.h"
  50. #include "ethernetif.h"
  51. #include "lwip/timers.h"
  52. #include "common_config.h"
  53. #include "settings_api.h"
  54. #include "stm32f4x7_eth.h"
  55. #include <string.h>
  56. #define netifMTU (1500)
  57. #define netifINTERFACE_TASK_STACK_SIZE ( 150 )
  58. #define netifINTERFACE_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
  59. #define netifGUARD_BLOCK_TIME ( 250 )
  60. /* The time to block waiting for input. */
  61. #define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( TickType_t ) 100 )
  62. /* Define those to better describe your network interface. */
  63. #define IFNAME0 's'
  64. #define IFNAME1 't'
  65. static struct netif *s_pxNetIf = NULL;
  66. SemaphoreHandle_t s_xSemaphore = NULL;
  67. /* Ethernet Rx & Tx DMA Descriptors */
  68. extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
  69. /* Ethernet Receive buffers */
  70. extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
  71. /* Ethernet Transmit buffers */
  72. extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
  73. /* Global pointers to track current transmit and receive descriptors */
  74. extern ETH_DMADESCTypeDef *DMATxDescToSet;
  75. extern ETH_DMADESCTypeDef *DMARxDescToGet;
  76. /* Global pointer for last received frame infos */
  77. extern ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
  78. static void ethernetif_input( void * pvParameters );
  79. static void arp_timer(void *arg);
  80. /**
  81. * In this function, the hardware should be initialized.
  82. * Called from ethernetif_init().
  83. *
  84. * @param netif the already initialized lwip network interface structure
  85. * for this ethernetif
  86. */
  87. static void low_level_init(struct netif *netif)
  88. {
  89. uint8_t mac[6];
  90. uint32_t i;
  91. /* set netif MAC hardware address length */
  92. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  93. /* set netif MAC hardware address */
  94. SETTINGS_GetMac(mac);
  95. netif->hwaddr[0] = mac[0];
  96. netif->hwaddr[1] = mac[1];
  97. netif->hwaddr[2] = mac[2];
  98. netif->hwaddr[3] = mac[3];
  99. netif->hwaddr[4] = mac[4];
  100. netif->hwaddr[5] = mac[5];
  101. /* set netif maximum transfer unit */
  102. netif->mtu = 1500;
  103. /* Accept broadcast address and ARP traffic */
  104. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  105. s_pxNetIf =netif;
  106. /* create binary semaphore used for informing ethernetif of frame reception */
  107. if (s_xSemaphore == NULL)
  108. {
  109. s_xSemaphore= xSemaphoreCreateCounting(20,0);
  110. }
  111. /* initialize MAC address in ethernet MAC */
  112. ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
  113. /* Initialize Tx Descriptors list: Chain Mode */
  114. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  115. /* Initialize Rx Descriptors list: Chain Mode */
  116. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  117. /* Enable Ethernet Rx interrrupt */
  118. {
  119. for(i=0; i<ETH_RXBUFNB; i++)
  120. {
  121. ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
  122. }
  123. }
  124. #ifdef CHECKSUM_BY_HARDWARE
  125. /* Enable the checksum insertion for the Tx frames */
  126. {
  127. for(i=0; i<ETH_TXBUFNB; i++)
  128. {
  129. ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
  130. }
  131. }
  132. #endif
  133. /* create the task that handles the ETH_MAC */
  134. xTaskCreate(ethernetif_input, "Eth_if", netifINTERFACE_TASK_STACK_SIZE, NULL,
  135. netifINTERFACE_TASK_PRIORITY,NULL);
  136. /* Enable MAC and DMA transmission and reception */
  137. ETH_Start();
  138. }
  139. /**
  140. * This function should do the actual transmission of the packet. The packet is
  141. * contained in the pbuf that is passed to the function. This pbuf
  142. * might be chained.
  143. *
  144. * @param netif the lwip network interface structure for this ethernetif
  145. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  146. * @return ERR_OK if the packet could be sent
  147. * an err_t value if the packet couldn't be sent
  148. *
  149. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  150. * strange results. You might consider waiting for space in the DMA queue
  151. * to become availale since the stack doesn't retry to send a packet
  152. * dropped because of memory failure (except for the TCP timers).
  153. */
  154. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  155. {
  156. static SemaphoreHandle_t xTxSemaphore = NULL;
  157. struct pbuf *q;
  158. uint32_t l = 0;
  159. u8 *buffer ;
  160. if (xTxSemaphore == NULL)
  161. {
  162. vSemaphoreCreateBinary (xTxSemaphore);
  163. }
  164. if (xSemaphoreTake(xTxSemaphore, netifGUARD_BLOCK_TIME))
  165. {
  166. buffer = (u8 *)(DMATxDescToSet->Buffer1Addr);
  167. for(q = p; q != NULL; q = q->next)
  168. {
  169. memcpy((u8_t*)&buffer[l], q->payload, q->len);
  170. l = l + q->len;
  171. }
  172. ETH_Prepare_Transmit_Descriptors(l);
  173. xSemaphoreGive(xTxSemaphore);
  174. }
  175. return ERR_OK;
  176. }
  177. /**
  178. * Should allocate a pbuf and transfer the bytes of the incoming
  179. * packet from the interface into the pbuf.
  180. *
  181. * @param netif the lwip network interface structure for this ethernetif
  182. * @return a pbuf filled with the received packet (including MAC header)
  183. * NULL on memory error
  184. */
  185. static struct pbuf * low_level_input(struct netif *netif)
  186. {
  187. struct pbuf *p, *q;
  188. u16_t len;
  189. uint32_t l=0,i =0;
  190. FrameTypeDef frame;
  191. u8 *buffer;
  192. __IO ETH_DMADESCTypeDef *DMARxNextDesc;
  193. p = NULL;
  194. /* Get received frame */
  195. frame = ETH_Get_Received_Frame_interrupt();
  196. /* check that frame has no error */
  197. if ((frame.descriptor->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET)
  198. {
  199. /* Obtain the size of the packet and put it into the "len" variable. */
  200. len = frame.length;
  201. buffer = (u8 *)frame.buffer;
  202. /* We allocate a pbuf chain of pbufs from the pool. */
  203. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  204. /* Copy received frame from ethernet driver buffer to stack buffer */
  205. if (p != NULL)
  206. {
  207. for (q = p; q != NULL; q = q->next)
  208. {
  209. memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len);
  210. l = l + q->len;
  211. }
  212. }
  213. }
  214. /* Release descriptors to DMA */
  215. /* Check if received frame with multiple DMA buffer segments */
  216. if (DMA_RX_FRAME_infos->Seg_Count > 1)
  217. {
  218. DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc;
  219. }
  220. else
  221. {
  222. DMARxNextDesc = frame.descriptor;
  223. }
  224. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  225. for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
  226. {
  227. DMARxNextDesc->Status = ETH_DMARxDesc_OWN;
  228. DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr);
  229. }
  230. /* Clear Segment_Count */
  231. DMA_RX_FRAME_infos->Seg_Count =0;
  232. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  233. if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
  234. {
  235. /* Clear RBUS ETHERNET DMA flag */
  236. ETH->DMASR = ETH_DMASR_RBUS;
  237. /* Resume DMA reception */
  238. ETH->DMARPDR = 0;
  239. }
  240. return p;
  241. }
  242. /**
  243. * This function is the ethernetif_input task, it is processed when a packet
  244. * is ready to be read from the interface. It uses the function low_level_input()
  245. * that should handle the actual reception of bytes from the network
  246. * interface. Then the type of the received packet is determined and
  247. * the appropriate input function is called.
  248. *
  249. * @param netif the lwip network interface structure for this ethernetif
  250. */
  251. /*
  252. void ethernetif_input( void * pvParameters )
  253. {
  254. struct pbuf *p;
  255. for( ;; )
  256. {
  257. if (xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE)
  258. {
  259. p = low_level_input( s_pxNetIf );
  260. if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
  261. {
  262. pbuf_free(p);
  263. p=NULL;
  264. }
  265. }
  266. }
  267. }
  268. */
  269. void ethernetif_input(void * pvParameters)
  270. {
  271. struct pbuf *p;
  272. for( ;; )
  273. {
  274. if(xSemaphoreTake(s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE)
  275. {
  276. GET_NEXT_FRAGMENT:
  277. p = low_level_input( s_pxNetIf );
  278. if (p != NULL)
  279. {
  280. if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
  281. {
  282. pbuf_free(p);
  283. p=NULL;
  284. }
  285. else
  286. {
  287. xSemaphoreTake(s_xSemaphore, 0);
  288. goto GET_NEXT_FRAGMENT;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Should be called at the beginning of the program to set up the
  296. * network interface. It calls the function low_level_init() to do the
  297. * actual setup of the hardware.
  298. *
  299. * This function should be passed as a parameter to netif_add().
  300. *
  301. * @param netif the lwip network interface structure for this ethernetif
  302. * @return ERR_OK if the loopif is initialized
  303. * ERR_MEM if private data couldn't be allocated
  304. * any other err_t on error
  305. */
  306. err_t ethernetif_init(struct netif *netif)
  307. {
  308. LWIP_ASSERT("netif != NULL", (netif != NULL));
  309. #if LWIP_NETIF_HOSTNAME
  310. /* Initialize interface hostname */
  311. netif->hostname = "lwip";
  312. #endif /* LWIP_NETIF_HOSTNAME */
  313. netif->name[0] = IFNAME0;
  314. netif->name[1] = IFNAME1;
  315. netif->output = etharp_output;
  316. netif->linkoutput = low_level_output;
  317. /* initialize the hardware */
  318. low_level_init(netif);
  319. etharp_init();
  320. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  321. return ERR_OK;
  322. }
  323. static void arp_timer(void *arg)
  324. {
  325. etharp_tmr();
  326. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  327. }