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