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