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