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