2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\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
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Kernel includes. */
\r
55 #include "FreeRTOS.h"
\r
59 /* Hardware includes. */
\r
62 #include "eth_phy.h"
\r
67 #include "uip_arp.h"
\r
69 /* Delay between polling the PHY to see if a link has been established. */
\r
70 #define fecLINK_DELAY ( 500 / portTICK_RATE_MS )
\r
72 /* Delay to wait for an MII access. */
\r
73 #define fecMII_DELAY ( 10 / portTICK_RATE_MS )
\r
74 #define fecMAX_POLLS ( 20 )
\r
76 /* Constants used to delay while waiting for a tx descriptor to be free. */
\r
77 #define fecMAX_WAIT_FOR_TX_BUFFER ( 200 / portTICK_RATE_MS )
\r
79 /* We only use a single Tx descriptor which can lead to Txed packets being sent
\r
80 twice (due to a bug in the FEC silicon). However, in this case the bug is used
\r
81 to our advantage in that it means the uip-split mechanism is not required. */
\r
82 #define fecNUM_FEC_TX_BUFFERS ( 1 )
\r
83 #define fecTX_BUFFER_TO_USE ( 0 )
\r
84 /*-----------------------------------------------------------*/
\r
86 /* The semaphore used to wake the uIP task when data arrives. */
\r
87 xSemaphoreHandle xFECSemaphore = NULL, xTxSemaphore = NULL;
\r
89 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
90 point to one of the Rx buffers to effect a zero copy policy. */
\r
91 unsigned portCHAR *uip_buf;
\r
93 /* The DMA descriptors. This is a char array to allow us to align it correctly. */
\r
94 static unsigned portCHAR xFECTxDescriptors_unaligned[ ( fecNUM_FEC_TX_BUFFERS * sizeof( FECBD ) ) + 16 ];
\r
95 static unsigned portCHAR xFECRxDescriptors_unaligned[ ( configNUM_FEC_RX_BUFFERS * sizeof( FECBD ) ) + 16 ];
\r
96 static FECBD *xFECTxDescriptors;
\r
97 static FECBD *xFECRxDescriptors;
\r
99 /* The DMA buffers. These are char arrays to allow them to be aligned correctly. */
\r
100 static unsigned portCHAR ucFECRxBuffers[ ( configNUM_FEC_RX_BUFFERS * configFEC_BUFFER_SIZE ) + 16 ];
\r
101 static unsigned portBASE_TYPE uxNextRxBuffer = 0, uxIndexToBufferOwner = 0;
\r
103 /*-----------------------------------------------------------*/
\r
106 * Enable all the required interrupts in the FEC and in the interrupt controller.
\r
108 static void prvEnableFECInterrupts( void );
\r
111 * Reset the FEC if we get into an unrecoverable state.
\r
113 static void prvResetFEC( portBASE_TYPE xCalledFromISR );
\r
115 /********************************************************************/
\r
118 * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE
\r
120 * Write a value to a PHY's MII register.
\r
124 * phy_addr Address of the PHY.
\r
125 * reg_addr Address of the register in the PHY.
\r
126 * data Data to be written to the PHY register.
\r
132 * Please refer to your PHY manual for registers and their meanings.
\r
133 * mii_write() polls for the FEC's MII interrupt event and clears it.
\r
134 * If after a suitable amount of time the event isn't triggered, a
\r
135 * value of 0 is returned.
\r
137 static int fec_mii_write( int phy_addr, int reg_addr, int data )
\r
139 int timeout, iReturn;
\r
142 /* Clear the MII interrupt bit */
\r
143 MCF_FEC_EIR = MCF_FEC_EIR_MII;
\r
145 /* Mask the MII interrupt */
\r
146 eimr = MCF_FEC_EIMR;
\r
147 MCF_FEC_EIMR &= ~MCF_FEC_EIMR_MII;
\r
149 /* Write to the MII Management Frame Register to kick-off the MII write */
\r
150 MCF_FEC_MMFR = MCF_FEC_MMFR_ST_01 | MCF_FEC_MMFR_OP_WRITE | MCF_FEC_MMFR_PA(phy_addr) | MCF_FEC_MMFR_RA(reg_addr) | MCF_FEC_MMFR_TA_10 | MCF_FEC_MMFR_DATA( data );
\r
152 /* Poll for the MII interrupt (interrupt should be masked) */
\r
153 for( timeout = 0; timeout < fecMAX_POLLS; timeout++ )
\r
155 if( MCF_FEC_EIR & MCF_FEC_EIR_MII )
\r
161 vTaskDelay( fecMII_DELAY );
\r
165 if( timeout == fecMAX_POLLS )
\r
174 /* Clear the MII interrupt bit */
\r
175 MCF_FEC_EIR = MCF_FEC_EIR_MII;
\r
177 /* Restore the EIMR */
\r
178 MCF_FEC_EIMR = eimr;
\r
183 /********************************************************************/
\r
185 * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE
\r
187 * Read a value from a PHY's MII register.
\r
191 * phy_addr Address of the PHY.
\r
192 * reg_addr Address of the register in the PHY.
\r
193 * data Pointer to storage for the Data to be read
\r
194 * from the PHY register (passed by reference)
\r
200 * Please refer to your PHY manual for registers and their meanings.
\r
201 * mii_read() polls for the FEC's MII interrupt event and clears it.
\r
202 * If after a suitable amount of time the event isn't triggered, a
\r
203 * value of 0 is returned.
\r
205 static int fec_mii_read( int phy_addr, int reg_addr, unsigned portSHORT* data )
\r
207 int timeout, iReturn;
\r
210 /* Clear the MII interrupt bit */
\r
211 MCF_FEC_EIR = MCF_FEC_EIR_MII;
\r
213 /* Mask the MII interrupt */
\r
214 eimr = MCF_FEC_EIMR;
\r
215 MCF_FEC_EIMR &= ~MCF_FEC_EIMR_MII;
\r
217 /* Write to the MII Management Frame Register to kick-off the MII read */
\r
218 MCF_FEC_MMFR = MCF_FEC_MMFR_ST_01 | MCF_FEC_MMFR_OP_READ | MCF_FEC_MMFR_PA(phy_addr) | MCF_FEC_MMFR_RA(reg_addr) | MCF_FEC_MMFR_TA_10;
\r
220 /* Poll for the MII interrupt (interrupt should be masked) */
\r
221 for( timeout = 0; timeout < fecMAX_POLLS; timeout++ )
\r
223 if (MCF_FEC_EIR & MCF_FEC_EIR_MII)
\r
229 vTaskDelay( fecMII_DELAY );
\r
233 if( timeout == fecMAX_POLLS )
\r
239 *data = (uint16)(MCF_FEC_MMFR & 0x0000FFFF);
\r
243 /* Clear the MII interrupt bit */
\r
244 MCF_FEC_EIR = MCF_FEC_EIR_MII;
\r
246 /* Restore the EIMR */
\r
247 MCF_FEC_EIMR = eimr;
\r
253 /********************************************************************/
\r
255 * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE
\r
257 * Generate the hash table settings for the given address
\r
260 * addr 48-bit (6 byte) Address to generate the hash for
\r
263 * The 6 most significant bits of the 32-bit CRC result
\r
265 static unsigned portCHAR fec_hash_address( const unsigned portCHAR* addr )
\r
267 unsigned portLONG crc;
\r
268 unsigned portCHAR byte;
\r
277 if((byte & 0x01)^(crc & 0x01))
\r
280 crc = crc ^ 0xEDB88320;
\r
291 return (unsigned portCHAR)(crc >> 26);
\r
294 /********************************************************************/
\r
296 * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE
\r
298 * Set the Physical (Hardware) Address and the Individual Address
\r
299 * Hash in the selected FEC
\r
303 * pa Physical (Hardware) Address for the selected FEC
\r
305 static void fec_set_address( const unsigned portCHAR *pa )
\r
307 unsigned portCHAR crc;
\r
310 * Set the Physical Address
\r
312 /* Set the source address for the controller */
\r
313 MCF_FEC_PALR = ( pa[ 0 ] << 24 ) | ( pa[ 1 ] << 16 ) | ( pa[ 2 ] << 8 ) | ( pa[ 3 ] << 0 );
\r
314 MCF_FEC_PAUR = ( pa[ 4 ] << 24 ) | ( pa[ 5 ] << 16 );
\r
317 * Calculate and set the hash for given Physical Address
\r
318 * in the Individual Address Hash registers
\r
320 crc = fec_hash_address( pa );
\r
323 MCF_FEC_IAUR |= (unsigned portLONG)(1 << (crc - 32));
\r
327 MCF_FEC_IALR |= (unsigned portLONG)(1 << crc);
\r
330 /*-----------------------------------------------------------*/
\r
332 static void prvInitialiseFECBuffers( void )
\r
334 unsigned portBASE_TYPE ux;
\r
335 unsigned portCHAR *pcBufPointer;
\r
337 /* Correctly align the Tx descriptor pointer. */
\r
338 pcBufPointer = &( xFECTxDescriptors_unaligned[ 0 ] );
\r
339 while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )
\r
344 xFECTxDescriptors = ( FECBD * ) pcBufPointer;
\r
346 /* Likewise the Rx descriptor pointer. */
\r
347 pcBufPointer = &( xFECRxDescriptors_unaligned[ 0 ] );
\r
348 while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )
\r
353 xFECRxDescriptors = ( FECBD * ) pcBufPointer;
\r
356 /* Setup the Tx buffers and descriptors. There is no separate Tx buffer
\r
357 to point to (the Rx buffers are actually used) so the data member is
\r
358 set to NULL for now. */
\r
359 for( ux = 0; ux < fecNUM_FEC_TX_BUFFERS; ux++ )
\r
361 xFECTxDescriptors[ ux ].status = TX_BD_TC;
\r
362 xFECTxDescriptors[ ux ].data = NULL;
\r
363 xFECTxDescriptors[ ux ].length = 0;
\r
366 /* Setup the Rx buffers and descriptors, having first ensured correct
\r
368 pcBufPointer = &( ucFECRxBuffers[ 0 ] );
\r
369 while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )
\r
374 for( ux = 0; ux < configNUM_FEC_RX_BUFFERS; ux++ )
\r
376 xFECRxDescriptors[ ux ].status = RX_BD_E;
\r
377 xFECRxDescriptors[ ux ].length = configFEC_BUFFER_SIZE;
\r
378 xFECRxDescriptors[ ux ].data = pcBufPointer;
\r
379 pcBufPointer += configFEC_BUFFER_SIZE;
\r
382 /* Set the wrap bit in the last descriptors to form a ring. */
\r
383 xFECTxDescriptors[ fecNUM_FEC_TX_BUFFERS - 1 ].status |= TX_BD_W;
\r
384 xFECRxDescriptors[ configNUM_FEC_RX_BUFFERS - 1 ].status |= RX_BD_W;
\r
386 uxNextRxBuffer = 0;
\r
388 /*-----------------------------------------------------------*/
\r
390 void vFECInit( void )
\r
392 unsigned portSHORT usData;
\r
393 struct uip_eth_addr xAddr;
\r
394 unsigned portBASE_TYPE ux;
\r
396 /* The MAC address is set at the foot of FreeRTOSConfig.h. */
\r
397 const unsigned portCHAR ucMACAddress[6] =
\r
399 configMAC_0, configMAC_1,configMAC_2, configMAC_3, configMAC_4, configMAC_5
\r
402 /* Create the semaphore used by the ISR to wake the uIP task. */
\r
403 vSemaphoreCreateBinary( xFECSemaphore );
\r
405 /* Create the semaphore used to unblock any tasks that might be waiting
\r
406 for a Tx descriptor. */
\r
407 vSemaphoreCreateBinary( xTxSemaphore );
\r
409 /* Initialise all the buffers and descriptors used by the DMA. */
\r
410 prvInitialiseFECBuffers();
\r
412 for( usData = 0; usData < 6; usData++ )
\r
414 xAddr.addr[ usData ] = ucMACAddress[ usData ];
\r
416 uip_setethaddr( xAddr );
\r
418 /* Set the Reset bit and clear the Enable bit */
\r
419 MCF_FEC_ECR = MCF_FEC_ECR_RESET;
\r
421 /* Wait at least 8 clock cycles */
\r
422 for( usData = 0; usData < 10; usData++ )
\r
427 /* Set MII speed to 2.5MHz. */
\r
428 MCF_FEC_MSCR = MCF_FEC_MSCR_MII_SPEED( ( ( ( configCPU_CLOCK_HZ / 1000000 ) / 5 ) + 1 ) );
\r
430 /* Initialize PLDPAR to enable Ethernet LEDs. */
\r
431 MCF_GPIO_PLDPAR = MCF_GPIO_PLDPAR_ACTLED_ACTLED | MCF_GPIO_PLDPAR_LINKLED_LINKLED | MCF_GPIO_PLDPAR_SPDLED_SPDLED
\r
432 | MCF_GPIO_PLDPAR_DUPLED_DUPLED | MCF_GPIO_PLDPAR_COLLED_COLLED | MCF_GPIO_PLDPAR_RXLED_RXLED
\r
433 | MCF_GPIO_PLDPAR_TXLED_TXLED;
\r
435 /* Initialize Port TA to enable Axcel control. */
\r
436 MCF_GPIO_PTAPAR = 0x00;
\r
437 MCF_GPIO_DDRTA = 0x0F;
\r
438 MCF_GPIO_PORTTA = 0x04;
\r
440 /* Set phy address to zero. */
\r
441 MCF_EPHY_EPHYCTL1 = MCF_EPHY_EPHYCTL1_PHYADD( 0 );
\r
443 /* Enable EPHY module with PHY clocks disabled. Do not turn on PHY clocks
\r
444 until both FEC and EPHY are completely setup (see Below). */
\r
445 MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0_DIS100 | MCF_EPHY_EPHYCTL0_DIS10);
\r
447 /* Enable auto_neg at start-up */
\r
448 MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0 & (MCF_EPHY_EPHYCTL0_ANDIS));
\r
450 /* Enable EPHY module. */
\r
451 MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0_EPHYEN | MCF_EPHY_EPHYCTL0);
\r
453 /* Let PHY PLLs be determined by PHY. */
\r
454 MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0 & ~(MCF_EPHY_EPHYCTL0_DIS100 | MCF_EPHY_EPHYCTL0_DIS10));
\r
457 vTaskDelay( fecLINK_DELAY );
\r
459 /* Can we talk to the PHY? */
\r
462 vTaskDelay( fecLINK_DELAY );
\r
464 fec_mii_read( configPHY_ADDRESS, PHY_PHYIDR1, &usData );
\r
466 } while( usData == 0xffff );
\r
470 /* Start auto negotiate. */
\r
471 fec_mii_write( configPHY_ADDRESS, PHY_BMCR, ( PHY_BMCR_AN_RESTART | PHY_BMCR_AN_ENABLE ) );
\r
473 /* Wait for auto negotiate to complete. */
\r
479 /* Hardware bug workaround! Force 100Mbps half duplex. */
\r
480 while( !fec_mii_read( configPHY_ADDRESS, 0, &usData ) ){};
\r
481 usData &= ~0x2000; /* 10Mbps */
\r
482 usData &= ~0x0100; /* Half Duplex */
\r
483 usData &= ~0x1000; /* Manual Mode */
\r
484 while( !fec_mii_write( configPHY_ADDRESS, 0, usData ) ){};
\r
485 while( !fec_mii_write( configPHY_ADDRESS, 0, (usData|0x0200) )){}; /* Force re-negotiate */
\r
488 vTaskDelay( fecLINK_DELAY );
\r
489 fec_mii_read( configPHY_ADDRESS, PHY_BMSR, &usData );
\r
491 } while( !( usData & PHY_BMSR_AN_COMPLETE ) );
\r
493 } while( 0 ); //while( !( usData & PHY_BMSR_LINK ) );
\r
495 /* When we get here we have a link - find out what has been negotiated. */
\r
496 fec_mii_read( configPHY_ADDRESS, PHY_ANLPAR, &usData );
\r
498 if( ( usData & PHY_ANLPAR_100BTX_FDX ) || ( usData & PHY_ANLPAR_100BTX ) )
\r
500 /* Speed is 100. */
\r
507 if( ( usData & PHY_ANLPAR_100BTX_FDX ) || ( usData & PHY_ANLPAR_10BTX_FDX ) )
\r
509 MCF_FEC_RCR &= (unsigned portLONG)~MCF_FEC_RCR_DRT;
\r
510 MCF_FEC_TCR |= MCF_FEC_TCR_FDEN;
\r
514 MCF_FEC_RCR |= MCF_FEC_RCR_DRT;
\r
515 MCF_FEC_TCR &= (unsigned portLONG)~MCF_FEC_TCR_FDEN;
\r
518 /* Clear the Individual and Group Address Hash registers */
\r
524 /* Set the Physical Address for the selected FEC */
\r
525 fec_set_address( ucMACAddress );
\r
527 /* Set Rx Buffer Size */
\r
528 MCF_FEC_EMRBR = (unsigned portSHORT)configFEC_BUFFER_SIZE;
\r
530 /* Point to the start of the circular Rx buffer descriptor queue */
\r
531 MCF_FEC_ERDSR = ( volatile unsigned portLONG ) &( xFECRxDescriptors[ 0 ] );
\r
533 /* Point to the start of the circular Tx buffer descriptor queue */
\r
534 MCF_FEC_ETSDR = ( volatile unsigned portLONG ) &( xFECTxDescriptors[ 0 ] );
\r
536 /* Mask all FEC interrupts */
\r
537 MCF_FEC_EIMR = ( unsigned portLONG ) -1;
\r
539 /* Clear all FEC interrupt events */
\r
540 MCF_FEC_EIR = ( unsigned portLONG ) -1;
\r
542 /* Initialize the Receive Control Register */
\r
543 MCF_FEC_RCR = MCF_FEC_RCR_MAX_FL(ETH_MAX_FRM) | MCF_FEC_RCR_FCE;
\r
545 MCF_FEC_RCR |= MCF_FEC_RCR_MII_MODE;
\r
547 #if( configUSE_PROMISCUOUS_MODE == 1 )
\r
549 MCF_FEC_RCR |= MCF_FEC_RCR_PROM;
\r
553 prvEnableFECInterrupts();
\r
555 /* Finally... enable. */
\r
556 MCF_FEC_ECR = MCF_FEC_ECR_ETHER_EN;
\r
557 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;
\r
559 /*-----------------------------------------------------------*/
\r
561 static void prvEnableFECInterrupts( void )
\r
563 const unsigned portBASE_TYPE uxFirstFECVector = 23, uxLastFECVector = 35;
\r
564 unsigned portBASE_TYPE ux;
\r
566 #if configFEC_INTERRUPT_PRIORITY > configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
567 #error configFEC_INTERRUPT_PRIORITY must be less than or equal to configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
570 /* Set the priority of each of the FEC interrupts. */
\r
571 for( ux = uxFirstFECVector; ux <= uxLastFECVector; ux++ )
\r
573 MCF_INTC0_ICR( ux ) = MCF_INTC_ICR_IL( configFEC_INTERRUPT_PRIORITY );
\r
576 /* Enable the FEC interrupts in the mask register */
\r
577 MCF_INTC0_IMRH &= ~( MCF_INTC_IMRH_INT_MASK33 | MCF_INTC_IMRH_INT_MASK34 | MCF_INTC_IMRH_INT_MASK35 );
\r
578 MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK25 | MCF_INTC_IMRL_INT_MASK26 | MCF_INTC_IMRL_INT_MASK27
\r
579 | MCF_INTC_IMRL_INT_MASK28 | MCF_INTC_IMRL_INT_MASK29 | MCF_INTC_IMRL_INT_MASK30
\r
580 | MCF_INTC_IMRL_INT_MASK31 | MCF_INTC_IMRL_INT_MASK23 | MCF_INTC_IMRL_INT_MASK24
\r
581 | MCF_INTC_IMRL_MASKALL );
\r
583 /* Clear any pending FEC interrupt events */
\r
584 MCF_FEC_EIR = MCF_FEC_EIR_CLEAR_ALL;
\r
586 /* Unmask all FEC interrupts */
\r
587 MCF_FEC_EIMR = MCF_FEC_EIMR_UNMASK_ALL;
\r
589 /*-----------------------------------------------------------*/
\r
591 static void prvResetFEC( portBASE_TYPE xCalledFromISR )
\r
595 /* A critical section is used unless this function is being called from
\r
597 if( xCalledFromISR == pdFALSE )
\r
599 taskENTER_CRITICAL();
\r
603 /* Reset all buffers and descriptors. */
\r
604 prvInitialiseFECBuffers();
\r
606 /* Set the Reset bit and clear the Enable bit */
\r
607 MCF_FEC_ECR = MCF_FEC_ECR_RESET;
\r
609 /* Wait at least 8 clock cycles */
\r
610 for( x = 0; x < 10; x++ )
\r
616 MCF_FEC_ECR = MCF_FEC_ECR_ETHER_EN;
\r
617 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;
\r
620 if( xCalledFromISR == pdFALSE )
\r
622 taskEXIT_CRITICAL();
\r
625 /*-----------------------------------------------------------*/
\r
627 unsigned short usFECGetRxedData( void )
\r
629 unsigned portSHORT usLen;
\r
631 /* Obtain the size of the packet and put it into the "len" variable. */
\r
632 usLen = xFECRxDescriptors[ uxNextRxBuffer ].length;
\r
634 if( ( usLen != 0 ) && ( ( xFECRxDescriptors[ uxNextRxBuffer ].status & RX_BD_E ) == 0 ) )
\r
636 uip_buf = xFECRxDescriptors[ uxNextRxBuffer ].data;
\r
645 /*-----------------------------------------------------------*/
\r
647 void vFECRxProcessingCompleted( void )
\r
649 /* Free the descriptor as the buffer it points to is no longer in use. */
\r
650 xFECRxDescriptors[ uxNextRxBuffer ].status |= RX_BD_E;
\r
651 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;
\r
653 if( uxNextRxBuffer >= configNUM_FEC_RX_BUFFERS )
\r
655 uxNextRxBuffer = 0;
\r
658 /*-----------------------------------------------------------*/
\r
660 void vFECSendData( void )
\r
662 /* Ensure no Tx frames are outstanding. */
\r
663 if( xSemaphoreTake( xTxSemaphore, fecMAX_WAIT_FOR_TX_BUFFER ) == pdPASS )
\r
665 /* Get a DMA buffer into which we can write the data to send. */
\r
666 if( xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].status & TX_BD_R )
\r
668 /*** ERROR didn't expect this. Sledge hammer error handling. ***/
\r
669 prvResetFEC( pdFALSE );
\r
671 /* Make sure we leave the semaphore in the expected state as nothing
\r
672 is being transmitted this will not happen in the Tx ISR. */
\r
673 xSemaphoreGive( xTxSemaphore );
\r
677 /* Setup the buffer descriptor for transmission. The data being
\r
678 sent is actually stored in one of the Rx descriptor buffers,
\r
679 pointed to by uip_buf. */
\r
680 xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].length = uip_len;
\r
681 xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].status |= ( TX_BD_R | TX_BD_L );
\r
682 xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].data = uip_buf;
\r
684 /* Remember which Rx descriptor owns the buffer we are sending. */
\r
685 uxIndexToBufferOwner = uxNextRxBuffer;
\r
687 /* We have finished with this Rx descriptor now. */
\r
689 if( uxNextRxBuffer >= configNUM_FEC_RX_BUFFERS )
\r
691 uxNextRxBuffer = 0;
\r
694 /* Continue the Tx DMA (in case it was waiting for a new TxBD) */
\r
695 MCF_FEC_TDAR = MCF_FEC_TDAR_X_DES_ACTIVE;
\r
700 /* Gave up waiting. Free the buffer back to the DMA. */
\r
701 vFECRxProcessingCompleted();
\r
704 /*-----------------------------------------------------------*/
\r
706 void vFEC_ISR( void )
\r
708 unsigned portLONG ulEvent;
\r
709 portBASE_TYPE xHighPriorityTaskWoken = pdFALSE;
\r
711 /* This handler is called in response to any of the many separate FEC
\r
714 /* Find the cause of the interrupt, then clear the interrupt. */
\r
715 ulEvent = MCF_FEC_EIR & MCF_FEC_EIMR;
\r
716 MCF_FEC_EIR = ulEvent;
\r
718 if( ( ulEvent & MCF_FEC_EIR_RXB ) || ( ulEvent & MCF_FEC_EIR_RXF ) )
\r
720 /* A packet has been received. Wake the handler task. */
\r
721 xSemaphoreGiveFromISR( xFECSemaphore, &xHighPriorityTaskWoken );
\r
724 if( ulEvent & ( MCF_FEC_EIR_UN | MCF_FEC_EIR_RL | MCF_FEC_EIR_LC | MCF_FEC_EIR_EBERR | MCF_FEC_EIR_BABT | MCF_FEC_EIR_BABR | MCF_FEC_EIR_HBERR ) )
\r
726 /* Sledge hammer error handling. */
\r
727 prvResetFEC( pdTRUE );
\r
730 if( ( ulEvent & MCF_FEC_EIR_TXF ) || ( ulEvent & MCF_FEC_EIR_TXB ) )
\r
732 /* The buffer being sent is pointed to by an Rx descriptor, now the
\r
733 buffer has been sent we can mark the Rx descriptor as free again. */
\r
734 xFECRxDescriptors[ uxIndexToBufferOwner ].status |= RX_BD_E;
\r
735 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;
\r
736 xSemaphoreGiveFromISR( xTxSemaphore, &xHighPriorityTaskWoken );
\r
739 portEND_SWITCHING_ISR( xHighPriorityTaskWoken );
\r
741 /*-----------------------------------------------------------*/
\r
743 /* Install the many different interrupt vectors, all of which call the same
\r
744 handler function. */
\r
745 void __attribute__ ((interrupt)) __cs3_isr_interrupt_87( void ) { vFEC_ISR(); }
\r
746 void __attribute__ ((interrupt)) __cs3_isr_interrupt_88( void ) { vFEC_ISR(); }
\r
747 void __attribute__ ((interrupt)) __cs3_isr_interrupt_89( void ) { vFEC_ISR(); }
\r
748 void __attribute__ ((interrupt)) __cs3_isr_interrupt_90( void ) { vFEC_ISR(); }
\r
749 void __attribute__ ((interrupt)) __cs3_isr_interrupt_91( void ) { vFEC_ISR(); }
\r
750 void __attribute__ ((interrupt)) __cs3_isr_interrupt_92( void ) { vFEC_ISR(); }
\r
751 void __attribute__ ((interrupt)) __cs3_isr_interrupt_93( void ) { vFEC_ISR(); }
\r
752 void __attribute__ ((interrupt)) __cs3_isr_interrupt_94( void ) { vFEC_ISR(); }
\r
753 void __attribute__ ((interrupt)) __cs3_isr_interrupt_95( void ) { vFEC_ISR(); }
\r
754 void __attribute__ ((interrupt)) __cs3_isr_interrupt_96( void ) { vFEC_ISR(); }
\r
755 void __attribute__ ((interrupt)) __cs3_isr_interrupt_97( void ) { vFEC_ISR(); }
\r
756 void __attribute__ ((interrupt)) __cs3_isr_interrupt_98( void ) { vFEC_ISR(); }
\r
757 void __attribute__ ((interrupt)) __cs3_isr_interrupt_99( void ) { vFEC_ISR(); }
\r