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