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