]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/Zynq/NetworkInterface.c
1c11976cbcec7779ed97ef48964cc7a25bcdd9d9
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / Zynq / NetworkInterface.c
1 /*\r
2 FreeRTOS+TCP V2.0.11\r
3 Copyright (C) 2017 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 "NetworkBufferManagement.h"\r
42 #include "NetworkInterface.h"\r
43 \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
49 \r
50 /* Provided memory configured as uncached. */\r
51 #include "uncached_memory.h"\r
52 \r
53 #ifndef BMSR_LINK_STATUS\r
54         #define BMSR_LINK_STATUS            0x0004UL\r
55 #endif\r
56 \r
57 #ifndef PHY_LS_HIGH_CHECK_TIME_MS\r
58         /* Check if the LinkSStatus in the PHY is still high after 15 seconds of not\r
59         receiving packets. */\r
60         #define PHY_LS_HIGH_CHECK_TIME_MS       15000\r
61 #endif\r
62 \r
63 #ifndef PHY_LS_LOW_CHECK_TIME_MS\r
64         /* Check if the LinkSStatus in the PHY is still low every second. */\r
65         #define PHY_LS_LOW_CHECK_TIME_MS        1000\r
66 #endif\r
67 \r
68 /* The size of each buffer when BufferAllocation_1 is used:\r
69 http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */\r
70 #define niBUFFER_1_PACKET_SIZE          1536\r
71 \r
72 /* Naming and numbering of PHY registers. */\r
73 #define PHY_REG_01_BMSR                 0x01    /* Basic mode status register */\r
74 \r
75 #ifndef iptraceEMAC_TASK_STARTING\r
76         #define iptraceEMAC_TASK_STARTING()     do { } while( 0 )\r
77 #endif\r
78 \r
79 /* Default the size of the stack used by the EMAC deferred handler task to twice\r
80 the size of the stack used by the idle task - but allow this to be overridden in\r
81 FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */\r
82 #ifndef configEMAC_TASK_STACK_SIZE\r
83         #define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )\r
84 #endif\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * Look for the link to be up every few milliseconds until either xMaxTime time\r
90  * has passed or a link is found.\r
91  */\r
92 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime );\r
93 \r
94 /*\r
95  * A deferred interrupt handler for all MAC/DMA interrupt sources.\r
96  */\r
97 static void prvEMACHandlerTask( void *pvParameters );\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* EMAC data/descriptions. */\r
102 static xemacpsif_s xEMACpsif;\r
103 struct xtopology_t xXTopology =\r
104 {\r
105         .emac_baseaddr = XPAR_PS7_ETHERNET_0_BASEADDR,\r
106         .emac_type = xemac_type_emacps,\r
107         .intc_baseaddr = 0x0,\r
108         .intc_emac_intr = 0x0,\r
109         .scugic_baseaddr = XPAR_PS7_SCUGIC_0_BASEADDR,\r
110         .scugic_emac_intr = 0x36,\r
111 };\r
112 \r
113 XEmacPs_Config mac_config =\r
114 {\r
115         .DeviceId = XPAR_PS7_ETHERNET_0_DEVICE_ID,      /**< Unique ID  of device */\r
116         .BaseAddress = XPAR_PS7_ETHERNET_0_BASEADDR /**< Physical base address of IPIF registers */\r
117 };\r
118 \r
119 extern int phy_detected;\r
120 \r
121 /* A copy of PHY register 1: 'PHY_REG_01_BMSR' */\r
122 static uint32_t ulPHYLinkStatus = 0;\r
123 \r
124 #if( ipconfigUSE_LLMNR == 1 )\r
125         static const uint8_t xLLMNR_MACAddress[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };\r
126 #endif\r
127 \r
128 /* ucMACAddress as it appears in main.c */\r
129 extern const uint8_t ucMACAddress[ 6 ];\r
130 \r
131 /* Holds the handle of the task used as a deferred interrupt processor.  The\r
132 handle is used so direct notifications can be sent to the task for all EMAC/DMA\r
133 related interrupts. */\r
134 TaskHandle_t xEMACTaskHandle = NULL;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 BaseType_t xNetworkInterfaceInitialise( void )\r
139 {\r
140 uint32_t ulLinkSpeed, ulDMAReg;\r
141 BaseType_t xStatus, xLinkStatus;\r
142 XEmacPs *pxEMAC_PS;\r
143 const TickType_t xWaitLinkDelay = pdMS_TO_TICKS( 7000UL ), xWaitRelinkDelay = pdMS_TO_TICKS( 1000UL );\r
144 \r
145         /* Guard against the init function being called more than once. */\r
146         if( xEMACTaskHandle == NULL )\r
147         {\r
148                 pxEMAC_PS = &( xEMACpsif.emacps );\r
149                 memset( &xEMACpsif, '\0', sizeof( xEMACpsif ) );\r
150 \r
151                 xStatus = XEmacPs_CfgInitialize( pxEMAC_PS, &mac_config, mac_config.BaseAddress);\r
152                 if( xStatus != XST_SUCCESS )\r
153                 {\r
154                         FreeRTOS_printf( ( "xEMACInit: EmacPs Configuration Failed....\n" ) );\r
155                 }\r
156 \r
157                 /* Initialize the mac and set the MAC address. */\r
158                 XEmacPs_SetMacAddress( pxEMAC_PS, ( void * ) ucMACAddress, 1 );\r
159 \r
160                 #if( ipconfigUSE_LLMNR == 1 )\r
161                 {\r
162                         /* Also add LLMNR multicast MAC address. */\r
163                         XEmacPs_SetMacAddress( pxEMAC_PS, ( void * )xLLMNR_MACAddress, 2 );\r
164                 }\r
165                 #endif  /* ipconfigUSE_LLMNR == 1 */\r
166 \r
167                 XEmacPs_SetMdioDivisor( pxEMAC_PS, MDC_DIV_224 );\r
168                 ulLinkSpeed = Phy_Setup( pxEMAC_PS );\r
169                 XEmacPs_SetOperatingSpeed( pxEMAC_PS, ulLinkSpeed);\r
170 \r
171                 /* Setting the operating speed of the MAC needs a delay. */\r
172                 vTaskDelay( pdMS_TO_TICKS( 25UL ) );\r
173 \r
174                 ulDMAReg = XEmacPs_ReadReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET);\r
175 \r
176                 /* DISC_WHEN_NO_AHB: when set, the GEM DMA will automatically discard receive\r
177                 packets from the receiver packet buffer memory when no AHB resource is available. */\r
178                 XEmacPs_WriteReg( pxEMAC_PS->Config.BaseAddress, XEMACPS_DMACR_OFFSET,\r
179                         ulDMAReg | XEMACPS_DMACR_DISC_WHEN_NO_AHB_MASK);\r
180 \r
181                 setup_isr( &xEMACpsif );\r
182                 init_dma( &xEMACpsif );\r
183                 start_emacps( &xEMACpsif );\r
184 \r
185                 prvGMACWaitLS( xWaitLinkDelay );\r
186 \r
187                 /* The deferred interrupt handler task is created at the highest\r
188                 possible priority to ensure the interrupt handler can return directly\r
189                 to it.  The task's handle is stored in xEMACTaskHandle so interrupts can\r
190                 notify the task when there is something to process. */\r
191                 xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, &xEMACTaskHandle );\r
192         }\r
193         else\r
194         {\r
195                 /* Initialisation was already performed, just wait for the link. */\r
196                 prvGMACWaitLS( xWaitRelinkDelay );\r
197         }\r
198 \r
199         /* Only return pdTRUE when the Link Status of the PHY is high, otherwise the\r
200         DHCP process and all other communication will fail. */\r
201         xLinkStatus = xGetPhyLinkStatus();\r
202 \r
203         return ( xLinkStatus != pdFALSE );\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxBuffer, BaseType_t bReleaseAfterSend )\r
208 {\r
209         if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )\r
210         {\r
211                 iptraceNETWORK_INTERFACE_TRANSMIT();\r
212                 emacps_send_message( &xEMACpsif, pxBuffer, bReleaseAfterSend );\r
213         }\r
214         else if( bReleaseAfterSend != pdFALSE )\r
215         {\r
216                 /* No link. */\r
217                 vReleaseNetworkBufferAndDescriptor( pxBuffer );\r
218         }\r
219 \r
220         return pdTRUE;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static inline unsigned long ulReadMDIO( unsigned ulRegister )\r
225 {\r
226 uint16_t usValue;\r
227 \r
228         XEmacPs_PhyRead( &( xEMACpsif.emacps ), phy_detected, ulRegister, &usValue );\r
229         return usValue;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static BaseType_t prvGMACWaitLS( TickType_t xMaxTime )\r
234 {\r
235 TickType_t xStartTime, xEndTime;\r
236 const TickType_t xShortDelay = pdMS_TO_TICKS( 20UL );\r
237 BaseType_t xReturn;\r
238 \r
239         xStartTime = xTaskGetTickCount();\r
240 \r
241         for( ;; )\r
242         {\r
243                 xEndTime = xTaskGetTickCount();\r
244 \r
245                 if( xEndTime - xStartTime > xMaxTime )\r
246                 {\r
247                         xReturn = pdFALSE;\r
248                         break;\r
249                 }\r
250                 ulPHYLinkStatus = ulReadMDIO( PHY_REG_01_BMSR );\r
251 \r
252                 if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )\r
253                 {\r
254                         xReturn = pdTRUE;\r
255                         break;\r
256                 }\r
257 \r
258                 vTaskDelay( xShortDelay );\r
259         }\r
260 \r
261         return xReturn;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )\r
266 {\r
267 static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ( ( aligned( 32 ) ) );\r
268 uint8_t *ucRAMBuffer = ucNetworkPackets;\r
269 uint32_t ul;\r
270 \r
271         for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )\r
272         {\r
273                 pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;\r
274                 *( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );\r
275                 ucRAMBuffer += niBUFFER_1_PACKET_SIZE;\r
276         }\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 BaseType_t xGetPhyLinkStatus( void )\r
281 {\r
282 BaseType_t xReturn;\r
283 \r
284         if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) == 0 )\r
285         {\r
286                 xReturn = pdFALSE;\r
287         }\r
288         else\r
289         {\r
290                 xReturn = pdTRUE;\r
291         }\r
292 \r
293         return xReturn;\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 static void prvEMACHandlerTask( void *pvParameters )\r
298 {\r
299 TimeOut_t xPhyTime;\r
300 TickType_t xPhyRemTime;\r
301 UBaseType_t uxLastMinBufferCount = 0;\r
302 UBaseType_t uxCurrentCount;\r
303 BaseType_t xResult = 0;\r
304 uint32_t xStatus;\r
305 const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );\r
306 \r
307         /* Remove compiler warnings about unused parameters. */\r
308         ( void ) pvParameters;\r
309 \r
310         /* A possibility to set some additional task properties like calling\r
311         portTASK_USES_FLOATING_POINT() */\r
312         iptraceEMAC_TASK_STARTING();\r
313 \r
314         vTaskSetTimeOutState( &xPhyTime );\r
315         xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );\r
316 \r
317         for( ;; )\r
318         {\r
319                 uxCurrentCount = uxGetMinimumFreeNetworkBuffers();\r
320                 if( uxLastMinBufferCount != uxCurrentCount )\r
321                 {\r
322                         /* The logging produced below may be helpful\r
323                         while tuning +TCP: see how many buffers are in use. */\r
324                         uxLastMinBufferCount = uxCurrentCount;\r
325                         FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",\r
326                                 uxGetNumberOfFreeNetworkBuffers(), uxCurrentCount ) );\r
327                 }\r
328 \r
329                 #if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )\r
330                 {\r
331                 static UBaseType_t uxLastMinQueueSpace = 0;\r
332 \r
333                         uxCurrentCount = uxGetMinimumIPQueueSpace();\r
334                         if( uxLastMinQueueSpace != uxCurrentCount )\r
335                         {\r
336                                 /* The logging produced below may be helpful\r
337                                 while tuning +TCP: see how many buffers are in use. */\r
338                                 uxLastMinQueueSpace = uxCurrentCount;\r
339                                 FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );\r
340                         }\r
341                 }\r
342                 #endif /* ipconfigCHECK_IP_QUEUE_SPACE */\r
343 \r
344                 if( ( xEMACpsif.isr_events & EMAC_IF_ALL_EVENT ) == 0 )\r
345                 {\r
346                         /* No events to process now, wait for the next. */\r
347                         ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );\r
348                 }\r
349 \r
350                 if( ( xEMACpsif.isr_events & EMAC_IF_RX_EVENT ) != 0 )\r
351                 {\r
352                         xEMACpsif.isr_events &= ~EMAC_IF_RX_EVENT;\r
353                         xResult = emacps_check_rx( &xEMACpsif );\r
354                 }\r
355 \r
356                 if( ( xEMACpsif.isr_events & EMAC_IF_TX_EVENT ) != 0 )\r
357                 {\r
358                         xEMACpsif.isr_events &= ~EMAC_IF_TX_EVENT;\r
359                         emacps_check_tx( &xEMACpsif );\r
360                 }\r
361 \r
362                 if( ( xEMACpsif.isr_events & EMAC_IF_ERR_EVENT ) != 0 )\r
363                 {\r
364                         xEMACpsif.isr_events &= ~EMAC_IF_ERR_EVENT;\r
365                         emacps_check_errors( &xEMACpsif );\r
366                 }\r
367 \r
368                 if( xResult > 0 )\r
369                 {\r
370                         /* A packet was received. No need to check for the PHY status now,\r
371                         but set a timer to check it later on. */\r
372                         vTaskSetTimeOutState( &xPhyTime );\r
373                         xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );\r
374                         xResult = 0;\r
375                 }\r
376                 else if( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE )\r
377                 {\r
378                         xStatus = ulReadMDIO( PHY_REG_01_BMSR );\r
379 \r
380                         if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != ( xStatus & BMSR_LINK_STATUS ) )\r
381                         {\r
382                                 ulPHYLinkStatus = xStatus;\r
383                                 FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 ) );\r
384                         }\r
385 \r
386                         vTaskSetTimeOutState( &xPhyTime );\r
387                         if( ( ulPHYLinkStatus & BMSR_LINK_STATUS ) != 0 )\r
388                         {\r
389                                 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );\r
390                         }\r
391                         else\r
392                         {\r
393                                 xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );\r
394                         }\r
395                 }\r
396         }\r
397 }\r
398 /*-----------------------------------------------------------*/\r