]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwip-1.4.0/ports/MicroBlaze-Ethernet-Lite/ethernetif.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwip-1.4.0 / ports / MicroBlaze-Ethernet-Lite / ethernetif.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* FreeRTOS includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "queue.h"\r
71 \r
72 /* BSP includes. */\r
73 #include "xemaclite.h"\r
74 #include "xintc_l.h"\r
75 \r
76 /* lwIP includes. */\r
77 #include "lwip/opt.h"\r
78 #include "lwip/def.h"\r
79 #include "lwip/mem.h"\r
80 #include "lwip/pbuf.h"\r
81 #include "lwip/sys.h"\r
82 #include <lwip/stats.h>\r
83 #include <lwip/snmp.h>\r
84 #include "netif/etharp.h"\r
85 \r
86 /* Define those to better describe your network interface. */\r
87 #define IFNAME0 'e'\r
88 #define IFNAME1 'l'\r
89 \r
90 /* When a packet is ready to be sent, if it cannot be sent immediately then\r
91  * the task performing the transmit will block for netifTX_BUFFER_FREE_WAIT\r
92  * milliseconds.  It will do this a maximum of netifMAX_TX_ATTEMPTS before\r
93  * giving up.\r
94  */\r
95 #define netifTX_BUFFER_FREE_WAIT        ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
96 #define netifMAX_TX_ATTEMPTS            ( 5 )\r
97 \r
98 #define netifMAX_MTU 1500\r
99 \r
100 struct xEthernetIf\r
101 {\r
102         struct eth_addr *ethaddr;\r
103         /* Add whatever per-interface state that is needed here. */\r
104 };\r
105 \r
106 /*\r
107  * Copy the received data into a pbuf.\r
108  */\r
109 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );\r
110 \r
111 /*\r
112  * Send data from a pbuf to the hardware.\r
113  */\r
114 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );\r
115 \r
116 /*\r
117  * Perform any hardware and/or driver initialisation necessary.\r
118  */\r
119 static void prvLowLevelInit( struct netif *pxNetIf );\r
120 \r
121 /*\r
122  * Functions that get registered as the Rx and Tx interrupt handers\r
123  * respectively.\r
124  */\r
125 static void prvRxHandler( void *pvNetIf );\r
126 static void prvTxHandler( void *pvUnused );\r
127 \r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* The instance of the xEmacLite IP being used in this driver. */\r
132 static XEmacLite xEMACInstance;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /**\r
137  * In this function, the hardware should be initialized.\r
138  * Called from ethernetif_init().\r
139  *\r
140  * @param pxNetIf the already initialized lwip network interface structure\r
141  *              for this etherpxNetIf\r
142  */\r
143 static void prvLowLevelInit( struct netif *pxNetIf )\r
144 {\r
145 portBASE_TYPE xStatus;\r
146 extern void vInitialisePHY( XEmacLite *xemaclitep );\r
147 unsigned portBASE_TYPE uxOriginalPriority;\r
148 \r
149         /* Hardware initialisation can take some time, so temporarily lower the\r
150         task priority to ensure other functionality is not adversely effected.\r
151         The priority will get raised again before this function exits. */\r
152         uxOriginalPriority = uxTaskPriorityGet( NULL );\r
153         vTaskPrioritySet( NULL, tskIDLE_PRIORITY );\r
154 \r
155         /* set MAC hardware address length */\r
156         pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
157 \r
158         /* set MAC hardware address */\r
159         pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;\r
160         pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;\r
161         pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;\r
162         pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;\r
163         pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;\r
164         pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;\r
165 \r
166         /* device capabilities */\r
167         pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;\r
168 \r
169         /* maximum transfer unit */\r
170         pxNetIf->mtu = netifMAX_MTU;\r
171 \r
172         /* Broadcast capability */\r
173         pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;\r
174 \r
175         /* Initialize the mac */\r
176         xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );\r
177 \r
178         if( xStatus == XST_SUCCESS )\r
179         {\r
180                 /* Set mac address */\r
181                 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );\r
182 \r
183                 /* Flush any frames already received */\r
184                 XEmacLite_FlushReceive( &xEMACInstance );\r
185 \r
186                 /* Set Rx, Tx interrupt handlers */\r
187                 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );\r
188                 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );\r
189 \r
190                 /* Enable Rx, Tx interrupts */\r
191                 XEmacLite_EnableInterrupts( &xEMACInstance );\r
192 \r
193                 /* Install the standard Xilinx library interrupt handler itself.\r
194                 *NOTE* The xPortInstallInterruptHandler() API function must be used\r
195                 for     this purpose. */\r
196                 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_EMACLITE_0_VEC_ID, ( XInterruptHandler ) XEmacLite_InterruptHandler, &xEMACInstance );\r
197 \r
198                 vInitialisePHY( &xEMACInstance );\r
199 \r
200                 /* Enable the interrupt in the interrupt controller.\r
201                 *NOTE* The vPortEnableInterrupt() API function must be used for this\r
202                 purpose. */\r
203                 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );\r
204         }\r
205 \r
206         /* Reset the task priority back to its original value. */\r
207         vTaskPrioritySet( NULL, uxOriginalPriority );\r
208 \r
209         configASSERT( xStatus == pdPASS );\r
210 }\r
211 \r
212 /**\r
213  * This function should do the actual transmission of the packet. The packet is\r
214  * contained in the pbuf that is passed to the function. This pbuf\r
215  * might be chained.\r
216  *\r
217  * @param pxNetIf the lwip network interface structure for this etherpxNetIf\r
218  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)\r
219  * @return ERR_OK if the packet could be sent\r
220  *               an err_t value if the packet couldn't be sent\r
221  *\r
222  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to\r
223  *         strange results. You might consider waiting for space in the DMA queue\r
224  *         to become availale since the stack doesn't retry to send a packet\r
225  *         dropped because of memory failure (except for the TCP timers).\r
226  */\r
227 \r
228 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )\r
229 {\r
230 \r
231         /* This is taken from lwIP example code and therefore does not conform\r
232         to the FreeRTOS coding standard. */\r
233 \r
234 struct pbuf *q;\r
235 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));\r
236 unsigned char *pucBuffer = ucBuffer;\r
237 unsigned char *pucChar;\r
238 struct eth_hdr *pxHeader;\r
239 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;\r
240 err_t xReturn = ERR_OK;\r
241 long x;\r
242 \r
243         ( void ) pxNetIf;\r
244 \r
245         #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF\r
246                 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);\r
247         #endif\r
248 \r
249         /* Initiate transfer. */\r
250         if( p->len == p->tot_len ) \r
251         {\r
252                 /* No pbuf chain, don't have to copy -> faster. */\r
253                 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];\r
254         } \r
255         else \r
256         {\r
257                 /* pbuf chain, copy into contiguous ucBuffer. */\r
258                 if( p->tot_len >= sizeof( ucBuffer ) )\r
259                 {\r
260                         LINK_STATS_INC( link.lenerr );\r
261                         LINK_STATS_INC( link.drop );\r
262                         snmp_inc_ifoutdiscards( pxNetIf );\r
263                         xReturn = ERR_BUF;\r
264                 }\r
265                 else\r
266                 {\r
267                         pucChar = ucBuffer;\r
268 \r
269                         for( q = p; q != NULL; q = q->next )\r
270                         {\r
271                                 /* Send the data from the pbuf to the interface, one pbuf at a\r
272                                 time. The size of the data in each pbuf is kept in the ->len\r
273                                 variable. */\r
274                                 /* send data from(q->payload, q->len); */\r
275                                 LWIP_DEBUGF( NETIF_DEBUG, ( "NETIF: send pucChar %p q->payload %p q->len %i q->next %p\n", pucChar, q->payload, ( int ) q->len, ( void* ) q->next ) );\r
276                                 if( q == p )\r
277                                 {\r
278                                         memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );\r
279                                         pucChar += q->len - ETH_PAD_SIZE;\r
280                                 }\r
281                                 else\r
282                                 {\r
283                                         memcpy( pucChar, q->payload, q->len );\r
284                                         pucChar += q->len;\r
285                                 }\r
286                         }\r
287                 }\r
288         }\r
289 \r
290         if( xReturn == ERR_OK )\r
291         {\r
292                 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )\r
293                 {\r
294                         xReturn =  XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );\r
295                         if( xReturn == XST_SUCCESS )\r
296                         {\r
297                                 break;\r
298                         }\r
299                         else\r
300                         {\r
301                                 vTaskDelay( netifTX_BUFFER_FREE_WAIT );\r
302                         }\r
303                 }\r
304 \r
305                 if( xReturn != XST_SUCCESS )\r
306                 {\r
307                         LINK_STATS_INC( link.memerr );\r
308                         LINK_STATS_INC( link.drop );\r
309                         snmp_inc_ifoutdiscards( pxNetIf );\r
310                         xReturn = ERR_BUF;\r
311                 }\r
312                 else\r
313                 {\r
314                         LINK_STATS_INC( link.xmit );\r
315                         snmp_add_ifoutoctets( pxNetIf, usTotalLength );\r
316                         pxHeader = ( struct eth_hdr * )p->payload;\r
317 \r
318                         if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 ) \r
319                         {\r
320                                 /* broadcast or multicast packet*/\r
321                                 snmp_inc_ifoutnucastpkts( pxNetIf );\r
322                         } \r
323                         else \r
324                         {\r
325                                 /* unicast packet */\r
326                                 snmp_inc_ifoutucastpkts( pxNetIf );\r
327                         }\r
328                 }\r
329         }\r
330 \r
331         return xReturn;\r
332 }\r
333 \r
334 /**\r
335  * Should allocate a pbuf and transfer the bytes of the incoming\r
336  * packet from the interface into the pbuf.\r
337  *\r
338  * @param pxNetIf the lwip network interface structure for this etherpxNetIf\r
339  * @return a pbuf filled with the received packet (including MAC header)\r
340  *               NULL on memory error\r
341  */\r
342 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )\r
343 {\r
344 struct pbuf *p = NULL, *q;\r
345 \r
346         if( usDataLength > 0U )\r
347         {\r
348                 #if ETH_PAD_SIZE\r
349                         len += ETH_PAD_SIZE; /* allow room for Ethernet padding */\r
350                 #endif\r
351 \r
352                 /* We allocate a pbuf chain of pbufs from the pool. */\r
353                 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );\r
354   \r
355                 if( p != NULL ) \r
356                 {\r
357                         #if ETH_PAD_SIZE\r
358                                 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */\r
359                         #endif\r
360 \r
361                         /* We iterate over the pbuf chain until we have read the entire\r
362                         * packet into the pbuf. */\r
363                         usDataLength = 0U;\r
364                         for( q = p; q != NULL; q = q->next ) \r
365                         {\r
366                                 /* Read enough bytes to fill this pbuf in the chain. The\r
367                                 * available data in the pbuf is given by the q->len\r
368                                 * variable.\r
369                                 * This does not necessarily have to be a memcpy, you can also preallocate\r
370                                 * pbufs for a DMA-enabled MAC and after receiving truncate it to the\r
371                                 * actually received size. In this case, ensure the usTotalLength member of the\r
372                                 * pbuf is the sum of the chained pbuf len members.\r
373                                 */\r
374                                 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );\r
375                                 usDataLength += q->len;\r
376                         }\r
377 \r
378                         #if ETH_PAD_SIZE\r
379                                 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */\r
380                         #endif\r
381 \r
382                         LINK_STATS_INC(link.recv);\r
383                 }\r
384         }\r
385 \r
386         return p;  \r
387 }\r
388 \r
389 /**\r
390  * Should be called at the beginning of the program to set up the\r
391  * network interface. It calls the function prvLowLevelInit() to do the\r
392  * actual setup of the hardware.\r
393  *\r
394  * This function should be passed as a parameter to pxNetIf_add().\r
395  *\r
396  * @param pxNetIf the lwip network interface structure for this etherpxNetIf\r
397  * @return ERR_OK if the loopif is initialized\r
398  *               ERR_MEM if private data couldn't be allocated\r
399  *               any other err_t on error\r
400  */\r
401 err_t ethernetif_init( struct netif *pxNetIf )\r
402 {\r
403 err_t xReturn = ERR_OK;\r
404 \r
405         /* This is taken from lwIP example code and therefore does not conform\r
406         to the FreeRTOS coding standard. */\r
407         \r
408 struct xEthernetIf *pxEthernetIf;\r
409 \r
410         LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );\r
411         \r
412         pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );\r
413         if( pxEthernetIf == NULL ) \r
414         {\r
415                 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );\r
416                 xReturn = ERR_MEM;\r
417         }\r
418         else\r
419         {\r
420                 #if LWIP_NETIF_HOSTNAME\r
421                 {\r
422                         /* Initialize interface hostname */\r
423                         pxNetIf->hostname = "lwip";\r
424                 }\r
425                 #endif /* LWIP_NETIF_HOSTNAME */\r
426 \r
427                 pxNetIf->state = pxEthernetIf;\r
428                 pxNetIf->name[ 0 ] = IFNAME0;\r
429                 pxNetIf->name[ 1 ] = IFNAME1;\r
430 \r
431                 /* We directly use etharp_output() here to save a function call.\r
432                 * You can instead declare your own function an call etharp_output()\r
433                 * from it if you have to do some checks before sending (e.g. if link\r
434                 * is available...) */\r
435                 pxNetIf->output = etharp_output;\r
436                 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;\r
437                 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
438                 pxNetIf->mtu = netifMAX_MTU;\r
439                 pxNetIf->linkoutput = prvLowLevelOutput;\r
440 \r
441                 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );\r
442 \r
443                 /* initialize the hardware */\r
444                 prvLowLevelInit( pxNetIf );\r
445         }\r
446 \r
447         return xReturn;\r
448 }\r
449 /*-----------------------------------------------------------*/\r
450 \r
451 static void prvRxHandler( void *pvNetIf )\r
452 {\r
453         /* This is taken from lwIP example code and therefore does not conform\r
454         to the FreeRTOS coding standard. */\r
455 \r
456 struct eth_hdr *pxHeader;\r
457 struct pbuf *p;\r
458 unsigned short usInputLength;\r
459 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));\r
460 extern portBASE_TYPE xInsideISR;\r
461 struct netif *pxNetIf = ( struct netif * ) pvNetIf;\r
462 \r
463         XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );\r
464 \r
465         /* Ensure the pbuf handling functions don't attempt to use critical\r
466         sections. */\r
467         xInsideISR++;\r
468 \r
469         usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );\r
470 \r
471         /* move received packet into a new pbuf */\r
472         p = prvLowLevelInput( ucBuffer, usInputLength );\r
473 \r
474         /* no packet could be read, silently ignore this */\r
475         if( p != NULL )\r
476         {\r
477                 /* points to packet payload, which starts with an Ethernet header */\r
478                 pxHeader = p->payload;\r
479 \r
480                 switch( htons( pxHeader->type ) )\r
481                 {\r
482                         /* IP or ARP packet? */\r
483                         case ETHTYPE_IP:\r
484                         case ETHTYPE_ARP:\r
485                                                                 /* full packet send to tcpip_thread to process */\r
486                                                                 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )\r
487                                                                 {\r
488                                                                         LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );\r
489                                                                         pbuf_free(p);\r
490                                                                         p = NULL;\r
491                                                                 }\r
492                                                                 break;\r
493 \r
494                         default:\r
495                                                                 pbuf_free( p );\r
496                                                                 p = NULL;\r
497                         break;\r
498                 }\r
499         }\r
500 \r
501         xInsideISR--;\r
502 }\r
503 /*-----------------------------------------------------------*/\r
504 \r
505 static void prvTxHandler( void *pvUnused )\r
506 {\r
507         ( void ) pvUnused;\r
508         XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );\r
509 }\r
510 \r
511 \r
512 \r
513 \r