ethernetif.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. extern uint8_t MAC[6];
  79. static void ethernetif_input( void * pvParameters );
  80. static void arp_timer(void *arg);
  81. /**
  82. * In this function, the hardware should be initialized.
  83. * Called from ethernetif_init().
  84. *
  85. * @param netif the already initialized lwip network interface structure
  86. * for this ethernetif
  87. */
  88. static void low_level_init(struct netif *netif)
  89. {
  90. uint8_t mac[6];
  91. uint32_t i;
  92. /* set netif MAC hardware address length */
  93. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  94. /* set netif MAC hardware address */
  95. #ifdef BT6702_SERVICE
  96. netif->hwaddr[0] = MAC[0];
  97. netif->hwaddr[1] = MAC[1];
  98. netif->hwaddr[2] = MAC[2];
  99. netif->hwaddr[3] = MAC[3];
  100. netif->hwaddr[4] = MAC[4];
  101. netif->hwaddr[5] = MAC[5];
  102. #else
  103. SETTINGS_GetMac(mac);
  104. netif->hwaddr[0] = mac[0];
  105. netif->hwaddr[1] = mac[1];
  106. netif->hwaddr[2] = mac[2];
  107. netif->hwaddr[3] = mac[3];
  108. netif->hwaddr[4] = mac[4];
  109. netif->hwaddr[5] = mac[5];
  110. #endif
  111. /* set netif maximum transfer unit */
  112. netif->mtu = 1500;
  113. /* Accept broadcast address and ARP traffic */
  114. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  115. s_pxNetIf =netif;
  116. /* create binary semaphore used for informing ethernetif of frame reception */
  117. if (s_xSemaphore == NULL)
  118. {
  119. s_xSemaphore= xSemaphoreCreateCounting(20,0);
  120. }
  121. /* initialize MAC address in ethernet MAC */
  122. ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
  123. /* Initialize Tx Descriptors list: Chain Mode */
  124. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  125. /* Initialize Rx Descriptors list: Chain Mode */
  126. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  127. /* Enable Ethernet Rx interrrupt */
  128. {
  129. for(i=0; i<ETH_RXBUFNB; i++)
  130. {
  131. ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
  132. }
  133. }
  134. #ifdef CHECKSUM_BY_HARDWARE
  135. /* Enable the checksum insertion for the Tx frames */
  136. {
  137. for(i=0; i<ETH_TXBUFNB; i++)
  138. {
  139. ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
  140. }
  141. }
  142. #endif
  143. /* create the task that handles the ETH_MAC */
  144. xTaskCreate(ethernetif_input, "Eth_if", netifINTERFACE_TASK_STACK_SIZE, NULL,
  145. netifINTERFACE_TASK_PRIORITY,NULL);
  146. /* Enable MAC and DMA transmission and reception */
  147. ETH_Start();
  148. }
  149. /**
  150. * This function should do the actual transmission of the packet. The packet is
  151. * contained in the pbuf that is passed to the function. This pbuf
  152. * might be chained.
  153. *
  154. * @param netif the lwip network interface structure for this ethernetif
  155. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  156. * @return ERR_OK if the packet could be sent
  157. * an err_t value if the packet couldn't be sent
  158. *
  159. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  160. * strange results. You might consider waiting for space in the DMA queue
  161. * to become availale since the stack doesn't retry to send a packet
  162. * dropped because of memory failure (except for the TCP timers).
  163. */
  164. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  165. {
  166. static SemaphoreHandle_t xTxSemaphore = NULL;
  167. struct pbuf *q;
  168. uint32_t l = 0;
  169. u8 *buffer ;
  170. if (xTxSemaphore == NULL)
  171. {
  172. vSemaphoreCreateBinary (xTxSemaphore);
  173. }
  174. if (xSemaphoreTake(xTxSemaphore, netifGUARD_BLOCK_TIME))
  175. {
  176. buffer = (u8 *)(DMATxDescToSet->Buffer1Addr);
  177. for(q = p; q != NULL; q = q->next)
  178. {
  179. memcpy((u8_t*)&buffer[l], q->payload, q->len);
  180. l = l + q->len;
  181. }
  182. ETH_Prepare_Transmit_Descriptors(l);
  183. xSemaphoreGive(xTxSemaphore);
  184. }
  185. return ERR_OK;
  186. }
  187. /**
  188. * Should allocate a pbuf and transfer the bytes of the incoming
  189. * packet from the interface into the pbuf.
  190. *
  191. * @param netif the lwip network interface structure for this ethernetif
  192. * @return a pbuf filled with the received packet (including MAC header)
  193. * NULL on memory error
  194. */
  195. static struct pbuf * low_level_input(struct netif *netif)
  196. {
  197. struct pbuf *p, *q;
  198. u16_t len;
  199. uint32_t l=0,i =0;
  200. FrameTypeDef frame;
  201. u8 *buffer;
  202. __IO ETH_DMADESCTypeDef *DMARxNextDesc;
  203. p = NULL;
  204. /* Get received frame */
  205. frame = ETH_Get_Received_Frame_interrupt();
  206. /* check that frame has no error */
  207. if ((frame.descriptor->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET)
  208. {
  209. /* Obtain the size of the packet and put it into the "len" variable. */
  210. len = frame.length;
  211. buffer = (u8 *)frame.buffer;
  212. /* We allocate a pbuf chain of pbufs from the pool. */
  213. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  214. /* Copy received frame from ethernet driver buffer to stack buffer */
  215. if (p != NULL)
  216. {
  217. for (q = p; q != NULL; q = q->next)
  218. {
  219. memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len);
  220. l = l + q->len;
  221. }
  222. }
  223. }
  224. /* Release descriptors to DMA */
  225. /* Check if received frame with multiple DMA buffer segments */
  226. if (DMA_RX_FRAME_infos->Seg_Count > 1)
  227. {
  228. DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc;
  229. }
  230. else
  231. {
  232. DMARxNextDesc = frame.descriptor;
  233. }
  234. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  235. for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
  236. {
  237. DMARxNextDesc->Status = ETH_DMARxDesc_OWN;
  238. DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr);
  239. }
  240. /* Clear Segment_Count */
  241. DMA_RX_FRAME_infos->Seg_Count =0;
  242. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  243. if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
  244. {
  245. /* Clear RBUS ETHERNET DMA flag */
  246. ETH->DMASR = ETH_DMASR_RBUS;
  247. /* Resume DMA reception */
  248. ETH->DMARPDR = 0;
  249. }
  250. return p;
  251. }
  252. /**
  253. * This function is the ethernetif_input task, it is processed when a packet
  254. * is ready to be read from the interface. It uses the function low_level_input()
  255. * that should handle the actual reception of bytes from the network
  256. * interface. Then the type of the received packet is determined and
  257. * the appropriate input function is called.
  258. *
  259. * @param netif the lwip network interface structure for this ethernetif
  260. */
  261. /*
  262. void ethernetif_input( void * pvParameters )
  263. {
  264. struct pbuf *p;
  265. for( ;; )
  266. {
  267. if (xSemaphoreTake( s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE)
  268. {
  269. p = low_level_input( s_pxNetIf );
  270. if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
  271. {
  272. pbuf_free(p);
  273. p=NULL;
  274. }
  275. }
  276. }
  277. }
  278. */
  279. void ethernetif_input(void * pvParameters)
  280. {
  281. struct pbuf *p;
  282. for( ;; )
  283. {
  284. if(xSemaphoreTake(s_xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT)==pdTRUE)
  285. {
  286. GET_NEXT_FRAGMENT:
  287. p = low_level_input( s_pxNetIf );
  288. if (p != NULL)
  289. {
  290. if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
  291. {
  292. pbuf_free(p);
  293. p=NULL;
  294. }
  295. else
  296. {
  297. xSemaphoreTake(s_xSemaphore, 0);
  298. goto GET_NEXT_FRAGMENT;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. /**
  305. * Should be called at the beginning of the program to set up the
  306. * network interface. It calls the function low_level_init() to do the
  307. * actual setup of the hardware.
  308. *
  309. * This function should be passed as a parameter to netif_add().
  310. *
  311. * @param netif the lwip network interface structure for this ethernetif
  312. * @return ERR_OK if the loopif is initialized
  313. * ERR_MEM if private data couldn't be allocated
  314. * any other err_t on error
  315. */
  316. err_t ethernetif_init(struct netif *netif)
  317. {
  318. LWIP_ASSERT("netif != NULL", (netif != NULL));
  319. #if LWIP_NETIF_HOSTNAME
  320. /* Initialize interface hostname */
  321. netif->hostname = "lwip";
  322. #endif /* LWIP_NETIF_HOSTNAME */
  323. netif->name[0] = IFNAME0;
  324. netif->name[1] = IFNAME1;
  325. netif->output = etharp_output;
  326. netif->linkoutput = low_level_output;
  327. /* initialize the hardware */
  328. low_level_init(netif);
  329. etharp_init();
  330. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  331. return ERR_OK;
  332. }
  333. static void arp_timer(void *arg)
  334. {
  335. etharp_tmr();
  336. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  337. }