2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Hardware specific includes. */
\r
29 #include "iodefine.h"
\r
30 #include "typedefine.h"
\r
31 #include "hwEthernet.h"
\r
32 #include "hwEthernetPhy.h"
\r
34 /* FreeRTOS includes. */
\r
35 #include "FreeRTOS.h"
\r
40 #include "net/uip.h"
\r
42 /* The time to wait between attempts to obtain a free buffer. */
\r
43 #define emacBUFFER_WAIT_DELAY_ms ( 3 / portTICK_PERIOD_MS )
\r
45 /* The number of times emacBUFFER_WAIT_DELAY_ms should be waited before giving
\r
46 up on attempting to obtain a free buffer all together. */
\r
47 #define emacBUFFER_WAIT_ATTEMPTS ( 30 )
\r
49 /* The number of Rx descriptors. */
\r
50 #define emacNUM_RX_DESCRIPTORS 3
\r
52 /* The number of Tx descriptors. When using uIP there is not point in having
\r
54 #define emacNUM_TX_BUFFERS 2
\r
56 /* The total number of EMAC buffers to allocate. */
\r
57 #define emacNUM_BUFFERS ( emacNUM_RX_DESCRIPTORS + emacNUM_TX_BUFFERS )
\r
59 /* The time to wait for the Tx descriptor to become free. */
\r
60 #define emacTX_WAIT_DELAY_ms ( 10 / portTICK_PERIOD_MS )
\r
62 /* The total number of times to wait emacTX_WAIT_DELAY_ms for the Tx descriptor to
\r
64 #define emacTX_WAIT_ATTEMPTS ( 5 )
\r
66 /* Only Rx end and Tx end interrupts are used by this driver. */
\r
67 #define emacTX_END_INTERRUPT ( 1UL << 21UL )
\r
68 #define emacRX_END_INTERRUPT ( 1UL << 18UL )
\r
70 /*-----------------------------------------------------------*/
\r
72 /* The buffers and descriptors themselves. */
\r
73 #pragma section RX_DESCR
\r
74 ethfifo xRxDescriptors[ emacNUM_RX_DESCRIPTORS ];
\r
75 #pragma section TX_DESCR
\r
76 ethfifo xTxDescriptors[ emacNUM_TX_BUFFERS ];
\r
77 #pragma section _ETHERNET_BUFFERS
\r
78 char xEthernetBuffers[ emacNUM_BUFFERS ][ UIP_BUFSIZE ];
\r
81 /* Used to indicate which buffers are free and which are in use. If an index
\r
82 contains 0 then the corresponding buffer in xEthernetBuffers is free, otherwise
\r
83 the buffer is in use or about to be used. */
\r
84 static unsigned char ucBufferInUse[ emacNUM_BUFFERS ];
\r
86 /*-----------------------------------------------------------*/
\r
89 * Initialise both the Rx and Tx descriptors.
\r
91 static void prvInitialiseDescriptors( void );
\r
94 * Return a pointer to a free buffer within xEthernetBuffers.
\r
96 static unsigned char *prvGetNextBuffer( void );
\r
99 * Return a buffer to the list of free buffers.
\r
101 static void prvReturnBuffer( unsigned char *pucBuffer );
\r
104 * Examine the status of the next Rx FIFO to see if it contains new data.
\r
106 static unsigned long prvCheckRxFifoStatus( void );
\r
109 * Setup the microcontroller for communication with the PHY.
\r
111 static void prvSetupPortPinsAndReset( void );
\r
114 * Configure the Ethernet interface peripherals.
\r
116 static void prvConfigureEtherCAndEDMAC( void );
\r
119 * Something has gone wrong with the descriptor usage. Reset all the buffers
\r
122 static void prvResetEverything( void );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* Points to the Rx descriptor currently in use. */
\r
127 static ethfifo *xCurrentRxDesc = NULL;
\r
129 /* The buffer used by the uIP stack to both receive and send. This points to
\r
130 one of the Ethernet buffers when its actually in use. */
\r
131 unsigned char *uip_buf = NULL;
\r
133 /*-----------------------------------------------------------*/
\r
135 void vInitEmac( void )
\r
137 /* Setup the SH hardware for MII communications. */
\r
138 prvSetupPortPinsAndReset();
\r
140 /* Set the Rx and Tx descriptors into their initial state. */
\r
141 prvInitialiseDescriptors();
\r
143 /* Set the MAC address into the ETHERC */
\r
144 EtherC.MAHR = ( ( unsigned long ) configMAC_ADDR0 << 24UL ) |
\r
145 ( ( unsigned long ) configMAC_ADDR1 << 16UL ) |
\r
146 ( ( unsigned long ) configMAC_ADDR2 << 8UL ) |
\r
147 ( unsigned long ) configMAC_ADDR3;
\r
149 EtherC.MALR.BIT.MA = ( ( unsigned long ) configMAC_ADDR4 << 8UL ) |
\r
150 ( unsigned long ) configMAC_ADDR5;
\r
152 /* Perform rest of interface hardware configuration. */
\r
153 prvConfigureEtherCAndEDMAC();
\r
155 /* Nothing received yet, so uip_buf points nowhere. */
\r
158 /* Initialize the PHY */
\r
161 /*-----------------------------------------------------------*/
\r
163 void vEMACWrite( void )
\r
167 /* Wait until the second transmission of the last packet has completed. */
\r
168 for( x = 0; x < emacTX_WAIT_ATTEMPTS; x++ )
\r
170 if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )
\r
172 /* Descriptor is still active. */
\r
173 vTaskDelay( emacTX_WAIT_DELAY_ms );
\r
181 /* Is the descriptor free after waiting for it? */
\r
182 if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )
\r
184 /* Something has gone wrong. */
\r
185 prvResetEverything();
\r
188 /* Setup both descriptors to transmit the frame. */
\r
189 xTxDescriptors[ 0 ].buf_p = ( char * ) uip_buf;
\r
190 xTxDescriptors[ 0 ].bufsize = uip_len;
\r
191 xTxDescriptors[ 1 ].buf_p = ( char * ) uip_buf;
\r
192 xTxDescriptors[ 1 ].bufsize = uip_len;
\r
194 /* uip_buf is being sent by the Tx descriptor. Allocate a new buffer
\r
195 for use by the stack. */
\r
196 uip_buf = prvGetNextBuffer();
\r
198 /* Clear previous settings and go. */
\r
199 xTxDescriptors[0].status &= ~( FP1 | FP0 );
\r
200 xTxDescriptors[0].status |= ( FP1 | FP0 | ACT );
\r
201 xTxDescriptors[1].status &= ~( FP1 | FP0 );
\r
202 xTxDescriptors[1].status |= ( FP1 | FP0 | ACT );
\r
204 EDMAC.EDTRR.LONG = 0x00000001;
\r
206 /*-----------------------------------------------------------*/
\r
208 unsigned long ulEMACRead( void )
\r
210 unsigned long ulBytesReceived;
\r
212 ulBytesReceived = prvCheckRxFifoStatus();
\r
214 if( ulBytesReceived > 0 )
\r
216 xCurrentRxDesc->status &= ~( FP1 | FP0 );
\r
217 xCurrentRxDesc->status |= ACT;
\r
219 if( EDMAC.EDRRR.LONG == 0x00000000L )
\r
221 /* Restart Ethernet if it has stopped */
\r
222 EDMAC.EDRRR.LONG = 0x00000001L;
\r
225 /* Mark the pxDescriptor buffer as free as uip_buf is going to be set to
\r
226 the buffer that contains the received data. */
\r
227 prvReturnBuffer( uip_buf );
\r
229 uip_buf = ( void * ) xCurrentRxDesc->buf_p;
\r
231 /* Move onto the next buffer in the ring. */
\r
232 xCurrentRxDesc = xCurrentRxDesc->next;
\r
235 return ulBytesReceived;
\r
237 /*-----------------------------------------------------------*/
\r
239 long lEMACWaitForLink( void )
\r
243 /* Set the link status. */
\r
244 switch( phyStatus() )
\r
246 /* Half duplex link */
\r
247 case PHY_LINK_100H:
\r
249 EtherC.ECMR.BIT.DM = 0;
\r
253 /* Full duplex link */
\r
254 case PHY_LINK_100F:
\r
256 EtherC.ECMR.BIT.DM = 1;
\r
265 if( lReturn == pdPASS )
\r
267 /* Enable receive and transmit. */
\r
268 EtherC.ECMR.BIT.RE = 1;
\r
269 EtherC.ECMR.BIT.TE = 1;
\r
271 /* Enable EDMAC receive */
\r
272 EDMAC.EDRRR.LONG = 0x1;
\r
277 /*-----------------------------------------------------------*/
\r
279 static void prvInitialiseDescriptors( void )
\r
281 ethfifo *pxDescriptor;
\r
284 for( x = 0; x < emacNUM_BUFFERS; x++ )
\r
286 /* Ensure none of the buffers are shown as in use at the start. */
\r
287 ucBufferInUse[ x ] = pdFALSE;
\r
290 /* Initialise the Rx descriptors. */
\r
291 for( x = 0; x < emacNUM_RX_DESCRIPTORS; x++ )
\r
293 pxDescriptor = &( xRxDescriptors[ x ] );
\r
294 pxDescriptor->buf_p = &( xEthernetBuffers[ x ][ 0 ] );
\r
296 pxDescriptor->bufsize = UIP_BUFSIZE;
\r
297 pxDescriptor->size = 0;
\r
298 pxDescriptor->status = ACT;
\r
299 pxDescriptor->next = &xRxDescriptors[ x + 1 ];
\r
301 /* Mark this buffer as in use. */
\r
302 ucBufferInUse[ x ] = pdTRUE;
\r
305 /* The last descriptor points back to the start. */
\r
306 pxDescriptor->status |= DL;
\r
307 pxDescriptor->next = &xRxDescriptors[ 0 ];
\r
309 /* Initialise the Tx descriptors. */
\r
310 for( x = 0; x < emacNUM_TX_BUFFERS; x++ )
\r
312 pxDescriptor = &( xTxDescriptors[ x ] );
\r
314 /* A buffer is not allocated to the Tx descriptor until a send is
\r
315 actually required. */
\r
316 pxDescriptor->buf_p = NULL;
\r
318 pxDescriptor->bufsize = UIP_BUFSIZE;
\r
319 pxDescriptor->size = 0;
\r
320 pxDescriptor->status = 0;
\r
321 pxDescriptor->next = &xTxDescriptors[ x + 1 ];
\r
324 /* The last descriptor points back to the start. */
\r
325 pxDescriptor->status |= DL;
\r
326 pxDescriptor->next = &( xTxDescriptors[ 0 ] );
\r
328 /* Use the first Rx descriptor to start with. */
\r
329 xCurrentRxDesc = &( xRxDescriptors[ 0 ] );
\r
331 /*-----------------------------------------------------------*/
\r
333 static unsigned char *prvGetNextBuffer( void )
\r
336 unsigned char *pucReturn = NULL;
\r
337 unsigned long ulAttempts = 0;
\r
339 while( pucReturn == NULL )
\r
341 /* Look through the buffers to find one that is not in use by
\r
343 for( x = 0; x < emacNUM_BUFFERS; x++ )
\r
345 if( ucBufferInUse[ x ] == pdFALSE )
\r
347 ucBufferInUse[ x ] = pdTRUE;
\r
348 pucReturn = ( unsigned char * ) &( xEthernetBuffers[ x ][ 0 ] );
\r
353 /* Was a buffer found? */
\r
354 if( pucReturn == NULL )
\r
358 if( ulAttempts >= emacBUFFER_WAIT_ATTEMPTS )
\r
363 /* Wait then look again. */
\r
364 vTaskDelay( emacBUFFER_WAIT_DELAY_ms );
\r
370 /*-----------------------------------------------------------*/
\r
372 static void prvReturnBuffer( unsigned char *pucBuffer )
\r
376 /* Return a buffer to the pool of free buffers. */
\r
377 for( ul = 0; ul < emacNUM_BUFFERS; ul++ )
\r
379 if( &( xEthernetBuffers[ ul ][ 0 ] ) == ( void * ) pucBuffer )
\r
381 ucBufferInUse[ ul ] = pdFALSE;
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvResetEverything( void )
\r
390 /* Temporary code just to see if this gets called. This function has not
\r
391 been implemented. */
\r
392 portDISABLE_INTERRUPTS();
\r
395 /*-----------------------------------------------------------*/
\r
397 static unsigned long prvCheckRxFifoStatus( void )
\r
399 unsigned long ulReturn = 0;
\r
401 if( ( xCurrentRxDesc->status & ACT ) != 0 )
\r
403 /* Current descriptor is still active. */
\r
405 else if( ( xCurrentRxDesc->status & FE ) != 0 )
\r
407 /* Frame error. Clear the error. */
\r
408 xCurrentRxDesc->status &= ~( FP1 | FP0 | FE );
\r
409 xCurrentRxDesc->status &= ~( RMAF | RRF | RTLF | RTSF | PRE | CERF );
\r
410 xCurrentRxDesc->status |= ACT;
\r
411 xCurrentRxDesc = xCurrentRxDesc->next;
\r
413 if( EDMAC.EDRRR.LONG == 0x00000000UL )
\r
415 /* Restart Ethernet if it has stopped. */
\r
416 EDMAC.EDRRR.LONG = 0x00000001UL;
\r
421 /* The descriptor contains a frame. Because of the size of the buffers
\r
422 the frame should always be complete. */
\r
423 if( (xCurrentRxDesc->status & FP0) == FP0 )
\r
425 ulReturn = xCurrentRxDesc->size;
\r
429 /* Do not expect to get here. */
\r
430 prvResetEverything();
\r
436 /*-----------------------------------------------------------*/
\r
438 static void prvSetupPortPinsAndReset( void )
\r
440 /* Initialisation code taken from Renesas example project. */
\r
442 PFC.PACRL4.BIT.PA12MD = 0x7; /* Set TX_CLK input (EtherC) */
\r
443 PFC.PACRL3.BIT.PA11MD = 0x7; /* Set TX_EN output (EtherC) */
\r
444 PFC.PACRL3.BIT.PA10MD = 0x7; /* Set MII_TXD0 output (EtherC) */
\r
445 PFC.PACRL3.BIT.PA9MD = 0x7; /* Set MII_TXD1 output (EtherC) */
\r
446 PFC.PACRL3.BIT.PA8MD = 0x7; /* Set MII_TXD2 output (EtherC) */
\r
447 PFC.PACRL2.BIT.PA7MD = 0x7; /* Set MII_TXD3 output (EtherC) */
\r
448 PFC.PACRL2.BIT.PA6MD = 0x7; /* Set TX_ER output (EtherC) */
\r
449 PFC.PDCRH4.BIT.PD31MD = 0x7; /* Set RX_DV input (EtherC) */
\r
450 PFC.PDCRH4.BIT.PD30MD = 0x7; /* Set RX_ER input (EtherC) */
\r
451 PFC.PDCRH4.BIT.PD29MD = 0x7; /* Set MII_RXD3 input (EtherC) */
\r
452 PFC.PDCRH4.BIT.PD28MD = 0x7; /* Set MII_RXD2 input (EtherC) */
\r
453 PFC.PDCRH3.BIT.PD27MD = 0x7; /* Set MII_RXD1 input (EtherC) */
\r
454 PFC.PDCRH3.BIT.PD26MD = 0x7; /* Set MII_RXD0 input (EtherC) */
\r
455 PFC.PDCRH3.BIT.PD25MD = 0x7; /* Set RX_CLK input (EtherC) */
\r
456 PFC.PDCRH3.BIT.PD24MD = 0x7; /* Set CRS input (EtherC) */
\r
457 PFC.PDCRH2.BIT.PD23MD = 0x7; /* Set COL input (EtherC) */
\r
458 PFC.PDCRH2.BIT.PD22MD = 0x7; /* Set WOL output (EtherC) */
\r
459 PFC.PDCRH2.BIT.PD21MD = 0x7; /* Set EXOUT output (EtherC) */
\r
460 PFC.PDCRH2.BIT.PD20MD = 0x7; /* Set MDC output (EtherC) */
\r
461 PFC.PDCRH1.BIT.PD19MD = 0x7; /* Set LINKSTA input (EtherC) */
\r
462 PFC.PDCRH1.BIT.PD18MD = 0x7; /* Set MDIO input/output (EtherC) */
\r
464 STB.CR4.BIT._ETHER = 0x0;
\r
465 EDMAC.EDMR.BIT.SWR = 1;
\r
467 /* Crude wait for reset to complete. */
\r
468 vTaskDelay( 500 / portTICK_PERIOD_MS );
\r
470 /*-----------------------------------------------------------*/
\r
472 static void prvConfigureEtherCAndEDMAC( void )
\r
474 /* Initialisation code taken from Renesas example project. */
\r
476 /* TODO: Check bit 5 */
\r
477 EtherC.ECSR.LONG = 0x00000037; /* Clear all EtherC statuS BFR, PSRTO, LCHNG, MPD, ICD */
\r
479 /* TODO: Check bit 5 */
\r
480 EtherC.ECSIPR.LONG = 0x00000020; /* Disable EtherC status change interrupt */
\r
481 EtherC.RFLR.LONG = 1518; /* Ether payload is 1500+ CRC */
\r
482 EtherC.IPGR.LONG = 0x00000014; /* Intergap is 96-bit time */
\r
485 EDMAC.EESR.LONG = 0x47FF0F9F; /* Clear all EtherC and EDMAC status bits */
\r
486 EDMAC.RDLAR = ( void * ) xCurrentRxDesc; /* Initialaize Rx Descriptor List Address */
\r
487 EDMAC.TDLAR = &( xTxDescriptors[ 0 ] ); /* Initialaize Tx Descriptor List Address */
\r
488 EDMAC.TRSCER.LONG = 0x00000000; /* Copy-back status is RFE & TFE only */
\r
489 EDMAC.TFTR.LONG = 0x00000000; /* Threshold of Tx_FIFO */
\r
490 EDMAC.FDR.LONG = 0x00000000; /* Transmit fifo & receive fifo is 256 bytes */
\r
491 EDMAC.RMCR.LONG = 0x00000003; /* Receive function is normal mode(continued) */
\r
493 /* Set the EDMAC interrupt priority - the interrupt priority must be
\r
494 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to
\r
495 generate the tick interrupt. */
\r
496 INTC.IPR19.BIT._EDMAC = portKERNEL_INTERRUPT_PRIORITY;
\r
497 EDMAC.EESIPR.LONG = emacTX_END_INTERRUPT | emacRX_END_INTERRUPT; /* Enable Rx and Tx end interrupts. */
\r
499 /* Clear the interrupt flag. */
\r
500 CMT0.CMCSR.BIT.CMF = 0;
\r
502 /*-----------------------------------------------------------*/
\r
504 void vEMAC_ISR_Handler( void )
\r
506 unsigned long ul = EDMAC.EESR.LONG;
\r
507 long lHigherPriorityTaskWoken = pdFALSE;
\r
508 extern SemaphoreHandle_t xEMACSemaphore;
\r
509 static long ulTxEndInts = 0;
\r
511 /* Has a Tx end occurred? */
\r
512 if( ul & emacTX_END_INTERRUPT )
\r
515 if( ulTxEndInts >= 2 )
\r
517 /* Only return the buffer to the pool once both Txes have completed. */
\r
518 prvReturnBuffer( ( void * ) xTxDescriptors[ 0 ].buf_p );
\r
521 EDMAC.EESR.LONG = emacTX_END_INTERRUPT;
\r
524 /* Has an Rx end occurred? */
\r
525 if( ul & emacRX_END_INTERRUPT )
\r
527 /* Make sure the Ethernet task is not blocked waiting for a packet. */
\r
528 xSemaphoreGiveFromISR( xEMACSemaphore, &lHigherPriorityTaskWoken );
\r
529 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );
\r
530 EDMAC.EESR.LONG = emacRX_END_INTERRUPT;
\r