2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Interrupt driven driver for the EMAC peripheral. This driver is not
\r
31 * reentrant, re-entrancy is handled by a semaphore at the network interface
\r
39 + Corrected the byte order when writing the MAC address to the MAC.
\r
40 + Support added for MII interfaces. Previously only RMII was supported.
\r
44 + The MII interface is now the default.
\r
45 + Modified the initialisation sequence slightly to allow auto init more
\r
50 + Made the function vClearEMACTxBuffer() more robust by moving the index
\r
51 manipulation into the if() statement. This allows the tx interrupt to
\r
52 execute even when there is no data to handle.
\r
56 + Corrected the Rx frame length mask when obtaining the length from the
\r
61 /* Standard includes. */
\r
64 /* Scheduler includes. */
\r
65 #include "FreeRTOS.h"
\r
69 /* Demo app includes. */
\r
70 #include "SAM7_EMAC.h"
\r
72 /* Hardware specific includes. */
\r
75 #include "AT91SAM7X256.h"
\r
78 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0
\r
79 to use an MII interface. */
\r
80 #define USE_RMII_INTERFACE 0
\r
83 /* The buffer addresses written into the descriptors must be aligned so the
\r
84 last few bits are zero. These bits have special meaning for the EMAC
\r
85 peripheral and cannot be used as part of the address. */
\r
86 #define emacADDRESS_MASK ( ( unsigned long ) 0xFFFFFFFC )
\r
88 /* Bit used within the address stored in the descriptor to mark the last
\r
89 descriptor in the array. */
\r
90 #define emacRX_WRAP_BIT ( ( unsigned long ) 0x02 )
\r
92 /* Bit used within the Tx descriptor status to indicate whether the
\r
93 descriptor is under the control of the EMAC or the software. */
\r
94 #define emacTX_BUF_USED ( ( unsigned long ) 0x80000000 )
\r
96 /* A short delay is used to wait for a buffer to become available, should
\r
97 one not be immediately available when trying to transmit a frame. */
\r
98 #define emacBUFFER_WAIT_DELAY ( 2 )
\r
99 #define emacMAX_WAIT_CYCLES ( ( portBASE_TYPE ) ( configTICK_RATE_HZ / 40 ) )
\r
101 /* The time to block waiting for input. */
\r
102 #define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( TickType_t ) 100 )
\r
104 /* Peripheral setup for the EMAC. */
\r
105 #define emacPERIPHERAL_A_SETUP ( ( unsigned long ) AT91C_PB2_ETX0 ) | \
\r
106 ( ( unsigned long ) AT91C_PB12_ETXER ) | \
\r
107 ( ( unsigned long ) AT91C_PB16_ECOL ) | \
\r
108 ( ( unsigned long ) AT91C_PB11_ETX3 ) | \
\r
109 ( ( unsigned long ) AT91C_PB6_ERX1 ) | \
\r
110 ( ( unsigned long ) AT91C_PB15_ERXDV ) | \
\r
111 ( ( unsigned long ) AT91C_PB13_ERX2 ) | \
\r
112 ( ( unsigned long ) AT91C_PB3_ETX1 ) | \
\r
113 ( ( unsigned long ) AT91C_PB8_EMDC ) | \
\r
114 ( ( unsigned long ) AT91C_PB5_ERX0 ) | \
\r
115 ( ( unsigned long ) AT91C_PB14_ERX3 ) | \
\r
116 ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV ) | \
\r
117 ( ( unsigned long ) AT91C_PB1_ETXEN ) | \
\r
118 ( ( unsigned long ) AT91C_PB10_ETX2 ) | \
\r
119 ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK ) | \
\r
120 ( ( unsigned long ) AT91C_PB9_EMDIO ) | \
\r
121 ( ( unsigned long ) AT91C_PB7_ERXER ) | \
\r
122 ( ( unsigned long ) AT91C_PB17_ERXCK );
\r
124 /* Misc defines. */
\r
125 #define emacINTERRUPT_LEVEL ( 5 )
\r
126 #define emacNO_DELAY ( 0 )
\r
127 #define emacTOTAL_FRAME_HEADER_SIZE ( 54 )
\r
128 #define emacPHY_INIT_DELAY ( 5000 / portTICK_PERIOD_MS )
\r
129 #define emacRESET_KEY ( ( unsigned long ) 0xA5000000 )
\r
130 #define emacRESET_LENGTH ( ( unsigned long ) ( 0x01 << 8 ) )
\r
132 /* The Atmel header file only defines the TX frame length mask. */
\r
133 #define emacRX_LENGTH_FRAME ( 0xfff )
\r
135 /*-----------------------------------------------------------*/
\r
137 /* Buffer written to by the EMAC DMA. Must be aligned as described by the
\r
138 comment above the emacADDRESS_MASK definition. */
\r
139 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
141 /* Buffer read by the EMAC DMA. Must be aligned as described by the comment
\r
142 above the emacADDRESS_MASK definition. */
\r
143 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
145 /* Descriptors used to communicate between the program and the EMAC peripheral.
\r
146 These descriptors hold the locations and state of the Rx and Tx buffers. */
\r
147 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];
\r
148 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];
\r
150 /* The IP and Ethernet addresses are read from the header files. */
\r
151 const char cMACAddress[ 6 ] = { emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };
\r
152 const unsigned char ucIPAddress[ 4 ] = { emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 };
\r
154 /*-----------------------------------------------------------*/
\r
156 /* See the header file for descriptions of public functions. */
\r
159 * Prototype for the EMAC interrupt function.
\r
161 void vEMACISR_Wrapper( void ) __attribute__ ((naked));
\r
164 * Initialise both the Tx and Rx descriptors used by the EMAC.
\r
166 static void prvSetupDescriptors(void);
\r
169 * Write our MAC address into the EMAC.
\r
171 static void prvSetupMACAddress( void );
\r
174 * Configure the EMAC and AIC for EMAC interrupts.
\r
176 static void prvSetupEMACInterrupt( void );
\r
179 * Some initialisation functions taken from the Atmel EMAC sample code.
\r
181 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );
\r
182 static portBASE_TYPE xGetLinkSpeed( void );
\r
183 static portBASE_TYPE prvProbePHY( void );
\r
184 #if USE_RMII_INTERFACE != 1
\r
185 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);
\r
189 /* The semaphore used by the EMAC ISR to wake the EMAC task. */
\r
190 static SemaphoreHandle_t xSemaphore = NULL;
\r
192 /* Holds the index to the next buffer from which data will be read. */
\r
193 static volatile unsigned long ulNextRxBuffer = 0;
\r
195 /*-----------------------------------------------------------*/
\r
197 /* See the header file for descriptions of public functions. */
\r
198 long lEMACSend( char *pcFrom, unsigned long ulLength, long lEndOfFrame )
\r
200 static unsigned portBASE_TYPE uxTxBufferIndex = 0;
\r
201 portBASE_TYPE xWaitCycles = 0;
\r
202 long lReturn = pdPASS;
\r
204 unsigned long ulLastBuffer, ulDataBuffered = 0, ulDataRemainingToSend, ulLengthToSend;
\r
206 /* If the length of data to be transmitted is greater than each individual
\r
207 transmit buffer then the data will be split into more than one buffer.
\r
208 Loop until the entire length has been buffered. */
\r
209 while( ulDataBuffered < ulLength )
\r
211 /* Is a buffer available? */
\r
212 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )
\r
214 /* There is no room to write the Tx data to the Tx buffer. Wait a
\r
215 short while, then try again. */
\r
217 if( xWaitCycles > emacMAX_WAIT_CYCLES )
\r
225 vTaskDelay( emacBUFFER_WAIT_DELAY );
\r
229 /* lReturn will only be pdPASS if a buffer is available. */
\r
230 if( lReturn == pdPASS )
\r
232 portENTER_CRITICAL();
\r
234 /* Get the address of the buffer from the descriptor, then copy
\r
235 the data into the buffer. */
\r
236 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;
\r
238 /* How much can we write to the buffer? */
\r
239 ulDataRemainingToSend = ulLength - ulDataBuffered;
\r
240 if( ulDataRemainingToSend <= ETH_TX_BUFFER_SIZE )
\r
242 /* We can write all the remaining bytes. */
\r
243 ulLengthToSend = ulDataRemainingToSend;
\r
247 /* We can not write more than ETH_TX_BUFFER_SIZE in one go. */
\r
248 ulLengthToSend = ETH_TX_BUFFER_SIZE;
\r
251 /* Copy the data into the buffer. */
\r
252 memcpy( ( void * ) pcBuffer, ( void * ) &( pcFrom[ ulDataBuffered ] ), ulLengthToSend );
\r
253 ulDataBuffered += ulLengthToSend;
\r
255 /* Is this the last data for the frame? */
\r
256 if( lEndOfFrame && ( ulDataBuffered >= ulLength ) )
\r
258 /* No more data remains for this frame so we can start the
\r
260 ulLastBuffer = AT91C_LAST_BUFFER;
\r
264 /* More data to come for this frame. */
\r
268 /* Fill out the necessary in the descriptor to get the data sent,
\r
269 then move to the next descriptor, wrapping if necessary. */
\r
270 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )
\r
272 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
274 | AT91C_TRANSMIT_WRAP;
\r
275 uxTxBufferIndex = 0;
\r
279 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
284 /* If this is the last buffer to be sent for this frame we can
\r
285 start the transmission. */
\r
288 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
\r
291 portEXIT_CRITICAL();
\r
301 /*-----------------------------------------------------------*/
\r
303 /* See the header file for descriptions of public functions. */
\r
304 unsigned long ulEMACInputLength( void )
\r
306 register unsigned long ulIndex, ulLength = 0;
\r
308 /* Skip any fragments. We are looking for the first buffer that contains
\r
309 data and has the SOF (start of frame) bit set. */
\r
310 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )
\r
312 /* Ignoring this buffer. Mark it as free again. */
\r
313 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
315 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
317 ulNextRxBuffer = 0;
\r
321 /* We are going to walk through the descriptors that make up this frame,
\r
322 but don't want to alter ulNextRxBuffer as this would prevent vEMACRead()
\r
323 from finding the data. Therefore use a copy of ulNextRxBuffer instead. */
\r
324 ulIndex = ulNextRxBuffer;
\r
326 /* Walk through the descriptors until we find the last buffer for this
\r
327 frame. The last buffer will give us the length of the entire frame. */
\r
328 while( ( xRxDescriptors[ ulIndex ].addr & AT91C_OWNERSHIP_BIT ) && !ulLength )
\r
330 ulLength = xRxDescriptors[ ulIndex ].U_Status.status & emacRX_LENGTH_FRAME;
\r
332 /* Increment to the next buffer, wrapping if necessary. */
\r
334 if( ulIndex >= NB_RX_BUFFERS )
\r
342 /*-----------------------------------------------------------*/
\r
344 /* See the header file for descriptions of public functions. */
\r
345 void vEMACRead( char *pcTo, unsigned long ulSectionLength, unsigned long ulTotalFrameLength )
\r
347 static unsigned long ulSectionBytesReadSoFar = 0, ulBufferPosition = 0, ulFameBytesReadSoFar = 0;
\r
348 static char *pcSource;
\r
349 register unsigned long ulBytesRemainingInBuffer, ulRemainingSectionBytes;
\r
351 /* Read ulSectionLength bytes from the Rx buffers. This is not necessarily any
\r
352 correspondence between the length of our Rx buffers, and the length of the
\r
353 data we are returning or the length of the data being requested. Therefore,
\r
354 between calls we have to remember not only which buffer we are currently
\r
355 processing, but our position within that buffer. This would be greatly
\r
356 simplified if PBUF_POOL_BUFSIZE could be guaranteed to be greater than
\r
357 the size of each Rx buffer, and that memory fragmentation did not occur.
\r
359 This function should only be called after a call to ulEMACInputLength().
\r
360 This will ensure ulNextRxBuffer is set to the correct buffer. */
\r
364 /* vEMACRead is called with pcTo set to NULL to indicate that we are about
\r
365 to read a new frame. Any fragments remaining in the frame we were
\r
366 processing during the last call should be dropped. */
\r
369 /* How many bytes are indicated as being in this buffer? If none then
\r
370 the buffer is completely full and the frame is contained within more
\r
371 than one buffer. */
\r
373 /* Reset our state variables ready for the next read from this buffer. */
\r
374 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
375 ulFameBytesReadSoFar = ( unsigned long ) 0;
\r
376 ulBufferPosition = ( unsigned long ) 0;
\r
380 /* Loop until we have obtained the required amount of data. */
\r
381 ulSectionBytesReadSoFar = 0;
\r
382 while( ulSectionBytesReadSoFar < ulSectionLength )
\r
384 /* We may have already read some data from this buffer. How much
\r
385 data remains in the buffer? */
\r
386 ulBytesRemainingInBuffer = ( ETH_RX_BUFFER_SIZE - ulBufferPosition );
\r
388 /* How many more bytes do we need to read before we have the
\r
389 required amount of data? */
\r
390 ulRemainingSectionBytes = ulSectionLength - ulSectionBytesReadSoFar;
\r
392 /* Do we want more data than remains in the buffer? */
\r
393 if( ulRemainingSectionBytes > ulBytesRemainingInBuffer )
\r
395 /* We want more data than remains in the buffer so we can
\r
396 write the remains of the buffer to the destination, then move
\r
397 onto the next buffer to get the rest. */
\r
398 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulBytesRemainingInBuffer );
\r
399 ulSectionBytesReadSoFar += ulBytesRemainingInBuffer;
\r
400 ulFameBytesReadSoFar += ulBytesRemainingInBuffer;
\r
402 /* Mark the buffer as free again. */
\r
403 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
405 /* Move onto the next buffer. */
\r
407 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
409 ulNextRxBuffer = ( unsigned long ) 0;
\r
412 /* Reset the variables for the new buffer. */
\r
413 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
414 ulBufferPosition = ( unsigned long ) 0;
\r
418 /* We have enough data in this buffer to send back. Read out
\r
419 enough data and remember how far we read up to. */
\r
420 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulRemainingSectionBytes );
\r
422 /* There may be more data in this buffer yet. Increment our
\r
423 position in this buffer past the data we have just read. */
\r
424 ulBufferPosition += ulRemainingSectionBytes;
\r
425 ulSectionBytesReadSoFar += ulRemainingSectionBytes;
\r
426 ulFameBytesReadSoFar += ulRemainingSectionBytes;
\r
428 /* Have we now finished with this buffer? */
\r
429 if( ( ulBufferPosition >= ETH_RX_BUFFER_SIZE ) || ( ulFameBytesReadSoFar >= ulTotalFrameLength ) )
\r
431 /* Mark the buffer as free again. */
\r
432 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
434 /* Move onto the next buffer. */
\r
436 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
438 ulNextRxBuffer = 0;
\r
441 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
442 ulBufferPosition = 0;
\r
448 /*-----------------------------------------------------------*/
\r
450 /* See the header file for descriptions of public functions. */
\r
451 SemaphoreHandle_t xEMACInit( void )
\r
453 /* Code supplied by Atmel -------------------------------*/
\r
455 /* Disable pull up on RXDV => PHY normal mode (not in test mode),
\r
456 PHY has internal pull down. */
\r
457 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;
\r
459 #if USE_RMII_INTERFACE != 1
\r
460 /* PHY has internal pull down : set MII mode. */
\r
461 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;
\r
464 /* Clear PB18 <=> PHY powerdown. */
\r
465 AT91C_BASE_PIOB->PIO_PER = 1 << 18;
\r
466 AT91C_BASE_PIOB->PIO_OER = 1 << 18;
\r
467 AT91C_BASE_PIOB->PIO_CODR = 1 << 18;
\r
469 /* After PHY power up, hardware reset. */
\r
470 AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;
\r
471 AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;
\r
473 /* Wait for hardware reset end. */
\r
474 while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )
\r
476 __asm volatile ( "NOP" );
\r
478 __asm volatile ( "NOP" );
\r
480 /* Setup the pins. */
\r
481 AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;
\r
482 AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;
\r
484 /* Enable com between EMAC PHY.
\r
486 Enable management port. */
\r
487 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
489 /* MDC = MCK/32. */
\r
490 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
492 /* Wait for PHY auto init end (rather crude delay!). */
\r
493 vTaskDelay( emacPHY_INIT_DELAY );
\r
495 /* PHY configuration. */
\r
496 #if USE_RMII_INTERFACE != 1
\r
498 unsigned long ulControl;
\r
500 /* PHY has internal pull down : disable MII isolate. */
\r
501 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
502 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
503 ulControl &= ~BMCR_ISOLATE;
\r
504 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );
\r
508 /* Disable management port again. */
\r
509 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
511 #if USE_RMII_INTERFACE != 1
\r
512 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */
\r
513 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;
\r
515 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator
\r
517 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
\r
520 /* End of code supplied by Atmel ------------------------*/
\r
522 /* Setup the buffers and descriptors. */
\r
523 prvSetupDescriptors();
\r
525 /* Load our MAC address into the EMAC. */
\r
526 prvSetupMACAddress();
\r
528 /* Are we connected? */
\r
529 if( prvProbePHY() )
\r
531 /* Enable the interrupt! */
\r
532 portENTER_CRITICAL();
\r
534 prvSetupEMACInterrupt();
\r
535 vPassEMACSemaphore( xSemaphore );
\r
537 portEXIT_CRITICAL();
\r
542 /*-----------------------------------------------------------*/
\r
544 /* See the header file for descriptions of public functions. */
\r
545 void vClearEMACTxBuffer( void )
\r
547 static unsigned portBASE_TYPE uxNextBufferToClear = 0;
\r
549 /* Called on Tx interrupt events to reset the AT91C_TRANSMIT_OK bit in each
\r
550 Tx buffer within the frame just transmitted. This marks all the buffers
\r
551 as available again.
\r
553 The first buffer in the frame should have the bit set automatically. */
\r
554 if( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_TRANSMIT_OK )
\r
556 /* Loop through the other buffers in the frame. */
\r
557 while( !( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_LAST_BUFFER ) )
\r
559 uxNextBufferToClear++;
\r
561 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
563 uxNextBufferToClear = 0;
\r
566 xTxDescriptors[ uxNextBufferToClear ].U_Status.status |= AT91C_TRANSMIT_OK;
\r
569 /* Start with the next buffer the next time a Tx interrupt is called. */
\r
570 uxNextBufferToClear++;
\r
572 /* Do we need to wrap back to the first buffer? */
\r
573 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
575 uxNextBufferToClear = 0;
\r
579 /*-----------------------------------------------------------*/
\r
581 static void prvSetupDescriptors(void)
\r
583 unsigned portBASE_TYPE xIndex;
\r
584 unsigned long ulAddress;
\r
586 /* Initialise xRxDescriptors descriptor. */
\r
587 for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )
\r
589 /* Calculate the address of the nth buffer within the array. */
\r
590 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );
\r
592 /* Write the buffer address into the descriptor. The DMA will place
\r
593 the data at this address when this descriptor is being used. Mask off
\r
594 the bottom bits of the address as these have special meaning. */
\r
595 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
598 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
599 to the first buffer. */
\r
600 xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;
\r
602 /* Initialise xTxDescriptors. */
\r
603 for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )
\r
605 /* Calculate the address of the nth buffer within the array. */
\r
606 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );
\r
608 /* Write the buffer address into the descriptor. The DMA will read
\r
609 data from here when the descriptor is being used. */
\r
610 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
611 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;
\r
614 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
615 to the first buffer. */
\r
616 xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;
\r
618 /* Tell the EMAC where to find the descriptors. */
\r
619 AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;
\r
620 AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;
\r
622 /* Clear all the bits in the receive status register. */
\r
623 AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );
\r
625 /* Enable the copy of data into the buffers, ignore broadcasts,
\r
626 and don't copy FCS. */
\r
627 AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);
\r
629 /* Enable Rx and Tx, plus the stats register. */
\r
630 AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );
\r
632 /*-----------------------------------------------------------*/
\r
634 static void prvSetupMACAddress( void )
\r
636 /* Must be written SA1L then SA1H. */
\r
637 AT91C_BASE_EMAC->EMAC_SA1L = ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |
\r
638 ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |
\r
639 ( ( unsigned long ) cMACAddress[ 1 ] << 8 ) |
\r
642 AT91C_BASE_EMAC->EMAC_SA1H = ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |
\r
645 /*-----------------------------------------------------------*/
\r
647 static void prvSetupEMACInterrupt( void )
\r
649 /* Create the semaphore used to trigger the EMAC task. */
\r
650 vSemaphoreCreateBinary( xSemaphore );
\r
653 /* We start by 'taking' the semaphore so the ISR can 'give' it when the
\r
654 first interrupt occurs. */
\r
655 xSemaphoreTake( xSemaphore, emacNO_DELAY );
\r
656 portENTER_CRITICAL();
\r
658 /* We want to interrupt on Rx and Tx events. */
\r
659 AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;
\r
661 /* Enable the interrupts in the AIC. */
\r
662 AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );
\r
663 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;
\r
665 portEXIT_CRITICAL();
\r
674 * The following functions are initialisation functions taken from the Atmel
\r
675 * EMAC sample code.
\r
679 static portBASE_TYPE prvProbePHY( void )
\r
681 unsigned long ulPHYId1, ulPHYId2, ulStatus;
\r
682 portBASE_TYPE xReturn = pdPASS;
\r
684 /* Code supplied by Atmel (reformatted) -----------------*/
\r
686 /* Enable management port */
\r
687 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
688 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
690 /* Read the PHY ID. */
\r
691 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );
\r
692 vReadPHY(AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );
\r
697 Bits 3:0 Revision Number Four bit manufacturer?s revision number.
\r
698 0001 stands for Rev. A, etc.
\r
700 if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )
\r
702 /* Did not expect this ID. */
\r
707 ulStatus = xGetLinkSpeed();
\r
709 if( ulStatus != pdPASS )
\r
715 /* Disable management port */
\r
716 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
718 /* End of code supplied by Atmel ------------------------*/
\r
722 /*-----------------------------------------------------------*/
\r
724 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )
\r
726 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
728 AT91C_BASE_EMAC->EMAC_MAN = (AT91C_EMAC_SOF & (0x01<<30))
\r
729 | (2 << 16) | (2 << 28)
\r
730 | ((ucPHYAddress & 0x1f) << 23)
\r
731 | (ucAddress << 18);
\r
733 /* Wait until IDLE bit in Network Status register is cleared. */
\r
734 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
739 *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff );
\r
741 /* End of code supplied by Atmel ------------------------*/
\r
743 /*-----------------------------------------------------------*/
\r
745 #if USE_RMII_INTERFACE != 1
\r
746 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )
\r
748 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
750 AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))
\r
751 | (2 << 16) | (1 << 28)
\r
752 | ((ucPHYAddress & 0x1f) << 23)
\r
753 | (ucAddress << 18))
\r
754 | (ulValue & 0xffff);
\r
756 /* Wait until IDLE bit in Network Status register is cleared */
\r
757 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
762 /* End of code supplied by Atmel ------------------------*/
\r
765 /*-----------------------------------------------------------*/
\r
767 static portBASE_TYPE xGetLinkSpeed( void )
\r
769 unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;
\r
771 /* Code supplied by Atmel (reformatted) -----------------*/
\r
773 /* Link status is latched, so read twice to get current value */
\r
774 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
775 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
777 if( !( ulBMSR & BMSR_LSTATUS ) )
\r
783 vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);
\r
784 if (ulBMCR & BMCR_ANENABLE)
\r
786 /* AutoNegotiation is enabled. */
\r
787 if (!(ulBMSR & BMSR_ANEGCOMPLETE))
\r
789 /* Auto-negotitation in progress. */
\r
793 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);
\r
794 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )
\r
796 ulSpeed = SPEED_100;
\r
800 ulSpeed = SPEED_10;
\r
803 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )
\r
805 ulDuplex = DUPLEX_FULL;
\r
809 ulDuplex = DUPLEX_HALF;
\r
814 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;
\r
815 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;
\r
818 /* Update the MAC */
\r
819 ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );
\r
820 if( ulSpeed == SPEED_100 )
\r
822 if( ulDuplex == DUPLEX_FULL )
\r
824 /* 100 Full Duplex */
\r
825 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;
\r
829 /* 100 Half Duplex */
\r
830 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;
\r
835 if (ulDuplex == DUPLEX_FULL)
\r
837 /* 10 Full Duplex */
\r
838 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;
\r
841 { /* 10 Half Duplex */
\r
842 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;
\r
846 /* End of code supplied by Atmel ------------------------*/
\r
850 /*-----------------------------------------------------------*/
\r
852 void vEMACWaitForInput( void )
\r
854 /* Just wait until we are signled from an ISR that data is available, or
\r
855 we simply time out. */
\r
856 xSemaphoreTake( xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );
\r