]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/Zynq/NetworkInterface.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / Zynq / NetworkInterface.c
1 /*\r
2  * FreeRTOS V202002.00\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /* Standard includes. */\r
27 #include <stdint.h>\r
28 #include <stdio.h>\r
29 #include <stdlib.h>\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 #include "queue.h"\r
35 #include "semphr.h"\r
36 \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
44 \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
50 \r
51 /* Provided memory configured as uncached. */\r
52 #include "uncached_memory.h"\r
53 \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
57 #endif\r
58 \r
59 #define niBMSR_LINK_STATUS         0x0004uL\r
60 \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
65 #endif\r
66 \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
70 #endif\r
71 \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
75 \r
76 /* Naming and numbering of PHY registers. */\r
77 #define PHY_REG_01_BMSR                 0x01    /* Basic mode status register */\r
78 \r
79 #ifndef iptraceEMAC_TASK_STARTING\r
80         #define iptraceEMAC_TASK_STARTING()     do { } while( 0 )\r
81 #endif\r
82 \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
88 #endif\r
89 \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
92 #endif\r
93 \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
96 #endif\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /*\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
102  */\r
103 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );\r
104 \r
105 /*\r
106  * A deferred interrupt handler for all MAC/DMA interrupt sources.\r
107  */\r
108 static void prvEMACHandlerTask( void *pvParameters );\r
109 \r
110 #if ( ipconfigHAS_PRINTF != 0 )\r
111         static void prvMonitorResources( void );\r
112 #endif\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /* EMAC data/descriptions. */\r
117 static xemacpsif_s xEMACpsif;\r
118 struct xtopology_t xXTopology =\r
119 {\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
126 };\r
127 \r
128 XEmacPs_Config mac_config =\r
129 {\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
132 };\r
133 \r
134 extern int phy_detected;\r
135 \r
136 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */\r
137 static uint32_t ulPHYLinkStatus = 0uL;\r
138 \r
139 #if( ipconfigUSE_LLMNR == 1 )\r
140         static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };\r
141 #endif\r
142 \r
143 /* ucMACAddress as it appears in main.c */\r
144 extern const uint8_t ucMACAddress[ 6 ];\r
145 \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
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 BaseType_t xNetworkInterfaceInitialise( void )\r
154 {\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
159 \r
160         /* Guard against the init function being called more than once. */\r
161         if( xEMACTaskHandle == NULL )\r
162         {\r
163                 pxEMAC_PS = &( xEMACpsif.emacps );\r
164                 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );\r
165 \r
166                 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);\r
167                 if( xStatus != XST_SUCCESS )\r
168                 {\r
169                         FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );\r
170                 }\r
171 \r
172                 /* Initialize the mac and set the MAC address. */\r
173                 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );\r
174 \r
175                 #if( ipconfigUSE_LLMNR == 1 )\r
176                 {\r
177                         /* Also add LLMNR multicast MAC address. */\r
178                         XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );\r
179                 }\r
180                 #endif  /* ipconfigUSE_LLMNR == 1 */\r
181 \r
182                 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );\r
183                 ulLinkSpeed = Phy_Setup( pxEMAC_PS );\r
184                 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);\r
185 \r
186                 /* Setting the operating speed of the MAC needs a delay. */\r
187                 vTaskDelay( pdMS_TO_TICKS( 25UL ) );\r
188 \r
189                 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);\r
190 \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
195 \r
196                 setup_isr( &xEMACpsif );\r
197                 init_dma( &xEMACpsif );\r
198                 start_emacps( &xEMACpsif );\r
199 \r
200                 prvGMACWaitLS( xWaitLinkDelay );\r
201 \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
207         }\r
208         else\r
209         {\r
210                 /* Initialisation was already performed, just wait for the link. */\r
211                 prvGMACWaitLS( xWaitRelinkDelay );\r
212         }\r
213 \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
217 \r
218         return ( xLinkStatus != pdFALSE );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )\r
223 {\r
224         #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM != 0 )\r
225         {\r
226         ProtocolPacket_t *pxPacket;\r
227 \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
233                 {\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
238                 }\r
239         }\r
240         #endif /* ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM */\r
241         if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )\r
242         {\r
243                 iptraceNETWORK_INTERFACE_TRANSMIT();\r
244                 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );\r
245         }\r
246         else if( bReleaseAfterSend != pdFALSE )\r
247         {\r
248                 /* No link. */\r
249                 vReleaseNetworkBufferAndDescriptor( pxBuffer );\r
250         }\r
251 \r
252         return pdTRUE;\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static inline unsigned long ulReadMDIO( unsigned ulRegister )\r
257 {\r
258 uint16_t usValue;\r
259 \r
260         XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );\r
261         return usValue;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )\r
266 {\r
267 TickType_t xStartTime, xEndTime;\r
268 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );\r
269 BaseType_t xReturn;\r
270 \r
271         xStartTime = xTaskGetTickCount();\r
272 \r
273         for( ;; )\r
274         {\r
275                 xEndTime = xTaskGetTickCount();\r
276 \r
277                 if( xEndTime - xStartTime > xMaxTime )\r
278                 {\r
279                         xReturn = pdFALSE;\r
280                         break;\r
281                 }\r
282                 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );\r
283 \r
284                 if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )\r
285                 {\r
286                         xReturn = pdTRUE;\r
287                         break;\r
288                 }\r
289 \r
290                 vTaskDelay( xShortDelay );\r
291         }\r
292 \r
293         return xReturn;\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )\r
298 {\r
299 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );\r
300 uint8_t *ucRAMBuffer = ucNetworkPackets;\r
301 uint32_t ul;\r
302 \r
303         for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )\r
304         {\r
305                 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;\r
306                 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );\r
307                 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 BaseType_t xGetPhyLinkStatus( void )\r
313 {\r
314 BaseType_t xReturn;\r
315 \r
316         if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )\r
317         {\r
318                 xReturn = pdFALSE;\r
319         }\r
320         else\r
321         {\r
322                 xReturn = pdTRUE;\r
323         }\r
324 \r
325         return xReturn;\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 #if ( ipconfigHAS_PRINTF != 0 )\r
330         static void prvMonitorResources()\r
331         {\r
332         static UBaseType_t uxLastMinBufferCount = 0u;\r
333         static size_t uxMinLastSize = 0uL;\r
334         UBaseType_t uxCurrentBufferCount;\r
335         size_t uxMinSize;\r
336 \r
337                 uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();\r
338 \r
339                 if( uxLastMinBufferCount != uxCurrentBufferCount )\r
340                 {\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
347                 }\r
348 \r
349                 uxMinSize = xPortGetMinimumEverFreeHeapSize();\r
350 \r
351                 if( uxMinLastSize != uxMinSize )\r
352                 {\r
353                         uxMinLastSize = uxMinSize;\r
354                         FreeRTOS_printf( ( "Heap: current %lu lowest %lu\n", xPortGetFreeHeapSize(), uxMinSize ) );\r
355                 }\r
356 \r
357                 #if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )\r
358                         {\r
359                                 static UBaseType_t uxLastMinQueueSpace = 0;\r
360                                 UBaseType_t uxCurrentCount = 0u;\r
361 \r
362                                 uxCurrentCount = uxGetMinimumIPQueueSpace();\r
363 \r
364                                 if( uxLastMinQueueSpace != uxCurrentCount )\r
365                                 {\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
370                                 }\r
371                         }\r
372                 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */\r
373         }\r
374 #endif /* ( ipconfigHAS_PRINTF != 0 ) */\r
375 /*-----------------------------------------------------------*/\r
376 \r
377 static void prvEMACHandlerTask( void *pvParameters )\r
378 {\r
379 TimeOut_t xPhyTime;\r
380 TickType_t xPhyRemTime;\r
381 BaseType_t xResult = 0;\r
382 uint32_t xStatus;\r
383 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );\r
384 \r
385         /* Remove compiler warnings about unused parameters. */\r
386         ( void ) pvParameters;\r
387 \r
388         /* A possibility to set some additional task properties like calling\r
389         portTASK_USES_FLOATING_POINT() */\r
390         iptraceEMAC_TASK_STARTING();\r
391 \r
392         vTaskSetTimeOutState( &xPhyTime );\r
393         xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );\r
394 \r
395         for( ;; )\r
396         {\r
397                 #if ( ipconfigHAS_PRINTF != 0 )\r
398                         {\r
399                                 prvMonitorResources();\r
400                         }\r
401                 #endif /* ipconfigHAS_PRINTF != 0 ) */\r
402 \r
403                 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )\r
404                 {\r
405                         /* No events to process now, wait for the next. */\r
406                         ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );\r
407                 }\r
408 \r
409                 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )\r
410                 {\r
411                         xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;\r
412                         xResult = emacps_check_rx( &xEMACpsif );\r
413                 }\r
414 \r
415                 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )\r
416                 {\r
417                         xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;\r
418                         emacps_check_tx( &xEMACpsif );\r
419                 }\r
420 \r
421                 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )\r
422                 {\r
423                         xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;\r
424                         emacps_check_errors( &xEMACpsif );\r
425                 }\r
426 \r
427                 if( xResult > 0 )\r
428                 {\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
433                         xResult = 0;\r
434                         if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) == 0uL )\r
435                         {\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
440                         }\r
441                 }\r
442                 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )\r
443                 {\r
444                         xStatus = ulReadMDIO( PHY_REG_01_BMSR );\r
445 \r
446                         if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != ( xStatus & niBMSR_LINK_STATUS ) )\r
447                         {\r
448                                 ulPHYLinkStatus = xStatus;\r
449                                 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL ) );\r
450                         }\r
451 \r
452                         vTaskSetTimeOutState( &xPhyTime );\r
453                         if( ( ulPHYLinkStatus & niBMSR_LINK_STATUS ) != 0uL )\r
454                         {\r
455                                 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );\r
456                         }\r
457                         else\r
458                         {\r
459                                 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );\r
460                         }\r
461                 }\r
462         }\r
463 }\r
464 /*-----------------------------------------------------------*/\r