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