3 Copyright (C) 2017 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://aws.amazon.com/freertos
\r
23 http://www.FreeRTOS.org
\r
26 /* Standard includes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\r
37 /* FreeRTOS+TCP includes. */
\r
38 #include "FreeRTOS_IP.h"
\r
39 #include "FreeRTOS_Sockets.h"
\r
40 #include "FreeRTOS_IP_Private.h"
\r
41 #include "FreeRTOS_ARP.h"
\r
42 #include "NetworkBufferManagement.h"
\r
43 #include "NetworkInterface.h"
\r
45 /* Xilinx library files. */
\r
46 #include <xemacps.h>
\r
47 #include "Zynq/x_topology.h"
\r
48 #include "Zynq/x_emacpsif.h"
\r
49 #include "Zynq/x_emacpsif_hw.h"
\r
51 /* Provided memory configured as uncached. */
\r
52 #include "uncached_memory.h"
\r
54 #ifndef niEMAC_HANDLER_TASK_PRIORITY
\r
55 #define niEMAC_HANDLER_TASK_PRIORITY configMAX_PRIORITIES - 1
\r
58 #define niBMSR_LINK_STATUS 0x0004UL
\r
59 #define niBMSR_AN_COMPLETE 0x0020u /* Auto-Negotiation process completed */
\r
61 #ifndef PHY_LS_HIGH_CHECK_TIME_MS
\r
62 /* Check if the LinkSStatus in the PHY is still high after 15 seconds of not
\r
63 receiving packets. */
\r
64 #define PHY_LS_HIGH_CHECK_TIME_MS 15000
\r
67 #ifndef PHY_LS_LOW_CHECK_TIME_MS
\r
68 /* Check if the LinkSStatus in the PHY is still low every second. */
\r
69 #define PHY_LS_LOW_CHECK_TIME_MS 1000
\r
72 /* The size of each buffer when BufferAllocation_1 is used:
\r
73 http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */
\r
74 #define niBUFFER_1_PACKET_SIZE 1536
\r
76 /* Naming and numbering of PHY registers. */
\r
77 #define PHY_REG_01_BMSR 0x01 /* Basic mode status register */
\r
79 #ifndef iptraceEMAC_TASK_STARTING
\r
80 #define iptraceEMAC_TASK_STARTING() do { } while( 0 )
\r
83 /* Default the size of the stack used by the EMAC deferred handler task to twice
\r
84 the size of the stack used by the idle task - but allow this to be overridden in
\r
85 FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */
\r
86 #ifndef configEMAC_TASK_STACK_SIZE
\r
87 #define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )
\r
90 /*-----------------------------------------------------------*/
\r
93 * Look for the link to be up every few milliseconds until either xMaxTime time
\r
94 * has passed or a link is found.
\r
96 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );
\r
99 * A deferred interrupt handler for all MAC/DMA interrupt sources.
\r
101 static void prvEMACHandlerTask( void *pvParameters );
\r
103 /*-----------------------------------------------------------*/
\r
105 /* EMAC data/descriptions. */
\r
106 static xemacpsif_s xEMACpsif;
\r
107 struct xtopology_t xXTopology =
\r
109 .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,
\r
110 .emac_type = xemac_type_emacps,
\r
111 .intc_baseaddr = 0x0,
\r
112 .intc_emac_intr = 0x0,
\r
113 .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,
\r
114 .scugic_emac_intr = 0x36,
\r
117 XEmacPs_Config mac_config =
\r
119 .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID, /**< Unique ID of device */
\r
120 .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */
\r
123 extern int phy_detected;
\r
125 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */
\r
126 static uint32_t ulPHYLinkStatus = 0;
\r
128 #if( ipconfigUSE_LLMNR == 1 )
\r
129 static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
\r
132 /* ucMACAddress as it appears in main.c */
\r
133 extern const uint8_t ucMACAddress[ 6 ];
\r
135 /* Holds the handle of the task used as a deferred interrupt processor. The
\r
136 handle is used so direct notifications can be sent to the task for all EMAC/DMA
\r
137 related interrupts. */
\r
138 TaskHandle_t xEMACTaskHandle = NULL;
\r
140 /*-----------------------------------------------------------*/
\r
142 BaseType_t xNetworkInterfaceInitialise( void )
\r
144 uint32_t ulLinkSpeed, ulDMAReg;
\r
145 BaseType_t xStatus, xLinkStatus;
\r
146 XEmacPs *pxEMAC_PS;
\r
147 const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );
\r
149 /* Guard against the init function being called more than once. */
\r
150 if( xEMACTaskHandle == NULL )
\r
152 pxEMAC_PS = &( xEMACpsif.emacps );
\r
153 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );
\r
155 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);
\r
156 if( xStatus != XST_SUCCESS )
\r
158 FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );
\r
161 /* Initialize the mac and set the MAC address. */
\r
162 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );
\r
164 #if( ipconfigUSE_LLMNR == 1 )
\r
166 /* Also add LLMNR multicast MAC address. */
\r
167 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );
\r
169 #endif /* ipconfigUSE_LLMNR == 1 */
\r
171 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );
\r
172 ulLinkSpeed = Phy_Setup( pxEMAC_PS );
\r
173 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);
\r
175 /* Setting the operating speed of the MAC needs a delay. */
\r
176 vTaskDelay( pdMS_TO_TICKS( 25UL ) );
\r
178 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);
\r
180 /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive
\r
181 packets from the receiver packet buffer memory when no AHB resource is available. */
\r
182 XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
\r
183 ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);
\r
185 setup_isr( &xEMACpsif );
\r
186 init_dma( &xEMACpsif );
\r
187 start_emacps( &xEMACpsif );
\r
189 prvGMACWaitLS( xWaitLinkDelay );
\r
191 /* The deferred interrupt handler task is created at the highest
\r
192 possible priority to ensure the interrupt handler can return directly
\r
193 to it. The task's handle is stored in xEMACTaskHandle so interrupts can
\r
194 notify the task when there is something to process. */
\r
195 xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, niEMAC_HANDLER_TASK_PRIORITY, &xEMACTaskHandle );
\r
199 /* Initialisation was already performed, just wait for the link. */
\r
200 prvGMACWaitLS( xWaitRelinkDelay );
\r
203 /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the
\r
204 DHCP process and all other communication will fail. */
\r
205 xLinkStatus = xGetPhyLinkStatus();
\r
207 return ( xLinkStatus != pdFALSE );
\r
209 /*-----------------------------------------------------------*/
\r
211 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )
\r
213 if( xCheckLoopback( pxBuffer, bReleaseAfterSend ) != 0 )
\r
215 /* The packet has been sent back to the IP-task.
\r
216 The IP-task will further handle it.
\r
217 Do not release the descriptor. */
\r
220 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )
\r
222 ProtocolPacket_t *pxPacket;
\r
224 /* If the peripheral must calculate the checksum, it wants
\r
225 the protocol checksum to have a value of zero. */
\r
226 pxPacket = ( ProtocolPacket_t * ) ( pxBuffer->pucEthernetBuffer );
\r
227 if( pxPacket->xICMPPacket.xIPHeader.ucProtocol == ipPROTOCOL_ICMP )
\r
229 IPHeader_t *pxIPHeader = &( pxPacket->xUDPPacket.xIPHeader );
\r
231 pxPacket->xICMPPacket.xICMPHeader.usChecksum = ( uint16_t )0u;
\r
232 pxIPHeader->usHeaderChecksum = 0u;
\r
233 pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
\r
234 pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
\r
236 usGenerateProtocolChecksum( (uint8_t*)&( pxPacket->xUDPPacket ), pxBuffer->xDataLength, pdTRUE );
\r
239 #endif /* ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM */
\r
240 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
242 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
243 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );
\r
245 else if( bReleaseAfterSend != pdFALSE )
\r
248 vReleaseNetworkBufferAndDescriptor( pxBuffer );
\r
253 /*-----------------------------------------------------------*/
\r
255 static inline unsigned long ulReadMDIO( unsigned ulRegister )
\r
259 XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );
\r
262 /*-----------------------------------------------------------*/
\r
264 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )
\r
266 TickType_t xStartTime, xEndTime;
\r
267 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );
\r
268 BaseType_t xReturn;
\r
270 xStartTime = xTaskGetTickCount();
\r
274 xEndTime = xTaskGetTickCount();
\r
276 if( xEndTime - xStartTime > xMaxTime )
\r
281 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
283 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
289 vTaskDelay( xShortDelay );
\r
294 /*-----------------------------------------------------------*/
\r
296 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
\r
298 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );
\r
299 uint8_t *ucRAMBuffer = ucNetworkPackets;
\r
302 for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
\r
304 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
\r
305 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
\r
306 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
\r
309 /*-----------------------------------------------------------*/
\r
311 BaseType_t xGetPhyLinkStatus( void )
\r
313 BaseType_t xReturn;
\r
315 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0 )
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvEMACHandlerTask( void *pvParameters )
\r
330 TimeOut_t xPhyTime;
\r
331 TickType_t xPhyRemTime;
\r
332 UBaseType_t uxCurrentCount;
\r
333 BaseType_t xResult = 0;
\r
335 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
\r
336 UBaseType_t uxLastMinBufferCount = 0;
\r
337 UBaseType_t uxCurrentBufferCount = 0;
\r
339 /* Remove compiler warnings about unused parameters. */
\r
340 ( void ) pvParameters;
\r
342 /* A possibility to set some additional task properties like calling
\r
343 portTASK_USES_FLOATING_POINT() */
\r
344 iptraceEMAC_TASK_STARTING();
\r
346 vTaskSetTimeOutState( &xPhyTime );
\r
347 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
351 uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
\r
352 if( uxLastMinBufferCount != uxCurrentBufferCount )
\r
354 /* The logging produced below may be helpful
\r
355 while tuning +TCP: see how many buffers are in use. */
\r
356 uxLastMinBufferCount = uxCurrentBufferCount;
\r
357 FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
\r
358 uxGetNumberOfFreeNetworkBuffers(), uxCurrentBufferCount ) );
\r
361 #if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
\r
363 static UBaseType_t uxLastMinQueueSpace = 0;
\r
365 uxCurrentCount = uxGetMinimumIPQueueSpace();
\r
366 if( uxLastMinQueueSpace != uxCurrentCount )
\r
368 /* The logging produced below may be helpful
\r
369 while tuning +TCP: see how many buffers are in use. */
\r
370 uxLastMinQueueSpace = uxCurrentCount;
\r
371 FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
\r
374 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */
\r
376 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )
\r
378 /* No events to process now, wait for the next. */
\r
379 ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
\r
382 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )
\r
384 xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;
\r
385 xResult = emacps_check_rx( &xEMACpsif );
\r
388 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )
\r
390 xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;
\r
391 emacps_check_tx( &xEMACpsif );
\r
394 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )
\r
396 xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;
\r
397 emacps_check_errors( &xEMACpsif );
\r
401 /* A packet was received. No need to check for the PHY status now,
\r
402 but set a timer to check it later on. */
\r
403 vTaskSetTimeOutState( &xPhyTime );
\r
404 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
406 /* Indicate that the Link Status is high, so that
\r
407 xNetworkInterfaceOutput() can send packets. */
\r
408 ulPHYLinkStatus |= niBMSR_LINK_STATUS;
\r
410 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )
\r
412 xStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
414 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != ( xStatus & niBMSR_LINK_STATUS ) )
\r
416 ulPHYLinkStatus = xStatus;
\r
417 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 ) );
\r
420 vTaskSetTimeOutState( &xPhyTime );
\r
421 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
423 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
427 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
432 /*-----------------------------------------------------------*/
\r