]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/EMAC/SAM7_EMAC.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / EMAC / SAM7_EMAC.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Interrupt driven driver for the EMAC peripheral.  This driver is not\r
77  * reentrant, re-entrancy is handled by a semaphore at the network interface\r
78  * level. \r
79  */\r
80 \r
81 \r
82 /*\r
83 Changes from V3.2.2\r
84 \r
85         + Corrected the byte order when writing the MAC address to the MAC.\r
86         + Support added for MII interfaces.  Previously only RMII was supported.\r
87 \r
88 Changes from V3.2.3\r
89 \r
90         + The MII interface is now the default.\r
91         + Modified the initialisation sequence slightly to allow auto init more\r
92           time to complete.\r
93 \r
94 Changes from V4.0.1\r
95 \r
96         + Made the function vClearEMACTxBuffer() more robust by moving the index\r
97           manipulation into the if() statement.  This allows the tx interrupt to\r
98           execute even when there is no data to handle.\r
99 \r
100 Changes from V4.0.4\r
101 \r
102         + Corrected the Rx frame length mask when obtaining the length from the\r
103           rx descriptor.\r
104 */\r
105 \r
106 \r
107 /* Standard includes. */\r
108 #include <string.h>\r
109 \r
110 /* Scheduler includes. */\r
111 #include "FreeRTOS.h"\r
112 #include "semphr.h"\r
113 #include "task.h"\r
114 \r
115 /* Demo app includes. */\r
116 #include "SAM7_EMAC.h"\r
117 \r
118 /* Hardware specific includes. */\r
119 #include "Emac.h"\r
120 #include "mii.h"\r
121 #include "AT91SAM7X256.h"\r
122 \r
123 \r
124 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0\r
125 to use an MII interface. */\r
126 #define USE_RMII_INTERFACE 0\r
127 \r
128 \r
129 /* The buffer addresses written into the descriptors must be aligned so the\r
130 last few bits are zero.  These bits have special meaning for the EMAC\r
131 peripheral and cannot be used as part of the address. */\r
132 #define emacADDRESS_MASK                        ( ( unsigned long ) 0xFFFFFFFC )\r
133 \r
134 /* Bit used within the address stored in the descriptor to mark the last\r
135 descriptor in the array. */\r
136 #define emacRX_WRAP_BIT                         ( ( unsigned long ) 0x02 )\r
137 \r
138 /* Bit used within the Tx descriptor status to indicate whether the\r
139 descriptor is under the control of the EMAC or the software. */\r
140 #define emacTX_BUF_USED                         ( ( unsigned long ) 0x80000000 )\r
141 \r
142 /* A short delay is used to wait for a buffer to become available, should\r
143 one not be immediately available when trying to transmit a frame. */\r
144 #define emacBUFFER_WAIT_DELAY           ( 2 )\r
145 #define emacMAX_WAIT_CYCLES                     ( ( portBASE_TYPE ) ( configTICK_RATE_HZ / 40 ) )\r
146 \r
147 /* The time to block waiting for input. */\r
148 #define emacBLOCK_TIME_WAITING_FOR_INPUT        ( ( portTickType ) 100 )\r
149 \r
150 /* Peripheral setup for the EMAC. */\r
151 #define emacPERIPHERAL_A_SETUP          ( ( unsigned long ) AT91C_PB2_ETX0                      ) | \\r
152                                                                         ( ( unsigned long ) AT91C_PB12_ETXER            ) | \\r
153                                                                         ( ( unsigned long ) AT91C_PB16_ECOL                     ) | \\r
154                                                                         ( ( unsigned long ) AT91C_PB11_ETX3                     ) | \\r
155                                                                         ( ( unsigned long ) AT91C_PB6_ERX1                      ) | \\r
156                                                                         ( ( unsigned long ) AT91C_PB15_ERXDV            ) | \\r
157                                                                         ( ( unsigned long ) AT91C_PB13_ERX2                     ) | \\r
158                                                                         ( ( unsigned long ) AT91C_PB3_ETX1                      ) | \\r
159                                                                         ( ( unsigned long ) AT91C_PB8_EMDC                      ) | \\r
160                                                                         ( ( unsigned long ) AT91C_PB5_ERX0                      ) | \\r
161                                                                         ( ( unsigned long ) AT91C_PB14_ERX3                     ) | \\r
162                                                                         ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV       ) | \\r
163                                                                         ( ( unsigned long ) AT91C_PB1_ETXEN                     ) | \\r
164                                                                         ( ( unsigned long ) AT91C_PB10_ETX2                     ) | \\r
165                                                                         ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK      ) | \\r
166                                                                         ( ( unsigned long ) AT91C_PB9_EMDIO                     ) | \\r
167                                                                         ( ( unsigned long ) AT91C_PB7_ERXER                     ) | \\r
168                                                                         ( ( unsigned long ) AT91C_PB17_ERXCK            );\r
169 \r
170 /* Misc defines. */\r
171 #define emacINTERRUPT_LEVEL                     ( 5 )\r
172 #define emacNO_DELAY                            ( 0 )\r
173 #define emacTOTAL_FRAME_HEADER_SIZE     ( 54 )\r
174 #define emacPHY_INIT_DELAY                      ( 5000 / portTICK_RATE_MS )\r
175 #define emacRESET_KEY                           ( ( unsigned long ) 0xA5000000 )\r
176 #define emacRESET_LENGTH                        ( ( unsigned long ) ( 0x01 << 8 ) )\r
177 \r
178 /* The Atmel header file only defines the TX frame length mask. */\r
179 #define emacRX_LENGTH_FRAME                     ( 0xfff )\r
180 \r
181 /*-----------------------------------------------------------*/\r
182 \r
183 /* Buffer written to by the EMAC DMA.  Must be aligned as described by the\r
184 comment above the emacADDRESS_MASK definition. */\r
185 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ] __attribute__ ((aligned (8)));\r
186 \r
187 /* Buffer read by the EMAC DMA.  Must be aligned as described by the comment\r
188 above the emacADDRESS_MASK definition. */\r
189 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ] __attribute__ ((aligned (8)));\r
190 \r
191 /* Descriptors used to communicate between the program and the EMAC peripheral.\r
192 These descriptors hold the locations and state of the Rx and Tx buffers. */\r
193 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];\r
194 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];\r
195 \r
196 /* The IP and Ethernet addresses are read from the header files. */\r
197 const char cMACAddress[ 6 ] = { emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };\r
198 const unsigned char ucIPAddress[ 4 ]  = { emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 };\r
199 \r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* See the header file for descriptions of public functions. */\r
203 \r
204 /*\r
205  * Prototype for the EMAC interrupt function.\r
206  */\r
207 void vEMACISR_Wrapper( void ) __attribute__ ((naked));\r
208 \r
209 /*\r
210  * Initialise both the Tx and Rx descriptors used by the EMAC.\r
211  */\r
212 static void prvSetupDescriptors(void);\r
213 \r
214 /*\r
215  * Write our MAC address into the EMAC.  \r
216  */\r
217 static void prvSetupMACAddress( void );\r
218 \r
219 /*\r
220  * Configure the EMAC and AIC for EMAC interrupts.\r
221  */\r
222 static void prvSetupEMACInterrupt( void );\r
223 \r
224 /*\r
225  * Some initialisation functions taken from the Atmel EMAC sample code.\r
226  */\r
227 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );\r
228 static portBASE_TYPE xGetLinkSpeed( void );\r
229 static portBASE_TYPE prvProbePHY( void );\r
230 #if USE_RMII_INTERFACE != 1\r
231         static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);\r
232 #endif\r
233 \r
234 \r
235 /* The semaphore used by the EMAC ISR to wake the EMAC task. */\r
236 static xSemaphoreHandle xSemaphore = NULL;\r
237 \r
238 /* Holds the index to the next buffer from which data will be read. */\r
239 static volatile unsigned long ulNextRxBuffer = 0;\r
240 \r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /* See the header file for descriptions of public functions. */\r
244 long lEMACSend( char *pcFrom, unsigned long ulLength, long lEndOfFrame )\r
245 {\r
246 static unsigned portBASE_TYPE uxTxBufferIndex = 0;\r
247 portBASE_TYPE xWaitCycles = 0;\r
248 long lReturn = pdPASS;\r
249 char *pcBuffer;\r
250 unsigned long ulLastBuffer, ulDataBuffered = 0, ulDataRemainingToSend, ulLengthToSend;\r
251 \r
252         /* If the length of data to be transmitted is greater than each individual\r
253         transmit buffer then the data will be split into more than one buffer.\r
254         Loop until the entire length has been buffered. */\r
255         while( ulDataBuffered < ulLength )\r
256         {\r
257                 /* Is a buffer available? */\r
258                 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )\r
259                 {\r
260                         /* There is no room to write the Tx data to the Tx buffer.  Wait a\r
261                         short while, then try again. */\r
262                         xWaitCycles++;\r
263                         if( xWaitCycles > emacMAX_WAIT_CYCLES )\r
264                         {\r
265                                 /* Give up. */\r
266                                 lReturn = pdFAIL;\r
267                                 break;\r
268                         }\r
269                         else\r
270                         {\r
271                                 vTaskDelay( emacBUFFER_WAIT_DELAY );\r
272                         }\r
273                 }\r
274         \r
275                 /* lReturn will only be pdPASS if a buffer is available. */\r
276                 if( lReturn == pdPASS )\r
277                 {\r
278                         portENTER_CRITICAL();\r
279                         {\r
280                                 /* Get the address of the buffer from the descriptor, then copy \r
281                                 the data into the buffer. */\r
282                                 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;\r
283 \r
284                                 /* How much can we write to the buffer? */\r
285                                 ulDataRemainingToSend = ulLength - ulDataBuffered;\r
286                                 if( ulDataRemainingToSend <= ETH_TX_BUFFER_SIZE )\r
287                                 {\r
288                                         /* We can write all the remaining bytes. */\r
289                                         ulLengthToSend = ulDataRemainingToSend;\r
290                                 }\r
291                                 else\r
292                                 {\r
293                                         /* We can not write more than ETH_TX_BUFFER_SIZE in one go. */\r
294                                         ulLengthToSend = ETH_TX_BUFFER_SIZE;\r
295                                 }\r
296 \r
297                                 /* Copy the data into the buffer. */\r
298                                 memcpy( ( void * ) pcBuffer, ( void * ) &( pcFrom[ ulDataBuffered ] ), ulLengthToSend );\r
299                                 ulDataBuffered += ulLengthToSend;\r
300 \r
301                                 /* Is this the last data for the frame? */\r
302                                 if( lEndOfFrame && ( ulDataBuffered >= ulLength ) )\r
303                                 {\r
304                                         /* No more data remains for this frame so we can start the \r
305                                         transmission. */\r
306                                         ulLastBuffer = AT91C_LAST_BUFFER;\r
307                                 }\r
308                                 else\r
309                                 {\r
310                                         /* More data to come for this frame. */\r
311                                         ulLastBuffer = 0;\r
312                                 }\r
313         \r
314                                 /* Fill out the necessary in the descriptor to get the data sent, \r
315                                 then move to the next descriptor, wrapping if necessary. */\r
316                                 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )\r
317                                 {                               \r
318                                         xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )\r
319                                                                                                                                                         | ulLastBuffer\r
320                                                                                                                                                         | AT91C_TRANSMIT_WRAP;\r
321                                         uxTxBufferIndex = 0;\r
322                                 }\r
323                                 else\r
324                                 {\r
325                                         xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )\r
326                                                                                                                                                         | ulLastBuffer;\r
327                                         uxTxBufferIndex++;\r
328                                 }\r
329         \r
330                                 /* If this is the last buffer to be sent for this frame we can\r
331                                 start the transmission. */\r
332                                 if( ulLastBuffer )\r
333                                 {\r
334                                         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;\r
335                                 }\r
336                         }\r
337                         portEXIT_CRITICAL();\r
338                 }\r
339                 else\r
340                 {\r
341                         break;\r
342                 }\r
343         }\r
344 \r
345         return lReturn;\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 /* See the header file for descriptions of public functions. */\r
350 unsigned long ulEMACInputLength( void )\r
351 {\r
352 register unsigned long ulIndex, ulLength = 0;\r
353 \r
354         /* Skip any fragments.  We are looking for the first buffer that contains\r
355         data and has the SOF (start of frame) bit set. */\r
356         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )\r
357         {\r
358                 /* Ignoring this buffer.  Mark it as free again. */\r
359                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );              \r
360                 ulNextRxBuffer++;\r
361                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
362                 {\r
363                         ulNextRxBuffer = 0;\r
364                 }\r
365         }\r
366 \r
367         /* We are going to walk through the descriptors that make up this frame, \r
368         but don't want to alter ulNextRxBuffer as this would prevent vEMACRead()\r
369         from finding the data.  Therefore use a copy of ulNextRxBuffer instead. */\r
370         ulIndex = ulNextRxBuffer;\r
371 \r
372         /* Walk through the descriptors until we find the last buffer for this \r
373         frame.  The last buffer will give us the length of the entire frame. */\r
374         while( ( xRxDescriptors[ ulIndex ].addr & AT91C_OWNERSHIP_BIT ) && !ulLength )\r
375         {\r
376                 ulLength = xRxDescriptors[ ulIndex ].U_Status.status & emacRX_LENGTH_FRAME;\r
377 \r
378                 /* Increment to the next buffer, wrapping if necessary. */\r
379                 ulIndex++;\r
380                 if( ulIndex >= NB_RX_BUFFERS )\r
381                 {\r
382                         ulIndex = 0;\r
383                 }\r
384         }\r
385 \r
386         return ulLength;\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 /* See the header file for descriptions of public functions. */\r
391 void vEMACRead( char *pcTo, unsigned long ulSectionLength, unsigned long ulTotalFrameLength )\r
392 {\r
393 static unsigned long ulSectionBytesReadSoFar = 0, ulBufferPosition = 0, ulFameBytesReadSoFar = 0;\r
394 static char *pcSource;\r
395 register unsigned long ulBytesRemainingInBuffer, ulRemainingSectionBytes;\r
396 \r
397         /* Read ulSectionLength bytes from the Rx buffers.  This is not necessarily any\r
398         correspondence between the length of our Rx buffers, and the length of the\r
399         data we are returning or the length of the data being requested.  Therefore, \r
400         between calls  we have to remember not only which buffer we are currently \r
401         processing, but our position within that buffer.  This would be greatly\r
402         simplified if PBUF_POOL_BUFSIZE could be guaranteed to be greater than\r
403         the size of each Rx buffer, and that memory fragmentation did not occur.\r
404         \r
405         This function should only be called after a call to ulEMACInputLength().\r
406         This will ensure ulNextRxBuffer is set to the correct buffer. */\r
407 \r
408 \r
409 \r
410         /* vEMACRead is called with pcTo set to NULL to indicate that we are about\r
411         to read a new frame.  Any fragments remaining in the frame we were \r
412         processing during the last call should be dropped. */\r
413         if( pcTo == NULL )\r
414         {\r
415                 /* How many bytes are indicated as being in this buffer?  If none then\r
416                 the buffer is completely full and the frame is contained within more\r
417                 than one buffer. */\r
418 \r
419                 /* Reset our state variables ready for the next read from this buffer. */\r
420         pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
421         ulFameBytesReadSoFar = ( unsigned long ) 0;\r
422                 ulBufferPosition = ( unsigned long ) 0;\r
423         }\r
424         else\r
425         {\r
426                 /* Loop until we have obtained the required amount of data. */\r
427         ulSectionBytesReadSoFar = 0;\r
428                 while( ulSectionBytesReadSoFar < ulSectionLength )\r
429                 {\r
430                         /* We may have already read some data from this buffer.  How much\r
431                         data remains in the buffer? */\r
432                         ulBytesRemainingInBuffer = ( ETH_RX_BUFFER_SIZE - ulBufferPosition );\r
433 \r
434                         /* How many more bytes do we need to read before we have the \r
435                         required amount of data? */\r
436             ulRemainingSectionBytes = ulSectionLength - ulSectionBytesReadSoFar;\r
437 \r
438                         /* Do we want more data than remains in the buffer? */\r
439                         if( ulRemainingSectionBytes > ulBytesRemainingInBuffer )\r
440                         {\r
441                                 /* We want more data than remains in the buffer so we can \r
442                                 write the remains of the buffer to the destination, then move\r
443                                 onto the next buffer to get the rest. */\r
444                                 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulBytesRemainingInBuffer );\r
445                                 ulSectionBytesReadSoFar += ulBytesRemainingInBuffer;\r
446                 ulFameBytesReadSoFar += ulBytesRemainingInBuffer;\r
447 \r
448                                 /* Mark the buffer as free again. */\r
449                                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
450 \r
451                                 /* Move onto the next buffer. */\r
452                                 ulNextRxBuffer++;\r
453                                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
454                                 {\r
455                                         ulNextRxBuffer = ( unsigned long ) 0;\r
456                                 }\r
457 \r
458                                 /* Reset the variables for the new buffer. */\r
459                                 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
460                                 ulBufferPosition = ( unsigned long ) 0;\r
461                         }\r
462                         else\r
463                         {\r
464                                 /* We have enough data in this buffer to send back.  Read out\r
465                                 enough data and remember how far we read up to. */\r
466                                 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulRemainingSectionBytes );\r
467 \r
468                                 /* There may be more data in this buffer yet.  Increment our \r
469                                 position in this buffer past the data we have just read. */\r
470                                 ulBufferPosition += ulRemainingSectionBytes;\r
471                                 ulSectionBytesReadSoFar += ulRemainingSectionBytes;\r
472                 ulFameBytesReadSoFar += ulRemainingSectionBytes;\r
473 \r
474                                 /* Have we now finished with this buffer? */\r
475                                 if( ( ulBufferPosition >= ETH_RX_BUFFER_SIZE ) || ( ulFameBytesReadSoFar >= ulTotalFrameLength ) )\r
476                                 {\r
477                                         /* Mark the buffer as free again. */\r
478                                         xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
479 \r
480                                         /* Move onto the next buffer. */\r
481                                         ulNextRxBuffer++;\r
482                                         if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
483                                         {\r
484                                                 ulNextRxBuffer = 0;\r
485                                         }\r
486         \r
487                                         pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
488                                         ulBufferPosition = 0;\r
489                                 }\r
490                         }\r
491                 }\r
492         }\r
493 }\r
494 /*-----------------------------------------------------------*/\r
495 \r
496 /* See the header file for descriptions of public functions. */\r
497 xSemaphoreHandle xEMACInit( void )\r
498 {\r
499         /* Code supplied by Atmel -------------------------------*/\r
500 \r
501         /* Disable pull up on RXDV => PHY normal mode (not in test mode),\r
502         PHY has internal pull down. */\r
503         AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;\r
504 \r
505         #if USE_RMII_INTERFACE != 1\r
506                 /* PHY has internal pull down : set MII mode. */\r
507                 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;\r
508         #endif\r
509 \r
510         /* Clear PB18 <=> PHY powerdown. */\r
511         AT91C_BASE_PIOB->PIO_PER = 1 << 18;\r
512         AT91C_BASE_PIOB->PIO_OER = 1 << 18;\r
513         AT91C_BASE_PIOB->PIO_CODR = 1 << 18;\r
514 \r
515         /* After PHY power up, hardware reset. */\r
516         AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;\r
517         AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;\r
518 \r
519         /* Wait for hardware reset end. */\r
520         while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )\r
521         {\r
522                 __asm volatile ( "NOP" );\r
523         }\r
524     __asm volatile ( "NOP" );\r
525 \r
526         /* Setup the pins. */\r
527         AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;\r
528         AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;\r
529 \r
530         /* Enable com between EMAC PHY.\r
531 \r
532         Enable management port. */\r
533         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
534 \r
535         /* MDC = MCK/32. */\r
536         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;     \r
537 \r
538         /* Wait for PHY auto init end (rather crude delay!). */\r
539         vTaskDelay( emacPHY_INIT_DELAY );\r
540 \r
541         /* PHY configuration. */\r
542         #if USE_RMII_INTERFACE != 1\r
543         {\r
544                 unsigned long ulControl;\r
545 \r
546                 /* PHY has internal pull down : disable MII isolate. */\r
547                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
548                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
549                 ulControl &= ~BMCR_ISOLATE;\r
550                 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );\r
551         }\r
552         #endif\r
553 \r
554         /* Disable management port again. */\r
555         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;\r
556 \r
557         #if USE_RMII_INTERFACE != 1\r
558                 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */\r
559                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;\r
560         #else\r
561                 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator\r
562                 on ERFCK). */\r
563                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;\r
564         #endif\r
565 \r
566         /* End of code supplied by Atmel ------------------------*/\r
567 \r
568         /* Setup the buffers and descriptors. */\r
569         prvSetupDescriptors();\r
570         \r
571         /* Load our MAC address into the EMAC. */\r
572         prvSetupMACAddress();\r
573 \r
574         /* Are we connected? */\r
575         if( prvProbePHY() )\r
576         {\r
577                 /* Enable the interrupt! */\r
578                 portENTER_CRITICAL();\r
579                 {\r
580                         prvSetupEMACInterrupt();\r
581                         vPassEMACSemaphore( xSemaphore );\r
582                 }\r
583                 portEXIT_CRITICAL();\r
584         }\r
585 \r
586         return xSemaphore;\r
587 }\r
588 /*-----------------------------------------------------------*/\r
589 \r
590 /* See the header file for descriptions of public functions. */\r
591 void vClearEMACTxBuffer( void )\r
592 {\r
593 static unsigned portBASE_TYPE uxNextBufferToClear = 0;\r
594 \r
595         /* Called on Tx interrupt events to reset the AT91C_TRANSMIT_OK bit in each \r
596         Tx buffer within the frame just transmitted.  This marks all the buffers\r
597         as available again.\r
598 \r
599         The first buffer in the frame should have the bit set automatically. */\r
600         if( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_TRANSMIT_OK )\r
601         {\r
602                 /* Loop through the other buffers in the frame. */\r
603                 while( !( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_LAST_BUFFER ) )\r
604                 {\r
605                         uxNextBufferToClear++;\r
606 \r
607                         if( uxNextBufferToClear >= NB_TX_BUFFERS )\r
608                         {\r
609                                 uxNextBufferToClear = 0;\r
610                         }\r
611 \r
612                         xTxDescriptors[ uxNextBufferToClear ].U_Status.status |= AT91C_TRANSMIT_OK;\r
613                 }\r
614 \r
615                 /* Start with the next buffer the next time a Tx interrupt is called. */\r
616                 uxNextBufferToClear++;\r
617 \r
618                 /* Do we need to wrap back to the first buffer? */\r
619                 if( uxNextBufferToClear >= NB_TX_BUFFERS )\r
620                 {\r
621                         uxNextBufferToClear = 0;\r
622                 }\r
623         }\r
624 }\r
625 /*-----------------------------------------------------------*/\r
626 \r
627 static void prvSetupDescriptors(void)\r
628 {\r
629 unsigned portBASE_TYPE xIndex;\r
630 unsigned long ulAddress;\r
631 \r
632         /* Initialise xRxDescriptors descriptor. */\r
633         for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )\r
634         {\r
635                 /* Calculate the address of the nth buffer within the array. */\r
636                 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );\r
637 \r
638                 /* Write the buffer address into the descriptor.  The DMA will place\r
639                 the data at this address when this descriptor is being used.  Mask off\r
640                 the bottom bits of the address as these have special meaning. */\r
641                 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
642         }       \r
643 \r
644         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
645         to the first buffer. */\r
646         xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;\r
647 \r
648         /* Initialise xTxDescriptors. */\r
649         for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )\r
650         {\r
651                 /* Calculate the address of the nth buffer within the array. */\r
652                 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );\r
653 \r
654                 /* Write the buffer address into the descriptor.  The DMA will read\r
655                 data from here when the descriptor is being used. */\r
656                 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
657                 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;\r
658         }       \r
659 \r
660         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
661         to the first buffer. */\r
662         xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;\r
663 \r
664         /* Tell the EMAC where to find the descriptors. */\r
665         AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;\r
666         AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;\r
667         \r
668         /* Clear all the bits in the receive status register. */\r
669         AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );\r
670 \r
671         /* Enable the copy of data into the buffers, ignore broadcasts, \r
672         and don't copy FCS. */\r
673         AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);\r
674 \r
675         /* Enable Rx and Tx, plus the stats register. */\r
676         AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );\r
677 }       \r
678 /*-----------------------------------------------------------*/\r
679 \r
680 static void prvSetupMACAddress( void )\r
681 {\r
682         /* Must be written SA1L then SA1H. */\r
683         AT91C_BASE_EMAC->EMAC_SA1L =    ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |\r
684                                                                         ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |\r
685                                                                         ( ( unsigned long ) cMACAddress[ 1 ] << 8  ) |\r
686                                                                         cMACAddress[ 0 ];\r
687 \r
688         AT91C_BASE_EMAC->EMAC_SA1H =    ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |\r
689                                                                         cMACAddress[ 4 ];\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 static void prvSetupEMACInterrupt( void )\r
694 {\r
695         /* Create the semaphore used to trigger the EMAC task. */\r
696         vSemaphoreCreateBinary( xSemaphore );\r
697         if( xSemaphore )\r
698         {\r
699                 /* We start by 'taking' the semaphore so the ISR can 'give' it when the\r
700                 first interrupt occurs. */\r
701                 xSemaphoreTake( xSemaphore, emacNO_DELAY );\r
702                 portENTER_CRITICAL();\r
703                 {\r
704                         /* We want to interrupt on Rx and Tx events. */\r
705                         AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;\r
706 \r
707                         /* Enable the interrupts in the AIC. */\r
708                         AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );\r
709             AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;\r
710                 }\r
711                 portEXIT_CRITICAL();\r
712         }\r
713 }\r
714 \r
715 \r
716 \r
717 \r
718 \r
719 /*\r
720  * The following functions are initialisation functions taken from the Atmel\r
721  * EMAC sample code.\r
722  */\r
723 \r
724 \r
725 static portBASE_TYPE prvProbePHY( void )\r
726 {\r
727 unsigned long ulPHYId1, ulPHYId2, ulStatus;\r
728 portBASE_TYPE xReturn = pdPASS;\r
729         \r
730         /* Code supplied by Atmel (reformatted) -----------------*/\r
731 \r
732         /* Enable management port */\r
733         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
734         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;\r
735 \r
736         /* Read the PHY ID. */\r
737         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );\r
738         vReadPHY(AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );\r
739 \r
740         /* AMD AM79C875:\r
741                         PHY_ID1 = 0x0022\r
742                         PHY_ID2 = 0x5541\r
743                         Bits 3:0 Revision Number Four bit manufacturer?s revision number.\r
744                                 0001 stands for Rev. A, etc.\r
745         */\r
746         if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )\r
747         {\r
748                 /* Did not expect this ID. */\r
749                 xReturn = pdFAIL;\r
750         }\r
751         else\r
752         {\r
753                 ulStatus = xGetLinkSpeed();\r
754 \r
755                 if( ulStatus != pdPASS )\r
756                 {\r
757                         xReturn = pdFAIL;\r
758                 }\r
759         }\r
760 \r
761         /* Disable management port */\r
762         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;   \r
763 \r
764         /* End of code supplied by Atmel ------------------------*/\r
765 \r
766         return xReturn;\r
767 }\r
768 /*-----------------------------------------------------------*/\r
769 \r
770 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )\r
771 {\r
772         /* Code supplied by Atmel (reformatted) ----------------------*/\r
773 \r
774         AT91C_BASE_EMAC->EMAC_MAN =     (AT91C_EMAC_SOF & (0x01<<30))\r
775                                                                         | (2 << 16) | (2 << 28)\r
776                                                                         | ((ucPHYAddress & 0x1f) << 23)\r
777                                                                         | (ucAddress << 18);\r
778 \r
779         /* Wait until IDLE bit in Network Status register is cleared. */\r
780         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
781         {\r
782                 __asm( "NOP" );\r
783         }\r
784 \r
785         *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff ); \r
786 \r
787         /* End of code supplied by Atmel ------------------------*/\r
788 }\r
789 /*-----------------------------------------------------------*/\r
790 \r
791 #if USE_RMII_INTERFACE != 1\r
792 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )\r
793 {\r
794         /* Code supplied by Atmel (reformatted) ----------------------*/\r
795 \r
796         AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))\r
797                                                                 | (2 << 16) | (1 << 28)\r
798                                                                 | ((ucPHYAddress & 0x1f) << 23)\r
799                                                                 | (ucAddress << 18))\r
800                                                                 | (ulValue & 0xffff);\r
801 \r
802         /* Wait until IDLE bit in Network Status register is cleared */\r
803         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
804         {\r
805                 __asm( "NOP" );\r
806         };\r
807 \r
808         /* End of code supplied by Atmel ------------------------*/\r
809 }\r
810 #endif\r
811 /*-----------------------------------------------------------*/\r
812 \r
813 static portBASE_TYPE xGetLinkSpeed( void )\r
814 {\r
815         unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;\r
816 \r
817         /* Code supplied by Atmel (reformatted) -----------------*/\r
818 \r
819         /* Link status is latched, so read twice to get current value */\r
820         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
821         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
822 \r
823         if( !( ulBMSR & BMSR_LSTATUS ) )\r
824         {       \r
825                 /* No Link. */\r
826                 return pdFAIL;\r
827         }\r
828 \r
829         vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);\r
830         if (ulBMCR & BMCR_ANENABLE)\r
831         {                               \r
832                 /* AutoNegotiation is enabled. */\r
833                 if (!(ulBMSR & BMSR_ANEGCOMPLETE))\r
834                 {\r
835                         /* Auto-negotitation in progress. */\r
836                         return pdFAIL;                          \r
837                 }               \r
838 \r
839                 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);\r
840                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )\r
841                 {\r
842                         ulSpeed = SPEED_100;\r
843                 }\r
844                 else\r
845                 {\r
846                         ulSpeed = SPEED_10;\r
847                 }\r
848 \r
849                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )\r
850                 {\r
851                         ulDuplex = DUPLEX_FULL;\r
852                 }\r
853                 else\r
854                 {\r
855                         ulDuplex = DUPLEX_HALF;\r
856                 }\r
857         }\r
858         else\r
859         {\r
860                 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;\r
861                 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;\r
862         }\r
863 \r
864         /* Update the MAC */\r
865         ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );\r
866         if( ulSpeed == SPEED_100 )\r
867         {\r
868                 if( ulDuplex == DUPLEX_FULL )\r
869                 {\r
870                         /* 100 Full Duplex */\r
871                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;\r
872                 }\r
873                 else\r
874                 {                                       \r
875                         /* 100 Half Duplex */\r
876                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;\r
877                 }\r
878         }\r
879         else\r
880         {\r
881                 if (ulDuplex == DUPLEX_FULL)\r
882                 {\r
883                         /* 10 Full Duplex */\r
884                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;\r
885                 }\r
886                 else\r
887                 {                       /* 10 Half Duplex */\r
888                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;\r
889                 }\r
890         }\r
891 \r
892         /* End of code supplied by Atmel ------------------------*/\r
893 \r
894         return pdPASS;\r
895 }\r
896 /*-----------------------------------------------------------*/\r
897 \r
898 void vEMACWaitForInput( void )\r
899 {\r
900         /* Just wait until we are signled from an ISR that data is available, or\r
901         we simply time out. */\r
902         xSemaphoreTake( xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );\r
903 }\r