2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 /* Kernel includes. */
\r
49 #include "FreeRTOS.h"
\r
53 /* Demo includes. */
\r
59 /* Hardware library includes. */
\r
60 #include "hw_types.h"
\r
61 #include "hw_memmap.h"
\r
62 #include "hw_ints.h"
\r
63 #include "hw_ethernet.h"
\r
64 #include "ethernet.h"
\r
65 #include "interrupt.h"
\r
67 #define emacNUM_RX_BUFFERS 5
\r
68 #define emacFRAM_SIZE_BYTES 2
\r
69 #define macNEGOTIATE_DELAY 2000
\r
70 #define macWAIT_SEND_TIME ( 10 )
\r
72 /* The task that handles the MAC peripheral. This is created at a high
\r
73 priority and is effectively a deferred interrupt handler. The peripheral
\r
74 handling is deferred to a task to prevent the entire FIFO having to be read
\r
75 from within an ISR. */
\r
76 void vMACHandleTask( void *pvParameters );
\r
78 /*-----------------------------------------------------------*/
\r
80 /* The semaphore used to wake the uIP task when data arrives. */
\r
81 xSemaphoreHandle xEMACSemaphore = NULL;
\r
83 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
84 is processed at the task level to prevent the need to read the entire FIFO from
\r
85 within the ISR itself. */
\r
86 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
88 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
89 point to one of the Rx buffers. */
\r
90 unsigned portCHAR *uip_buf;
\r
92 /* Buffers into which Rx data is placed. */
\r
93 static unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ] __attribute__((aligned(4)));
\r
95 /* The length of the data within each of the Rx buffers. */
\r
96 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
98 /* Used to keep a track of the number of bytes to transmit. */
\r
99 static unsigned portLONG ulNextTxSpace;
\r
101 /*-----------------------------------------------------------*/
\r
103 portBASE_TYPE vInitEMAC( void )
\r
105 unsigned long ulTemp;
\r
106 portBASE_TYPE xReturn;
\r
108 /* Ensure all interrupts are disabled. */
\r
109 EthernetIntDisable( ETH_BASE, ( ETH_INT_PHY | ETH_INT_MDIO | ETH_INT_RXER | ETH_INT_RXOF | ETH_INT_TX | ETH_INT_TXER | ETH_INT_RX));
\r
111 /* Clear any interrupts that were already pending. */
\r
112 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
113 EthernetIntClear( ETH_BASE, ulTemp );
\r
115 /* Initialise the MAC and connect. */
\r
116 EthernetInit( ETH_BASE );
\r
117 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
118 EthernetEnable( ETH_BASE );
\r
120 /* Mark each Rx buffer as empty. */
\r
121 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
123 ulRxLength[ ulTemp ] = 0;
\r
126 /* Create the queue and task used to defer the MAC processing to the
\r
128 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
129 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
130 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
131 vTaskDelay( macNEGOTIATE_DELAY );
\r
133 /* We are only interested in Rx interrupts. */
\r
134 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
135 IntEnable( INT_ETH );
\r
136 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
140 /*-----------------------------------------------------------*/
\r
142 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
144 static unsigned long ulNextRxBuffer = 0;
\r
147 iLen = ulRxLength[ ulNextRxBuffer ];
\r
151 /* Leave room for the size at the start of the buffer. */
\r
152 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
154 ulRxLength[ ulNextRxBuffer ] = 0;
\r
157 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
159 ulNextRxBuffer = 0;
\r
165 /*-----------------------------------------------------------*/
\r
167 void vInitialiseSend( void )
\r
169 /* Set the index to the first byte to send - skipping over the size
\r
173 /*-----------------------------------------------------------*/
\r
175 void vIncrementTxLength( unsigned portLONG ulLength )
\r
177 ulNextTxSpace += ulLength;
\r
179 /*-----------------------------------------------------------*/
\r
181 void vSendBufferToMAC( void )
\r
183 unsigned long *pulSource;
\r
184 unsigned portSHORT * pus;
\r
185 unsigned portLONG ulNextWord;
\r
187 /* Locate the data to be send. */
\r
188 pus = ( unsigned portSHORT * ) uip_buf;
\r
190 /* Add in the size of the data. */
\r
192 *pus = ulNextTxSpace;
\r
194 /* Wait for data to be sent if there is no space immediately. */
\r
195 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
197 vTaskDelay( macWAIT_SEND_TIME );
\r
200 pulSource = ( unsigned portLONG * ) pus;
\r
202 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
204 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
209 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
211 /*-----------------------------------------------------------*/
\r
213 void vEMAC_ISR( void )
\r
215 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
216 unsigned portLONG ulTemp;
\r
218 /* Clear the interrupt. */
\r
219 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
220 EthernetIntClear( ETH_BASE, ulTemp );
\r
222 /* Was it an Rx interrupt? */
\r
223 if( ulTemp & ETH_INT_RX )
\r
225 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
226 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
229 /* Switch to the uIP task. */
\r
230 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
232 /*-----------------------------------------------------------*/
\r
234 void vMACHandleTask( void *pvParameters )
\r
236 unsigned long ulLen = 0, i;
\r
237 unsigned portLONG ulLength, ulInt;
\r
238 unsigned long *pulBuffer;
\r
239 static unsigned portLONG ulNextRxBuffer = 0;
\r
240 portBASE_TYPE xSwitchRequired = pdFALSE;
\r
244 /* Wait for something to do. */
\r
245 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
247 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
249 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
251 /* Leave room at the start of the buffer for the size. */
\r
252 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
253 *pulBuffer = ( ulLength >> 16 );
\r
255 /* Get the size of the data. */
\r
256 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
257 ulLength &= 0xFFFF;
\r
263 if( ulLength >= UIP_BUFSIZE )
\r
265 /* The data won't fit in our buffer. Ensure we don't
\r
266 try to write into the buffer. */
\r
270 /* Read out the data into our buffer. */
\r
271 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
273 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
277 /* Store the length of the data into the separate array. */
\r
278 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
280 /* Use the next buffer the next time through. */
\r
282 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
284 ulNextRxBuffer = 0;
\r
287 /* Ensure the uIP task is not blocked as data has arrived. */
\r
288 xSemaphoreGive( xEMACSemaphore );
\r
292 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r