2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* FreeRTOS includes. */
\r
29 #include "FreeRTOS.h"
\r
34 #include "xemaclite.h"
\r
35 #include "xintc_l.h"
\r
37 /* lwIP includes. */
\r
38 #include "lwip/opt.h"
\r
39 #include "lwip/def.h"
\r
40 #include "lwip/mem.h"
\r
41 #include "lwip/pbuf.h"
\r
42 #include "lwip/sys.h"
\r
43 #include <lwip/stats.h>
\r
44 #include <lwip/snmp.h>
\r
45 #include "netif/etharp.h"
\r
47 /* Define those to better describe your network interface. */
\r
51 /* When a packet is ready to be sent, if it cannot be sent immediately then
\r
52 * the task performing the transmit will block for netifTX_BUFFER_FREE_WAIT
\r
53 * milliseconds. It will do this a maximum of netifMAX_TX_ATTEMPTS before
\r
56 #define netifTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_PERIOD_MS )
\r
57 #define netifMAX_TX_ATTEMPTS ( 5 )
\r
59 #define netifMAX_MTU 1500
\r
63 struct eth_addr *ethaddr;
\r
64 /* Add whatever per-interface state that is needed here. */
\r
68 * Copy the received data into a pbuf.
\r
70 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength );
\r
73 * Send data from a pbuf to the hardware.
\r
75 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
78 * Perform any hardware and/or driver initialisation necessary.
\r
80 static void prvLowLevelInit( struct netif *pxNetIf );
\r
83 * Functions that get registered as the Rx and Tx interrupt handers
\r
86 static void prvRxHandler( void *pvNetIf );
\r
87 static void prvTxHandler( void *pvUnused );
\r
90 /*-----------------------------------------------------------*/
\r
92 /* The instance of the xEmacLite IP being used in this driver. */
\r
93 static XEmacLite xEMACInstance;
\r
95 /*-----------------------------------------------------------*/
\r
98 * In this function, the hardware should be initialized.
\r
99 * Called from ethernetif_init().
\r
101 * @param pxNetIf the already initialized lwip network interface structure
\r
102 * for this etherpxNetIf
\r
104 static void prvLowLevelInit( struct netif *pxNetIf )
\r
106 portBASE_TYPE xStatus;
\r
107 extern void vInitialisePHY( XEmacLite *xemaclitep );
\r
108 unsigned portBASE_TYPE uxOriginalPriority;
\r
110 /* Hardware initialisation can take some time, so temporarily lower the
\r
111 task priority to ensure other functionality is not adversely effected.
\r
112 The priority will get raised again before this function exits. */
\r
113 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
114 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
116 /* set MAC hardware address length */
\r
117 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
119 /* set MAC hardware address */
\r
120 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
121 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
122 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
123 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
124 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
125 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
127 /* device capabilities */
\r
128 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
130 /* maximum transfer unit */
\r
131 pxNetIf->mtu = netifMAX_MTU;
\r
133 /* Broadcast capability */
\r
134 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
136 /* Initialize the mac */
\r
137 xStatus = XEmacLite_Initialize( &xEMACInstance, XPAR_EMACLITE_0_DEVICE_ID );
\r
139 if( xStatus == XST_SUCCESS )
\r
141 /* Set mac address */
\r
142 XEmacLite_SetMacAddress( &xEMACInstance, ( Xuint8* )( pxNetIf->hwaddr ) );
\r
144 /* Flush any frames already received */
\r
145 XEmacLite_FlushReceive( &xEMACInstance );
\r
147 /* Set Rx, Tx interrupt handlers */
\r
148 XEmacLite_SetRecvHandler( &xEMACInstance, ( void * ) pxNetIf, prvRxHandler );
\r
149 XEmacLite_SetSendHandler( &xEMACInstance, NULL, prvTxHandler );
\r
151 /* Enable Rx, Tx interrupts */
\r
152 XEmacLite_EnableInterrupts( &xEMACInstance );
\r
154 /* Install the standard Xilinx library interrupt handler itself.
\r
155 *NOTE* The xPortInstallInterruptHandler() API function must be used
\r
156 for this purpose. */
\r
157 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_EMACLITE_0_VEC_ID, ( XInterruptHandler ) XEmacLite_InterruptHandler, &xEMACInstance );
\r
159 vInitialisePHY( &xEMACInstance );
\r
161 /* Enable the interrupt in the interrupt controller.
\r
162 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
164 vPortEnableInterrupt( XPAR_INTC_0_EMACLITE_0_VEC_ID );
\r
167 /* Reset the task priority back to its original value. */
\r
168 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
170 configASSERT( xStatus == pdPASS );
\r
174 * This function should do the actual transmission of the packet. The packet is
\r
175 * contained in the pbuf that is passed to the function. This pbuf
\r
176 * might be chained.
\r
178 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
179 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
180 * @return ERR_OK if the packet could be sent
\r
181 * an err_t value if the packet couldn't be sent
\r
183 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
184 * strange results. You might consider waiting for space in the DMA queue
\r
185 * to become availale since the stack doesn't retry to send a packet
\r
186 * dropped because of memory failure (except for the TCP timers).
\r
189 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
192 /* This is taken from lwIP example code and therefore does not conform
\r
193 to the FreeRTOS coding standard. */
\r
196 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
197 unsigned char *pucBuffer = ucBuffer;
\r
198 unsigned char *pucChar;
\r
199 struct eth_hdr *pxHeader;
\r
200 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
201 err_t xReturn = ERR_OK;
\r
206 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
207 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
210 /* Initiate transfer. */
\r
211 if( p->len == p->tot_len )
\r
213 /* No pbuf chain, don't have to copy -> faster. */
\r
214 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
218 /* pbuf chain, copy into contiguous ucBuffer. */
\r
219 if( p->tot_len >= sizeof( ucBuffer ) )
\r
221 LINK_STATS_INC( link.lenerr );
\r
222 LINK_STATS_INC( link.drop );
\r
223 snmp_inc_ifoutdiscards( pxNetIf );
\r
228 pucChar = ucBuffer;
\r
230 for( q = p; q != NULL; q = q->next )
\r
232 /* Send the data from the pbuf to the interface, one pbuf at a
\r
233 time. The size of the data in each pbuf is kept in the ->len
\r
235 /* send data from(q->payload, q->len); */
\r
236 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
239 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
240 pucChar += q->len - ETH_PAD_SIZE;
\r
244 memcpy( pucChar, q->payload, q->len );
\r
251 if( xReturn == ERR_OK )
\r
253 for( x = 0; x < netifMAX_TX_ATTEMPTS; x++ )
\r
255 xReturn = XEmacLite_Send( &xEMACInstance, pucBuffer, ( int ) usTotalLength );
\r
256 if( xReturn == XST_SUCCESS )
\r
262 vTaskDelay( netifTX_BUFFER_FREE_WAIT );
\r
266 if( xReturn != XST_SUCCESS )
\r
268 LINK_STATS_INC( link.memerr );
\r
269 LINK_STATS_INC( link.drop );
\r
270 snmp_inc_ifoutdiscards( pxNetIf );
\r
275 LINK_STATS_INC( link.xmit );
\r
276 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
277 pxHeader = ( struct eth_hdr * )p->payload;
\r
279 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
281 /* broadcast or multicast packet*/
\r
282 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
286 /* unicast packet */
\r
287 snmp_inc_ifoutucastpkts( pxNetIf );
\r
296 * Should allocate a pbuf and transfer the bytes of the incoming
\r
297 * packet from the interface into the pbuf.
\r
299 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
300 * @return a pbuf filled with the received packet (including MAC header)
\r
301 * NULL on memory error
\r
303 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, unsigned short usDataLength )
\r
305 struct pbuf *p = NULL, *q;
\r
307 if( usDataLength > 0U )
\r
310 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
313 /* We allocate a pbuf chain of pbufs from the pool. */
\r
314 p = pbuf_alloc( PBUF_RAW, usDataLength, PBUF_POOL );
\r
319 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
322 /* We iterate over the pbuf chain until we have read the entire
\r
323 * packet into the pbuf. */
\r
325 for( q = p; q != NULL; q = q->next )
\r
327 /* Read enough bytes to fill this pbuf in the chain. The
\r
328 * available data in the pbuf is given by the q->len
\r
330 * This does not necessarily have to be a memcpy, you can also preallocate
\r
331 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
332 * actually received size. In this case, ensure the usTotalLength member of the
\r
333 * pbuf is the sum of the chained pbuf len members.
\r
335 memcpy( q->payload, &( pucInputData[ usDataLength ] ), q->len );
\r
336 usDataLength += q->len;
\r
340 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
343 LINK_STATS_INC(link.recv);
\r
351 * Should be called at the beginning of the program to set up the
\r
352 * network interface. It calls the function prvLowLevelInit() to do the
\r
353 * actual setup of the hardware.
\r
355 * This function should be passed as a parameter to pxNetIf_add().
\r
357 * @param pxNetIf the lwip network interface structure for this etherpxNetIf
\r
358 * @return ERR_OK if the loopif is initialized
\r
359 * ERR_MEM if private data couldn't be allocated
\r
360 * any other err_t on error
\r
362 err_t ethernetif_init( struct netif *pxNetIf )
\r
364 err_t xReturn = ERR_OK;
\r
366 /* This is taken from lwIP example code and therefore does not conform
\r
367 to the FreeRTOS coding standard. */
\r
369 struct xEthernetIf *pxEthernetIf;
\r
371 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
373 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
374 if( pxEthernetIf == NULL )
\r
376 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
381 #if LWIP_NETIF_HOSTNAME
\r
383 /* Initialize interface hostname */
\r
384 pxNetIf->hostname = "lwip";
\r
386 #endif /* LWIP_NETIF_HOSTNAME */
\r
388 pxNetIf->state = pxEthernetIf;
\r
389 pxNetIf->name[ 0 ] = IFNAME0;
\r
390 pxNetIf->name[ 1 ] = IFNAME1;
\r
392 /* We directly use etharp_output() here to save a function call.
\r
393 * You can instead declare your own function an call etharp_output()
\r
394 * from it if you have to do some checks before sending (e.g. if link
\r
395 * is available...) */
\r
396 pxNetIf->output = etharp_output;
\r
397 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
398 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
399 pxNetIf->mtu = netifMAX_MTU;
\r
400 pxNetIf->linkoutput = prvLowLevelOutput;
\r
402 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
404 /* initialize the hardware */
\r
405 prvLowLevelInit( pxNetIf );
\r
410 /*-----------------------------------------------------------*/
\r
412 static void prvRxHandler( void *pvNetIf )
\r
414 /* This is taken from lwIP example code and therefore does not conform
\r
415 to the FreeRTOS coding standard. */
\r
417 struct eth_hdr *pxHeader;
\r
419 unsigned short usInputLength;
\r
420 static unsigned char ucBuffer[ 1520 ] __attribute__((aligned(32)));
\r
421 extern portBASE_TYPE xInsideISR;
\r
422 struct netif *pxNetIf = ( struct netif * ) pvNetIf;
\r
424 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r
426 /* Ensure the pbuf handling functions don't attempt to use critical
\r
430 usInputLength = ( long ) XEmacLite_Recv( &xEMACInstance, ucBuffer );
\r
432 /* move received packet into a new pbuf */
\r
433 p = prvLowLevelInput( ucBuffer, usInputLength );
\r
435 /* no packet could be read, silently ignore this */
\r
438 /* points to packet payload, which starts with an Ethernet header */
\r
439 pxHeader = p->payload;
\r
441 switch( htons( pxHeader->type ) )
\r
443 /* IP or ARP packet? */
\r
446 /* full packet send to tcpip_thread to process */
\r
447 if( pxNetIf->input( p, pxNetIf ) != ERR_OK )
\r
449 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
464 /*-----------------------------------------------------------*/
\r
466 static void prvTxHandler( void *pvUnused )
\r
469 XIntc_AckIntr( XPAR_ETHERNET_LITE_BASEADDR, XPAR_ETHERNET_LITE_IP2INTC_IRPT_MASK );
\r