2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
52 /* Kernel includes. */
\r
53 #include "FreeRTOS.h"
\r
57 /* Demo includes. */
\r
63 /* Hardware library includes. */
\r
64 #include "hw_types.h"
\r
65 #include "hw_memmap.h"
\r
66 #include "hw_ints.h"
\r
67 #include "hw_ethernet.h"
\r
68 #include "ethernet.h"
\r
69 #include "interrupt.h"
\r
71 #define emacNUM_RX_BUFFERS 5
\r
72 #define emacFRAM_SIZE_BYTES 2
\r
73 #define macNEGOTIATE_DELAY 2000
\r
74 #define macWAIT_SEND_TIME ( 10 )
\r
76 /* The task that handles the MAC peripheral. This is created at a high
\r
77 priority and is effectively a deferred interrupt handler. The peripheral
\r
78 handling is deferred to a task to prevent the entire FIFO having to be read
\r
79 from within an ISR. */
\r
80 void vMACHandleTask( void *pvParameters );
\r
82 /*-----------------------------------------------------------*/
\r
84 /* The semaphore used to wake the uIP task when data arrives. */
\r
85 xSemaphoreHandle xEMACSemaphore = NULL;
\r
87 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
88 is processed at the task level to prevent the need to read the entire FIFO from
\r
89 within the ISR itself. */
\r
90 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
92 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
93 point to one of the Rx buffers. */
\r
94 unsigned portCHAR *uip_buf;
\r
96 /* Buffers into which Rx data is placed. */
\r
99 unsigned portLONG ulJustForAlignment;
\r
100 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
103 /* The length of the data within each of the Rx buffers. */
\r
104 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
106 /* Used to keep a track of the number of bytes to transmit. */
\r
107 static unsigned portLONG ulNextTxSpace;
\r
109 /*-----------------------------------------------------------*/
\r
111 portBASE_TYPE vInitEMAC( void )
\r
113 unsigned long ulTemp;
\r
114 portBASE_TYPE xReturn;
\r
116 /* Ensure all interrupts are disabled. */
\r
117 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
119 /* Clear any interrupts that were already pending. */
\r
120 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
121 EthernetIntClear( ETH_BASE, ulTemp );
\r
123 /* Initialise the MAC and connect. */
\r
124 EthernetInit( ETH_BASE );
\r
125 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
126 EthernetEnable( ETH_BASE );
\r
128 /* Mark each Rx buffer as empty. */
\r
129 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
131 ulRxLength[ ulTemp ] = 0;
\r
134 /* Create the queue and task used to defer the MAC processing to the
\r
136 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
137 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
138 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
139 vTaskDelay( macNEGOTIATE_DELAY );
\r
141 /* We are only interested in Rx interrupts. */
\r
142 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
143 IntEnable( INT_ETH );
\r
144 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
148 /*-----------------------------------------------------------*/
\r
150 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
152 static unsigned long ulNextRxBuffer = 0;
\r
155 iLen = ulRxLength[ ulNextRxBuffer ];
\r
159 /* Leave room for the size at the start of the buffer. */
\r
160 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
162 ulRxLength[ ulNextRxBuffer ] = 0;
\r
165 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
167 ulNextRxBuffer = 0;
\r
173 /*-----------------------------------------------------------*/
\r
175 void vInitialiseSend( void )
\r
177 /* Set the index to the first byte to send - skipping over the size
\r
181 /*-----------------------------------------------------------*/
\r
183 void vIncrementTxLength( unsigned portLONG ulLength )
\r
185 ulNextTxSpace += ulLength;
\r
187 /*-----------------------------------------------------------*/
\r
189 void vSendBufferToMAC( void )
\r
191 unsigned long *pulSource;
\r
192 unsigned portSHORT * pus;
\r
193 unsigned portLONG ulNextWord;
\r
195 /* Locate the data to be send. */
\r
196 pus = ( unsigned portSHORT * ) uip_buf;
\r
198 /* Add in the size of the data. */
\r
200 *pus = ulNextTxSpace;
\r
202 /* Wait for data to be sent if there is no space immediately. */
\r
203 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
205 vTaskDelay( macWAIT_SEND_TIME );
\r
208 pulSource = ( unsigned portLONG * ) pus;
\r
210 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
212 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
217 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
219 /*-----------------------------------------------------------*/
\r
221 void vEMAC_ISR( void )
\r
223 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
224 unsigned portLONG ulTemp;
\r
226 /* Clear the interrupt. */
\r
227 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
228 EthernetIntClear( ETH_BASE, ulTemp );
\r
230 /* Was it an Rx interrupt? */
\r
231 if( ulTemp & ETH_INT_RX )
\r
233 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
234 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
237 /* Switch to the uIP task. */
\r
238 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
240 /*-----------------------------------------------------------*/
\r
242 void vMACHandleTask( void *pvParameters )
\r
244 unsigned long i, ulInt;
\r
245 unsigned portLONG ulLength;
\r
246 unsigned long *pulBuffer;
\r
247 static unsigned portLONG ulNextRxBuffer = 0;
\r
251 /* Wait for something to do. */
\r
252 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
254 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
256 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
258 /* Leave room at the start of the buffer for the size. */
\r
259 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
260 *pulBuffer = ( ulLength >> 16 );
\r
262 /* Get the size of the data. */
\r
263 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
264 ulLength &= 0xFFFF;
\r
270 if( ulLength >= UIP_BUFSIZE )
\r
272 /* The data won't fit in our buffer. Ensure we don't
\r
273 try to write into the buffer. */
\r
277 /* Read out the data into our buffer. */
\r
278 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
280 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
284 /* Store the length of the data into the separate array. */
\r
285 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
287 /* Use the next buffer the next time through. */
\r
289 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
291 ulNextRxBuffer = 0;
\r
294 /* Ensure the uIP task is not blocked as data has arrived. */
\r
295 xSemaphoreGive( xEMACSemaphore );
\r
299 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r