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