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