]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/webserver/SAM7_EMAC.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / webserver / SAM7_EMAC.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 /* Standard includes. */\r
67 #include <string.h>\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "semphr.h"\r
72 #include "task.h"\r
73 \r
74 /* Demo application includes. */\r
75 #include "SAM7_EMAC.h"\r
76 \r
77 /* uIP includes. */\r
78 #include "uip.h"\r
79 \r
80 /* Hardware specific includes. */\r
81 #include "Emac.h"\r
82 #include "mii.h"\r
83 #include "AT91SAM7X256.h"\r
84 \r
85 \r
86 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0\r
87 to use an MII interface. */\r
88 #define USE_RMII_INTERFACE 0\r
89 \r
90 /* The buffer addresses written into the descriptors must be aligned so the\r
91 last few bits are zero.  These bits have special meaning for the EMAC\r
92 peripheral and cannot be used as part of the address. */\r
93 #define emacADDRESS_MASK                        ( ( unsigned long ) 0xFFFFFFFC )\r
94 \r
95 /* Bit used within the address stored in the descriptor to mark the last\r
96 descriptor in the array. */\r
97 #define emacRX_WRAP_BIT                         ( ( unsigned long ) 0x02 )\r
98 \r
99 /* Bit used within the Tx descriptor status to indicate whether the\r
100 descriptor is under the control of the EMAC or the software. */\r
101 #define emacTX_BUF_USED                         ( ( unsigned long ) 0x80000000 )\r
102 \r
103 /* A short delay is used to wait for a buffer to become available, should\r
104 one not be immediately available when trying to transmit a frame. */\r
105 #define emacBUFFER_WAIT_DELAY           ( 2 )\r
106 #define emacMAX_WAIT_CYCLES                     ( configTICK_RATE_HZ / 40 )\r
107 \r
108 /* Misc defines. */\r
109 #define emacINTERRUPT_LEVEL                     ( 5 )\r
110 #define emacNO_DELAY                            ( 0 )\r
111 #define emacTOTAL_FRAME_HEADER_SIZE     ( 54 )\r
112 #define emacPHY_INIT_DELAY                      ( 5000 / portTICK_PERIOD_MS )\r
113 #define emacRESET_KEY                           ( ( unsigned long ) 0xA5000000 )\r
114 #define emacRESET_LENGTH                        ( ( unsigned long ) ( 0x01 << 8 ) )\r
115 \r
116 /* The Atmel header file only defines the TX frame length mask. */\r
117 #define emacRX_LENGTH_FRAME                     ( 0xfff )\r
118 \r
119 /* Peripheral setup for the EMAC. */\r
120 #define emacPERIPHERAL_A_SETUP          ( ( unsigned long ) AT91C_PB2_ETX0                      ) | \\r
121                                                                         ( ( unsigned long ) AT91C_PB12_ETXER            ) | \\r
122                                                                         ( ( unsigned long ) AT91C_PB16_ECOL                     ) | \\r
123                                                                         ( ( unsigned long ) AT91C_PB11_ETX3                     ) | \\r
124                                                                         ( ( unsigned long ) AT91C_PB6_ERX1                      ) | \\r
125                                                                         ( ( unsigned long ) AT91C_PB15_ERXDV            ) | \\r
126                                                                         ( ( unsigned long ) AT91C_PB13_ERX2                     ) | \\r
127                                                                         ( ( unsigned long ) AT91C_PB3_ETX1                      ) | \\r
128                                                                         ( ( unsigned long ) AT91C_PB8_EMDC                      ) | \\r
129                                                                         ( ( unsigned long ) AT91C_PB5_ERX0                      ) | \\r
130                                                                         ( ( unsigned long ) AT91C_PB14_ERX3                     ) | \\r
131                                                                         ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV       ) | \\r
132                                                                         ( ( unsigned long ) AT91C_PB1_ETXEN                     ) | \\r
133                                                                         ( ( unsigned long ) AT91C_PB10_ETX2                     ) | \\r
134                                                                         ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK      ) | \\r
135                                                                         ( ( unsigned long ) AT91C_PB9_EMDIO                     ) | \\r
136                                                                         ( ( unsigned long ) AT91C_PB7_ERXER                     ) | \\r
137                                                                         ( ( unsigned long ) AT91C_PB17_ERXCK            );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * Prototype for the EMAC interrupt function - called by the asm wrapper.\r
143  */\r
144 extern void vEMACISR_Wrapper( void ) __attribute__((naked));\r
145 \r
146 /*\r
147  * Initialise both the Tx and Rx descriptors used by the EMAC.\r
148  */\r
149 static void prvSetupDescriptors(void);\r
150 \r
151 /*\r
152  * Write our MAC address into the EMAC.  The MAC address is set as one of the\r
153  * uip options.\r
154  */\r
155 static void prvSetupMACAddress( void );\r
156 \r
157 /*\r
158  * Configure the EMAC and AIC for EMAC interrupts.\r
159  */\r
160 static void prvSetupEMACInterrupt( void );\r
161 \r
162 /*\r
163  * Some initialisation functions taken from the Atmel EMAC sample code.\r
164  */\r
165 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );\r
166 #if USE_RMII_INTERFACE != 1\r
167         static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);\r
168 #endif\r
169 static portBASE_TYPE xGetLinkSpeed( void );\r
170 static portBASE_TYPE prvProbePHY( void );\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 #pragma data_alignment=8\r
177 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ];\r
178 \r
179 /* Buffer read by the EMAC DMA.  Must be aligned as described by he comment\r
180 above the emacADDRESS_MASK definition. */\r
181 #pragma data_alignment=8\r
182 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ];\r
183 \r
184 /* Descriptors used to communicate between the program and the EMAC peripheral.\r
185 These descriptors hold the locations and state of the Rx and Tx buffers. */\r
186 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];\r
187 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];\r
188 \r
189 /* The IP and Ethernet addresses are read from the uIP setup. */\r
190 const char cMACAddress[ 6 ] = { uipMAC_ADDR0, uipMAC_ADDR1, uipMAC_ADDR2, uipMAC_ADDR3, uipMAC_ADDR4, uipMAC_ADDR5 };\r
191 const unsigned char ucIPAddress[ 4 ]  = { uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 };\r
192 \r
193 /* The semaphore used by the EMAC ISR to wake the EMAC task. */\r
194 static SemaphoreHandle_t xSemaphore = NULL;\r
195 \r
196 /*-----------------------------------------------------------*/\r
197 \r
198 SemaphoreHandle_t xEMACInit( void )\r
199 {\r
200         /* Code supplied by Atmel -------------------------------*/\r
201 \r
202         /* Disable pull up on RXDV => PHY normal mode (not in test mode),\r
203         PHY has internal pull down. */\r
204         AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;\r
205 \r
206         #if USE_RMII_INTERFACE != 1\r
207                 /* PHY has internal pull down : set MII mode. */\r
208                 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;\r
209         #endif\r
210 \r
211         /* Clear PB18 <=> PHY powerdown. */\r
212         AT91C_BASE_PIOB->PIO_PER = 1 << 18;\r
213         AT91C_BASE_PIOB->PIO_OER = 1 << 18;\r
214         AT91C_BASE_PIOB->PIO_CODR = 1 << 18;\r
215 \r
216         /* After PHY power up, hardware reset. */\r
217         AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;\r
218         AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;\r
219 \r
220         /* Wait for hardware reset end. */\r
221         while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )\r
222         {\r
223                 __asm volatile ( "NOP" );\r
224         }\r
225     __asm volatile ( "NOP" );\r
226 \r
227         /* Setup the pins. */\r
228         AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;\r
229         AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;\r
230 \r
231         /* Enable com between EMAC PHY.\r
232 \r
233         Enable management port. */\r
234         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
235 \r
236         /* MDC = MCK/32. */\r
237         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;     \r
238 \r
239         /* Wait for PHY auto init end (rather crude delay!). */\r
240         vTaskDelay( emacPHY_INIT_DELAY );\r
241 \r
242         /* PHY configuration. */\r
243         #if USE_RMII_INTERFACE != 1\r
244         {\r
245                 unsigned long ulControl;\r
246 \r
247                 /* PHY has internal pull down : disable MII isolate. */\r
248                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
249                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
250                 ulControl &= ~BMCR_ISOLATE;\r
251                 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );\r
252         }\r
253         #endif\r
254 \r
255         /* Disable management port again. */\r
256         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;\r
257 \r
258         #if USE_RMII_INTERFACE != 1\r
259                 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */\r
260                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;\r
261         #else\r
262                 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator\r
263                 on ERFCK). */\r
264                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;\r
265         #endif\r
266 \r
267         /* End of code supplied by Atmel ------------------------*/\r
268 \r
269         /* Setup the buffers and descriptors. */\r
270         prvSetupDescriptors();\r
271         \r
272         /* Load our MAC address into the EMAC. */\r
273         prvSetupMACAddress();\r
274 \r
275         /* Are we connected? */\r
276         if( prvProbePHY() )\r
277         {\r
278                 /* Enable the interrupt! */\r
279                 portENTER_CRITICAL();\r
280                 {\r
281                         prvSetupEMACInterrupt();\r
282                         vPassEMACSemaphore( xSemaphore );\r
283                 }\r
284                 portEXIT_CRITICAL();\r
285         }\r
286 \r
287         return xSemaphore;\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 long lEMACSend( void )\r
292 {\r
293 static unsigned portBASE_TYPE uxTxBufferIndex = 0;\r
294 portBASE_TYPE xWaitCycles = 0;\r
295 long lReturn = pdPASS;\r
296 char *pcBuffer;\r
297 \r
298         /* Is a buffer available? */\r
299         while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )\r
300         {\r
301                 /* There is no room to write the Tx data to the Tx buffer.  Wait a\r
302                 short while, then try again. */\r
303                 xWaitCycles++;\r
304                 if( xWaitCycles > emacMAX_WAIT_CYCLES )\r
305                 {\r
306                         /* Give up. */\r
307                         lReturn = pdFAIL;\r
308                         break;\r
309                 }\r
310                 else\r
311                 {\r
312                         vTaskDelay( emacBUFFER_WAIT_DELAY );\r
313                 }\r
314         }\r
315 \r
316         /* lReturn will only be pdPASS if a buffer is available. */\r
317         if( lReturn == pdPASS )\r
318         {\r
319                 /* Copy the headers into the Tx buffer.  These will be in the uIP buffer. */\r
320                 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;\r
321                 memcpy( ( void * ) pcBuffer, ( void * ) uip_buf, emacTOTAL_FRAME_HEADER_SIZE );\r
322 \r
323                 /* If there is room, also copy in the application data if any. */\r
324                 if( ( uip_len > emacTOTAL_FRAME_HEADER_SIZE ) && ( uip_len <= ( ETH_TX_BUFFER_SIZE - emacTOTAL_FRAME_HEADER_SIZE ) ) )\r
325                 {\r
326                         memcpy( ( void * ) &( pcBuffer[ emacTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - emacTOTAL_FRAME_HEADER_SIZE ) );\r
327                 }\r
328 \r
329                 /* Send. */     \r
330                 portENTER_CRITICAL();\r
331                 {\r
332                         if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )\r
333                         {\r
334                                 /* Fill out the necessary in the descriptor to get the data sent. */\r
335                                 xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )\r
336                                                                                                                                                 | AT91C_LAST_BUFFER\r
337                                                                                                                                                 | AT91C_TRANSMIT_WRAP;\r
338                                 uxTxBufferIndex = 0;\r
339                         }\r
340                         else\r
341                         {\r
342                                 /* Fill out the necessary in the descriptor to get the data sent. */\r
343                                 xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )\r
344                                                                                                                                                 | AT91C_LAST_BUFFER;\r
345                                 uxTxBufferIndex++;\r
346                         }\r
347         \r
348                         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;\r
349                 }\r
350                 portEXIT_CRITICAL();\r
351         }\r
352 \r
353         return lReturn;\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 unsigned long ulEMACPoll( void )\r
358 {\r
359 static unsigned portBASE_TYPE ulNextRxBuffer = 0;\r
360 unsigned long ulSectionLength = 0, ulLengthSoFar = 0, ulEOF = pdFALSE;\r
361 char *pcSource;\r
362 \r
363         /* Skip any fragments. */\r
364         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )\r
365         {\r
366                 /* Mark the buffer as free again. */\r
367                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );              \r
368                 ulNextRxBuffer++;\r
369                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
370                 {\r
371                         ulNextRxBuffer = 0;\r
372                 }\r
373         }\r
374 \r
375         /* Is there a packet ready? */\r
376 \r
377         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !ulSectionLength )\r
378         {\r
379                 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
380                 ulSectionLength = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & emacRX_LENGTH_FRAME;\r
381 \r
382                 if( ulSectionLength == 0 )\r
383                 {\r
384                         /* The frame is longer than the buffer pointed to by this\r
385                         descriptor so copy the entire buffer to uIP - then move onto\r
386                         the next descriptor to get the rest of the frame. */\r
387                         if( ( ulLengthSoFar + ETH_RX_BUFFER_SIZE ) <= UIP_BUFSIZE )\r
388                         {\r
389                                 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ETH_RX_BUFFER_SIZE );\r
390                                 ulLengthSoFar += ETH_RX_BUFFER_SIZE;\r
391                         }                       \r
392                 }\r
393                 else\r
394                 {\r
395                         /* This is the last section of the frame.  Copy the section to\r
396                         uIP. */\r
397                         if( ulSectionLength < UIP_BUFSIZE )\r
398                         {\r
399                                 /* The section length holds the length of the entire frame.\r
400                                 ulLengthSoFar holds the length of the frame sections already\r
401                                 copied to uIP, so the length of the final section is\r
402                                 ulSectionLength - ulLengthSoFar; */\r
403                                 if( ulSectionLength > ulLengthSoFar )\r
404                                 {\r
405                                         memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ( ulSectionLength - ulLengthSoFar ) );\r
406                                 }\r
407                         }                       \r
408 \r
409                         /* Is this the last buffer for the frame?  If not why? */\r
410                         ulEOF = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_EOF;\r
411                 }\r
412 \r
413                 /* Mark the buffer as free again. */\r
414                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
415 \r
416                 /* Increment to the next buffer, wrapping if necessary. */\r
417                 ulNextRxBuffer++;\r
418                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
419                 {\r
420                         ulNextRxBuffer = 0;\r
421                 }\r
422         }\r
423 \r
424         /* If we obtained data but for some reason did not find the end of the\r
425         frame then discard the data as it must contain an error. */\r
426         if( !ulEOF )\r
427         {\r
428                 ulSectionLength = 0;\r
429         }\r
430 \r
431         return ulSectionLength;\r
432 }\r
433 /*-----------------------------------------------------------*/\r
434 \r
435 static void prvSetupDescriptors(void)\r
436 {\r
437 unsigned portBASE_TYPE xIndex;\r
438 unsigned long ulAddress;\r
439 \r
440         /* Initialise xRxDescriptors descriptor. */\r
441         for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )\r
442         {\r
443                 /* Calculate the address of the nth buffer within the array. */\r
444                 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );\r
445 \r
446                 /* Write the buffer address into the descriptor.  The DMA will place\r
447                 the data at this address when this descriptor is being used.  Mask off\r
448                 the bottom bits of the address as these have special meaning. */\r
449                 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
450         }       \r
451 \r
452         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
453         to the first buffer. */\r
454         xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;\r
455 \r
456         /* Initialise xTxDescriptors. */\r
457         for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )\r
458         {\r
459                 /* Calculate the address of the nth buffer within the array. */\r
460                 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );\r
461 \r
462                 /* Write the buffer address into the descriptor.  The DMA will read\r
463                 data from here when the descriptor is being used. */\r
464                 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
465                 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;\r
466         }       \r
467 \r
468         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
469         to the first buffer. */\r
470         xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;\r
471 \r
472         /* Tell the EMAC where to find the descriptors. */\r
473         AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;\r
474         AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;\r
475         \r
476         /* Clear all the bits in the receive status register. */\r
477         AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );\r
478 \r
479         /* Enable the copy of data into the buffers, ignore broadcasts,\r
480         and don't copy FCS. */\r
481         AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);\r
482 \r
483         /* Enable Rx and Tx, plus the stats register. */\r
484         AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );\r
485 }       \r
486 /*-----------------------------------------------------------*/\r
487 \r
488 static void prvSetupMACAddress( void )\r
489 {\r
490         /* Must be written SA1L then SA1H. */\r
491         AT91C_BASE_EMAC->EMAC_SA1L =    ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |\r
492                                                                         ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |\r
493                                                                         ( ( unsigned long ) cMACAddress[ 1 ] << 8  ) |\r
494                                                                         cMACAddress[ 0 ];\r
495 \r
496         AT91C_BASE_EMAC->EMAC_SA1H =    ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |\r
497                                                                         cMACAddress[ 4 ];\r
498 }\r
499 /*-----------------------------------------------------------*/\r
500 \r
501 static void prvSetupEMACInterrupt( void )\r
502 {\r
503         /* Create the semaphore used to trigger the EMAC task. */\r
504         vSemaphoreCreateBinary( xSemaphore );\r
505         if( xSemaphore )\r
506         {\r
507                 /* We start by 'taking' the semaphore so the ISR can 'give' it when the\r
508                 first interrupt occurs. */\r
509                 xSemaphoreTake( xSemaphore, emacNO_DELAY );\r
510                 portENTER_CRITICAL();\r
511                 {\r
512                         /* We want to interrupt on Rx events. */\r
513                         AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP;\r
514 \r
515                         /* Enable the interrupts in the AIC. */\r
516                         AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );\r
517                         AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;\r
518                 }\r
519                 portEXIT_CRITICAL();\r
520         }\r
521 }\r
522 /*-----------------------------------------------------------*/\r
523 \r
524 \r
525 \r
526 \r
527 /*\r
528  * The following functions are initialisation functions taken from the Atmel\r
529  * EMAC sample code.\r
530  */\r
531 \r
532 static portBASE_TYPE prvProbePHY( void )\r
533 {\r
534 unsigned long ulPHYId1, ulPHYId2, ulStatus;\r
535 portBASE_TYPE xReturn = pdPASS;\r
536         \r
537         /* Code supplied by Atmel (reformatted) -----------------*/\r
538 \r
539         /* Enable management port */\r
540         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
541         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;\r
542 \r
543         /* Read the PHY ID. */\r
544         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );\r
545         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );\r
546 \r
547         /* AMD AM79C875:\r
548                         PHY_ID1 = 0x0022\r
549                         PHY_ID2 = 0x5541\r
550                         Bits 3:0 Revision Number Four bit manufacturer\92s revision number.\r
551                                 0001 stands for Rev. A, etc.\r
552         */\r
553         if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )\r
554         {\r
555                 /* Did not expect this ID. */\r
556                 xReturn = pdFAIL;\r
557         }\r
558         else\r
559         {\r
560                 ulStatus = xGetLinkSpeed();\r
561 \r
562                 if( ulStatus != pdPASS )\r
563                 {\r
564                         xReturn = pdFAIL;\r
565                 }\r
566         }\r
567 \r
568         /* Disable management port */\r
569         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;   \r
570 \r
571         /* End of code supplied by Atmel ------------------------*/\r
572 \r
573         return xReturn;\r
574 }\r
575 /*-----------------------------------------------------------*/\r
576 \r
577 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )\r
578 {\r
579         /* Code supplied by Atmel (reformatted) ----------------------*/\r
580 \r
581         AT91C_BASE_EMAC->EMAC_MAN =     (AT91C_EMAC_SOF & (0x01<<30))\r
582                                                                         | (2 << 16) | (2 << 28)\r
583                                                                         | ((ucPHYAddress & 0x1f) << 23)\r
584                                                                         | (ucAddress << 18);\r
585 \r
586         /* Wait until IDLE bit in Network Status register is cleared. */\r
587         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
588         {\r
589                 __asm( "NOP" );\r
590         }\r
591 \r
592         *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff ); \r
593 \r
594         /* End of code supplied by Atmel ------------------------*/\r
595 }\r
596 /*-----------------------------------------------------------*/\r
597 \r
598 #if USE_RMII_INTERFACE != 1\r
599 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )\r
600 {\r
601         /* Code supplied by Atmel (reformatted) ----------------------*/\r
602 \r
603         AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))\r
604                                                                 | (2 << 16) | (1 << 28)\r
605                                                                 | ((ucPHYAddress & 0x1f) << 23)\r
606                                                                 | (ucAddress << 18))\r
607                                                                 | (ulValue & 0xffff);\r
608 \r
609         /* Wait until IDLE bit in Network Status register is cleared */\r
610         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
611         {\r
612                 __asm( "NOP" );\r
613         };\r
614 \r
615         /* End of code supplied by Atmel ------------------------*/\r
616 }\r
617 #endif\r
618 /*-----------------------------------------------------------*/\r
619 \r
620 static portBASE_TYPE xGetLinkSpeed( void )\r
621 {\r
622         unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;\r
623 \r
624         /* Code supplied by Atmel (reformatted) -----------------*/\r
625 \r
626         /* Link status is latched, so read twice to get current value */\r
627         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
628         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
629 \r
630         if( !( ulBMSR & BMSR_LSTATUS ) )\r
631         {       \r
632                 /* No Link. */\r
633                 return pdFAIL;\r
634         }\r
635 \r
636         vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);\r
637         if (ulBMCR & BMCR_ANENABLE)\r
638         {                               \r
639                 /* AutoNegotiation is enabled. */\r
640                 if (!(ulBMSR & BMSR_ANEGCOMPLETE))\r
641                 {\r
642                         /* Auto-negotiation in progress. */\r
643                         return pdFAIL;                          \r
644                 }               \r
645 \r
646                 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);\r
647                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )\r
648                 {\r
649                         ulSpeed = SPEED_100;\r
650                 }\r
651                 else\r
652                 {\r
653                         ulSpeed = SPEED_10;\r
654                 }\r
655 \r
656                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )\r
657                 {\r
658                         ulDuplex = DUPLEX_FULL;\r
659                 }\r
660                 else\r
661                 {\r
662                         ulDuplex = DUPLEX_HALF;\r
663                 }\r
664         }\r
665         else\r
666         {\r
667                 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;\r
668                 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;\r
669         }\r
670 \r
671         /* Update the MAC */\r
672         ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );\r
673         if( ulSpeed == SPEED_100 )\r
674         {\r
675                 if( ulDuplex == DUPLEX_FULL )\r
676                 {\r
677                         /* 100 Full Duplex */\r
678                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;\r
679                 }\r
680                 else\r
681                 {                                       \r
682                         /* 100 Half Duplex */\r
683                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;\r
684                 }\r
685         }\r
686         else\r
687         {\r
688                 if (ulDuplex == DUPLEX_FULL)\r
689                 {\r
690                         /* 10 Full Duplex */\r
691                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;\r
692                 }\r
693                 else\r
694                 {\r
695                         /* 10 Half Duplex */\r
696                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;\r
697                 }\r
698         }\r
699 \r
700         /* End of code supplied by Atmel ------------------------*/\r
701 \r
702         return pdPASS;\r
703 }\r