2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\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
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
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\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
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
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
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\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
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
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
65 /* FreeRTOS includes. */
\r
66 #include "FreeRTOS.h"
\r
71 #include "xemaclite.h"
\r
72 #include "xintc_l.h"
\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
84 /* Define those to better describe your network interface. */
\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
93 #define netifTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
94 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
96 #define netifMAX_MTU 1500
\r
100 struct eth_addr *ethaddr;
\r
101 /* Add whatever per-interface state that is needed here. */
\r
105 * Copy the received data into a pbuf.
\r
107 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
110 * Send data from a pbuf to the hardware.
\r
112 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
115 * Perform any hardware and/or driver initialisation necessary.
\r
117 static void prvLowLevelInit( struct netif *pxNetIf );
\r
120 * Functions that get registered as the Rx and Tx interrupt handers
\r
123 static void prvRxHandler( void *pvNetIf );
\r
124 static void prvTxHandler( void *pvUnused );
\r
127 /*-----------------------------------------------------------*/
\r
129 /* The instance of the xEmacLite IP being used in this driver. */
\r
130 static XEmacLite xEMACInstance;
\r
132 /*-----------------------------------------------------------*/
\r
135 * In this function, the hardware should be initialized.
\r
136 * Called from ethernetif_init().
\r
138 * @param pxNetIf the already initialized lwip network interface structure
\r
139 * for this etherpxNetIf
\r
141 static void prvLowLevelInit( struct netif *pxNetIf )
\r
143 portBASE_TYPE xStatus;
\r
144 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
145 unsigned portBASE_TYPE uxOriginalPriority;
\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
153 /* set MAC hardware address length */
\r
154 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\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
164 /* device capabilities */
\r
165 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
167 /* maximum transfer unit */
\r
168 pxNetIf->mtu = netifMAX_MTU;
\r
170 /* Broadcast capability */
\r
171 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
173 /* Initialize the mac */
\r
174 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
176 if( xStatus == XST_SUCCESS )
\r
178 /* Set mac address */
\r
179 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
181 /* Flush any frames already received */
\r
182 XEmacLite_FlushReceive( &xEMACInstance );
\r
184 /* Set Rx, Tx interrupt handlers */
\r
185 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
186 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
188 /* Enable Rx, Tx interrupts */
\r
189 XEmacLite_EnableInterrupts( &xEMACInstance );
\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
196 vInitialisePHY( &xEMACInstance );
\r
198 /* Enable the interrupt in the interrupt controller.
\r
199 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
201 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
204 /* Reset the task priority back to its original value. */
\r
205 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
207 configASSERT( xStatus == pdPASS );
\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
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
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
226 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
229 /* This is taken from lwIP example code and therefore does not conform
\r
230 to the FreeRTOS coding standard. */
\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
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
247 /* Initiate transfer. */
\r
248 if( p->len == p->tot_len )
\r
250 /* No pbuf chain, don't have to copy -> faster. */
\r
251 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
255 /* pbuf chain, copy into contiguous ucBuffer. */
\r
256 if( p->tot_len >= sizeof( ucBuffer ) )
\r
258 LINK_STATS_INC( link.lenerr );
\r
259 LINK_STATS_INC( link.drop );
\r
260 snmp_inc_ifoutdiscards( pxNetIf );
\r
265 pucChar = ucBuffer;
\r
267 for( q = p; q != NULL; q = q->next )
\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
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
276 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
277 pucChar += q->len - ETH_PAD_SIZE;
\r
281 memcpy( pucChar, q->payload, q->len );
\r
288 if( xReturn == ERR_OK )
\r
290 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
292 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
293 if( xReturn == XST_SUCCESS )
\r
299 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
303 if( xReturn != XST_SUCCESS )
\r
305 LINK_STATS_INC( link.memerr );
\r
306 LINK_STATS_INC( link.drop );
\r
307 snmp_inc_ifoutdiscards( pxNetIf );
\r
312 LINK_STATS_INC( link.xmit );
\r
313 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
314 pxHeader = ( struct eth_hdr * )p->payload;
\r
316 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
318 /* broadcast or multicast packet*/
\r
319 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
323 /* unicast packet */
\r
324 snmp_inc_ifoutucastpkts( pxNetIf );
\r
333 * Should allocate a pbuf and transfer the bytes of the incoming
\r
334 * packet from the interface into the pbuf.
\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
340 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
342 struct pbuf *p = NULL, *q;
\r
344 if( usDataLength > 0U )
\r
347 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
350 /* We allocate a pbuf chain of pbufs from the pool. */
\r
351 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
356 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
359 /* We iterate over the pbuf chain until we have read the entire
\r
360 * packet into the pbuf. */
\r
362 for( q = p; q != NULL; q = q->next )
\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
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
372 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
373 usDataLength += q->len;
\r
377 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
380 LINK_STATS_INC(link.recv);
\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
392 * This function should be passed as a parameter to pxNetIf_add().
\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
399 err_t ethernetif_init( struct netif *pxNetIf )
\r
401 err_t xReturn = ERR_OK;
\r
403 /* This is taken from lwIP example code and therefore does not conform
\r
404 to the FreeRTOS coding standard. */
\r
406 struct xEthernetIf *pxEthernetIf;
\r
408 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
410 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
411 if( pxEthernetIf == NULL )
\r
413 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
418 #if LWIP_NETIF_HOSTNAME
\r
420 /* Initialize interface hostname */
\r
421 pxNetIf->hostname = "lwip";
\r
423 #endif /* LWIP_NETIF_HOSTNAME */
\r
425 pxNetIf->state = pxEthernetIf;
\r
426 pxNetIf->name[ 0 ] = IFNAME0;
\r
427 pxNetIf->name[ 1 ] = IFNAME1;
\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
439 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
441 /* initialize the hardware */
\r
442 prvLowLevelInit( pxNetIf );
\r
447 /*-----------------------------------------------------------*/
\r
449 static void prvRxHandler( void *pvNetIf )
\r
451 /* This is taken from lwIP example code and therefore does not conform
\r
452 to the FreeRTOS coding standard. */
\r
454 struct eth_hdr *pxHeader;
\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
461 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
463 /* Ensure the pbuf handling functions don't attempt to use critical
\r
467 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
469 /* move received packet into a new pbuf */
\r
470 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
472 /* no packet could be read, silently ignore this */
\r
475 /* points to packet payload, which starts with an Ethernet header */
\r
476 pxHeader = p->payload;
\r
478 switch( htons( pxHeader->type ) )
\r
480 /* IP or ARP packet? */
\r
483 /* full packet send to tcpip_thread to process */
\r
484 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
486 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
501 /*-----------------------------------------------------------*/
\r
503 static void prvTxHandler( void *pvUnused )
\r
506 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r