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