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