]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F107_GCC_Rowley/webserver/emac.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_STM32F107_GCC_Rowley / webserver / emac.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* FreeRTOS includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "semphr.h"\r
68 #include "task.h"\r
69 #include "emac.h"\r
70 \r
71 /* Library includes. */\r
72 #include "stm32fxxx_eth.h"\r
73 #include "stm32f10x_gpio.h"\r
74 #include "stm32f10x_rcc.h"\r
75 #include "stm32f10x_nvic.h"\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* Hardware specifics. */\r
80 #define uipRCC_MAC_CLOCK                        ( 1UL << 14UL )\r
81 #define uipRCC_MAC_TX_CLOCK                     ( 1UL << 15UL )\r
82 #define uipRCC_MAC_RX_CLOCK                     ( 1UL << 16UL )\r
83 #define uipPHY_ADDRESS                          ( 1 )\r
84 #define uipENET_IRQ_NUM                         ( 61 )\r
85 #define uipMODE_MII                                     ( 1UL << 23UL )\r
86 #define uipREMAP_MAC_IO                         ( 1UL << 21UL )\r
87 \r
88 /* The number of descriptors to chain together for use by the Rx DMA. */\r
89 #define uipNUM_RX_DESCRIPTORS           4\r
90 \r
91 /* The total number of buffers to be available.  At most (?) there should be\r
92 one available for each Rx descriptor, one for current use, and one that is\r
93 in the process of being transmitted. */\r
94 #define uipNUM_BUFFERS                          ( uipNUM_RX_DESCRIPTORS + 2 )\r
95 \r
96 /* Each buffer is sized to fit an entire Ethernet packet.  This is for\r
97 simplicity and speed, but could waste RAM. */\r
98 #define uipMAX_PACKET_SIZE                      1520\r
99 \r
100 /* The field in the descriptor that is unused by this configuration is used to\r
101 hold the send count.  This is just #defined to a meaningful name. */\r
102 #define SendCount Buffer2NextDescAddr\r
103 \r
104 /* If no buffers are available, then wait this long before looking again.... */\r
105 #define uipBUFFER_WAIT_DELAY    ( 3 / portTICK_RATE_MS )\r
106 \r
107 /* ...and don't look more than this many times. */\r
108 #define uipBUFFER_WAIT_ATTEMPTS ( 30 )\r
109 \r
110 /* Let the DMA know that a new descriptor has been made available to it. */\r
111 #define prvRxDescriptorAvailable()              ETH_DMA->DMARPDR = 0\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Configure the IO for Ethernet use.\r
117  */\r
118 static void prvSetupEthGPIO( void );\r
119 \r
120 /*\r
121  * Return a pointer to an unused buffer, marking the returned buffer as now\r
122  * in use.\r
123  */\r
124 static unsigned char *prvGetNextBuffer( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* Allocate the Rx descriptors used by the DMA. */\r
129 static ETH_DMADESCTypeDef  xRxDescriptors[ uipNUM_RX_DESCRIPTORS ] __attribute__((aligned(4)));\r
130 \r
131 /* Allocate the descriptor used for transmitting.  It might be that better\r
132 performance could be achieved by having more than one Tx descriptor, but\r
133 in this simple case only one is used. */\r
134 static volatile ETH_DMADESCTypeDef  xTxDescriptor __attribute__((aligned(4)));\r
135 \r
136 /* Buffers used for receiving and transmitting data. */\r
137 static unsigned char ucMACBuffers[ uipNUM_BUFFERS ][ uipMAX_PACKET_SIZE ] __attribute__((aligned(4)));\r
138 \r
139 /* Each ucBufferInUse index corresponds to a position in the same index in the\r
140 ucMACBuffers array.  If the index contains a 1 then the buffer within\r
141 ucMACBuffers is in use, if it contains a 0 then the buffer is free. */\r
142 static unsigned char ucBufferInUse[ uipNUM_BUFFERS ] = { 0 };\r
143 \r
144 /* Index to the Rx descriptor to inspect next when looking for a received\r
145 packet. */\r
146 static unsigned long ulNextDescriptor;\r
147 \r
148 /* The uip_buffer is not a fixed array, but instead gets pointed to the buffers\r
149 allocated within this file. */\r
150 extern unsigned char * uip_buf;\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 portBASE_TYPE xEthInitialise( void )\r
155 {\r
156 static ETH_InitTypeDef xEthInit; /* Static so as not to take up too much stack space. */\r
157 NVIC_InitTypeDef xNVICInit;\r
158 const unsigned char ucMACAddress[] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
159 portBASE_TYPE xReturn;\r
160 unsigned long ul;\r
161 \r
162         /* Start with things in a safe known state. */\r
163         ETH_DeInit();\r
164         for( ul = 0; ul < uipNUM_RX_DESCRIPTORS; ul++ )\r
165         {\r
166                 ETH_DMARxDescReceiveITConfig( &( xRxDescriptors[ ul ] ), DISABLE );\r
167         }\r
168 \r
169         /* Route clock to the peripheral. */\r
170     RCC->AHBENR |= ( uipRCC_MAC_CLOCK | uipRCC_MAC_TX_CLOCK | uipRCC_MAC_RX_CLOCK );\r
171 \r
172         /* Set the MAC address. */\r
173         ETH_MACAddressConfig( ETH_MAC_Address0, ( unsigned char * ) ucMACAddress );\r
174 \r
175         /* Use MII mode. */\r
176     AFIO->MAPR &= ~( uipMODE_MII );\r
177 \r
178         /* Configure all the GPIO as required for MAC/PHY interfacing. */\r
179         prvSetupEthGPIO();\r
180 \r
181         /* Reset the peripheral. */\r
182         ETH_SoftwareReset();\r
183         while( ETH_GetSoftwareResetStatus() == SET );\r
184 \r
185         /* Initialise using the whopping big structure.  Code space could be saved\r
186         by making this a const struct, however that would mean changes to the\r
187         structure within the library header files could break the code, so for now\r
188         just set everything manually at run time. */\r
189         xEthInit.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;\r
190         xEthInit.ETH_Watchdog = ETH_Watchdog_Disable;\r
191         xEthInit.ETH_Jabber = ETH_Jabber_Disable;\r
192         xEthInit.ETH_JumboFrame = ETH_JumboFrame_Disable;\r
193         xEthInit.ETH_InterFrameGap = ETH_InterFrameGap_96Bit;\r
194         xEthInit.ETH_CarrierSense = ETH_CarrierSense_Enable;\r
195         xEthInit.ETH_Speed = ETH_Speed_10M;\r
196         xEthInit.ETH_ReceiveOwn = ETH_ReceiveOwn_Disable;\r
197         xEthInit.ETH_LoopbackMode = ETH_LoopbackMode_Disable;\r
198         xEthInit.ETH_Mode = ETH_Mode_HalfDuplex;\r
199         xEthInit.ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;\r
200         xEthInit.ETH_RetryTransmission = ETH_RetryTransmission_Disable;\r
201         xEthInit.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;\r
202         xEthInit.ETH_BackOffLimit = ETH_BackOffLimit_10;\r
203         xEthInit.ETH_DeferralCheck = ETH_DeferralCheck_Disable;\r
204         xEthInit.ETH_ReceiveAll = ETH_ReceiveAll_Enable;\r
205         xEthInit.ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;\r
206         xEthInit.ETH_PassControlFrames = ETH_PassControlFrames_ForwardPassedAddrFilter;\r
207         xEthInit.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;\r
208         xEthInit.ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;\r
209         xEthInit.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;\r
210         xEthInit.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;\r
211         xEthInit.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;\r
212         xEthInit.ETH_HashTableHigh = 0x0;\r
213         xEthInit.ETH_HashTableLow = 0x0;\r
214         xEthInit.ETH_PauseTime = 0x0;\r
215         xEthInit.ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;\r
216         xEthInit.ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;\r
217         xEthInit.ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;\r
218         xEthInit.ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;\r
219         xEthInit.ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;\r
220         xEthInit.ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;\r
221         xEthInit.ETH_VLANTagIdentifier = 0x0;\r
222         xEthInit.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;\r
223         xEthInit.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;\r
224         xEthInit.ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Disable;\r
225         xEthInit.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;\r
226         xEthInit.ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;\r
227         xEthInit.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;\r
228         xEthInit.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;\r
229         xEthInit.ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;\r
230         xEthInit.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;\r
231         xEthInit.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;\r
232         xEthInit.ETH_FixedBurst = ETH_FixedBurst_Disable;\r
233         xEthInit.ETH_RxDMABurstLength = ETH_RxDMABurstLength_1Beat;\r
234         xEthInit.ETH_TxDMABurstLength = ETH_TxDMABurstLength_1Beat;\r
235         xEthInit.ETH_DescriptorSkipLength = 0x0;\r
236         xEthInit.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;\r
237 \r
238         xReturn = ETH_Init( &xEthInit, uipPHY_ADDRESS );\r
239 \r
240         /* Check a link was established. */\r
241         if( xReturn != pdFAIL )\r
242         {\r
243                 /* Rx and Tx interrupts are used. */\r
244                 ETH_DMAITConfig( ETH_DMA_IT_NIS | ETH_DMA_IT_R | ETH_DMA_IT_T, ENABLE );\r
245 \r
246                 /* Only a single Tx descriptor is used.  For now it is set to use an Rx\r
247                 buffer, but will get updated to point to where ever uip_buf is\r
248                 pointing prior to its use. */\r
249                 ETH_DMATxDescChainInit( ( void * ) &xTxDescriptor, ( void * ) ucMACBuffers, 1 );\r
250                 ETH_DMARxDescChainInit( xRxDescriptors, ( void * ) ucMACBuffers, uipNUM_RX_DESCRIPTORS );\r
251                 for( ul = 0; ul < uipNUM_RX_DESCRIPTORS; ul++ )\r
252                 {\r
253                         /* Ensure received data generates an interrupt. */\r
254                         ETH_DMARxDescReceiveITConfig( &( xRxDescriptors[ ul ] ), ENABLE );\r
255 \r
256                         /* Fix up the addresses used by the descriptors.\r
257                         The way ETH_DMARxDescChainInit() is not compatible with the buffer\r
258                         declarations in this file. */\r
259                         xRxDescriptors[ ul ].Buffer1Addr = ( unsigned long ) &( ucMACBuffers[ ul ][ 0 ] );\r
260 \r
261                         /* Mark the buffer used by this descriptor as in use. */\r
262             ucBufferInUse[ ul ] = pdTRUE;\r
263                 }\r
264 \r
265                 /* When receiving data, start at the first descriptor. */\r
266                 ulNextDescriptor = 0;\r
267 \r
268                 /* Initialise uip_buf to ensure it points somewhere valid. */\r
269                 uip_buf = prvGetNextBuffer();\r
270 \r
271                 /* SendCount must be initialised to 2 to ensure the Tx descriptor looks\r
272                 as if its available (as if it has already been sent twice. */\r
273         xTxDescriptor.SendCount = 2;\r
274 \r
275                 /* Switch on the interrupts in the NVIC. */\r
276                 xNVICInit.NVIC_IRQChannel = uipENET_IRQ_NUM;\r
277                 xNVICInit.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;\r
278                 xNVICInit.NVIC_IRQChannelSubPriority = 0;\r
279                 xNVICInit.NVIC_IRQChannelCmd = ENABLE;\r
280                 NVIC_Init( &xNVICInit );\r
281 \r
282                 /* Buffers and descriptors are all set up, now enable the MAC. */\r
283                 ETH_Start();\r
284 \r
285                 /* Let the DMA know there are Rx descriptors available. */\r
286                 prvRxDescriptorAvailable();\r
287         }\r
288 \r
289         return xReturn;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static unsigned char *prvGetNextBuffer( void )\r
294 {\r
295 portBASE_TYPE x;\r
296 unsigned char *ucReturn = NULL;\r
297 unsigned long ulAttempts = 0;\r
298 \r
299         while( ucReturn == NULL )\r
300         {\r
301                 /* Look through the buffers to find one that is not in use by\r
302                 anything else. */\r
303                 for( x = 0; x < uipNUM_BUFFERS; x++ )\r
304                 {\r
305                         if( ucBufferInUse[ x ] == pdFALSE )\r
306                         {\r
307                                 ucBufferInUse[ x ] = pdTRUE;\r
308                                 ucReturn = &( ucMACBuffers[ x ][ 0 ] );\r
309                                 break;\r
310                         }\r
311                 }\r
312 \r
313                 /* Was a buffer found? */\r
314                 if( ucReturn == NULL )\r
315                 {\r
316                         ulAttempts++;\r
317 \r
318                         if( ulAttempts >= uipBUFFER_WAIT_ATTEMPTS )\r
319                         {\r
320                                 break;\r
321                         }\r
322 \r
323                         /* Wait then look again. */\r
324                         vTaskDelay( uipBUFFER_WAIT_DELAY );\r
325                 }\r
326         }\r
327 \r
328         return ucReturn;\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 unsigned short usGetMACRxData( void )\r
333 {\r
334 unsigned short usReturn;\r
335 \r
336         if( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_ES ) != 0 )\r
337         {\r
338                 /* Error in Rx.  Discard the frame and give it back to the DMA. */\r
339                 xRxDescriptors[ ulNextDescriptor ].Status = ETH_DMARxDesc_OWN;\r
340                 prvRxDescriptorAvailable();\r
341 \r
342                 /* No data to return. */\r
343                 usReturn = 0UL;\r
344 \r
345                 /* Start from the next descriptor the next time this function is called. */\r
346                 ulNextDescriptor++;\r
347                 if( ulNextDescriptor >= uipNUM_RX_DESCRIPTORS )\r
348                 {\r
349                         ulNextDescriptor = 0UL;\r
350                 }\r
351         }\r
352         else if( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_OWN ) == 0 )\r
353         {\r
354                 /* Mark the current buffer as free as uip_buf is going to be set to\r
355                 the buffer that contains the received data. */\r
356                 vReturnBuffer( uip_buf );\r
357 \r
358                 /* Get the received data length from the top 2 bytes of the Status\r
359                 word and the data itself. */\r
360                 usReturn = ( unsigned short ) ( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_FL ) >> 16UL );\r
361                 uip_buf = ( unsigned char * ) ( xRxDescriptors[ ulNextDescriptor ].Buffer1Addr );\r
362 \r
363                 /* Allocate a new buffer to the descriptor. */\r
364                 xRxDescriptors[ ulNextDescriptor ].Buffer1Addr = ( unsigned long ) prvGetNextBuffer();\r
365 \r
366                 /* Give the descriptor back to the DMA. */\r
367                 xRxDescriptors[ ulNextDescriptor ].Status = ETH_DMARxDesc_OWN;\r
368                 prvRxDescriptorAvailable();\r
369 \r
370                 /* Start from the next descriptor the next time this function is called. */\r
371                 ulNextDescriptor++;\r
372                 if( ulNextDescriptor >= uipNUM_RX_DESCRIPTORS )\r
373                 {\r
374                         ulNextDescriptor = 0UL;\r
375                 }\r
376         }\r
377         else\r
378         {\r
379                 /* No received data at all. */\r
380                 usReturn = 0UL;\r
381         }\r
382 \r
383         return usReturn;\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 void vSendMACData( unsigned short usDataLen )\r
388 {\r
389 unsigned long ulAttempts = 0UL;\r
390 \r
391         /* Check to see if the Tx descriptor is free.  The check against <2 is to\r
392         ensure the buffer has been sent twice and in so doing preventing a race\r
393         condition with the DMA on the ETH_DMATxDesc_OWN bit. */\r
394         while( ( xTxDescriptor.SendCount < 2 ) && ( xTxDescriptor.Status & ETH_DMATxDesc_OWN ) == ETH_DMATxDesc_OWN )\r
395         {\r
396                 /* Wait for the Tx descriptor to become available. */\r
397                 vTaskDelay( uipBUFFER_WAIT_DELAY );\r
398 \r
399                 ulAttempts++;\r
400                 if( ulAttempts > uipBUFFER_WAIT_ATTEMPTS )\r
401                 {\r
402                         /* Something has gone wrong as the Tx descriptor is still in use.\r
403                         Clear it down manually, the data it was sending will probably be\r
404                         lost. */\r
405                         xTxDescriptor.Status &= ~ETH_DMATxDesc_OWN;\r
406                         vReturnBuffer( ( unsigned char * ) xTxDescriptor.Buffer1Addr );\r
407                         break;\r
408                 }\r
409         }\r
410 \r
411         /* Setup the Tx descriptor for transmission. */\r
412         xTxDescriptor.SendCount = 0;\r
413         xTxDescriptor.Buffer1Addr = ( unsigned long ) uip_buf;\r
414         xTxDescriptor.ControlBufferSize = ( unsigned long ) usDataLen;\r
415         xTxDescriptor.Status = ETH_DMATxDesc_OWN | ETH_DMATxDesc_LS | ETH_DMATxDesc_FS | ETH_DMATxDesc_TER | ETH_DMATxDesc_TCH | ETH_DMATxDesc_IC;\r
416         ETH_DMA->DMASR = ETH_DMASR_TBUS;\r
417         ETH_DMA->DMATPDR = 0;\r
418 \r
419         /* uip_buf is being sent by the Tx descriptor.  Allocate a new buffer. */\r
420         uip_buf = prvGetNextBuffer();\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 static void prvSetupEthGPIO( void )\r
425 {\r
426 GPIO_InitTypeDef xEthInit;\r
427 \r
428         /* Remap MAC IO. */\r
429         AFIO->MAPR |=  ( uipREMAP_MAC_IO );\r
430 \r
431         /* Set PA2, PA8, PB5, PB8, PB11, PB12, PB13, PC1 and PC2 for Ethernet\r
432         interfacing. */\r
433         xEthInit.GPIO_Pin = GPIO_Pin_2;/* | GPIO_Pin_8; This should be set when the 25MHz is generated by MCO. */\r
434         xEthInit.GPIO_Speed = GPIO_Speed_50MHz;\r
435         xEthInit.GPIO_Mode = GPIO_Mode_AF_PP;\r
436         GPIO_Init( GPIOA, &xEthInit );\r
437 \r
438         xEthInit.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; /*5*/\r
439         GPIO_Init( GPIOB, &xEthInit );\r
440 \r
441         xEthInit.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;\r
442         GPIO_Init( GPIOC, &xEthInit );\r
443 \r
444 \r
445         /* Configure PA0, PA1, PA3, PB10, PC3, PD8, PD9, PD10, PD11 and PD12 as\r
446         inputs. */\r
447         xEthInit.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;\r
448         xEthInit.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
449         GPIO_Init( GPIOA, &xEthInit );\r
450 \r
451         xEthInit.GPIO_Pin = GPIO_Pin_10;\r
452         GPIO_Init( GPIOB, &xEthInit );\r
453 \r
454         xEthInit.GPIO_Pin = GPIO_Pin_3;\r
455         GPIO_Init( GPIOC, &xEthInit );\r
456 \r
457         xEthInit.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;\r
458         GPIO_Init( GPIOD, &xEthInit );\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 void vReturnBuffer( unsigned char *pucBuffer )\r
463 {\r
464 unsigned long ul;\r
465 \r
466         /* Mark a buffer as free for use. */\r
467         for( ul = 0; ul < uipNUM_BUFFERS; ul++ )\r
468         {\r
469                 if( ucMACBuffers[ ul ] == pucBuffer )\r
470                 {\r
471                         ucBufferInUse[ ul ] = pdFALSE;\r
472                         break;\r
473                 }\r
474         }\r
475 }\r
476 /*-----------------------------------------------------------*/\r
477 \r
478 void vMAC_ISR( void )\r
479 {\r
480 unsigned long ulStatus;\r
481 extern xSemaphoreHandle xEMACSemaphore;\r
482 long xHigherPriorityTaskWoken = pdFALSE;\r
483 \r
484         /* What caused the interrupt? */\r
485         ulStatus = ETH_DMA->DMASR;\r
486 \r
487         /* Clear everything before leaving. */\r
488     ETH_DMA->DMASR = ulStatus;\r
489 \r
490         if( ulStatus & ETH_DMA_IT_R )\r
491         {\r
492                 /* Data was received.  Ensure the uIP task is not blocked as data has\r
493                 arrived. */\r
494                 xSemaphoreGiveFromISR( xEMACSemaphore, &xHigherPriorityTaskWoken );\r
495         }\r
496 \r
497         if( ulStatus & ETH_DMA_IT_T )\r
498         {\r
499                 /* Data was transmitted. */\r
500                 if( xTxDescriptor.SendCount == 0 )\r
501                 {\r
502                         /* Send again! */\r
503                         ( xTxDescriptor.SendCount )++;\r
504 \r
505                         xTxDescriptor.Status = ETH_DMATxDesc_OWN | ETH_DMATxDesc_LS | ETH_DMATxDesc_FS | ETH_DMATxDesc_TER | ETH_DMATxDesc_TCH | ETH_DMATxDesc_IC;\r
506                         ETH_DMA->DMASR = ETH_DMASR_TBUS;\r
507                         ETH_DMA->DMATPDR = 0;\r
508                 }\r
509                 else\r
510                 {\r
511                         /* The Tx buffer is no longer required. */\r
512                         vReturnBuffer( ( unsigned char * ) xTxDescriptor.Buffer1Addr );\r
513                 }\r
514         }\r
515 \r
516         /* If xSemaphoreGiveFromISR() unblocked a task, and the unblocked task has\r
517         a higher priority than the currently executing task, then\r
518         xHigherPriorityTaskWoken will have been set to pdTRUE and this ISR should\r
519         return directly to the higher priority unblocked task. */\r
520         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
521 }\r
522 \r