2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 /* Hardware specific includes. */
\r
97 #include "iodefine.h"
\r
98 #include "typedefine.h"
\r
99 #include "r_ether.h"
\r
102 /* FreeRTOS includes. */
\r
103 #include "FreeRTOS.h"
\r
105 #include "semphr.h"
\r
107 /* uIP includes. */
\r
108 #include "net/uip.h"
\r
110 /* The time to wait between attempts to obtain a free buffer. */
\r
111 #define emacBUFFER_WAIT_DELAY_ms ( 3 / portTICK_PERIOD_MS )
\r
113 /* The number of times emacBUFFER_WAIT_DELAY_ms should be waited before giving
\r
114 up on attempting to obtain a free buffer all together. */
\r
115 #define emacBUFFER_WAIT_ATTEMPTS ( 30 )
\r
117 /* The number of Rx descriptors. */
\r
118 #define emacNUM_RX_DESCRIPTORS 8
\r
120 /* The number of Tx descriptors. When using uIP there is not point in having
\r
122 #define emacNUM_TX_BUFFERS 2
\r
124 /* The total number of EMAC buffers to allocate. */
\r
125 #define emacNUM_BUFFERS ( emacNUM_RX_DESCRIPTORS + emacNUM_TX_BUFFERS )
\r
127 /* The time to wait for the Tx descriptor to become free. */
\r
128 #define emacTX_WAIT_DELAY_ms ( 10 / portTICK_PERIOD_MS )
\r
130 /* The total number of times to wait emacTX_WAIT_DELAY_ms for the Tx descriptor to
\r
132 #define emacTX_WAIT_ATTEMPTS ( 50 )
\r
134 /* Only Rx end and Tx end interrupts are used by this driver. */
\r
135 #define emacTX_END_INTERRUPT ( 1UL << 21UL )
\r
136 #define emacRX_END_INTERRUPT ( 1UL << 18UL )
\r
138 /*-----------------------------------------------------------*/
\r
140 /* The buffers and descriptors themselves. */
\r
141 #pragma section _RX_DESC
\r
142 volatile ethfifo xRxDescriptors[ emacNUM_RX_DESCRIPTORS ];
\r
143 #pragma section _TX_DESC
\r
144 volatile ethfifo xTxDescriptors[ emacNUM_TX_BUFFERS ];
\r
145 #pragma section _ETHERNET_BUFFERS
\r
148 unsigned long ulAlignmentVariable;
\r
149 char cBuffer[ emacNUM_BUFFERS ][ UIP_BUFSIZE ];
\r
150 } xEthernetBuffers;
\r
156 /* Used to indicate which buffers are free and which are in use. If an index
\r
157 contains 0 then the corresponding buffer in xEthernetBuffers is free, otherwise
\r
158 the buffer is in use or about to be used. */
\r
159 static unsigned char ucBufferInUse[ emacNUM_BUFFERS ];
\r
161 /*-----------------------------------------------------------*/
\r
164 * Initialise both the Rx and Tx descriptors.
\r
166 static void prvInitialiseDescriptors( void );
\r
169 * Return a pointer to a free buffer within xEthernetBuffers.
\r
171 static unsigned char *prvGetNextBuffer( void );
\r
174 * Return a buffer to the list of free buffers.
\r
176 static void prvReturnBuffer( unsigned char *pucBuffer );
\r
179 * Examine the status of the next Rx FIFO to see if it contains new data.
\r
181 static unsigned long prvCheckRxFifoStatus( void );
\r
184 * Setup the microcontroller for communication with the PHY.
\r
186 static void prvResetMAC( void );
\r
189 * Configure the Ethernet interface peripherals.
\r
191 static void prvConfigureEtherCAndEDMAC( void );
\r
194 * Something has gone wrong with the descriptor usage. Reset all the buffers
\r
197 static void prvResetEverything( void );
\r
199 /*-----------------------------------------------------------*/
\r
201 /* Points to the Rx descriptor currently in use. */
\r
202 static ethfifo *pxCurrentRxDesc = NULL;
\r
204 /* The buffer used by the uIP stack to both receive and send. This points to
\r
205 one of the Ethernet buffers when its actually in use. */
\r
206 unsigned char *uip_buf = NULL;
\r
208 /*-----------------------------------------------------------*/
\r
210 void vInitEmac( void )
\r
212 /* Software reset. */
\r
215 /* Set the Rx and Tx descriptors into their initial state. */
\r
216 prvInitialiseDescriptors();
\r
218 /* Set the MAC address into the ETHERC */
\r
219 ETHERC.MAHR = ( ( unsigned long ) configMAC_ADDR0 << 24UL ) |
\r
220 ( ( unsigned long ) configMAC_ADDR1 << 16UL ) |
\r
221 ( ( unsigned long ) configMAC_ADDR2 << 8UL ) |
\r
222 ( unsigned long ) configMAC_ADDR3;
\r
224 ETHERC.MALR.BIT.MA = ( ( unsigned long ) configMAC_ADDR4 << 8UL ) |
\r
225 ( unsigned long ) configMAC_ADDR5;
\r
227 /* Perform rest of interface hardware configuration. */
\r
228 prvConfigureEtherCAndEDMAC();
\r
230 /* Nothing received yet, so uip_buf points nowhere. */
\r
233 /* Initialize the PHY */
\r
236 /*-----------------------------------------------------------*/
\r
238 void vEMACWrite( void )
\r
242 /* Wait until the second transmission of the last packet has completed. */
\r
243 for( x = 0; x < emacTX_WAIT_ATTEMPTS; x++ )
\r
245 if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )
\r
247 /* Descriptor is still active. */
\r
248 vTaskDelay( emacTX_WAIT_DELAY_ms );
\r
256 /* Is the descriptor free after waiting for it? */
\r
257 if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )
\r
259 /* Something has gone wrong. */
\r
260 prvResetEverything();
\r
263 /* Setup both descriptors to transmit the frame. */
\r
264 xTxDescriptors[ 0 ].buf_p = ( char * ) uip_buf;
\r
265 xTxDescriptors[ 0 ].bufsize = uip_len;
\r
266 xTxDescriptors[ 1 ].buf_p = ( char * ) uip_buf;
\r
267 xTxDescriptors[ 1 ].bufsize = uip_len;
\r
269 /* uip_buf is being sent by the Tx descriptor. Allocate a new buffer
\r
270 for use by the stack. */
\r
271 uip_buf = prvGetNextBuffer();
\r
273 /* Clear previous settings and go. */
\r
274 xTxDescriptors[0].status &= ~( FP1 | FP0 );
\r
275 xTxDescriptors[0].status |= ( FP1 | FP0 | ACT );
\r
276 xTxDescriptors[1].status &= ~( FP1 | FP0 );
\r
277 xTxDescriptors[1].status |= ( FP1 | FP0 | ACT );
\r
279 EDMAC.EDTRR.LONG = 0x00000001;
\r
281 /*-----------------------------------------------------------*/
\r
283 unsigned long ulEMACRead( void )
\r
285 unsigned long ulBytesReceived;
\r
287 ulBytesReceived = prvCheckRxFifoStatus();
\r
289 if( ulBytesReceived > 0 )
\r
291 /* Mark the pxDescriptor buffer as free as uip_buf is going to be set to
\r
292 the buffer that contains the received data. */
\r
293 prvReturnBuffer( uip_buf );
\r
295 /* Point uip_buf to the data about ot be processed. */
\r
296 uip_buf = ( void * ) pxCurrentRxDesc->buf_p;
\r
298 /* Allocate a new buffer to the descriptor, as uip_buf is now using it's
\r
300 pxCurrentRxDesc->buf_p = prvGetNextBuffer();
\r
302 /* Prepare the descriptor to go again. */
\r
303 pxCurrentRxDesc->status &= ~( FP1 | FP0 );
\r
304 pxCurrentRxDesc->status |= ACT;
\r
306 /* Move onto the next buffer in the ring. */
\r
307 pxCurrentRxDesc = pxCurrentRxDesc->next;
\r
309 if( EDMAC.EDRRR.LONG == 0x00000000L )
\r
311 /* Restart Ethernet if it has stopped */
\r
312 EDMAC.EDRRR.LONG = 0x00000001L;
\r
316 return ulBytesReceived;
\r
318 /*-----------------------------------------------------------*/
\r
320 long lEMACWaitForLink( void )
\r
324 /* Set the link status. */
\r
325 switch( phy_set_autonegotiate() )
\r
327 /* Half duplex link */
\r
328 case PHY_LINK_100H:
\r
329 ETHERC.ECMR.BIT.DM = 0;
\r
330 ETHERC.ECMR.BIT.RTM = 1;
\r
335 ETHERC.ECMR.BIT.DM = 0;
\r
336 ETHERC.ECMR.BIT.RTM = 0;
\r
341 /* Full duplex link */
\r
342 case PHY_LINK_100F:
\r
343 ETHERC.ECMR.BIT.DM = 1;
\r
344 ETHERC.ECMR.BIT.RTM = 1;
\r
349 ETHERC.ECMR.BIT.DM = 1;
\r
350 ETHERC.ECMR.BIT.RTM = 0;
\r
359 if( lReturn == pdPASS )
\r
361 /* Enable receive and transmit. */
\r
362 ETHERC.ECMR.BIT.RE = 1;
\r
363 ETHERC.ECMR.BIT.TE = 1;
\r
365 /* Enable EDMAC receive */
\r
366 EDMAC.EDRRR.LONG = 0x1;
\r
371 /*-----------------------------------------------------------*/
\r
373 static void prvInitialiseDescriptors( void )
\r
375 ethfifo *pxDescriptor;
\r
378 for( x = 0; x < emacNUM_BUFFERS; x++ )
\r
380 /* Ensure none of the buffers are shown as in use at the start. */
\r
381 ucBufferInUse[ x ] = pdFALSE;
\r
384 /* Initialise the Rx descriptors. */
\r
385 for( x = 0; x < emacNUM_RX_DESCRIPTORS; x++ )
\r
387 pxDescriptor = &( xRxDescriptors[ x ] );
\r
388 pxDescriptor->buf_p = &( xEthernetBuffers.cBuffer[ x ][ 0 ] );
\r
390 pxDescriptor->bufsize = UIP_BUFSIZE;
\r
391 pxDescriptor->size = 0;
\r
392 pxDescriptor->status = ACT;
\r
393 pxDescriptor->next = &xRxDescriptors[ x + 1 ];
\r
395 /* Mark this buffer as in use. */
\r
396 ucBufferInUse[ x ] = pdTRUE;
\r
399 /* The last descriptor points back to the start. */
\r
400 pxDescriptor->status |= DL;
\r
401 pxDescriptor->next = &xRxDescriptors[ 0 ];
\r
403 /* Initialise the Tx descriptors. */
\r
404 for( x = 0; x < emacNUM_TX_BUFFERS; x++ )
\r
406 pxDescriptor = &( xTxDescriptors[ x ] );
\r
408 /* A buffer is not allocated to the Tx descriptor until a send is
\r
409 actually required. */
\r
410 pxDescriptor->buf_p = NULL;
\r
412 pxDescriptor->bufsize = UIP_BUFSIZE;
\r
413 pxDescriptor->size = 0;
\r
414 pxDescriptor->status = 0;
\r
415 pxDescriptor->next = &xTxDescriptors[ x + 1 ];
\r
418 /* The last descriptor points back to the start. */
\r
419 pxDescriptor->status |= DL;
\r
420 pxDescriptor->next = &( xTxDescriptors[ 0 ] );
\r
422 /* Use the first Rx descriptor to start with. */
\r
423 pxCurrentRxDesc = &( xRxDescriptors[ 0 ] );
\r
425 /*-----------------------------------------------------------*/
\r
427 static unsigned char *prvGetNextBuffer( void )
\r
430 unsigned char *pucReturn = NULL;
\r
431 unsigned long ulAttempts = 0;
\r
433 while( pucReturn == NULL )
\r
435 /* Look through the buffers to find one that is not in use by
\r
437 for( x = 0; x < emacNUM_BUFFERS; x++ )
\r
439 if( ucBufferInUse[ x ] == pdFALSE )
\r
441 ucBufferInUse[ x ] = pdTRUE;
\r
442 pucReturn = ( unsigned char * ) &( xEthernetBuffers.cBuffer[ x ][ 0 ] );
\r
447 /* Was a buffer found? */
\r
448 if( pucReturn == NULL )
\r
452 if( ulAttempts >= emacBUFFER_WAIT_ATTEMPTS )
\r
457 /* Wait then look again. */
\r
458 vTaskDelay( emacBUFFER_WAIT_DELAY_ms );
\r
464 /*-----------------------------------------------------------*/
\r
466 static void prvReturnBuffer( unsigned char *pucBuffer )
\r
470 /* Return a buffer to the pool of free buffers. */
\r
471 for( ul = 0; ul < emacNUM_BUFFERS; ul++ )
\r
473 if( &( xEthernetBuffers.cBuffer[ ul ][ 0 ] ) == ( void * ) pucBuffer )
\r
475 ucBufferInUse[ ul ] = pdFALSE;
\r
480 /*-----------------------------------------------------------*/
\r
482 static void prvResetEverything( void )
\r
484 /* Temporary code just to see if this gets called. This function has not
\r
485 been implemented. */
\r
486 portDISABLE_INTERRUPTS();
\r
489 /*-----------------------------------------------------------*/
\r
491 static unsigned long prvCheckRxFifoStatus( void )
\r
493 unsigned long ulReturn = 0;
\r
495 if( ( pxCurrentRxDesc->status & ACT ) != 0 )
\r
497 /* Current descriptor is still active. */
\r
499 else if( ( pxCurrentRxDesc->status & FE ) != 0 )
\r
501 /* Frame error. Clear the error. */
\r
502 pxCurrentRxDesc->status &= ~( FP1 | FP0 | FE );
\r
503 pxCurrentRxDesc->status &= ~( RMAF | RRF | RTLF | RTSF | PRE | CERF );
\r
504 pxCurrentRxDesc->status |= ACT;
\r
505 pxCurrentRxDesc = pxCurrentRxDesc->next;
\r
507 if( EDMAC.EDRRR.LONG == 0x00000000UL )
\r
509 /* Restart Ethernet if it has stopped. */
\r
510 EDMAC.EDRRR.LONG = 0x00000001UL;
\r
515 /* The descriptor contains a frame. Because of the size of the buffers
\r
516 the frame should always be complete. */
\r
517 if( ( pxCurrentRxDesc->status & FP0 ) == FP0 )
\r
519 ulReturn = pxCurrentRxDesc->size;
\r
523 /* Do not expect to get here. */
\r
524 prvResetEverything();
\r
530 /*-----------------------------------------------------------*/
\r
532 static void prvResetMAC( void )
\r
534 /* Ensure the EtherC and EDMAC are enabled. */
\r
535 SYSTEM.MSTPCRB.BIT.MSTPB15 = 0;
\r
536 vTaskDelay( 100 / portTICK_PERIOD_MS );
\r
538 EDMAC.EDMR.BIT.SWR = 1;
\r
540 /* Crude wait for reset to complete. */
\r
541 vTaskDelay( 500 / portTICK_PERIOD_MS );
\r
543 /*-----------------------------------------------------------*/
\r
545 static void prvConfigureEtherCAndEDMAC( void )
\r
547 /* Initialisation code taken from Renesas example project. */
\r
549 /* TODO: Check bit 5 */
\r
550 ETHERC.ECSR.LONG = 0x00000037; /* Clear all ETHERC statuS BFR, PSRTO, LCHNG, MPD, ICD */
\r
552 /* Set the EDMAC interrupt priority. */
\r
553 _IPR( _ETHER_EINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
555 /* Enable interrupts of interest only. */
\r
556 EDMAC.EESIPR.LONG = emacTX_END_INTERRUPT | emacRX_END_INTERRUPT;
\r
557 ETHERC.RFLR.LONG = 1518; /* Ether payload is 1500+ CRC */
\r
558 ETHERC.IPGR.LONG = 0x00000014; /* Intergap is 96-bit time */
\r
561 EDMAC.EESR.LONG = 0x47FF0F9F; /* Clear all ETHERC and EDMAC status bits */
\r
563 EDMAC.EDMR.BIT.DE = 1;
\r
565 EDMAC.RDLAR = ( void * ) pxCurrentRxDesc; /* Initialaize Rx Descriptor List Address */
\r
566 EDMAC.TDLAR = &( xTxDescriptors[ 0 ] ); /* Initialaize Tx Descriptor List Address */
\r
567 EDMAC.TRSCER.LONG = 0x00000000; /* Copy-back status is RFE & TFE only */
\r
568 EDMAC.TFTR.LONG = 0x00000000; /* Threshold of Tx_FIFO */
\r
569 EDMAC.FDR.LONG = 0x00000000; /* Transmit fifo & receive fifo is 256 bytes */
\r
570 EDMAC.RMCR.LONG = 0x00000003; /* Receive function is normal mode(continued) */
\r
571 ETHERC.ECMR.BIT.PRM = 0; /* Ensure promiscuous mode is off. */
\r
573 /* Enable the interrupt... */
\r
574 _IEN( _ETHER_EINT ) = 1;
\r
576 /*-----------------------------------------------------------*/
\r
578 #pragma interrupt ( vEMAC_ISR_Handler( vect = VECT_ETHER_EINT, enable ) )
\r
579 void vEMAC_ISR_Handler( void )
\r
581 unsigned long ul = EDMAC.EESR.LONG;
\r
582 long lHigherPriorityTaskWoken = pdFALSE;
\r
583 extern QueueHandle_t xEMACEventQueue;
\r
584 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
586 /* Has a Tx end occurred? */
\r
587 if( ul & emacTX_END_INTERRUPT )
\r
589 /* Only return the buffer to the pool once both Txes have completed. */
\r
590 prvReturnBuffer( ( void * ) xTxDescriptors[ 0 ].buf_p );
\r
591 EDMAC.EESR.LONG = emacTX_END_INTERRUPT;
\r
594 /* Has an Rx end occurred? */
\r
595 if( ul & emacRX_END_INTERRUPT )
\r
597 /* Make sure the Ethernet task is not blocked waiting for a packet. */
\r
598 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
599 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );
\r
600 EDMAC.EESR.LONG = emacRX_END_INTERRUPT;
\r