2 * FreeRTOS+TCP V2.0.0
\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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
34 /* FreeRTOS includes. */
\r
35 #include "FreeRTOS.h"
\r
40 /* FreeRTOS+TCP includes. */
\r
41 #include "FreeRTOS_IP.h"
\r
42 #include "FreeRTOS_Sockets.h"
\r
43 #include "FreeRTOS_IP_Private.h"
\r
44 #include "NetworkBufferManagement.h"
\r
45 #include "NetworkInterface.h"
\r
47 /* Xilinx library files. */
\r
48 #include <xemacps.h>
\r
49 #include "Zynq/x_topology.h"
\r
50 #include "Zynq/x_emacpsif.h"
\r
51 #include "Zynq/x_emacpsif_hw.h"
\r
53 /* Provided memory configured as uncached. */
\r
54 #include "uncached_memory.h"
\r
56 #ifndef BMSR_LINK_STATUS
\r
57 #define BMSR_LINK_STATUS 0x0004UL
\r
60 #ifndef PHY_LS_HIGH_CHECK_TIME_MS
\r
61 /* Check if the LinkSStatus in the PHY is still high after 15 seconds of not
\r
62 receiving packets. */
\r
63 #define PHY_LS_HIGH_CHECK_TIME_MS 15000
\r
66 #ifndef PHY_LS_LOW_CHECK_TIME_MS
\r
67 /* Check if the LinkSStatus in the PHY is still low every second. */
\r
68 #define PHY_LS_LOW_CHECK_TIME_MS 1000
\r
71 /* The size of each buffer when BufferAllocation_1 is used:
\r
72 http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */
\r
73 #define niBUFFER_1_PACKET_SIZE 1536
\r
75 /* Naming and numbering of PHY registers. */
\r
76 #define PHY_REG_01_BMSR 0x01 /* Basic mode status register */
\r
78 #ifndef iptraceEMAC_TASK_STARTING
\r
79 #define iptraceEMAC_TASK_STARTING() do { } while( 0 )
\r
82 /* Default the size of the stack used by the EMAC deferred handler task to twice
\r
83 the size of the stack used by the idle task - but allow this to be overridden in
\r
84 FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */
\r
85 #ifndef configEMAC_TASK_STACK_SIZE
\r
86 #define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )
\r
89 /*-----------------------------------------------------------*/
\r
92 * Look for the link to be up every few milliseconds until either xMaxTime time
\r
93 * has passed or a link is found.
\r
95 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );
\r
98 * A deferred interrupt handler for all MAC/DMA interrupt sources.
\r
100 static void prvEMACHandlerTask( void *pvParameters );
\r
102 /*-----------------------------------------------------------*/
\r
104 /* EMAC data/descriptions. */
\r
105 static xemacpsif_s xEMACpsif;
\r
106 struct xtopology_t xXTopology =
\r
108 .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,
\r
109 .emac_type = xemac_type_emacps,
\r
110 .intc_baseaddr = 0x0,
\r
111 .intc_emac_intr = 0x0,
\r
112 .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,
\r
113 .scugic_emac_intr = 0x36,
\r
116 XEmacPs_Config mac_config =
\r
118 .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID, /**< Unique ID of device */
\r
119 .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */
\r
122 extern int phy_detected;
\r
124 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */
\r
125 static uint32_t ulPHYLinkStatus = 0;
\r
127 #if( ipconfigUSE_LLMNR == 1 )
\r
128 static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
\r
131 /* ucMACAddress as it appears in main.c */
\r
132 extern const uint8_t ucMACAddress[ 6 ];
\r
134 /* Holds the handle of the task used as a deferred interrupt processor. The
\r
135 handle is used so direct notifications can be sent to the task for all EMAC/DMA
\r
136 related interrupts. */
\r
137 TaskHandle_t xEMACTaskHandle = NULL;
\r
139 /*-----------------------------------------------------------*/
\r
141 BaseType_t xNetworkInterfaceInitialise( void )
\r
143 uint32_t ulLinkSpeed, ulDMAReg;
\r
144 BaseType_t xStatus, xLinkStatus;
\r
145 XEmacPs *pxEMAC_PS;
\r
146 const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );
\r
148 /* Guard against the init function being called more than once. */
\r
149 if( xEMACTaskHandle == NULL )
\r
151 pxEMAC_PS = &( xEMACpsif.emacps );
\r
152 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );
\r
154 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);
\r
155 if( xStatus != XST_SUCCESS )
\r
157 FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );
\r
160 /* Initialize the mac and set the MAC address. */
\r
161 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );
\r
163 #if( ipconfigUSE_LLMNR == 1 )
\r
165 /* Also add LLMNR multicast MAC address. */
\r
166 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );
\r
168 #endif /* ipconfigUSE_LLMNR == 1 */
\r
170 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );
\r
171 ulLinkSpeed = Phy_Setup( pxEMAC_PS );
\r
172 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);
\r
174 /* Setting the operating speed of the MAC needs a delay. */
\r
175 vTaskDelay( pdMS_TO_TICKS( 25UL ) );
\r
177 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);
\r
179 /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive
\r
180 packets from the receiver packet buffer memory when no AHB resource is available. */
\r
181 XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
\r
182 ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);
\r
184 setup_isr( &xEMACpsif );
\r
185 init_dma( &xEMACpsif );
\r
186 start_emacps( &xEMACpsif );
\r
188 prvGMACWaitLS( xWaitLinkDelay );
\r
190 /* The deferred interrupt handler task is created at the highest
\r
191 possible priority to ensure the interrupt handler can return directly
\r
192 to it. The task's handle is stored in xEMACTaskHandle so interrupts can
\r
193 notify the task when there is something to process. */
\r
194 xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, &xEMACTaskHandle );
\r
198 /* Initialisation was already performed, just wait for the link. */
\r
199 prvGMACWaitLS( xWaitRelinkDelay );
\r
202 /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the
\r
203 DHCP process and all other communication will fail. */
\r
204 xLinkStatus = xGetPhyLinkStatus();
\r
206 return ( xLinkStatus != pdFALSE );
\r
208 /*-----------------------------------------------------------*/
\r
210 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )
\r
212 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )
\r
214 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
215 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );
\r
217 else if( bReleaseAfterSend != pdFALSE )
\r
220 vReleaseNetworkBufferAndDescriptor( pxBuffer );
\r
225 /*-----------------------------------------------------------*/
\r
227 static inline unsigned long ulReadMDIO( unsigned ulRegister )
\r
231 XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );
\r
234 /*-----------------------------------------------------------*/
\r
236 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )
\r
238 TickType_t xStartTime, xEndTime;
\r
239 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );
\r
240 BaseType_t xReturn;
\r
242 xStartTime = xTaskGetTickCount();
\r
246 xEndTime = xTaskGetTickCount();
\r
248 if( xEndTime - xStartTime > xMaxTime )
\r
253 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
255 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )
\r
261 vTaskDelay( xShortDelay );
\r
266 /*-----------------------------------------------------------*/
\r
268 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
\r
270 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );
\r
271 uint8_t *ucRAMBuffer = ucNetworkPackets;
\r
274 for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
\r
276 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
\r
277 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
\r
278 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
\r
281 /*-----------------------------------------------------------*/
\r
283 BaseType_t xGetPhyLinkStatus( void )
\r
285 BaseType_t xReturn;
\r
287 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) == 0 )
\r
298 /*-----------------------------------------------------------*/
\r
300 static void prvEMACHandlerTask( void *pvParameters )
\r
302 TimeOut_t xPhyTime;
\r
303 TickType_t xPhyRemTime;
\r
304 UBaseType_t uxLastMinBufferCount = 0;
\r
305 UBaseType_t uxCurrentCount;
\r
306 BaseType_t xResult = 0;
\r
308 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
\r
310 /* Remove compiler warnings about unused parameters. */
\r
311 ( void ) pvParameters;
\r
313 /* A possibility to set some additional task properties like calling
\r
314 portTASK_USES_FLOATING_POINT() */
\r
315 iptraceEMAC_TASK_STARTING();
\r
317 vTaskSetTimeOutState( &xPhyTime );
\r
318 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
322 uxCurrentCount = uxGetMinimumFreeNetworkBuffers();
\r
323 if( uxLastMinBufferCount != uxCurrentCount )
\r
325 /* The logging produced below may be helpful
\r
326 while tuning +TCP: see how many buffers are in use. */
\r
327 uxLastMinBufferCount = uxCurrentCount;
\r
328 FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
\r
329 uxGetNumberOfFreeNetworkBuffers(), uxCurrentCount ) );
\r
332 #if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
\r
334 static UBaseType_t uxLastMinQueueSpace = 0;
\r
336 uxCurrentCount = uxGetMinimumIPQueueSpace();
\r
337 if( uxLastMinQueueSpace != uxCurrentCount )
\r
339 /* The logging produced below may be helpful
\r
340 while tuning +TCP: see how many buffers are in use. */
\r
341 uxLastMinQueueSpace = uxCurrentCount;
\r
342 FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
\r
345 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */
\r
347 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )
\r
349 /* No events to process now, wait for the next. */
\r
350 ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
\r
353 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )
\r
355 xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;
\r
356 xResult = emacps_check_rx( &xEMACpsif );
\r
359 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )
\r
361 xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;
\r
362 emacps_check_tx( &xEMACpsif );
\r
365 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )
\r
367 xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;
\r
368 emacps_check_errors( &xEMACpsif );
\r
373 /* A packet was received. No need to check for the PHY status now,
\r
374 but set a timer to check it later on. */
\r
375 vTaskSetTimeOutState( &xPhyTime );
\r
376 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
379 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )
\r
381 xStatus = ulReadMDIO( PHY_REG_01_BMSR );
\r
383 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != ( xStatus & BMSR_LINK_STATUS ) )
\r
385 ulPHYLinkStatus = xStatus;
\r
386 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 ) );
\r
389 vTaskSetTimeOutState( &xPhyTime );
\r
390 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )
\r
392 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
\r
396 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
\r
401 /*-----------------------------------------------------------*/
\r