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