2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
81 #include "xemaclite.h"
\r
82 #include "xintc_l.h"
\r
84 /* lwIP includes. */
\r
85 #include "lwip/opt.h"
\r
86 #include "lwip/def.h"
\r
87 #include "lwip/mem.h"
\r
88 #include "lwip/pbuf.h"
\r
89 #include "lwip/sys.h"
\r
90 #include <lwip/stats.h>
\r
91 #include <lwip/snmp.h>
\r
92 #include "netif/etharp.h"
\r
94 /* Define those to better describe your network interface. */
\r
98 /* When a packet is ready to be sent, if it cannot be sent immediately then
\r
99 * the task performing the transmit will block for netifTX_BUFFER_FREE_WAIT
\r
100 * milliseconds. It will do this a maximum of netifMAX_TX_ATTEMPTS before
\r
103 #define netifTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
104 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
106 #define netifMAX_MTU 1500
\r
110 struct eth_addr *ethaddr;
\r
111 /* Add whatever per-interface state that is needed here. */
\r
115 * Copy the received data into a pbuf.
\r
117 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
120 * Send data from a pbuf to the hardware.
\r
122 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
125 * Perform any hardware and/or driver initialisation necessary.
\r
127 static void prvLowLevelInit( struct netif *pxNetIf );
\r
130 * Functions that get registered as the Rx and Tx interrupt handers
\r
133 static void prvRxHandler( void *pvNetIf );
\r
134 static void prvTxHandler( void *pvUnused );
\r
137 /*-----------------------------------------------------------*/
\r
139 /* The instance of the xEmacLite IP being used in this driver. */
\r
140 static XEmacLite xEMACInstance;
\r
142 /*-----------------------------------------------------------*/
\r
145 * In this function, the hardware should be initialized.
\r
146 * Called from ethernetif_init().
\r
148 * @param pxNetIf the already initialized lwip network interface structure
\r
149 * for this etherpxNetIf
\r
151 static void prvLowLevelInit( struct netif *pxNetIf )
\r
153 portBASE_TYPE xStatus;
\r
154 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
155 unsigned portBASE_TYPE uxOriginalPriority;
\r
157 /* Hardware initialisation can take some time, so temporarily lower the
\r
158 task priority to ensure other functionality is not adversely effected.
\r
159 The priority will get raised again before this function exits. */
\r
160 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
161 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
163 /* set MAC hardware address length */
\r
164 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
166 /* set MAC hardware address */
\r
167 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
168 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
169 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
170 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
171 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
172 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
174 /* device capabilities */
\r
175 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
177 /* maximum transfer unit */
\r
178 pxNetIf->mtu = netifMAX_MTU;
\r
180 /* Broadcast capability */
\r
181 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
183 /* Initialize the mac */
\r
184 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
186 if( xStatus == XST_SUCCESS )
\r
188 /* Set mac address */
\r
189 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
191 /* Flush any frames already received */
\r
192 XEmacLite_FlushReceive( &xEMACInstance );
\r
194 /* Set Rx, Tx interrupt handlers */
\r
195 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
196 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
198 /* Enable Rx, Tx interrupts */
\r
199 XEmacLite_EnableInterrupts( &xEMACInstance );
\r
201 /* Install the standard Xilinx library interrupt handler itself.
\r
202 *NOTE* The xPortInstallInterruptHandler() API function must be used
\r
203 for this purpose. */
\r
204 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_EMACLITE_0_VEC_ID, ( XInterruptHandler ) XEmacLite_InterruptHandler, &xEMACInstance );
\r
206 vInitialisePHY( &xEMACInstance );
\r
208 /* Enable the interrupt in the interrupt controller.
\r
209 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
211 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
214 /* Reset the task priority back to its original value. */
\r
215 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
217 configASSERT( xStatus == pdPASS );
\r
221 * This function should do the actual transmission of the packet. The packet is
\r
222 * contained in the pbuf that is passed to the function. This pbuf
\r
223 * might be chained.
\r
225 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
226 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
227 * @return ERR_OK if the packet could be sent
\r
228 * an err_t value if the packet couldn't be sent
\r
230 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
231 * strange results. You might consider waiting for space in the DMA queue
\r
232 * to become availale since the stack doesn't retry to send a packet
\r
233 * dropped because of memory failure (except for the TCP timers).
\r
236 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
239 /* This is taken from lwIP example code and therefore does not conform
\r
240 to the FreeRTOS coding standard. */
\r
243 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
244 unsigned char *pucBuffer = ucBuffer;
\r
245 unsigned char *pucChar;
\r
246 struct eth_hdr *pxHeader;
\r
247 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
248 err_t xReturn = ERR_OK;
\r
253 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
254 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
257 /* Initiate transfer. */
\r
258 if( p->len == p->tot_len )
\r
260 /* No pbuf chain, don't have to copy -> faster. */
\r
261 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
265 /* pbuf chain, copy into contiguous ucBuffer. */
\r
266 if( p->tot_len >= sizeof( ucBuffer ) )
\r
268 LINK_STATS_INC( link.lenerr );
\r
269 LINK_STATS_INC( link.drop );
\r
270 snmp_inc_ifoutdiscards( pxNetIf );
\r
275 pucChar = ucBuffer;
\r
277 for( q = p; q != NULL; q = q->next )
\r
279 /* Send the data from the pbuf to the interface, one pbuf at a
\r
280 time. The size of the data in each pbuf is kept in the ->len
\r
282 /* send data from(q->payload, q->len); */
\r
283 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
286 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
287 pucChar += q->len - ETH_PAD_SIZE;
\r
291 memcpy( pucChar, q->payload, q->len );
\r
298 if( xReturn == ERR_OK )
\r
300 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
302 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
303 if( xReturn == XST_SUCCESS )
\r
309 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
313 if( xReturn != XST_SUCCESS )
\r
315 LINK_STATS_INC( link.memerr );
\r
316 LINK_STATS_INC( link.drop );
\r
317 snmp_inc_ifoutdiscards( pxNetIf );
\r
322 LINK_STATS_INC( link.xmit );
\r
323 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
324 pxHeader = ( struct eth_hdr * )p->payload;
\r
326 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
328 /* broadcast or multicast packet*/
\r
329 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
333 /* unicast packet */
\r
334 snmp_inc_ifoutucastpkts( pxNetIf );
\r
343 * Should allocate a pbuf and transfer the bytes of the incoming
\r
344 * packet from the interface into the pbuf.
\r
346 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
347 * @return a pbuf filled with the received packet (including MAC header)
\r
348 * NULL on memory error
\r
350 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
352 struct pbuf *p = NULL, *q;
\r
354 if( usDataLength > 0U )
\r
357 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
360 /* We allocate a pbuf chain of pbufs from the pool. */
\r
361 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
366 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
369 /* We iterate over the pbuf chain until we have read the entire
\r
370 * packet into the pbuf. */
\r
372 for( q = p; q != NULL; q = q->next )
\r
374 /* Read enough bytes to fill this pbuf in the chain. The
\r
375 * available data in the pbuf is given by the q->len
\r
377 * This does not necessarily have to be a memcpy, you can also preallocate
\r
378 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
379 * actually received size. In this case, ensure the usTotalLength member of the
\r
380 * pbuf is the sum of the chained pbuf len members.
\r
382 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
383 usDataLength += q->len;
\r
387 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
390 LINK_STATS_INC(link.recv);
\r
398 * Should be called at the beginning of the program to set up the
\r
399 * network interface. It calls the function prvLowLevelInit() to do the
\r
400 * actual setup of the hardware.
\r
402 * This function should be passed as a parameter to pxNetIf_add().
\r
404 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
405 * @return ERR_OK if the loopif is initialized
\r
406 * ERR_MEM if private data couldn't be allocated
\r
407 * any other err_t on error
\r
409 err_t ethernetif_init( struct netif *pxNetIf )
\r
411 err_t xReturn = ERR_OK;
\r
413 /* This is taken from lwIP example code and therefore does not conform
\r
414 to the FreeRTOS coding standard. */
\r
416 struct xEthernetIf *pxEthernetIf;
\r
418 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
420 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
421 if( pxEthernetIf == NULL )
\r
423 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
428 #if LWIP_NETIF_HOSTNAME
\r
430 /* Initialize interface hostname */
\r
431 pxNetIf->hostname = "lwip";
\r
433 #endif /* LWIP_NETIF_HOSTNAME */
\r
435 pxNetIf->state = pxEthernetIf;
\r
436 pxNetIf->name[ 0 ] = IFNAME0;
\r
437 pxNetIf->name[ 1 ] = IFNAME1;
\r
439 /* We directly use etharp_output() here to save a function call.
\r
440 * You can instead declare your own function an call etharp_output()
\r
441 * from it if you have to do some checks before sending (e.g. if link
\r
442 * is available...) */
\r
443 pxNetIf->output = etharp_output;
\r
444 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
445 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
446 pxNetIf->mtu = netifMAX_MTU;
\r
447 pxNetIf->linkoutput = prvLowLevelOutput;
\r
449 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
451 /* initialize the hardware */
\r
452 prvLowLevelInit( pxNetIf );
\r
457 /*-----------------------------------------------------------*/
\r
459 static void prvRxHandler( void *pvNetIf )
\r
461 /* This is taken from lwIP example code and therefore does not conform
\r
462 to the FreeRTOS coding standard. */
\r
464 struct eth_hdr *pxHeader;
\r
466 unsigned short usInputLength;
\r
467 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
468 extern portBASE_TYPE xInsideISR;
\r
469 struct netif *pxNetIf = ( struct netif * ) pvNetIf;
\r
471 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
473 /* Ensure the pbuf handling functions don't attempt to use critical
\r
477 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
479 /* move received packet into a new pbuf */
\r
480 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
482 /* no packet could be read, silently ignore this */
\r
485 /* points to packet payload, which starts with an Ethernet header */
\r
486 pxHeader = p->payload;
\r
488 switch( htons( pxHeader->type ) )
\r
490 /* IP or ARP packet? */
\r
493 /* full packet send to tcpip_thread to process */
\r
494 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
496 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
511 /*-----------------------------------------------------------*/
\r
513 static void prvTxHandler( void *pvUnused )
\r
516 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r