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