2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* Standard includes. */
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
75 /* Demo application includes. */
\r
76 #include "SAM7_EMAC.h"
\r
81 /* Hardware specific includes. */
\r
84 #include "AT91SAM7X256.h"
\r
87 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0
\r
88 to use an MII interface. */
\r
89 #define USE_RMII_INTERFACE 0
\r
91 /* The buffer addresses written into the descriptors must be aligned so the
\r
92 last few bits are zero. These bits have special meaning for the EMAC
\r
93 peripheral and cannot be used as part of the address. */
\r
94 #define emacADDRESS_MASK ( ( unsigned long ) 0xFFFFFFFC )
\r
96 /* Bit used within the address stored in the descriptor to mark the last
\r
97 descriptor in the array. */
\r
98 #define emacRX_WRAP_BIT ( ( unsigned long ) 0x02 )
\r
100 /* Bit used within the Tx descriptor status to indicate whether the
\r
101 descriptor is under the control of the EMAC or the software. */
\r
102 #define emacTX_BUF_USED ( ( unsigned long ) 0x80000000 )
\r
104 /* A short delay is used to wait for a buffer to become available, should
\r
105 one not be immediately available when trying to transmit a frame. */
\r
106 #define emacBUFFER_WAIT_DELAY ( 2 )
\r
107 #define emacMAX_WAIT_CYCLES ( configTICK_RATE_HZ / 40 )
\r
109 /* Misc defines. */
\r
110 #define emacINTERRUPT_LEVEL ( 5 )
\r
111 #define emacNO_DELAY ( 0 )
\r
112 #define emacTOTAL_FRAME_HEADER_SIZE ( 54 )
\r
113 #define emacPHY_INIT_DELAY ( 5000 / portTICK_RATE_MS )
\r
114 #define emacRESET_KEY ( ( unsigned long ) 0xA5000000 )
\r
115 #define emacRESET_LENGTH ( ( unsigned long ) ( 0x01 << 8 ) )
\r
117 /* The Atmel header file only defines the TX frame length mask. */
\r
118 #define emacRX_LENGTH_FRAME ( 0xfff )
\r
120 /* Peripheral setup for the EMAC. */
\r
121 #define emacPERIPHERAL_A_SETUP ( ( unsigned long ) AT91C_PB2_ETX0 ) | \
\r
122 ( ( unsigned long ) AT91C_PB12_ETXER ) | \
\r
123 ( ( unsigned long ) AT91C_PB16_ECOL ) | \
\r
124 ( ( unsigned long ) AT91C_PB11_ETX3 ) | \
\r
125 ( ( unsigned long ) AT91C_PB6_ERX1 ) | \
\r
126 ( ( unsigned long ) AT91C_PB15_ERXDV ) | \
\r
127 ( ( unsigned long ) AT91C_PB13_ERX2 ) | \
\r
128 ( ( unsigned long ) AT91C_PB3_ETX1 ) | \
\r
129 ( ( unsigned long ) AT91C_PB8_EMDC ) | \
\r
130 ( ( unsigned long ) AT91C_PB5_ERX0 ) | \
\r
131 ( ( unsigned long ) AT91C_PB14_ERX3 ) | \
\r
132 ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV ) | \
\r
133 ( ( unsigned long ) AT91C_PB1_ETXEN ) | \
\r
134 ( ( unsigned long ) AT91C_PB10_ETX2 ) | \
\r
135 ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK ) | \
\r
136 ( ( unsigned long ) AT91C_PB9_EMDIO ) | \
\r
137 ( ( unsigned long ) AT91C_PB7_ERXER ) | \
\r
138 ( ( unsigned long ) AT91C_PB17_ERXCK );
\r
140 /*-----------------------------------------------------------*/
\r
143 * Prototype for the EMAC interrupt function - called by the asm wrapper.
\r
145 extern void vEMACISR_Wrapper( void ) __attribute__((naked));
\r
148 * Initialise both the Tx and Rx descriptors used by the EMAC.
\r
150 static void prvSetupDescriptors(void);
\r
153 * Write our MAC address into the EMAC. The MAC address is set as one of the
\r
156 static void prvSetupMACAddress( void );
\r
159 * Configure the EMAC and AIC for EMAC interrupts.
\r
161 static void prvSetupEMACInterrupt( void );
\r
164 * Some initialisation functions taken from the Atmel EMAC sample code.
\r
166 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );
\r
167 #if USE_RMII_INTERFACE != 1
\r
168 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);
\r
170 static portBASE_TYPE xGetLinkSpeed( void );
\r
171 static portBASE_TYPE prvProbePHY( void );
\r
173 /*-----------------------------------------------------------*/
\r
175 /* Buffer written to by the EMAC DMA. Must be aligned as described by the
\r
176 comment above the emacADDRESS_MASK definition. */
\r
177 #pragma data_alignment=8
\r
178 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ];
\r
180 /* Buffer read by the EMAC DMA. Must be aligned as described by he comment
\r
181 above the emacADDRESS_MASK definition. */
\r
182 #pragma data_alignment=8
\r
183 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ];
\r
185 /* Descriptors used to communicate between the program and the EMAC peripheral.
\r
186 These descriptors hold the locations and state of the Rx and Tx buffers. */
\r
187 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];
\r
188 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];
\r
190 /* The IP and Ethernet addresses are read from the uIP setup. */
\r
191 const char cMACAddress[ 6 ] = { uipMAC_ADDR0, uipMAC_ADDR1, uipMAC_ADDR2, uipMAC_ADDR3, uipMAC_ADDR4, uipMAC_ADDR5 };
\r
192 const unsigned char ucIPAddress[ 4 ] = { uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 };
\r
194 /* The semaphore used by the EMAC ISR to wake the EMAC task. */
\r
195 static xSemaphoreHandle xSemaphore = NULL;
\r
197 /*-----------------------------------------------------------*/
\r
199 xSemaphoreHandle xEMACInit( void )
\r
201 /* Code supplied by Atmel -------------------------------*/
\r
203 /* Disable pull up on RXDV => PHY normal mode (not in test mode),
\r
204 PHY has internal pull down. */
\r
205 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;
\r
207 #if USE_RMII_INTERFACE != 1
\r
208 /* PHY has internal pull down : set MII mode. */
\r
209 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;
\r
212 /* Clear PB18 <=> PHY powerdown. */
\r
213 AT91C_BASE_PIOB->PIO_PER = 1 << 18;
\r
214 AT91C_BASE_PIOB->PIO_OER = 1 << 18;
\r
215 AT91C_BASE_PIOB->PIO_CODR = 1 << 18;
\r
217 /* After PHY power up, hardware reset. */
\r
218 AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;
\r
219 AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;
\r
221 /* Wait for hardware reset end. */
\r
222 while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )
\r
224 __asm volatile ( "NOP" );
\r
226 __asm volatile ( "NOP" );
\r
228 /* Setup the pins. */
\r
229 AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;
\r
230 AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;
\r
232 /* Enable com between EMAC PHY.
\r
234 Enable management port. */
\r
235 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
237 /* MDC = MCK/32. */
\r
238 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
240 /* Wait for PHY auto init end (rather crude delay!). */
\r
241 vTaskDelay( emacPHY_INIT_DELAY );
\r
243 /* PHY configuration. */
\r
244 #if USE_RMII_INTERFACE != 1
\r
246 unsigned long ulControl;
\r
248 /* PHY has internal pull down : disable MII isolate. */
\r
249 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
250 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
251 ulControl &= ~BMCR_ISOLATE;
\r
252 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );
\r
256 /* Disable management port again. */
\r
257 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
259 #if USE_RMII_INTERFACE != 1
\r
260 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */
\r
261 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;
\r
263 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator
\r
265 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
\r
268 /* End of code supplied by Atmel ------------------------*/
\r
270 /* Setup the buffers and descriptors. */
\r
271 prvSetupDescriptors();
\r
273 /* Load our MAC address into the EMAC. */
\r
274 prvSetupMACAddress();
\r
276 /* Are we connected? */
\r
277 if( prvProbePHY() )
\r
279 /* Enable the interrupt! */
\r
280 portENTER_CRITICAL();
\r
282 prvSetupEMACInterrupt();
\r
283 vPassEMACSemaphore( xSemaphore );
\r
285 portEXIT_CRITICAL();
\r
290 /*-----------------------------------------------------------*/
\r
292 long lEMACSend( void )
\r
294 static unsigned portBASE_TYPE uxTxBufferIndex = 0;
\r
295 portBASE_TYPE xWaitCycles = 0;
\r
296 long lReturn = pdPASS;
\r
299 /* Is a buffer available? */
\r
300 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )
\r
302 /* There is no room to write the Tx data to the Tx buffer. Wait a
\r
303 short while, then try again. */
\r
305 if( xWaitCycles > emacMAX_WAIT_CYCLES )
\r
313 vTaskDelay( emacBUFFER_WAIT_DELAY );
\r
317 /* lReturn will only be pdPASS if a buffer is available. */
\r
318 if( lReturn == pdPASS )
\r
320 /* Copy the headers into the Tx buffer. These will be in the uIP buffer. */
\r
321 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;
\r
322 memcpy( ( void * ) pcBuffer, ( void * ) uip_buf, emacTOTAL_FRAME_HEADER_SIZE );
\r
324 /* If there is room, also copy in the application data if any. */
\r
325 if( ( uip_len > emacTOTAL_FRAME_HEADER_SIZE ) && ( uip_len <= ( ETH_TX_BUFFER_SIZE - emacTOTAL_FRAME_HEADER_SIZE ) ) )
\r
327 memcpy( ( void * ) &( pcBuffer[ emacTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - emacTOTAL_FRAME_HEADER_SIZE ) );
\r
331 portENTER_CRITICAL();
\r
333 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )
\r
335 /* Fill out the necessary in the descriptor to get the data sent. */
\r
336 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
337 | AT91C_LAST_BUFFER
\r
338 | AT91C_TRANSMIT_WRAP;
\r
339 uxTxBufferIndex = 0;
\r
343 /* Fill out the necessary in the descriptor to get the data sent. */
\r
344 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
345 | AT91C_LAST_BUFFER;
\r
349 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
\r
351 portEXIT_CRITICAL();
\r
356 /*-----------------------------------------------------------*/
\r
358 unsigned long ulEMACPoll( void )
\r
360 static unsigned portBASE_TYPE ulNextRxBuffer = 0;
\r
361 unsigned long ulSectionLength = 0, ulLengthSoFar = 0, ulEOF = pdFALSE;
\r
364 /* Skip any fragments. */
\r
365 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )
\r
367 /* Mark the buffer as free again. */
\r
368 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
370 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
372 ulNextRxBuffer = 0;
\r
376 /* Is there a packet ready? */
\r
378 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !ulSectionLength )
\r
380 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
381 ulSectionLength = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & emacRX_LENGTH_FRAME;
\r
383 if( ulSectionLength == 0 )
\r
385 /* The frame is longer than the buffer pointed to by this
\r
386 descriptor so copy the entire buffer to uIP - then move onto
\r
387 the next descriptor to get the rest of the frame. */
\r
388 if( ( ulLengthSoFar + ETH_RX_BUFFER_SIZE ) <= UIP_BUFSIZE )
\r
390 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ETH_RX_BUFFER_SIZE );
\r
391 ulLengthSoFar += ETH_RX_BUFFER_SIZE;
\r
396 /* This is the last section of the frame. Copy the section to
\r
398 if( ulSectionLength < UIP_BUFSIZE )
\r
400 /* The section length holds the length of the entire frame.
\r
401 ulLengthSoFar holds the length of the frame sections already
\r
402 copied to uIP, so the length of the final section is
\r
403 ulSectionLength - ulLengthSoFar; */
\r
404 if( ulSectionLength > ulLengthSoFar )
\r
406 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ( ulSectionLength - ulLengthSoFar ) );
\r
410 /* Is this the last buffer for the frame? If not why? */
\r
411 ulEOF = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_EOF;
\r
414 /* Mark the buffer as free again. */
\r
415 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
417 /* Increment to the next buffer, wrapping if necessary. */
\r
419 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
421 ulNextRxBuffer = 0;
\r
425 /* If we obtained data but for some reason did not find the end of the
\r
426 frame then discard the data as it must contain an error. */
\r
429 ulSectionLength = 0;
\r
432 return ulSectionLength;
\r
434 /*-----------------------------------------------------------*/
\r
436 static void prvSetupDescriptors(void)
\r
438 unsigned portBASE_TYPE xIndex;
\r
439 unsigned long ulAddress;
\r
441 /* Initialise xRxDescriptors descriptor. */
\r
442 for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )
\r
444 /* Calculate the address of the nth buffer within the array. */
\r
445 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );
\r
447 /* Write the buffer address into the descriptor. The DMA will place
\r
448 the data at this address when this descriptor is being used. Mask off
\r
449 the bottom bits of the address as these have special meaning. */
\r
450 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
453 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
454 to the first buffer. */
\r
455 xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;
\r
457 /* Initialise xTxDescriptors. */
\r
458 for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )
\r
460 /* Calculate the address of the nth buffer within the array. */
\r
461 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );
\r
463 /* Write the buffer address into the descriptor. The DMA will read
\r
464 data from here when the descriptor is being used. */
\r
465 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
466 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;
\r
469 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
470 to the first buffer. */
\r
471 xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;
\r
473 /* Tell the EMAC where to find the descriptors. */
\r
474 AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;
\r
475 AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;
\r
477 /* Clear all the bits in the receive status register. */
\r
478 AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );
\r
480 /* Enable the copy of data into the buffers, ignore broadcasts,
\r
481 and don't copy FCS. */
\r
482 AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);
\r
484 /* Enable Rx and Tx, plus the stats register. */
\r
485 AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );
\r
487 /*-----------------------------------------------------------*/
\r
489 static void prvSetupMACAddress( void )
\r
491 /* Must be written SA1L then SA1H. */
\r
492 AT91C_BASE_EMAC->EMAC_SA1L = ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |
\r
493 ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |
\r
494 ( ( unsigned long ) cMACAddress[ 1 ] << 8 ) |
\r
497 AT91C_BASE_EMAC->EMAC_SA1H = ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |
\r
500 /*-----------------------------------------------------------*/
\r
502 static void prvSetupEMACInterrupt( void )
\r
504 /* Create the semaphore used to trigger the EMAC task. */
\r
505 vSemaphoreCreateBinary( xSemaphore );
\r
508 /* We start by 'taking' the semaphore so the ISR can 'give' it when the
\r
509 first interrupt occurs. */
\r
510 xSemaphoreTake( xSemaphore, emacNO_DELAY );
\r
511 portENTER_CRITICAL();
\r
513 /* We want to interrupt on Rx events. */
\r
514 AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP;
\r
516 /* Enable the interrupts in the AIC. */
\r
517 AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );
\r
518 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;
\r
520 portEXIT_CRITICAL();
\r
523 /*-----------------------------------------------------------*/
\r
529 * The following functions are initialisation functions taken from the Atmel
\r
530 * EMAC sample code.
\r
533 static portBASE_TYPE prvProbePHY( void )
\r
535 unsigned long ulPHYId1, ulPHYId2, ulStatus;
\r
536 portBASE_TYPE xReturn = pdPASS;
\r
538 /* Code supplied by Atmel (reformatted) -----------------*/
\r
540 /* Enable management port */
\r
541 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
542 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
544 /* Read the PHY ID. */
\r
545 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );
\r
546 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );
\r
551 Bits 3:0 Revision Number Four bit manufacturer
\92s revision number.
\r
552 0001 stands for Rev. A, etc.
\r
554 if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )
\r
556 /* Did not expect this ID. */
\r
561 ulStatus = xGetLinkSpeed();
\r
563 if( ulStatus != pdPASS )
\r
569 /* Disable management port */
\r
570 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
572 /* End of code supplied by Atmel ------------------------*/
\r
576 /*-----------------------------------------------------------*/
\r
578 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )
\r
580 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
582 AT91C_BASE_EMAC->EMAC_MAN = (AT91C_EMAC_SOF & (0x01<<30))
\r
583 | (2 << 16) | (2 << 28)
\r
584 | ((ucPHYAddress & 0x1f) << 23)
\r
585 | (ucAddress << 18);
\r
587 /* Wait until IDLE bit in Network Status register is cleared. */
\r
588 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
593 *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff );
\r
595 /* End of code supplied by Atmel ------------------------*/
\r
597 /*-----------------------------------------------------------*/
\r
599 #if USE_RMII_INTERFACE != 1
\r
600 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )
\r
602 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
604 AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))
\r
605 | (2 << 16) | (1 << 28)
\r
606 | ((ucPHYAddress & 0x1f) << 23)
\r
607 | (ucAddress << 18))
\r
608 | (ulValue & 0xffff);
\r
610 /* Wait until IDLE bit in Network Status register is cleared */
\r
611 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
616 /* End of code supplied by Atmel ------------------------*/
\r
619 /*-----------------------------------------------------------*/
\r
621 static portBASE_TYPE xGetLinkSpeed( void )
\r
623 unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;
\r
625 /* Code supplied by Atmel (reformatted) -----------------*/
\r
627 /* Link status is latched, so read twice to get current value */
\r
628 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
629 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
631 if( !( ulBMSR & BMSR_LSTATUS ) )
\r
637 vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);
\r
638 if (ulBMCR & BMCR_ANENABLE)
\r
640 /* AutoNegotiation is enabled. */
\r
641 if (!(ulBMSR & BMSR_ANEGCOMPLETE))
\r
643 /* Auto-negotiation in progress. */
\r
647 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);
\r
648 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )
\r
650 ulSpeed = SPEED_100;
\r
654 ulSpeed = SPEED_10;
\r
657 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )
\r
659 ulDuplex = DUPLEX_FULL;
\r
663 ulDuplex = DUPLEX_HALF;
\r
668 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;
\r
669 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;
\r
672 /* Update the MAC */
\r
673 ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );
\r
674 if( ulSpeed == SPEED_100 )
\r
676 if( ulDuplex == DUPLEX_FULL )
\r
678 /* 100 Full Duplex */
\r
679 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;
\r
683 /* 100 Half Duplex */
\r
684 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;
\r
689 if (ulDuplex == DUPLEX_FULL)
\r
691 /* 10 Full Duplex */
\r
692 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;
\r
696 /* 10 Half Duplex */
\r
697 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;
\r
701 /* End of code supplied by Atmel ------------------------*/
\r