2 * FreeRTOS V202002.00
\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://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 the priority of the task prvEMACHandlerTask(). */
\r
56 #define niEMAC_HANDLER_TASK_PRIORITY configMAX_PRIORITIES - 1
\r
59 #define niBMSR_LINK_STATUS 0x0004uL
\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 #if( ipconfigZERO_COPY_RX_DRIVER == 0 || ipconfigZERO_COPY_TX_DRIVER == 0 )
\r
91 #error Please define both 'ipconfigZERO_COPY_RX_DRIVER' and 'ipconfigZERO_COPY_TX_DRIVER' as 1
\r
94 #if( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 0 || ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
\r
95 #warning Please define both 'ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM' and 'ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM' as 1
\r
97 /*-----------------------------------------------------------*/
\r
100 * Look for the link to be up every few milliseconds until either xMaxTime time
\r
101 * has passed or a link is found.
\r
103 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );
\r
106 * A deferred interrupt handler for all MAC/DMA interrupt sources.
\r
108 static void prvEMACHandlerTask( void *pvParameters );
\r
110 #if ( ipconfigHAS_PRINTF != 0 )
\r
111 static void prvMonitorResources( void );
\r
114 /*-----------------------------------------------------------*/
\r
116 /* EMAC data/descriptions. */
\r
117 static xemacpsif_s xEMACpsif;
\r
118 struct xtopology_t xXTopology =
\r
120 .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,
\r
121 .emac_type = xemac_type_emacps,
\r
122 .intc_baseaddr = 0x0,
\r
123 .intc_emac_intr = 0x0,
\r
124 .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,
\r
125 .scugic_emac_intr = 0x36,
\r
128 XEmacPs_Config mac_config =
\r
130 .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID, /**< Unique ID of device */
\r
131 .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */
\r
134 extern int phy_detected;
\r
136 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */
\r
137 static uint32_t ulPHYLinkStatus = 0uL;
\r
139 #if( ipconfigUSE_LLMNR == 1 )
\r
140 static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
\r
143 /* ucMACAddress as it appears in main.c */
\r
144 extern const uint8_t ucMACAddress[ 6 ];
\r
146 /* Holds the handle of the task used as a deferred interrupt processor. The
\r
147 handle is used so direct notifications can be sent to the task for all EMAC/DMA
\r
148 related interrupts. */
\r
149 TaskHandle_t xEMACTaskHandle = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
153 BaseType_t xNetworkInterfaceInitialise( void )
\r
155 uint32_t ulLinkSpeed, ulDMAReg;
\r
156 BaseType_t xStatus, xLinkStatus;
\r
157 XEmacPs *pxEMAC_PS;
\r
158 const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );
\r
160 /* Guard against the init function being called more than once. */
\r
161 if( xEMACTaskHandle == NULL )
\r
163 pxEMAC_PS = &( xEMACpsif.emacps );
\r
164 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );
\r
166 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);
\r
167 if( xStatus != XST_SUCCESS )
\r
169 FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );
\r
172 /* Initialize the mac and set the MAC address. */
\r
173 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );
\r
175 #if( ipconfigUSE_LLMNR == 1 )
\r
177 /* Also add LLMNR multicast MAC address. */
\r
178 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );
\r
180 #endif /* ipconfigUSE_LLMNR == 1 */
\r
182 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );
\r
183 ulLinkSpeed = Phy_Setup( pxEMAC_PS );
\r
184 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);
\r
186 /* Setting the operating speed of the MAC needs a delay. */
\r
187 vTaskDelay( pdMS_TO_TICKS( 25UL ) );
\r
189 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);
\r
191 /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive
\r
192 packets from the receiver packet buffer memory when no AHB resource is available. */
\r
193 XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
\r
194 ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);
\r
196 setup_isr( &xEMACpsif );
\r
197 init_dma( &xEMACpsif );
\r
198 start_emacps( &xEMACpsif );
\r
200 prvGMACWaitLS( xWaitLinkDelay );
\r
202 /* The deferred interrupt handler task is created at the highest
\r
203 possible priority to ensure the interrupt handler can return directly
\r
204 to it. The task's handle is stored in xEMACTaskHandle so interrupts can
\r
205 notify the task when there is something to process. */
\r
206 xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, niEMAC_HANDLER_TASK_PRIORITY, &xEMACTaskHandle );
\r
210 /* Initialisation was already performed, just wait for the link. */
\r
211 prvGMACWaitLS( xWaitRelinkDelay );
\r
214 /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the
\r
215 DHCP process and all other communication will fail. */
\r
216 xLinkStatus = xGetPhyLinkStatus();
\r
218 return ( xLinkStatus != pdFALSE );
\r
220 /*-----------------------------------------------------------*/
\r
222 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )
\r
224 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )
\r
226 ProtocolPacket_t *pxPacket;
\r
228 /* If the peripheral must calculate the checksum, it wants
\r
229 the protocol checksum to have a value of zero. */
\r
230 pxPacket = ( ProtocolPacket_t * ) ( pxBuffer->pucEthernetBuffer );
\r
231 if( ( pxPacket->xICMPPacket.xIPHeader.ucProtocol != ipPROTOCOL_UDP ) &&
\r
232 ( pxPacket->xICMPPacket.xIPHeader.ucProtocol != ipPROTOCOL_TCP ) )
\r
234 /* The EMAC will calculate the checksum of the IP-header.
\r
235 It can only calculate protocol checksums of UDP and TCP,
\r
236 so for ICMP and other protocols it must be done manually. */
\r
237 usGenerateProtocolChecksum( (uint8_t*)&( pxPacket->xUDPPacket ), pxBuffer->xDataLength, pdTRUE );
\r
240 #endif /* ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM */
\r
241 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )
\r
243 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
244 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );
\r
246 else if( bReleaseAfterSend != pdFALSE )
\r
249 vReleaseNetworkBufferAndDescriptor( pxBuffer );
\r
254 /*-----------------------------------------------------------*/
\r
256 static inline unsigned long ulReadMDIO( unsigned ulRegister )
\r
260 XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );
\r
263 /*-----------------------------------------------------------*/
\r
265 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )
\r
267 TickType_t xStartTime, xEndTime;
\r
268 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );
\r
269 BaseType_t xReturn;
\r
271 xStartTime = xTaskGetTickCount();
\r
275 xEndTime = xTaskGetTickCount();
\r
277 if( xEndTime - xStartTime > xMaxTime )
\r
282 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
284 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )
\r
290 vTaskDelay( xShortDelay );
\r
295 /*-----------------------------------------------------------*/
\r
297 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
\r
299 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );
\r
300 uint8_t *ucRAMBuffer = ucNetworkPackets;
\r
303 for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
\r
305 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
\r
306 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
\r
307 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
\r
310 /*-----------------------------------------------------------*/
\r
312 BaseType_t xGetPhyLinkStatus( void )
\r
314 BaseType_t xReturn;
\r
316 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )
\r
327 /*-----------------------------------------------------------*/
\r
329 #if ( ipconfigHAS_PRINTF != 0 )
\r
330 static void prvMonitorResources()
\r
332 static UBaseType_t uxLastMinBufferCount = 0u;
\r
333 static size_t uxMinLastSize = 0uL;
\r
334 UBaseType_t uxCurrentBufferCount;
\r
337 uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
\r
339 if( uxLastMinBufferCount != uxCurrentBufferCount )
\r
341 /* The logging produced below may be helpful
\r
342 * while tuning +TCP: see how many buffers are in use. */
\r
343 uxLastMinBufferCount = uxCurrentBufferCount;
\r
344 FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
\r
345 uxGetNumberOfFreeNetworkBuffers(),
\r
346 uxCurrentBufferCount ) );
\r
349 uxMinSize = xPortGetMinimumEverFreeHeapSize();
\r
351 if( uxMinLastSize != uxMinSize )
\r
353 uxMinLastSize = uxMinSize;
\r
354 FreeRTOS_printf( ( "Heap: current %lu lowest %lu\n", xPortGetFreeHeapSize(), uxMinSize ) );
\r
357 #if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
\r
359 static UBaseType_t uxLastMinQueueSpace = 0;
\r
360 UBaseType_t uxCurrentCount = 0u;
\r
362 uxCurrentCount = uxGetMinimumIPQueueSpace();
\r
364 if( uxLastMinQueueSpace != uxCurrentCount )
\r
366 /* The logging produced below may be helpful
\r
367 * while tuning +TCP: see how many buffers are in use. */
\r
368 uxLastMinQueueSpace = uxCurrentCount;
\r
369 FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
\r
372 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */
\r
374 #endif /* ( ipconfigHAS_PRINTF != 0 ) */
\r
375 /*-----------------------------------------------------------*/
\r
377 static void prvEMACHandlerTask( void *pvParameters )
\r
379 TimeOut_t xPhyTime;
\r
380 TickType_t xPhyRemTime;
\r
381 BaseType_t xResult = 0;
\r
383 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
\r
385 /* Remove compiler warnings about unused parameters. */
\r
386 ( void ) pvParameters;
\r
388 /* A possibility to set some additional task properties like calling
\r
389 portTASK_USES_FLOATING_POINT() */
\r
390 iptraceEMAC_TASK_STARTING();
\r
392 vTaskSetTimeOutState( &xPhyTime );
\r
393 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
397 #if ( ipconfigHAS_PRINTF != 0 )
\r
399 prvMonitorResources();
\r
401 #endif /* ipconfigHAS_PRINTF != 0 ) */
\r
403 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )
\r
405 /* No events to process now, wait for the next. */
\r
406 ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
\r
409 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )
\r
411 xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;
\r
412 xResult = emacps_check_rx( &xEMACpsif );
\r
415 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )
\r
417 xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;
\r
418 emacps_check_tx( &xEMACpsif );
\r
421 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )
\r
423 xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;
\r
424 emacps_check_errors( &xEMACpsif );
\r
429 /* A packet was received. No need to check for the PHY status now,
\r
430 but set a timer to check it later on. */
\r
431 vTaskSetTimeOutState( &xPhyTime );
\r
432 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
434 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )
\r
436 /* Indicate that the Link Status is high, so that
\r
437 xNetworkInterfaceOutput() can send packets. */
\r
438 ulPHYLinkStatus |= niBMSR_LINK_STATUS;
\r
439 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS assume 1\n" ) );
\r
442 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )
\r
444 xStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
446 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != ( xStatus & niBMSR_LINK_STATUS ) )
\r
448 ulPHYLinkStatus = xStatus;
\r
449 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL ) );
\r
452 vTaskSetTimeOutState( &xPhyTime );
\r
453 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )
\r
455 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
459 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
464 /*-----------------------------------------------------------*/
\r