2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
34 * Interrupt driven driver for the EMAC peripheral. This driver is not
\r
35 * reentrant, re-entrancy is handled by a semaphore at the network interface
\r
43 + Corrected the byte order when writing the MAC address to the MAC.
\r
44 + Support added for MII interfaces. Previously only RMII was supported.
\r
48 + The MII interface is now the default.
\r
49 + Modified the initialisation sequence slightly to allow auto init more
\r
54 /* Standard includes. */
\r
57 /* Scheduler includes. */
\r
58 #include "FreeRTOS.h"
\r
62 /* Demo app includes. */
\r
63 #include "SAM7_EMAC.h"
\r
65 /* Hardware specific includes. */
\r
68 #include "AT91SAM7X256.h"
\r
71 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0
\r
72 to use an MII interface. */
\r
73 #define USE_RMII_INTERFACE 0
\r
76 /* The buffer addresses written into the descriptors must be aligned so the
\r
77 last few bits are zero. These bits have special meaning for the EMAC
\r
78 peripheral and cannot be used as part of the address. */
\r
79 #define emacADDRESS_MASK ( ( unsigned portLONG ) 0xFFFFFFFC )
\r
81 /* Bit used within the address stored in the descriptor to mark the last
\r
82 descriptor in the array. */
\r
83 #define emacRX_WRAP_BIT ( ( unsigned portLONG ) 0x02 )
\r
85 /* Bit used within the Tx descriptor status to indicate whether the
\r
86 descriptor is under the control of the EMAC or the software. */
\r
87 #define emacTX_BUF_USED ( ( unsigned portLONG ) 0x80000000 )
\r
89 /* A short delay is used to wait for a buffer to become available, should
\r
90 one not be immediately available when trying to transmit a frame. */
\r
91 #define emacBUFFER_WAIT_DELAY ( 2 )
\r
92 #define emacMAX_WAIT_CYCLES ( ( portBASE_TYPE ) ( configTICK_RATE_HZ / 40 ) )
\r
94 /* The time to block waiting for input. */
\r
95 #define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( portTickType ) 100 )
\r
97 /* Peripheral setup for the EMAC. */
\r
98 #define emacPERIPHERAL_A_SETUP ( ( unsigned portLONG ) AT91C_PB2_ETX0 ) | \
\r
99 ( ( unsigned portLONG ) AT91C_PB12_ETXER ) | \
\r
100 ( ( unsigned portLONG ) AT91C_PB16_ECOL ) | \
\r
101 ( ( unsigned portLONG ) AT91C_PB11_ETX3 ) | \
\r
102 ( ( unsigned portLONG ) AT91C_PB6_ERX1 ) | \
\r
103 ( ( unsigned portLONG ) AT91C_PB15_ERXDV ) | \
\r
104 ( ( unsigned portLONG ) AT91C_PB13_ERX2 ) | \
\r
105 ( ( unsigned portLONG ) AT91C_PB3_ETX1 ) | \
\r
106 ( ( unsigned portLONG ) AT91C_PB8_EMDC ) | \
\r
107 ( ( unsigned portLONG ) AT91C_PB5_ERX0 ) | \
\r
108 ( ( unsigned portLONG ) AT91C_PB14_ERX3 ) | \
\r
109 ( ( unsigned portLONG ) AT91C_PB4_ECRS_ECRSDV ) | \
\r
110 ( ( unsigned portLONG ) AT91C_PB1_ETXEN ) | \
\r
111 ( ( unsigned portLONG ) AT91C_PB10_ETX2 ) | \
\r
112 ( ( unsigned portLONG ) AT91C_PB0_ETXCK_EREFCK ) | \
\r
113 ( ( unsigned portLONG ) AT91C_PB9_EMDIO ) | \
\r
114 ( ( unsigned portLONG ) AT91C_PB7_ERXER ) | \
\r
115 ( ( unsigned portLONG ) AT91C_PB17_ERXCK );
\r
117 /* Misc defines. */
\r
118 #define emacINTERRUPT_LEVEL ( 5 )
\r
119 #define emacNO_DELAY ( 0 )
\r
120 #define emacTOTAL_FRAME_HEADER_SIZE ( 54 )
\r
121 #define emacPHY_INIT_DELAY ( 5000 / portTICK_RATE_MS )
\r
122 #define emacRESET_KEY ( ( unsigned portLONG ) 0xA5000000 )
\r
123 #define emacRESET_LENGTH ( ( unsigned portLONG ) ( 0x01 << 8 ) )
\r
125 /*-----------------------------------------------------------*/
\r
127 /* Buffer written to by the EMAC DMA. Must be aligned as described by the
\r
128 comment above the emacADDRESS_MASK definition. */
\r
129 static volatile portCHAR pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
131 /* Buffer read by the EMAC DMA. Must be aligned as described by the comment
\r
132 above the emacADDRESS_MASK definition. */
\r
133 static portCHAR pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
135 /* Descriptors used to communicate between the program and the EMAC peripheral.
\r
136 These descriptors hold the locations and state of the Rx and Tx buffers. */
\r
137 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];
\r
138 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];
\r
140 /* The IP and Ethernet addresses are read from the header files. */
\r
141 const portCHAR cMACAddress[ 6 ] = { emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };
\r
142 const unsigned char ucIPAddress[ 4 ] = { emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 };
\r
144 /*-----------------------------------------------------------*/
\r
146 /* See the header file for descriptions of public functions. */
\r
149 * Prototype for the EMAC interrupt function - called by the asm wrapper.
\r
151 void vEMACISR( void ) __attribute__ ((naked));
\r
154 * Initialise both the Tx and Rx descriptors used by the EMAC.
\r
156 static void prvSetupDescriptors(void);
\r
159 * Write our MAC address into the EMAC.
\r
161 static void prvSetupMACAddress( void );
\r
164 * Configure the EMAC and AIC for EMAC interrupts.
\r
166 static void prvSetupEMACInterrupt( void );
\r
169 * Some initialisation functions taken from the Atmel EMAC sample code.
\r
171 static void vReadPHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG *pulValue );
\r
172 static portBASE_TYPE xGetLinkSpeed( void );
\r
173 static portBASE_TYPE prvProbePHY( void );
\r
174 #if USE_RMII_INTERFACE != 1
\r
175 static void vWritePHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG ulValue);
\r
179 /* The semaphore used by the EMAC ISR to wake the EMAC task. */
\r
180 static xSemaphoreHandle xSemaphore = NULL;
\r
182 /* Holds the index to the next buffer from which data will be read. */
\r
183 static volatile unsigned portLONG ulNextRxBuffer = 0;
\r
185 /*-----------------------------------------------------------*/
\r
187 /* See the header file for descriptions of public functions. */
\r
188 portLONG lEMACSend( portCHAR *pcFrom, unsigned portLONG ulLength, portLONG lEndOfFrame )
\r
190 static unsigned portBASE_TYPE uxTxBufferIndex = 0;
\r
191 portBASE_TYPE xWaitCycles = 0;
\r
192 portLONG lReturn = pdPASS;
\r
193 portCHAR *pcBuffer;
\r
194 unsigned portLONG ulLastBuffer, ulDataBuffered = 0, ulDataRemainingToSend, ulLengthToSend;
\r
196 /* If the length of data to be transmitted is greater than each individual
\r
197 transmit buffer then the data will be split into more than one buffer.
\r
198 Loop until the entire length has been buffered. */
\r
199 while( ulDataBuffered < ulLength )
\r
201 /* Is a buffer available? */
\r
202 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )
\r
204 /* There is no room to write the Tx data to the Tx buffer. Wait a
\r
205 short while, then try again. */
\r
207 if( xWaitCycles > emacMAX_WAIT_CYCLES )
\r
215 vTaskDelay( emacBUFFER_WAIT_DELAY );
\r
219 /* lReturn will only be pdPASS if a buffer is available. */
\r
220 if( lReturn == pdPASS )
\r
222 portENTER_CRITICAL();
\r
224 /* Get the address of the buffer from the descriptor, then copy
\r
225 the data into the buffer. */
\r
226 pcBuffer = ( portCHAR * ) xTxDescriptors[ uxTxBufferIndex ].addr;
\r
228 /* How much can we write to the buffer? */
\r
229 ulDataRemainingToSend = ulLength - ulDataBuffered;
\r
230 if( ulDataRemainingToSend <= ETH_TX_BUFFER_SIZE )
\r
232 /* We can write all the remaining bytes. */
\r
233 ulLengthToSend = ulDataRemainingToSend;
\r
237 /* We can not write more than ETH_TX_BUFFER_SIZE in one go. */
\r
238 ulLengthToSend = ETH_TX_BUFFER_SIZE;
\r
241 /* Copy the data into the buffer. */
\r
242 memcpy( ( void * ) pcBuffer, ( void * ) &( pcFrom[ ulDataBuffered ] ), ulLengthToSend );
\r
243 ulDataBuffered += ulLengthToSend;
\r
245 /* Is this the last data for the frame? */
\r
246 if( lEndOfFrame && ( ulDataBuffered >= ulLength ) )
\r
248 /* No more data remains for this frame so we can start the
\r
250 ulLastBuffer = AT91C_LAST_BUFFER;
\r
254 /* More data to come for this frame. */
\r
258 /* Fill out the necessary in the descriptor to get the data sent,
\r
259 then move to the next descriptor, wrapping if necessary. */
\r
260 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )
\r
262 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned portLONG ) AT91C_LENGTH_FRAME )
\r
264 | AT91C_TRANSMIT_WRAP;
\r
265 uxTxBufferIndex = 0;
\r
269 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned portLONG ) AT91C_LENGTH_FRAME )
\r
274 /* If this is the last buffer to be sent for this frame we can
\r
275 start the transmission. */
\r
278 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
\r
281 portEXIT_CRITICAL();
\r
291 /*-----------------------------------------------------------*/
\r
293 /* See the header file for descriptions of public functions. */
\r
294 unsigned portLONG ulEMACInputLength( void )
\r
296 register unsigned portLONG ulIndex, ulLength = 0;
\r
298 /* Skip any fragments. We are looking for the first buffer that contains
\r
299 data and has the SOF (start of frame) bit set. */
\r
300 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )
\r
302 /* Ignoring this buffer. Mark it as free again. */
\r
303 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
305 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
307 ulNextRxBuffer = 0;
\r
311 /* We are going to walk through the descriptors that make up this frame,
\r
312 but don't want to alter ulNextRxBuffer as this would prevent vEMACRead()
\r
313 from finding the data. Therefore use a copy of ulNextRxBuffer instead. */
\r
314 ulIndex = ulNextRxBuffer;
\r
316 /* Walk through the descriptors until we find the last buffer for this
\r
317 frame. The last buffer will give us the length of the entire frame. */
\r
318 while( ( xRxDescriptors[ ulIndex ].addr & AT91C_OWNERSHIP_BIT ) && !ulLength )
\r
320 ulLength = xRxDescriptors[ ulIndex ].U_Status.status & AT91C_LENGTH_FRAME;
\r
322 /* Increment to the next buffer, wrapping if necessary. */
\r
324 if( ulIndex >= NB_RX_BUFFERS )
\r
332 /*-----------------------------------------------------------*/
\r
334 /* See the header file for descriptions of public functions. */
\r
335 void vEMACRead( portCHAR *pcTo, unsigned portLONG ulSectionLength, unsigned portLONG ulTotalFrameLength )
\r
337 static unsigned portLONG ulSectionBytesReadSoFar = 0, ulBufferPosition = 0, ulFameBytesReadSoFar = 0;
\r
338 static portCHAR *pcSource;
\r
339 register unsigned portLONG ulBytesRemainingInBuffer, ulRemainingSectionBytes;
\r
341 /* Read ulSectionLength bytes from the Rx buffers. This is not necessarily any
\r
342 correspondence between the length of our Rx buffers, and the length of the
\r
343 data we are returning or the length of the data being requested. Therefore,
\r
344 between calls we have to remember not only which buffer we are currently
\r
345 processing, but our position within that buffer. This would be greatly
\r
346 simplified if PBUF_POOL_BUFSIZE could be guaranteed to be greater than
\r
347 the size of each Rx buffer, and that memory fragmentation did not occur.
\r
349 This function should only be called after a call to ulEMACInputLength().
\r
350 This will ensure ulNextRxBuffer is set to the correct buffer. */
\r
354 /* vEMACRead is called with pcTo set to NULL to indicate that we are about
\r
355 to read a new frame. Any fragments remaining in the frame we were
\r
356 processing during the last call should be dropped. */
\r
359 /* How many bytes are indicated as being in this buffer? If none then
\r
360 the buffer is completely full and the frame is contained within more
\r
361 than one buffer. */
\r
363 /* Reset our state variables ready for the next read from this buffer. */
\r
364 pcSource = ( portCHAR * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
365 ulFameBytesReadSoFar = ( unsigned portLONG ) 0;
\r
366 ulBufferPosition = ( unsigned portLONG ) 0;
\r
370 /* Loop until we have obtained the required amount of data. */
\r
371 ulSectionBytesReadSoFar = 0;
\r
372 while( ulSectionBytesReadSoFar < ulSectionLength )
\r
374 /* We may have already read some data from this buffer. How much
\r
375 data remains in the buffer? */
\r
376 ulBytesRemainingInBuffer = ( ETH_RX_BUFFER_SIZE - ulBufferPosition );
\r
378 /* How many more bytes do we need to read before we have the
\r
379 required amount of data? */
\r
380 ulRemainingSectionBytes = ulSectionLength - ulSectionBytesReadSoFar;
\r
382 /* Do we want more data than remains in the buffer? */
\r
383 if( ulRemainingSectionBytes > ulBytesRemainingInBuffer )
\r
385 /* We want more data than remains in the buffer so we can
\r
386 write the remains of the buffer to the destination, then move
\r
387 onto the next buffer to get the rest. */
\r
388 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulBytesRemainingInBuffer );
\r
389 ulSectionBytesReadSoFar += ulBytesRemainingInBuffer;
\r
390 ulFameBytesReadSoFar += ulBytesRemainingInBuffer;
\r
392 /* Mark the buffer as free again. */
\r
393 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
395 /* Move onto the next buffer. */
\r
397 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
399 ulNextRxBuffer = ( unsigned portLONG ) 0;
\r
402 /* Reset the variables for the new buffer. */
\r
403 pcSource = ( portCHAR * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
404 ulBufferPosition = ( unsigned portLONG ) 0;
\r
408 /* We have enough data in this buffer to send back. Read out
\r
409 enough data and remember how far we read up to. */
\r
410 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulRemainingSectionBytes );
\r
412 /* There may be more data in this buffer yet. Increment our
\r
413 position in this buffer past the data we have just read. */
\r
414 ulBufferPosition += ulRemainingSectionBytes;
\r
415 ulSectionBytesReadSoFar += ulRemainingSectionBytes;
\r
416 ulFameBytesReadSoFar += ulRemainingSectionBytes;
\r
418 /* Have we now finished with this buffer? */
\r
419 if( ( ulBufferPosition >= ETH_RX_BUFFER_SIZE ) || ( ulFameBytesReadSoFar >= ulTotalFrameLength ) )
\r
421 /* Mark the buffer as free again. */
\r
422 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
424 /* Move onto the next buffer. */
\r
426 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
428 ulNextRxBuffer = 0;
\r
431 pcSource = ( portCHAR * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
432 ulBufferPosition = 0;
\r
438 /*-----------------------------------------------------------*/
\r
440 /* See the header file for descriptions of public functions. */
\r
441 xSemaphoreHandle xEMACInit( void )
\r
443 /* Code supplied by Atmel -------------------------------*/
\r
445 /* Disable pull up on RXDV => PHY normal mode (not in test mode),
\r
446 PHY has internal pull down. */
\r
447 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;
\r
449 #if USE_RMII_INTERFACE != 1
\r
450 /* PHY has internal pull down : set MII mode. */
\r
451 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;
\r
454 /* Clear PB18 <=> PHY powerdown. */
\r
455 AT91C_BASE_PIOB->PIO_PER = 1 << 18;
\r
456 AT91C_BASE_PIOB->PIO_OER = 1 << 18;
\r
457 AT91C_BASE_PIOB->PIO_CODR = 1 << 18;
\r
459 /* After PHY power up, hardware reset. */
\r
460 AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;
\r
461 AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;
\r
463 /* Wait for hardware reset end. */
\r
464 while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )
\r
466 __asm volatile ( "NOP" );
\r
468 __asm volatile ( "NOP" );
\r
470 /* Setup the pins. */
\r
471 AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;
\r
472 AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;
\r
474 /* Enable com between EMAC PHY.
\r
476 Enable management port. */
\r
477 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
479 /* MDC = MCK/32. */
\r
480 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
482 /* Wait for PHY auto init end (rather crude delay!). */
\r
483 vTaskDelay( emacPHY_INIT_DELAY );
\r
485 /* PHY configuration. */
\r
486 #if USE_RMII_INTERFACE != 1
\r
488 unsigned portLONG ulControl;
\r
490 /* PHY has internal pull down : disable MII isolate. */
\r
491 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
492 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
493 ulControl &= ~BMCR_ISOLATE;
\r
494 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );
\r
498 /* Disable management port again. */
\r
499 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
501 #if USE_RMII_INTERFACE != 1
\r
502 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */
\r
503 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;
\r
505 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator
\r
507 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
\r
510 /* End of code supplied by Atmel ------------------------*/
\r
512 /* Setup the buffers and descriptors. */
\r
513 prvSetupDescriptors();
\r
515 /* Load our MAC address into the EMAC. */
\r
516 prvSetupMACAddress();
\r
518 /* Are we connected? */
\r
519 if( prvProbePHY() )
\r
521 /* Enable the interrupt! */
\r
522 portENTER_CRITICAL();
\r
524 prvSetupEMACInterrupt();
\r
525 vPassEMACSemaphore( xSemaphore );
\r
527 portEXIT_CRITICAL();
\r
532 /*-----------------------------------------------------------*/
\r
534 /* See the header file for descriptions of public functions. */
\r
535 void vClearEMACTxBuffer( void )
\r
537 static unsigned portBASE_TYPE uxNextBufferToClear = 0;
\r
539 /* Called on Tx interrupt events to reset the AT91C_TRANSMIT_OK bit in each
\r
540 Tx buffer within the frame just transmitted. This marks all the buffers
\r
541 as available again.
\r
543 The first buffer in the frame should have the bit set automatically. */
\r
544 if( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_TRANSMIT_OK )
\r
546 /* Loop through the other buffers in the frame. */
\r
547 while( !( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_LAST_BUFFER ) )
\r
549 uxNextBufferToClear++;
\r
551 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
553 uxNextBufferToClear = 0;
\r
556 xTxDescriptors[ uxNextBufferToClear ].U_Status.status |= AT91C_TRANSMIT_OK;
\r
560 /* Start with the next buffer the next time a Tx interrupt is called. */
\r
561 uxNextBufferToClear++;
\r
563 /* Do we need to wrap back to the first buffer? */
\r
564 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
566 uxNextBufferToClear = 0;
\r
569 /*-----------------------------------------------------------*/
\r
571 static void prvSetupDescriptors(void)
\r
573 unsigned portBASE_TYPE xIndex;
\r
574 unsigned portLONG ulAddress;
\r
576 /* Initialise xRxDescriptors descriptor. */
\r
577 for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )
\r
579 /* Calculate the address of the nth buffer within the array. */
\r
580 ulAddress = ( unsigned portLONG )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );
\r
582 /* Write the buffer address into the descriptor. The DMA will place
\r
583 the data at this address when this descriptor is being used. Mask off
\r
584 the bottom bits of the address as these have special meaning. */
\r
585 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
588 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
589 to the first buffer. */
\r
590 xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;
\r
592 /* Initialise xTxDescriptors. */
\r
593 for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )
\r
595 /* Calculate the address of the nth buffer within the array. */
\r
596 ulAddress = ( unsigned portLONG )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );
\r
598 /* Write the buffer address into the descriptor. The DMA will read
\r
599 data from here when the descriptor is being used. */
\r
600 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
601 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;
\r
604 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
605 to the first buffer. */
\r
606 xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;
\r
608 /* Tell the EMAC where to find the descriptors. */
\r
609 AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned portLONG ) xRxDescriptors;
\r
610 AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned portLONG ) xTxDescriptors;
\r
612 /* Clear all the bits in the receive status register. */
\r
613 AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );
\r
615 /* Enable the copy of data into the buffers, ignore broadcasts,
\r
616 and don't copy FCS. */
\r
617 AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);
\r
619 /* Enable Rx and Tx, plus the stats register. */
\r
620 AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );
\r
622 /*-----------------------------------------------------------*/
\r
624 static void prvSetupMACAddress( void )
\r
626 /* Must be written SA1L then SA1H. */
\r
627 AT91C_BASE_EMAC->EMAC_SA1L = ( ( unsigned portLONG ) cMACAddress[ 3 ] << 24 ) |
\r
628 ( ( unsigned portLONG ) cMACAddress[ 2 ] << 16 ) |
\r
629 ( ( unsigned portLONG ) cMACAddress[ 1 ] << 8 ) |
\r
632 AT91C_BASE_EMAC->EMAC_SA1H = ( ( unsigned portLONG ) cMACAddress[ 5 ] << 8 ) |
\r
635 /*-----------------------------------------------------------*/
\r
637 static void prvSetupEMACInterrupt( void )
\r
639 /* Create the semaphore used to trigger the EMAC task. */
\r
640 vSemaphoreCreateBinary( xSemaphore );
\r
643 /* We start by 'taking' the semaphore so the ISR can 'give' it when the
\r
644 first interrupt occurs. */
\r
645 xSemaphoreTake( xSemaphore, emacNO_DELAY );
\r
646 portENTER_CRITICAL();
\r
648 /* We want to interrupt on Rx and Tx events. */
\r
649 AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;
\r
651 /* Enable the interrupts in the AIC. */
\r
652 AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR );
\r
653 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;
\r
655 portEXIT_CRITICAL();
\r
664 * The following functions are initialisation functions taken from the Atmel
\r
665 * EMAC sample code.
\r
669 static portBASE_TYPE prvProbePHY( void )
\r
671 unsigned portLONG ulPHYId1, ulPHYId2, ulStatus;
\r
672 portBASE_TYPE xReturn = pdPASS;
\r
674 /* Code supplied by Atmel (reformatted) -----------------*/
\r
676 /* Enable management port */
\r
677 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
678 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
680 /* Read the PHY ID. */
\r
681 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );
\r
682 vReadPHY(AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );
\r
687 Bits 3:0 Revision Number Four bit manufacturer?s revision number.
\r
688 0001 stands for Rev. A, etc.
\r
690 if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )
\r
692 /* Did not expect this ID. */
\r
697 ulStatus = xGetLinkSpeed();
\r
699 if( ulStatus != pdPASS )
\r
705 /* Disable management port */
\r
706 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
708 /* End of code supplied by Atmel ------------------------*/
\r
712 /*-----------------------------------------------------------*/
\r
714 static void vReadPHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG *pulValue )
\r
716 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
718 AT91C_BASE_EMAC->EMAC_MAN = (AT91C_EMAC_SOF & (0x01<<30))
\r
719 | (2 << 16) | (2 << 28)
\r
720 | ((ucPHYAddress & 0x1f) << 23)
\r
721 | (ucAddress << 18);
\r
723 /* Wait until IDLE bit in Network Status register is cleared. */
\r
724 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
729 *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff );
\r
731 /* End of code supplied by Atmel ------------------------*/
\r
733 /*-----------------------------------------------------------*/
\r
735 #if USE_RMII_INTERFACE != 1
\r
736 static void vWritePHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG ulValue )
\r
738 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
740 AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))
\r
741 | (2 << 16) | (1 << 28)
\r
742 | ((ucPHYAddress & 0x1f) << 23)
\r
743 | (ucAddress << 18))
\r
744 | (ulValue & 0xffff);
\r
746 /* Wait until IDLE bit in Network Status register is cleared */
\r
747 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
752 /* End of code supplied by Atmel ------------------------*/
\r
755 /*-----------------------------------------------------------*/
\r
757 static portBASE_TYPE xGetLinkSpeed( void )
\r
759 unsigned portLONG ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;
\r
761 /* Code supplied by Atmel (reformatted) -----------------*/
\r
763 /* Link status is latched, so read twice to get current value */
\r
764 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
765 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
767 if( !( ulBMSR & BMSR_LSTATUS ) )
\r
773 vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);
\r
774 if (ulBMCR & BMCR_ANENABLE)
\r
776 /* AutoNegotiation is enabled. */
\r
777 if (!(ulBMSR & BMSR_ANEGCOMPLETE))
\r
779 /* Auto-negotitation in progress. */
\r
783 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);
\r
784 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )
\r
786 ulSpeed = SPEED_100;
\r
790 ulSpeed = SPEED_10;
\r
793 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )
\r
795 ulDuplex = DUPLEX_FULL;
\r
799 ulDuplex = DUPLEX_HALF;
\r
804 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;
\r
805 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;
\r
808 /* Update the MAC */
\r
809 ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );
\r
810 if( ulSpeed == SPEED_100 )
\r
812 if( ulDuplex == DUPLEX_FULL )
\r
814 /* 100 Full Duplex */
\r
815 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;
\r
819 /* 100 Half Duplex */
\r
820 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;
\r
825 if (ulDuplex == DUPLEX_FULL)
\r
827 /* 10 Full Duplex */
\r
828 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;
\r
831 { /* 10 Half Duplex */
\r
832 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;
\r
836 /* End of code supplied by Atmel ------------------------*/
\r
840 /*-----------------------------------------------------------*/
\r
842 void vEMACWaitForInput( void )
\r
844 /* Just wait until we are signled from an ISR that data is available, or
\r
845 we simply time out. */
\r
846 xSemaphoreTake( xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );
\r