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