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