2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Kernel includes. */
\r
55 #include "FreeRTOS.h"
\r
59 /* Demo includes. */
\r
65 /* Hardware library includes. */
\r
66 #include "hw_types.h"
\r
67 #include "hw_memmap.h"
\r
68 #include "hw_ints.h"
\r
69 #include "hw_ethernet.h"
\r
70 #include "ethernet.h"
\r
71 #include "interrupt.h"
\r
73 #define emacNUM_RX_BUFFERS 5
\r
74 #define emacFRAM_SIZE_BYTES 2
\r
75 #define macNEGOTIATE_DELAY 2000
\r
76 #define macWAIT_SEND_TIME ( 10 )
\r
78 /* The task that handles the MAC peripheral. This is created at a high
\r
79 priority and is effectively a deferred interrupt handler. The peripheral
\r
80 handling is deferred to a task to prevent the entire FIFO having to be read
\r
81 from within an ISR. */
\r
82 void vMACHandleTask( void *pvParameters );
\r
84 /*-----------------------------------------------------------*/
\r
86 /* The semaphore used to wake the uIP task when data arrives. */
\r
87 xSemaphoreHandle xEMACSemaphore = NULL;
\r
89 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
90 is processed at the task level to prevent the need to read the entire FIFO from
\r
91 within the ISR itself. */
\r
92 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
94 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
95 point to one of the Rx buffers. */
\r
96 unsigned portCHAR *uip_buf;
\r
98 /* Buffers into which Rx data is placed. */
\r
99 static unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
101 /* The length of the data within each of the Rx buffers. */
\r
102 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
104 /* Used to keep a track of the number of bytes to transmit. */
\r
105 static unsigned portLONG ulNextTxSpace;
\r
107 /*-----------------------------------------------------------*/
\r
109 portBASE_TYPE vInitEMAC( void )
\r
111 unsigned long ulTemp;
\r
112 portBASE_TYPE xReturn;
\r
114 /* Ensure all interrupts are disabled. */
\r
115 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
117 /* Clear any interrupts that were already pending. */
\r
118 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
119 EthernetIntClear( ETH_BASE, ulTemp );
\r
121 /* Initialise the MAC and connect. */
\r
122 EthernetInit( ETH_BASE );
\r
123 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
124 EthernetEnable( ETH_BASE );
\r
126 /* Mark each Rx buffer as empty. */
\r
127 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
129 ulRxLength[ ulTemp ] = 0;
\r
132 /* Create the queue and task used to defer the MAC processing to the
\r
134 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
135 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
136 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
137 vTaskDelay( macNEGOTIATE_DELAY );
\r
139 /* We are only interested in Rx interrupts. */
\r
140 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
141 IntEnable( INT_ETH );
\r
142 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
146 /*-----------------------------------------------------------*/
\r
148 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
150 static unsigned long ulNextRxBuffer = 0;
\r
153 iLen = ulRxLength[ ulNextRxBuffer ];
\r
157 /* Leave room for the size at the start of the buffer. */
\r
158 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
160 ulRxLength[ ulNextRxBuffer ] = 0;
\r
163 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
165 ulNextRxBuffer = 0;
\r
171 /*-----------------------------------------------------------*/
\r
173 void vInitialiseSend( void )
\r
175 /* Set the index to the first byte to send - skipping over the size
\r
179 /*-----------------------------------------------------------*/
\r
181 void vIncrementTxLength( unsigned portLONG ulLength )
\r
183 ulNextTxSpace += ulLength;
\r
185 /*-----------------------------------------------------------*/
\r
187 void vSendBufferToMAC( void )
\r
189 unsigned long *pulSource;
\r
190 unsigned portSHORT * pus;
\r
191 unsigned portLONG ulNextWord;
\r
193 /* Locate the data to be send. */
\r
194 pus = ( unsigned portSHORT * ) uip_buf;
\r
196 /* Add in the size of the data. */
\r
198 *pus = ulNextTxSpace;
\r
200 /* Wait for data to be sent if there is no space immediately. */
\r
201 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
203 vTaskDelay( macWAIT_SEND_TIME );
\r
206 pulSource = ( unsigned portLONG * ) pus;
\r
208 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
210 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
215 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
217 /*-----------------------------------------------------------*/
\r
219 void vEMAC_ISR( void )
\r
221 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
222 unsigned portLONG ulTemp;
\r
224 /* Clear the interrupt. */
\r
225 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
226 EthernetIntClear( ETH_BASE, ulTemp );
\r
228 /* Was it an Rx interrupt? */
\r
229 if( ulTemp & ETH_INT_RX )
\r
231 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
232 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
235 /* Switch to the uIP task. */
\r
236 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
238 /*-----------------------------------------------------------*/
\r
240 void vMACHandleTask( void *pvParameters )
\r
242 unsigned long ulLen = 0, i;
\r
243 unsigned portLONG ulLength, ulInt;
\r
244 unsigned long *pulBuffer;
\r
245 static unsigned portLONG ulNextRxBuffer = 0;
\r
246 portBASE_TYPE xSwitchRequired = pdFALSE;
\r
250 /* Wait for something to do. */
\r
251 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
253 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
255 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
257 /* Leave room at the start of the buffer for the size. */
\r
258 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
259 *pulBuffer = ( ulLength >> 16 );
\r
261 /* Get the size of the data. */
\r
262 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
263 ulLength &= 0xFFFF;
\r
269 if( ulLength >= UIP_BUFSIZE )
\r
271 /* The data won't fit in our buffer. Ensure we don't
\r
272 try to write into the buffer. */
\r
276 /* Read out the data into our buffer. */
\r
277 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
279 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
283 /* Store the length of the data into the separate array. */
\r
284 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
286 /* Use the next buffer the next time through. */
\r
288 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
290 ulNextRxBuffer = 0;
\r
293 /* Ensure the uIP task is not blocked as data has arrived. */
\r
294 xSemaphoreGive( xEMACSemaphore );
\r
298 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r