2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
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
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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
66 /* FreeRTOS includes. */
\r
67 #include "FreeRTOS.h"
\r
72 #include "xemaclite.h"
\r
73 #include "xintc_l.h"
\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
85 /* Define those to better describe your network interface. */
\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
94 #define netifTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_PERIOD_MS )
\r
95 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
97 #define netifMAX_MTU 1500
\r
101 struct eth_addr *ethaddr;
\r
102 /* Add whatever per-interface state that is needed here. */
\r
106 * Copy the received data into a pbuf.
\r
108 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
111 * Send data from a pbuf to the hardware.
\r
113 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
116 * Perform any hardware and/or driver initialisation necessary.
\r
118 static void prvLowLevelInit( struct netif *pxNetIf );
\r
121 * Functions that get registered as the Rx and Tx interrupt handers
\r
124 static void prvRxHandler( void *pvNetIf );
\r
125 static void prvTxHandler( void *pvUnused );
\r
128 /*-----------------------------------------------------------*/
\r
130 /* The instance of the xEmacLite IP being used in this driver. */
\r
131 static XEmacLite xEMACInstance;
\r
133 /*-----------------------------------------------------------*/
\r
136 * In this function, the hardware should be initialized.
\r
137 * Called from ethernetif_init().
\r
139 * @param pxNetIf the already initialized lwip network interface structure
\r
140 * for this etherpxNetIf
\r
142 static void prvLowLevelInit( struct netif *pxNetIf )
\r
144 portBASE_TYPE xStatus;
\r
145 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
146 unsigned portBASE_TYPE uxOriginalPriority;
\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
154 /* set MAC hardware address length */
\r
155 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\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
165 /* device capabilities */
\r
166 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
168 /* maximum transfer unit */
\r
169 pxNetIf->mtu = netifMAX_MTU;
\r
171 /* Broadcast capability */
\r
172 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
174 /* Initialize the mac */
\r
175 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
177 if( xStatus == XST_SUCCESS )
\r
179 /* Set mac address */
\r
180 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
182 /* Flush any frames already received */
\r
183 XEmacLite_FlushReceive( &xEMACInstance );
\r
185 /* Set Rx, Tx interrupt handlers */
\r
186 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
187 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
189 /* Enable Rx, Tx interrupts */
\r
190 XEmacLite_EnableInterrupts( &xEMACInstance );
\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
197 vInitialisePHY( &xEMACInstance );
\r
199 /* Enable the interrupt in the interrupt controller.
\r
200 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
202 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
205 /* Reset the task priority back to its original value. */
\r
206 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
208 configASSERT( xStatus == pdPASS );
\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
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
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
227 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
230 /* This is taken from lwIP example code and therefore does not conform
\r
231 to the FreeRTOS coding standard. */
\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
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
248 /* Initiate transfer. */
\r
249 if( p->len == p->tot_len )
\r
251 /* No pbuf chain, don't have to copy -> faster. */
\r
252 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
256 /* pbuf chain, copy into contiguous ucBuffer. */
\r
257 if( p->tot_len >= sizeof( ucBuffer ) )
\r
259 LINK_STATS_INC( link.lenerr );
\r
260 LINK_STATS_INC( link.drop );
\r
261 snmp_inc_ifoutdiscards( pxNetIf );
\r
266 pucChar = ucBuffer;
\r
268 for( q = p; q != NULL; q = q->next )
\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
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
277 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
278 pucChar += q->len - ETH_PAD_SIZE;
\r
282 memcpy( pucChar, q->payload, q->len );
\r
289 if( xReturn == ERR_OK )
\r
291 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
293 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
294 if( xReturn == XST_SUCCESS )
\r
300 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
304 if( xReturn != XST_SUCCESS )
\r
306 LINK_STATS_INC( link.memerr );
\r
307 LINK_STATS_INC( link.drop );
\r
308 snmp_inc_ifoutdiscards( pxNetIf );
\r
313 LINK_STATS_INC( link.xmit );
\r
314 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
315 pxHeader = ( struct eth_hdr * )p->payload;
\r
317 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
319 /* broadcast or multicast packet*/
\r
320 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
324 /* unicast packet */
\r
325 snmp_inc_ifoutucastpkts( pxNetIf );
\r
334 * Should allocate a pbuf and transfer the bytes of the incoming
\r
335 * packet from the interface into the pbuf.
\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
341 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
343 struct pbuf *p = NULL, *q;
\r
345 if( usDataLength > 0U )
\r
348 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
351 /* We allocate a pbuf chain of pbufs from the pool. */
\r
352 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
357 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
360 /* We iterate over the pbuf chain until we have read the entire
\r
361 * packet into the pbuf. */
\r
363 for( q = p; q != NULL; q = q->next )
\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
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
373 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
374 usDataLength += q->len;
\r
378 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
381 LINK_STATS_INC(link.recv);
\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
393 * This function should be passed as a parameter to pxNetIf_add().
\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
400 err_t ethernetif_init( struct netif *pxNetIf )
\r
402 err_t xReturn = ERR_OK;
\r
404 /* This is taken from lwIP example code and therefore does not conform
\r
405 to the FreeRTOS coding standard. */
\r
407 struct xEthernetIf *pxEthernetIf;
\r
409 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
411 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
412 if( pxEthernetIf == NULL )
\r
414 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
419 #if LWIP_NETIF_HOSTNAME
\r
421 /* Initialize interface hostname */
\r
422 pxNetIf->hostname = "lwip";
\r
424 #endif /* LWIP_NETIF_HOSTNAME */
\r
426 pxNetIf->state = pxEthernetIf;
\r
427 pxNetIf->name[ 0 ] = IFNAME0;
\r
428 pxNetIf->name[ 1 ] = IFNAME1;
\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
440 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
442 /* initialize the hardware */
\r
443 prvLowLevelInit( pxNetIf );
\r
448 /*-----------------------------------------------------------*/
\r
450 static void prvRxHandler( void *pvNetIf )
\r
452 /* This is taken from lwIP example code and therefore does not conform
\r
453 to the FreeRTOS coding standard. */
\r
455 struct eth_hdr *pxHeader;
\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
462 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
464 /* Ensure the pbuf handling functions don't attempt to use critical
\r
468 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
470 /* move received packet into a new pbuf */
\r
471 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
473 /* no packet could be read, silently ignore this */
\r
476 /* points to packet payload, which starts with an Ethernet header */
\r
477 pxHeader = p->payload;
\r
479 switch( htons( pxHeader->type ) )
\r
481 /* IP or ARP packet? */
\r
484 /* full packet send to tcpip_thread to process */
\r
485 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
487 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
502 /*-----------------------------------------------------------*/
\r
504 static void prvTxHandler( void *pvUnused )
\r
507 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r