2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* FreeRTOS includes. */
\r
68 #include "FreeRTOS.h"
\r
73 #include "xemaclite.h"
\r
74 #include "xintc_l.h"
\r
76 /* lwIP includes. */
\r
77 #include "lwip/opt.h"
\r
78 #include "lwip/def.h"
\r
79 #include "lwip/mem.h"
\r
80 #include "lwip/pbuf.h"
\r
81 #include "lwip/sys.h"
\r
82 #include <lwip/stats.h>
\r
83 #include <lwip/snmp.h>
\r
84 #include "netif/etharp.h"
\r
86 /* Define those to better describe your network interface. */
\r
90 /* When a packet is ready to be sent, if it cannot be sent immediately then
\r
91 * the task performing the transmit will block for netifTX_BUFFER_FREE_WAIT
\r
92 * milliseconds. It will do this a maximum of netifMAX_TX_ATTEMPTS before
\r
95 #define netifTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
96 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
98 #define netifMAX_MTU 1500
\r
102 struct eth_addr *ethaddr;
\r
103 /* Add whatever per-interface state that is needed here. */
\r
107 * Copy the received data into a pbuf.
\r
109 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
112 * Send data from a pbuf to the hardware.
\r
114 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
117 * Perform any hardware and/or driver initialisation necessary.
\r
119 static void prvLowLevelInit( struct netif *pxNetIf );
\r
122 * Functions that get registered as the Rx and Tx interrupt handers
\r
125 static void prvRxHandler( void *pvNetIf );
\r
126 static void prvTxHandler( void *pvUnused );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* The instance of the xEmacLite IP being used in this driver. */
\r
132 static XEmacLite xEMACInstance;
\r
134 /*-----------------------------------------------------------*/
\r
137 * In this function, the hardware should be initialized.
\r
138 * Called from ethernetif_init().
\r
140 * @param pxNetIf the already initialized lwip network interface structure
\r
141 * for this etherpxNetIf
\r
143 static void prvLowLevelInit( struct netif *pxNetIf )
\r
145 portBASE_TYPE xStatus;
\r
146 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
147 unsigned portBASE_TYPE uxOriginalPriority;
\r
149 /* Hardware initialisation can take some time, so temporarily lower the
\r
150 task priority to ensure other functionality is not adversely effected.
\r
151 The priority will get raised again before this function exits. */
\r
152 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
153 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
155 /* set MAC hardware address length */
\r
156 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
158 /* set MAC hardware address */
\r
159 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
160 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
161 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
162 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
163 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
164 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
166 /* device capabilities */
\r
167 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
169 /* maximum transfer unit */
\r
170 pxNetIf->mtu = netifMAX_MTU;
\r
172 /* Broadcast capability */
\r
173 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
175 /* Initialize the mac */
\r
176 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
178 if( xStatus == XST_SUCCESS )
\r
180 /* Set mac address */
\r
181 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
183 /* Flush any frames already received */
\r
184 XEmacLite_FlushReceive( &xEMACInstance );
\r
186 /* Set Rx, Tx interrupt handlers */
\r
187 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
188 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
190 /* Enable Rx, Tx interrupts */
\r
191 XEmacLite_EnableInterrupts( &xEMACInstance );
\r
193 /* Install the standard Xilinx library interrupt handler itself.
\r
194 *NOTE* The xPortInstallInterruptHandler() API function must be used
\r
195 for this purpose. */
\r
196 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_EMACLITE_0_VEC_ID, ( XInterruptHandler ) XEmacLite_InterruptHandler, &xEMACInstance );
\r
198 vInitialisePHY( &xEMACInstance );
\r
200 /* Enable the interrupt in the interrupt controller.
\r
201 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
203 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
206 /* Reset the task priority back to its original value. */
\r
207 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
209 configASSERT( xStatus == pdPASS );
\r
213 * This function should do the actual transmission of the packet. The packet is
\r
214 * contained in the pbuf that is passed to the function. This pbuf
\r
215 * might be chained.
\r
217 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
218 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
219 * @return ERR_OK if the packet could be sent
\r
220 * an err_t value if the packet couldn't be sent
\r
222 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
223 * strange results. You might consider waiting for space in the DMA queue
\r
224 * to become availale since the stack doesn't retry to send a packet
\r
225 * dropped because of memory failure (except for the TCP timers).
\r
228 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
231 /* This is taken from lwIP example code and therefore does not conform
\r
232 to the FreeRTOS coding standard. */
\r
235 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
236 unsigned char *pucBuffer = ucBuffer;
\r
237 unsigned char *pucChar;
\r
238 struct eth_hdr *pxHeader;
\r
239 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
240 err_t xReturn = ERR_OK;
\r
245 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
246 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
249 /* Initiate transfer. */
\r
250 if( p->len == p->tot_len )
\r
252 /* No pbuf chain, don't have to copy -> faster. */
\r
253 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
257 /* pbuf chain, copy into contiguous ucBuffer. */
\r
258 if( p->tot_len >= sizeof( ucBuffer ) )
\r
260 LINK_STATS_INC( link.lenerr );
\r
261 LINK_STATS_INC( link.drop );
\r
262 snmp_inc_ifoutdiscards( pxNetIf );
\r
267 pucChar = ucBuffer;
\r
269 for( q = p; q != NULL; q = q->next )
\r
271 /* Send the data from the pbuf to the interface, one pbuf at a
\r
272 time. The size of the data in each pbuf is kept in the ->len
\r
274 /* send data from(q->payload, q->len); */
\r
275 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
278 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
279 pucChar += q->len - ETH_PAD_SIZE;
\r
283 memcpy( pucChar, q->payload, q->len );
\r
290 if( xReturn == ERR_OK )
\r
292 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
294 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
295 if( xReturn == XST_SUCCESS )
\r
301 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
305 if( xReturn != XST_SUCCESS )
\r
307 LINK_STATS_INC( link.memerr );
\r
308 LINK_STATS_INC( link.drop );
\r
309 snmp_inc_ifoutdiscards( pxNetIf );
\r
314 LINK_STATS_INC( link.xmit );
\r
315 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
316 pxHeader = ( struct eth_hdr * )p->payload;
\r
318 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
320 /* broadcast or multicast packet*/
\r
321 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
325 /* unicast packet */
\r
326 snmp_inc_ifoutucastpkts( pxNetIf );
\r
335 * Should allocate a pbuf and transfer the bytes of the incoming
\r
336 * packet from the interface into the pbuf.
\r
338 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
339 * @return a pbuf filled with the received packet (including MAC header)
\r
340 * NULL on memory error
\r
342 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
344 struct pbuf *p = NULL, *q;
\r
346 if( usDataLength > 0U )
\r
349 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
352 /* We allocate a pbuf chain of pbufs from the pool. */
\r
353 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
358 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
361 /* We iterate over the pbuf chain until we have read the entire
\r
362 * packet into the pbuf. */
\r
364 for( q = p; q != NULL; q = q->next )
\r
366 /* Read enough bytes to fill this pbuf in the chain. The
\r
367 * available data in the pbuf is given by the q->len
\r
369 * This does not necessarily have to be a memcpy, you can also preallocate
\r
370 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
371 * actually received size. In this case, ensure the usTotalLength member of the
\r
372 * pbuf is the sum of the chained pbuf len members.
\r
374 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
375 usDataLength += q->len;
\r
379 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
382 LINK_STATS_INC(link.recv);
\r
390 * Should be called at the beginning of the program to set up the
\r
391 * network interface. It calls the function prvLowLevelInit() to do the
\r
392 * actual setup of the hardware.
\r
394 * This function should be passed as a parameter to pxNetIf_add().
\r
396 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
397 * @return ERR_OK if the loopif is initialized
\r
398 * ERR_MEM if private data couldn't be allocated
\r
399 * any other err_t on error
\r
401 err_t ethernetif_init( struct netif *pxNetIf )
\r
403 err_t xReturn = ERR_OK;
\r
405 /* This is taken from lwIP example code and therefore does not conform
\r
406 to the FreeRTOS coding standard. */
\r
408 struct xEthernetIf *pxEthernetIf;
\r
410 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
412 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
413 if( pxEthernetIf == NULL )
\r
415 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
420 #if LWIP_NETIF_HOSTNAME
\r
422 /* Initialize interface hostname */
\r
423 pxNetIf->hostname = "lwip";
\r
425 #endif /* LWIP_NETIF_HOSTNAME */
\r
427 pxNetIf->state = pxEthernetIf;
\r
428 pxNetIf->name[ 0 ] = IFNAME0;
\r
429 pxNetIf->name[ 1 ] = IFNAME1;
\r
431 /* We directly use etharp_output() here to save a function call.
\r
432 * You can instead declare your own function an call etharp_output()
\r
433 * from it if you have to do some checks before sending (e.g. if link
\r
434 * is available...) */
\r
435 pxNetIf->output = etharp_output;
\r
436 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
437 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
438 pxNetIf->mtu = netifMAX_MTU;
\r
439 pxNetIf->linkoutput = prvLowLevelOutput;
\r
441 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
443 /* initialize the hardware */
\r
444 prvLowLevelInit( pxNetIf );
\r
449 /*-----------------------------------------------------------*/
\r
451 static void prvRxHandler( void *pvNetIf )
\r
453 /* This is taken from lwIP example code and therefore does not conform
\r
454 to the FreeRTOS coding standard. */
\r
456 struct eth_hdr *pxHeader;
\r
458 unsigned short usInputLength;
\r
459 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
460 extern portBASE_TYPE xInsideISR;
\r
461 struct netif *pxNetIf = ( struct netif * ) pvNetIf;
\r
463 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
465 /* Ensure the pbuf handling functions don't attempt to use critical
\r
469 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
471 /* move received packet into a new pbuf */
\r
472 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
474 /* no packet could be read, silently ignore this */
\r
477 /* points to packet payload, which starts with an Ethernet header */
\r
478 pxHeader = p->payload;
\r
480 switch( htons( pxHeader->type ) )
\r
482 /* IP or ARP packet? */
\r
485 /* full packet send to tcpip_thread to process */
\r
486 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
488 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
503 /*-----------------------------------------------------------*/
\r
505 static void prvTxHandler( void *pvUnused )
\r
508 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r