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