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