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