2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
70 /* FreeRTOS includes. */
\r
71 #include "FreeRTOS.h"
\r
76 #include "xemaclite.h"
\r
77 #include "xintc_l.h"
\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
89 /* Define those to better describe your network interface. */
\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
98 #define netifTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_PERIOD_MS )
\r
99 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
101 #define netifMAX_MTU 1500
\r
105 struct eth_addr *ethaddr;
\r
106 /* Add whatever per-interface state that is needed here. */
\r
110 * Copy the received data into a pbuf.
\r
112 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
115 * Send data from a pbuf to the hardware.
\r
117 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
120 * Perform any hardware and/or driver initialisation necessary.
\r
122 static void prvLowLevelInit( struct netif *pxNetIf );
\r
125 * Functions that get registered as the Rx and Tx interrupt handers
\r
128 static void prvRxHandler( void *pvNetIf );
\r
129 static void prvTxHandler( void *pvUnused );
\r
132 /*-----------------------------------------------------------*/
\r
134 /* The instance of the xEmacLite IP being used in this driver. */
\r
135 static XEmacLite xEMACInstance;
\r
137 /*-----------------------------------------------------------*/
\r
140 * In this function, the hardware should be initialized.
\r
141 * Called from ethernetif_init().
\r
143 * @param pxNetIf the already initialized lwip network interface structure
\r
144 * for this etherpxNetIf
\r
146 static void prvLowLevelInit( struct netif *pxNetIf )
\r
148 portBASE_TYPE xStatus;
\r
149 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
150 unsigned portBASE_TYPE uxOriginalPriority;
\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
158 /* set MAC hardware address length */
\r
159 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\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
169 /* device capabilities */
\r
170 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
172 /* maximum transfer unit */
\r
173 pxNetIf->mtu = netifMAX_MTU;
\r
175 /* Broadcast capability */
\r
176 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
178 /* Initialize the mac */
\r
179 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
181 if( xStatus == XST_SUCCESS )
\r
183 /* Set mac address */
\r
184 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
186 /* Flush any frames already received */
\r
187 XEmacLite_FlushReceive( &xEMACInstance );
\r
189 /* Set Rx, Tx interrupt handlers */
\r
190 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
191 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
193 /* Enable Rx, Tx interrupts */
\r
194 XEmacLite_EnableInterrupts( &xEMACInstance );
\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
201 vInitialisePHY( &xEMACInstance );
\r
203 /* Enable the interrupt in the interrupt controller.
\r
204 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
206 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
209 /* Reset the task priority back to its original value. */
\r
210 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
212 configASSERT( xStatus == pdPASS );
\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
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
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
231 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
234 /* This is taken from lwIP example code and therefore does not conform
\r
235 to the FreeRTOS coding standard. */
\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
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
252 /* Initiate transfer. */
\r
253 if( p->len == p->tot_len )
\r
255 /* No pbuf chain, don't have to copy -> faster. */
\r
256 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
260 /* pbuf chain, copy into contiguous ucBuffer. */
\r
261 if( p->tot_len >= sizeof( ucBuffer ) )
\r
263 LINK_STATS_INC( link.lenerr );
\r
264 LINK_STATS_INC( link.drop );
\r
265 snmp_inc_ifoutdiscards( pxNetIf );
\r
270 pucChar = ucBuffer;
\r
272 for( q = p; q != NULL; q = q->next )
\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
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
281 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
282 pucChar += q->len - ETH_PAD_SIZE;
\r
286 memcpy( pucChar, q->payload, q->len );
\r
293 if( xReturn == ERR_OK )
\r
295 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
297 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
298 if( xReturn == XST_SUCCESS )
\r
304 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
308 if( xReturn != XST_SUCCESS )
\r
310 LINK_STATS_INC( link.memerr );
\r
311 LINK_STATS_INC( link.drop );
\r
312 snmp_inc_ifoutdiscards( pxNetIf );
\r
317 LINK_STATS_INC( link.xmit );
\r
318 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
319 pxHeader = ( struct eth_hdr * )p->payload;
\r
321 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
323 /* broadcast or multicast packet*/
\r
324 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
328 /* unicast packet */
\r
329 snmp_inc_ifoutucastpkts( pxNetIf );
\r
338 * Should allocate a pbuf and transfer the bytes of the incoming
\r
339 * packet from the interface into the pbuf.
\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
345 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
347 struct pbuf *p = NULL, *q;
\r
349 if( usDataLength > 0U )
\r
352 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
355 /* We allocate a pbuf chain of pbufs from the pool. */
\r
356 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
361 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
364 /* We iterate over the pbuf chain until we have read the entire
\r
365 * packet into the pbuf. */
\r
367 for( q = p; q != NULL; q = q->next )
\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
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
377 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
378 usDataLength += q->len;
\r
382 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
385 LINK_STATS_INC(link.recv);
\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
397 * This function should be passed as a parameter to pxNetIf_add().
\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
404 err_t ethernetif_init( struct netif *pxNetIf )
\r
406 err_t xReturn = ERR_OK;
\r
408 /* This is taken from lwIP example code and therefore does not conform
\r
409 to the FreeRTOS coding standard. */
\r
411 struct xEthernetIf *pxEthernetIf;
\r
413 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
415 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
416 if( pxEthernetIf == NULL )
\r
418 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
423 #if LWIP_NETIF_HOSTNAME
\r
425 /* Initialize interface hostname */
\r
426 pxNetIf->hostname = "lwip";
\r
428 #endif /* LWIP_NETIF_HOSTNAME */
\r
430 pxNetIf->state = pxEthernetIf;
\r
431 pxNetIf->name[ 0 ] = IFNAME0;
\r
432 pxNetIf->name[ 1 ] = IFNAME1;
\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
444 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
446 /* initialize the hardware */
\r
447 prvLowLevelInit( pxNetIf );
\r
452 /*-----------------------------------------------------------*/
\r
454 static void prvRxHandler( void *pvNetIf )
\r
456 /* This is taken from lwIP example code and therefore does not conform
\r
457 to the FreeRTOS coding standard. */
\r
459 struct eth_hdr *pxHeader;
\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
466 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
468 /* Ensure the pbuf handling functions don't attempt to use critical
\r
472 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
474 /* move received packet into a new pbuf */
\r
475 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
477 /* no packet could be read, silently ignore this */
\r
480 /* points to packet payload, which starts with an Ethernet header */
\r
481 pxHeader = p->payload;
\r
483 switch( htons( pxHeader->type ) )
\r
485 /* IP or ARP packet? */
\r
488 /* full packet send to tcpip_thread to process */
\r
489 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
491 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
506 /*-----------------------------------------------------------*/
\r
508 static void prvTxHandler( void *pvUnused )
\r
511 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r