2 * FreeRTOS+TCP V2.0.11
\r
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 "NetworkBufferManagement.h"
\r
42 #include "NetworkInterface.h"
\r
44 /* Xilinx library files. */
\r
45 #include <xemacps.h>
\r
46 #include "Zynq/x_topology.h"
\r
47 #include "Zynq/x_emacpsif.h"
\r
48 #include "Zynq/x_emacpsif_hw.h"
\r
50 /* Provided memory configured as uncached. */
\r
51 #include "uncached_memory.h"
\r
53 #ifndef niEMAC_HANDLER_TASK_PRIORITY
\r
54 #define niEMAC_HANDLER_TASK_PRIORITY configMAX_PRIORITIES - 1
\r
57 #define niBMSR_LINK_STATUS 0x0004UL
\r
59 #ifndef PHY_LS_HIGH_CHECK_TIME_MS
\r
60 /* Check if the LinkSStatus in the PHY is still high after 15 seconds of not
\r
61 receiving packets. */
\r
62 #define PHY_LS_HIGH_CHECK_TIME_MS 15000
\r
65 #ifndef PHY_LS_LOW_CHECK_TIME_MS
\r
66 /* Check if the LinkSStatus in the PHY is still low every second. */
\r
67 #define PHY_LS_LOW_CHECK_TIME_MS 1000
\r
70 /* The size of each buffer when BufferAllocation_1 is used:
\r
71 http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */
\r
72 #define niBUFFER_1_PACKET_SIZE 1536
\r
74 /* Naming and numbering of PHY registers. */
\r
75 #define PHY_REG_01_BMSR 0x01 /* Basic mode status register */
\r
77 #ifndef iptraceEMAC_TASK_STARTING
\r
78 #define iptraceEMAC_TASK_STARTING() do { } while( 0 )
\r
81 /* Default the size of the stack used by the EMAC deferred handler task to twice
\r
82 the size of the stack used by the idle task - but allow this to be overridden in
\r
83 FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */
\r
84 #ifndef configEMAC_TASK_STACK_SIZE
\r
85 #define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )
\r
88 /*-----------------------------------------------------------*/
\r
91 * Look for the link to be up every few milliseconds until either xMaxTime time
\r
92 * has passed or a link is found.
\r
94 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );
\r
97 * A deferred interrupt handler for all MAC/DMA interrupt sources.
\r
99 static void prvEMACHandlerTask( void *pvParameters );
\r
101 /*-----------------------------------------------------------*/
\r
103 /* EMAC data/descriptions. */
\r
104 static xemacpsif_s xEMACpsif;
\r
105 struct xtopology_t xXTopology =
\r
107 .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,
\r
108 .emac_type = xemac_type_emacps,
\r
109 .intc_baseaddr = 0x0,
\r
110 .intc_emac_intr = 0x0,
\r
111 .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,
\r
112 .scugic_emac_intr = 0x36,
\r
115 XEmacPs_Config mac_config =
\r
117 .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID, /**< Unique ID of device */
\r
118 .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */
\r
121 extern int phy_detected;
\r
123 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */
\r
124 static uint32_t ulPHYLinkStatus = 0;
\r
126 #if( ipconfigUSE_LLMNR == 1 )
\r
127 static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
\r
130 /* ucMACAddress as it appears in main.c */
\r
131 extern const uint8_t ucMACAddress[ 6 ];
\r
133 /* Holds the handle of the task used as a deferred interrupt processor. The
\r
134 handle is used so direct notifications can be sent to the task for all EMAC/DMA
\r
135 related interrupts. */
\r
136 TaskHandle_t xEMACTaskHandle = NULL;
\r
138 /*-----------------------------------------------------------*/
\r
140 BaseType_t xNetworkInterfaceInitialise( void )
\r
142 uint32_t ulLinkSpeed, ulDMAReg;
\r
143 BaseType_t xStatus, xLinkStatus;
\r
144 XEmacPs *pxEMAC_PS;
\r
145 const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );
\r
147 /* Guard against the init function being called more than once. */
\r
148 if( xEMACTaskHandle == NULL )
\r
150 pxEMAC_PS = &( xEMACpsif.emacps );
\r
151 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );
\r
153 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);
\r
154 if( xStatus != XST_SUCCESS )
\r
156 FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );
\r
159 /* Initialize the mac and set the MAC address. */
\r
160 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );
\r
162 #if( ipconfigUSE_LLMNR == 1 )
\r
164 /* Also add LLMNR multicast MAC address. */
\r
165 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );
\r
167 #endif /* ipconfigUSE_LLMNR == 1 */
\r
169 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );
\r
170 ulLinkSpeed = Phy_Setup( pxEMAC_PS );
\r
171 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);
\r
173 /* Setting the operating speed of the MAC needs a delay. */
\r
174 vTaskDelay( pdMS_TO_TICKS( 25UL ) );
\r
176 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);
\r
178 /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive
\r
179 packets from the receiver packet buffer memory when no AHB resource is available. */
\r
180 XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
\r
181 ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);
\r
183 setup_isr( &xEMACpsif );
\r
184 init_dma( &xEMACpsif );
\r
185 start_emacps( &xEMACpsif );
\r
187 prvGMACWaitLS( xWaitLinkDelay );
\r
189 /* The deferred interrupt handler task is created at the highest
\r
190 possible priority to ensure the interrupt handler can return directly
\r
191 to it. The task's handle is stored in xEMACTaskHandle so interrupts can
\r
192 notify the task when there is something to process. */
\r
193 xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, niEMAC_HANDLER_TASK_PRIORITY, &xEMACTaskHandle );
\r
197 /* Initialisation was already performed, just wait for the link. */
\r
198 prvGMACWaitLS( xWaitRelinkDelay );
\r
201 /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the
\r
202 DHCP process and all other communication will fail. */
\r
203 xLinkStatus = xGetPhyLinkStatus();
\r
205 return ( xLinkStatus != pdFALSE );
\r
207 /*-----------------------------------------------------------*/
\r
209 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )
\r
211 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )
\r
213 ProtocolPacket_t *pxPacket;
\r
215 /* If the peripheral must calculate the checksum, it wants
\r
216 the protocol checksum to have a value of zero. */
\r
217 pxPacket = ( ProtocolPacket_t * ) ( pxBuffer->pucEthernetBuffer );
\r
218 if( pxPacket->xICMPPacket.xIPHeader.ucProtocol == ipPROTOCOL_ICMP )
\r
220 IPHeader_t *pxIPHeader = &( pxPacket->xUDPPacket.xIPHeader );
\r
222 pxPacket->xICMPPacket.xICMPHeader.usChecksum = ( uint16_t )0u;
\r
223 pxIPHeader->usHeaderChecksum = 0u;
\r
224 pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
\r
225 pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
\r
227 usGenerateProtocolChecksum( (uint8_t*)&( pxPacket->xUDPPacket ), pxBuffer->xDataLength, pdTRUE );
\r
230 #endif /* ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM */
\r
231 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
233 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
234 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );
\r
236 else if( bReleaseAfterSend != pdFALSE )
\r
239 vReleaseNetworkBufferAndDescriptor( pxBuffer );
\r
244 /*-----------------------------------------------------------*/
\r
246 static inline unsigned long ulReadMDIO( unsigned ulRegister )
\r
250 XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );
\r
253 /*-----------------------------------------------------------*/
\r
255 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )
\r
257 TickType_t xStartTime, xEndTime;
\r
258 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );
\r
259 BaseType_t xReturn;
\r
261 xStartTime = xTaskGetTickCount();
\r
265 xEndTime = xTaskGetTickCount();
\r
267 if( xEndTime - xStartTime > xMaxTime )
\r
272 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
274 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
280 vTaskDelay( xShortDelay );
\r
285 /*-----------------------------------------------------------*/
\r
287 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
\r
289 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );
\r
290 uint8_t *ucRAMBuffer = ucNetworkPackets;
\r
293 for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
\r
295 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
\r
296 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
\r
297 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
\r
300 /*-----------------------------------------------------------*/
\r
302 BaseType_t xGetPhyLinkStatus( void )
\r
304 BaseType_t xReturn;
\r
306 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0 )
\r
317 /*-----------------------------------------------------------*/
\r
319 static void prvEMACHandlerTask( void *pvParameters )
\r
321 TimeOut_t xPhyTime;
\r
322 TickType_t xPhyRemTime;
\r
323 UBaseType_t uxCurrentCount;
\r
324 BaseType_t xResult = 0;
\r
326 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
\r
327 UBaseType_t uxLastMinBufferCount = 0;
\r
328 UBaseType_t uxCurrentBufferCount = 0;
\r
330 /* Remove compiler warnings about unused parameters. */
\r
331 ( void ) pvParameters;
\r
333 /* A possibility to set some additional task properties like calling
\r
334 portTASK_USES_FLOATING_POINT() */
\r
335 iptraceEMAC_TASK_STARTING();
\r
337 vTaskSetTimeOutState( &xPhyTime );
\r
338 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
342 uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
\r
343 if( uxLastMinBufferCount != uxCurrentBufferCount )
\r
345 /* The logging produced below may be helpful
\r
346 while tuning +TCP: see how many buffers are in use. */
\r
347 uxLastMinBufferCount = uxCurrentBufferCount;
\r
348 FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
\r
349 uxGetNumberOfFreeNetworkBuffers(), uxCurrentBufferCount ) );
\r
352 #if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
\r
354 static UBaseType_t uxLastMinQueueSpace = 0;
\r
356 uxCurrentCount = uxGetMinimumIPQueueSpace();
\r
357 if( uxLastMinQueueSpace != uxCurrentCount )
\r
359 /* The logging produced below may be helpful
\r
360 while tuning +TCP: see how many buffers are in use. */
\r
361 uxLastMinQueueSpace = uxCurrentCount;
\r
362 FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
\r
365 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */
\r
367 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )
\r
369 /* No events to process now, wait for the next. */
\r
370 ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
\r
373 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )
\r
375 xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;
\r
376 xResult = emacps_check_rx( &xEMACpsif );
\r
379 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )
\r
381 xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;
\r
382 emacps_check_tx( &xEMACpsif );
\r
385 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )
\r
387 xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;
\r
388 emacps_check_errors( &xEMACpsif );
\r
393 /* A packet was received. No need to check for the PHY status now,
\r
394 but set a timer to check it later on. */
\r
395 vTaskSetTimeOutState( &xPhyTime );
\r
396 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
398 /* Indicate that the Link Status is high, so that
\r
399 xNetworkInterfaceOutput() can send packets. */
\r
400 ulPHYLinkStatus |= niBMSR_LINK_STATUS;
\r
402 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )
\r
404 xStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
406 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != ( xStatus & niBMSR_LINK_STATUS ) )
\r
408 ulPHYLinkStatus = xStatus;
\r
409 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 ) );
\r
412 vTaskSetTimeOutState( &xPhyTime );
\r
413 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0 )
\r
415 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
419 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
424 /*-----------------------------------------------------------*/
\r