2 FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Interrupt driven driver for the EMAC peripheral. This driver is not
\r
68 * reentrant, re-entrancy is handled by a semaphore at the network interface
\r
76 + Corrected the byte order when writing the MAC address to the MAC.
\r
77 + Support added for MII interfaces. Previously only RMII was supported.
\r
81 + The MII interface is now the default.
\r
82 + Modified the initialisation sequence slightly to allow auto init more
\r
87 + Made the function vClearEMACTxBuffer() more robust by moving the index
\r
88 manipulation into the if() statement. This allows the tx interrupt to
\r
89 execute even when there is no data to handle.
\r
93 + Corrected the Rx frame length mask when obtaining the length from the
\r
98 /* Standard includes. */
\r
101 /* Scheduler includes. */
\r
102 #include "FreeRTOS.h"
\r
103 #include "semphr.h"
\r
106 /* Demo app includes. */
\r
107 #include "SAM7_EMAC.h"
\r
109 /* Hardware specific includes. */
\r
112 #include "AT91SAM7X256.h"
\r
115 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0
\r
116 to use an MII interface. */
\r
117 #define USE_RMII_INTERFACE 0
\r
120 /* The buffer addresses written into the descriptors must be aligned so the
\r
121 last few bits are zero. These bits have special meaning for the EMAC
\r
122 peripheral and cannot be used as part of the address. */
\r
123 #define emacADDRESS_MASK ( ( unsigned long ) 0xFFFFFFFC )
\r
125 /* Bit used within the address stored in the descriptor to mark the last
\r
126 descriptor in the array. */
\r
127 #define emacRX_WRAP_BIT ( ( unsigned long ) 0x02 )
\r
129 /* Bit used within the Tx descriptor status to indicate whether the
\r
130 descriptor is under the control of the EMAC or the software. */
\r
131 #define emacTX_BUF_USED ( ( unsigned long ) 0x80000000 )
\r
133 /* A short delay is used to wait for a buffer to become available, should
\r
134 one not be immediately available when trying to transmit a frame. */
\r
135 #define emacBUFFER_WAIT_DELAY ( 2 )
\r
136 #define emacMAX_WAIT_CYCLES ( ( portBASE_TYPE ) ( configTICK_RATE_HZ / 40 ) )
\r
138 /* The time to block waiting for input. */
\r
139 #define emacBLOCK_TIME_WAITING_FOR_INPUT ( ( TickType_t ) 100 )
\r
141 /* Peripheral setup for the EMAC. */
\r
142 #define emacPERIPHERAL_A_SETUP ( ( unsigned long ) AT91C_PB2_ETX0 ) | \
\r
143 ( ( unsigned long ) AT91C_PB12_ETXER ) | \
\r
144 ( ( unsigned long ) AT91C_PB16_ECOL ) | \
\r
145 ( ( unsigned long ) AT91C_PB11_ETX3 ) | \
\r
146 ( ( unsigned long ) AT91C_PB6_ERX1 ) | \
\r
147 ( ( unsigned long ) AT91C_PB15_ERXDV ) | \
\r
148 ( ( unsigned long ) AT91C_PB13_ERX2 ) | \
\r
149 ( ( unsigned long ) AT91C_PB3_ETX1 ) | \
\r
150 ( ( unsigned long ) AT91C_PB8_EMDC ) | \
\r
151 ( ( unsigned long ) AT91C_PB5_ERX0 ) | \
\r
152 ( ( unsigned long ) AT91C_PB14_ERX3 ) | \
\r
153 ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV ) | \
\r
154 ( ( unsigned long ) AT91C_PB1_ETXEN ) | \
\r
155 ( ( unsigned long ) AT91C_PB10_ETX2 ) | \
\r
156 ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK ) | \
\r
157 ( ( unsigned long ) AT91C_PB9_EMDIO ) | \
\r
158 ( ( unsigned long ) AT91C_PB7_ERXER ) | \
\r
159 ( ( unsigned long ) AT91C_PB17_ERXCK );
\r
161 /* Misc defines. */
\r
162 #define emacINTERRUPT_LEVEL ( 5 )
\r
163 #define emacNO_DELAY ( 0 )
\r
164 #define emacTOTAL_FRAME_HEADER_SIZE ( 54 )
\r
165 #define emacPHY_INIT_DELAY ( 5000 / portTICK_PERIOD_MS )
\r
166 #define emacRESET_KEY ( ( unsigned long ) 0xA5000000 )
\r
167 #define emacRESET_LENGTH ( ( unsigned long ) ( 0x01 << 8 ) )
\r
169 /* The Atmel header file only defines the TX frame length mask. */
\r
170 #define emacRX_LENGTH_FRAME ( 0xfff )
\r
172 /*-----------------------------------------------------------*/
\r
174 /* Buffer written to by the EMAC DMA. Must be aligned as described by the
\r
175 comment above the emacADDRESS_MASK definition. */
\r
176 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
178 /* Buffer read by the EMAC DMA. Must be aligned as described by the comment
\r
179 above the emacADDRESS_MASK definition. */
\r
180 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ] __attribute__ ((aligned (8)));
\r
182 /* Descriptors used to communicate between the program and the EMAC peripheral.
\r
183 These descriptors hold the locations and state of the Rx and Tx buffers. */
\r
184 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];
\r
185 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];
\r
187 /* The IP and Ethernet addresses are read from the header files. */
\r
188 const char cMACAddress[ 6 ] = { emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };
\r
189 const unsigned char ucIPAddress[ 4 ] = { emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 };
\r
191 /*-----------------------------------------------------------*/
\r
193 /* See the header file for descriptions of public functions. */
\r
196 * Prototype for the EMAC interrupt function.
\r
198 void vEMACISR_Wrapper( void ) __attribute__ ((naked));
\r
201 * Initialise both the Tx and Rx descriptors used by the EMAC.
\r
203 static void prvSetupDescriptors(void);
\r
206 * Write our MAC address into the EMAC.
\r
208 static void prvSetupMACAddress( void );
\r
211 * Configure the EMAC and AIC for EMAC interrupts.
\r
213 static void prvSetupEMACInterrupt( void );
\r
216 * Some initialisation functions taken from the Atmel EMAC sample code.
\r
218 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );
\r
219 static portBASE_TYPE xGetLinkSpeed( void );
\r
220 static portBASE_TYPE prvProbePHY( void );
\r
221 #if USE_RMII_INTERFACE != 1
\r
222 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);
\r
226 /* The semaphore used by the EMAC ISR to wake the EMAC task. */
\r
227 static SemaphoreHandle_t xSemaphore = NULL;
\r
229 /* Holds the index to the next buffer from which data will be read. */
\r
230 static volatile unsigned long ulNextRxBuffer = 0;
\r
232 /*-----------------------------------------------------------*/
\r
234 /* See the header file for descriptions of public functions. */
\r
235 long lEMACSend( char *pcFrom, unsigned long ulLength, long lEndOfFrame )
\r
237 static unsigned portBASE_TYPE uxTxBufferIndex = 0;
\r
238 portBASE_TYPE xWaitCycles = 0;
\r
239 long lReturn = pdPASS;
\r
241 unsigned long ulLastBuffer, ulDataBuffered = 0, ulDataRemainingToSend, ulLengthToSend;
\r
243 /* If the length of data to be transmitted is greater than each individual
\r
244 transmit buffer then the data will be split into more than one buffer.
\r
245 Loop until the entire length has been buffered. */
\r
246 while( ulDataBuffered < ulLength )
\r
248 /* Is a buffer available? */
\r
249 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )
\r
251 /* There is no room to write the Tx data to the Tx buffer. Wait a
\r
252 short while, then try again. */
\r
254 if( xWaitCycles > emacMAX_WAIT_CYCLES )
\r
262 vTaskDelay( emacBUFFER_WAIT_DELAY );
\r
266 /* lReturn will only be pdPASS if a buffer is available. */
\r
267 if( lReturn == pdPASS )
\r
269 portENTER_CRITICAL();
\r
271 /* Get the address of the buffer from the descriptor, then copy
\r
272 the data into the buffer. */
\r
273 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;
\r
275 /* How much can we write to the buffer? */
\r
276 ulDataRemainingToSend = ulLength - ulDataBuffered;
\r
277 if( ulDataRemainingToSend <= ETH_TX_BUFFER_SIZE )
\r
279 /* We can write all the remaining bytes. */
\r
280 ulLengthToSend = ulDataRemainingToSend;
\r
284 /* We can not write more than ETH_TX_BUFFER_SIZE in one go. */
\r
285 ulLengthToSend = ETH_TX_BUFFER_SIZE;
\r
288 /* Copy the data into the buffer. */
\r
289 memcpy( ( void * ) pcBuffer, ( void * ) &( pcFrom[ ulDataBuffered ] ), ulLengthToSend );
\r
290 ulDataBuffered += ulLengthToSend;
\r
292 /* Is this the last data for the frame? */
\r
293 if( lEndOfFrame && ( ulDataBuffered >= ulLength ) )
\r
295 /* No more data remains for this frame so we can start the
\r
297 ulLastBuffer = AT91C_LAST_BUFFER;
\r
301 /* More data to come for this frame. */
\r
305 /* Fill out the necessary in the descriptor to get the data sent,
\r
306 then move to the next descriptor, wrapping if necessary. */
\r
307 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )
\r
309 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
311 | AT91C_TRANSMIT_WRAP;
\r
312 uxTxBufferIndex = 0;
\r
316 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )
\r
321 /* If this is the last buffer to be sent for this frame we can
\r
322 start the transmission. */
\r
325 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
\r
328 portEXIT_CRITICAL();
\r
338 /*-----------------------------------------------------------*/
\r
340 /* See the header file for descriptions of public functions. */
\r
341 unsigned long ulEMACInputLength( void )
\r
343 register unsigned long ulIndex, ulLength = 0;
\r
345 /* Skip any fragments. We are looking for the first buffer that contains
\r
346 data and has the SOF (start of frame) bit set. */
\r
347 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )
\r
349 /* Ignoring this buffer. Mark it as free again. */
\r
350 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
352 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
354 ulNextRxBuffer = 0;
\r
358 /* We are going to walk through the descriptors that make up this frame,
\r
359 but don't want to alter ulNextRxBuffer as this would prevent vEMACRead()
\r
360 from finding the data. Therefore use a copy of ulNextRxBuffer instead. */
\r
361 ulIndex = ulNextRxBuffer;
\r
363 /* Walk through the descriptors until we find the last buffer for this
\r
364 frame. The last buffer will give us the length of the entire frame. */
\r
365 while( ( xRxDescriptors[ ulIndex ].addr & AT91C_OWNERSHIP_BIT ) && !ulLength )
\r
367 ulLength = xRxDescriptors[ ulIndex ].U_Status.status & emacRX_LENGTH_FRAME;
\r
369 /* Increment to the next buffer, wrapping if necessary. */
\r
371 if( ulIndex >= NB_RX_BUFFERS )
\r
379 /*-----------------------------------------------------------*/
\r
381 /* See the header file for descriptions of public functions. */
\r
382 void vEMACRead( char *pcTo, unsigned long ulSectionLength, unsigned long ulTotalFrameLength )
\r
384 static unsigned long ulSectionBytesReadSoFar = 0, ulBufferPosition = 0, ulFameBytesReadSoFar = 0;
\r
385 static char *pcSource;
\r
386 register unsigned long ulBytesRemainingInBuffer, ulRemainingSectionBytes;
\r
388 /* Read ulSectionLength bytes from the Rx buffers. This is not necessarily any
\r
389 correspondence between the length of our Rx buffers, and the length of the
\r
390 data we are returning or the length of the data being requested. Therefore,
\r
391 between calls we have to remember not only which buffer we are currently
\r
392 processing, but our position within that buffer. This would be greatly
\r
393 simplified if PBUF_POOL_BUFSIZE could be guaranteed to be greater than
\r
394 the size of each Rx buffer, and that memory fragmentation did not occur.
\r
396 This function should only be called after a call to ulEMACInputLength().
\r
397 This will ensure ulNextRxBuffer is set to the correct buffer. */
\r
401 /* vEMACRead is called with pcTo set to NULL to indicate that we are about
\r
402 to read a new frame. Any fragments remaining in the frame we were
\r
403 processing during the last call should be dropped. */
\r
406 /* How many bytes are indicated as being in this buffer? If none then
\r
407 the buffer is completely full and the frame is contained within more
\r
408 than one buffer. */
\r
410 /* Reset our state variables ready for the next read from this buffer. */
\r
411 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
412 ulFameBytesReadSoFar = ( unsigned long ) 0;
\r
413 ulBufferPosition = ( unsigned long ) 0;
\r
417 /* Loop until we have obtained the required amount of data. */
\r
418 ulSectionBytesReadSoFar = 0;
\r
419 while( ulSectionBytesReadSoFar < ulSectionLength )
\r
421 /* We may have already read some data from this buffer. How much
\r
422 data remains in the buffer? */
\r
423 ulBytesRemainingInBuffer = ( ETH_RX_BUFFER_SIZE - ulBufferPosition );
\r
425 /* How many more bytes do we need to read before we have the
\r
426 required amount of data? */
\r
427 ulRemainingSectionBytes = ulSectionLength - ulSectionBytesReadSoFar;
\r
429 /* Do we want more data than remains in the buffer? */
\r
430 if( ulRemainingSectionBytes > ulBytesRemainingInBuffer )
\r
432 /* We want more data than remains in the buffer so we can
\r
433 write the remains of the buffer to the destination, then move
\r
434 onto the next buffer to get the rest. */
\r
435 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulBytesRemainingInBuffer );
\r
436 ulSectionBytesReadSoFar += ulBytesRemainingInBuffer;
\r
437 ulFameBytesReadSoFar += ulBytesRemainingInBuffer;
\r
439 /* Mark the buffer as free again. */
\r
440 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
442 /* Move onto the next buffer. */
\r
444 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
446 ulNextRxBuffer = ( unsigned long ) 0;
\r
449 /* Reset the variables for the new buffer. */
\r
450 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
451 ulBufferPosition = ( unsigned long ) 0;
\r
455 /* We have enough data in this buffer to send back. Read out
\r
456 enough data and remember how far we read up to. */
\r
457 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulRemainingSectionBytes );
\r
459 /* There may be more data in this buffer yet. Increment our
\r
460 position in this buffer past the data we have just read. */
\r
461 ulBufferPosition += ulRemainingSectionBytes;
\r
462 ulSectionBytesReadSoFar += ulRemainingSectionBytes;
\r
463 ulFameBytesReadSoFar += ulRemainingSectionBytes;
\r
465 /* Have we now finished with this buffer? */
\r
466 if( ( ulBufferPosition >= ETH_RX_BUFFER_SIZE ) || ( ulFameBytesReadSoFar >= ulTotalFrameLength ) )
\r
468 /* Mark the buffer as free again. */
\r
469 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
471 /* Move onto the next buffer. */
\r
473 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
475 ulNextRxBuffer = 0;
\r
478 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
479 ulBufferPosition = 0;
\r
485 /*-----------------------------------------------------------*/
\r
487 /* See the header file for descriptions of public functions. */
\r
488 SemaphoreHandle_t xEMACInit( void )
\r
490 /* Code supplied by Atmel -------------------------------*/
\r
492 /* Disable pull up on RXDV => PHY normal mode (not in test mode),
\r
493 PHY has internal pull down. */
\r
494 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;
\r
496 #if USE_RMII_INTERFACE != 1
\r
497 /* PHY has internal pull down : set MII mode. */
\r
498 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;
\r
501 /* Clear PB18 <=> PHY powerdown. */
\r
502 AT91C_BASE_PIOB->PIO_PER = 1 << 18;
\r
503 AT91C_BASE_PIOB->PIO_OER = 1 << 18;
\r
504 AT91C_BASE_PIOB->PIO_CODR = 1 << 18;
\r
506 /* After PHY power up, hardware reset. */
\r
507 AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;
\r
508 AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;
\r
510 /* Wait for hardware reset end. */
\r
511 while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )
\r
513 __asm volatile ( "NOP" );
\r
515 __asm volatile ( "NOP" );
\r
517 /* Setup the pins. */
\r
518 AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;
\r
519 AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;
\r
521 /* Enable com between EMAC PHY.
\r
523 Enable management port. */
\r
524 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
526 /* MDC = MCK/32. */
\r
527 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
529 /* Wait for PHY auto init end (rather crude delay!). */
\r
530 vTaskDelay( emacPHY_INIT_DELAY );
\r
532 /* PHY configuration. */
\r
533 #if USE_RMII_INTERFACE != 1
\r
535 unsigned long ulControl;
\r
537 /* PHY has internal pull down : disable MII isolate. */
\r
538 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
539 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
540 ulControl &= ~BMCR_ISOLATE;
\r
541 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );
\r
545 /* Disable management port again. */
\r
546 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
548 #if USE_RMII_INTERFACE != 1
\r
549 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */
\r
550 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;
\r
552 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator
\r
554 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
\r
557 /* End of code supplied by Atmel ------------------------*/
\r
559 /* Setup the buffers and descriptors. */
\r
560 prvSetupDescriptors();
\r
562 /* Load our MAC address into the EMAC. */
\r
563 prvSetupMACAddress();
\r
565 /* Are we connected? */
\r
566 if( prvProbePHY() )
\r
568 /* Enable the interrupt! */
\r
569 portENTER_CRITICAL();
\r
571 prvSetupEMACInterrupt();
\r
572 vPassEMACSemaphore( xSemaphore );
\r
574 portEXIT_CRITICAL();
\r
579 /*-----------------------------------------------------------*/
\r
581 /* See the header file for descriptions of public functions. */
\r
582 void vClearEMACTxBuffer( void )
\r
584 static unsigned portBASE_TYPE uxNextBufferToClear = 0;
\r
586 /* Called on Tx interrupt events to reset the AT91C_TRANSMIT_OK bit in each
\r
587 Tx buffer within the frame just transmitted. This marks all the buffers
\r
588 as available again.
\r
590 The first buffer in the frame should have the bit set automatically. */
\r
591 if( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_TRANSMIT_OK )
\r
593 /* Loop through the other buffers in the frame. */
\r
594 while( !( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_LAST_BUFFER ) )
\r
596 uxNextBufferToClear++;
\r
598 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
600 uxNextBufferToClear = 0;
\r
603 xTxDescriptors[ uxNextBufferToClear ].U_Status.status |= AT91C_TRANSMIT_OK;
\r
606 /* Start with the next buffer the next time a Tx interrupt is called. */
\r
607 uxNextBufferToClear++;
\r
609 /* Do we need to wrap back to the first buffer? */
\r
610 if( uxNextBufferToClear >= NB_TX_BUFFERS )
\r
612 uxNextBufferToClear = 0;
\r
616 /*-----------------------------------------------------------*/
\r
618 static void prvSetupDescriptors(void)
\r
620 unsigned portBASE_TYPE xIndex;
\r
621 unsigned long ulAddress;
\r
623 /* Initialise xRxDescriptors descriptor. */
\r
624 for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )
\r
626 /* Calculate the address of the nth buffer within the array. */
\r
627 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );
\r
629 /* Write the buffer address into the descriptor. The DMA will place
\r
630 the data at this address when this descriptor is being used. Mask off
\r
631 the bottom bits of the address as these have special meaning. */
\r
632 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
635 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
636 to the first buffer. */
\r
637 xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;
\r
639 /* Initialise xTxDescriptors. */
\r
640 for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )
\r
642 /* Calculate the address of the nth buffer within the array. */
\r
643 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );
\r
645 /* Write the buffer address into the descriptor. The DMA will read
\r
646 data from here when the descriptor is being used. */
\r
647 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
648 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;
\r
651 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
652 to the first buffer. */
\r
653 xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;
\r
655 /* Tell the EMAC where to find the descriptors. */
\r
656 AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;
\r
657 AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;
\r
659 /* Clear all the bits in the receive status register. */
\r
660 AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );
\r
662 /* Enable the copy of data into the buffers, ignore broadcasts,
\r
663 and don't copy FCS. */
\r
664 AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);
\r
666 /* Enable Rx and Tx, plus the stats register. */
\r
667 AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );
\r
669 /*-----------------------------------------------------------*/
\r
671 static void prvSetupMACAddress( void )
\r
673 /* Must be written SA1L then SA1H. */
\r
674 AT91C_BASE_EMAC->EMAC_SA1L = ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |
\r
675 ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |
\r
676 ( ( unsigned long ) cMACAddress[ 1 ] << 8 ) |
\r
679 AT91C_BASE_EMAC->EMAC_SA1H = ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |
\r
682 /*-----------------------------------------------------------*/
\r
684 static void prvSetupEMACInterrupt( void )
\r
686 /* Create the semaphore used to trigger the EMAC task. */
\r
687 vSemaphoreCreateBinary( xSemaphore );
\r
690 /* We start by 'taking' the semaphore so the ISR can 'give' it when the
\r
691 first interrupt occurs. */
\r
692 xSemaphoreTake( xSemaphore, emacNO_DELAY );
\r
693 portENTER_CRITICAL();
\r
695 /* We want to interrupt on Rx and Tx events. */
\r
696 AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;
\r
698 /* Enable the interrupts in the AIC. */
\r
699 AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );
\r
700 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;
\r
702 portEXIT_CRITICAL();
\r
711 * The following functions are initialisation functions taken from the Atmel
\r
712 * EMAC sample code.
\r
716 static portBASE_TYPE prvProbePHY( void )
\r
718 unsigned long ulPHYId1, ulPHYId2, ulStatus;
\r
719 portBASE_TYPE xReturn = pdPASS;
\r
721 /* Code supplied by Atmel (reformatted) -----------------*/
\r
723 /* Enable management port */
\r
724 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
725 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
727 /* Read the PHY ID. */
\r
728 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );
\r
729 vReadPHY(AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );
\r
734 Bits 3:0 Revision Number Four bit manufacturer?s revision number.
\r
735 0001 stands for Rev. A, etc.
\r
737 if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )
\r
739 /* Did not expect this ID. */
\r
744 ulStatus = xGetLinkSpeed();
\r
746 if( ulStatus != pdPASS )
\r
752 /* Disable management port */
\r
753 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
755 /* End of code supplied by Atmel ------------------------*/
\r
759 /*-----------------------------------------------------------*/
\r
761 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )
\r
763 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
765 AT91C_BASE_EMAC->EMAC_MAN = (AT91C_EMAC_SOF & (0x01<<30))
\r
766 | (2 << 16) | (2 << 28)
\r
767 | ((ucPHYAddress & 0x1f) << 23)
\r
768 | (ucAddress << 18);
\r
770 /* Wait until IDLE bit in Network Status register is cleared. */
\r
771 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
776 *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff );
\r
778 /* End of code supplied by Atmel ------------------------*/
\r
780 /*-----------------------------------------------------------*/
\r
782 #if USE_RMII_INTERFACE != 1
\r
783 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )
\r
785 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
787 AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))
\r
788 | (2 << 16) | (1 << 28)
\r
789 | ((ucPHYAddress & 0x1f) << 23)
\r
790 | (ucAddress << 18))
\r
791 | (ulValue & 0xffff);
\r
793 /* Wait until IDLE bit in Network Status register is cleared */
\r
794 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
799 /* End of code supplied by Atmel ------------------------*/
\r
802 /*-----------------------------------------------------------*/
\r
804 static portBASE_TYPE xGetLinkSpeed( void )
\r
806 unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;
\r
808 /* Code supplied by Atmel (reformatted) -----------------*/
\r
810 /* Link status is latched, so read twice to get current value */
\r
811 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
812 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
814 if( !( ulBMSR & BMSR_LSTATUS ) )
\r
820 vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);
\r
821 if (ulBMCR & BMCR_ANENABLE)
\r
823 /* AutoNegotiation is enabled. */
\r
824 if (!(ulBMSR & BMSR_ANEGCOMPLETE))
\r
826 /* Auto-negotitation in progress. */
\r
830 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);
\r
831 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )
\r
833 ulSpeed = SPEED_100;
\r
837 ulSpeed = SPEED_10;
\r
840 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )
\r
842 ulDuplex = DUPLEX_FULL;
\r
846 ulDuplex = DUPLEX_HALF;
\r
851 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;
\r
852 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;
\r
855 /* Update the MAC */
\r
856 ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );
\r
857 if( ulSpeed == SPEED_100 )
\r
859 if( ulDuplex == DUPLEX_FULL )
\r
861 /* 100 Full Duplex */
\r
862 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;
\r
866 /* 100 Half Duplex */
\r
867 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;
\r
872 if (ulDuplex == DUPLEX_FULL)
\r
874 /* 10 Full Duplex */
\r
875 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;
\r
878 { /* 10 Half Duplex */
\r
879 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;
\r
883 /* End of code supplied by Atmel ------------------------*/
\r
887 /*-----------------------------------------------------------*/
\r
889 void vEMACWaitForInput( void )
\r
891 /* Just wait until we are signled from an ISR that data is available, or
\r
892 we simply time out. */
\r
893 xSemaphoreTake( xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );
\r