2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 /* Kernel includes. */
\r
70 #include "FreeRTOS.h"
\r
74 /* Demo includes. */
\r
80 /* Hardware library includes. */
\r
81 #include "hw_types.h"
\r
82 #include "hw_memmap.h"
\r
83 #include "hw_ints.h"
\r
84 #include "hw_ethernet.h"
\r
85 #include "ethernet.h"
\r
86 #include "interrupt.h"
\r
88 #define emacNUM_RX_BUFFERS 5
\r
89 #define emacFRAM_SIZE_BYTES 2
\r
90 #define macNEGOTIATE_DELAY 2000
\r
91 #define macWAIT_SEND_TIME ( 10 )
\r
93 /* The task that handles the MAC peripheral. This is created at a high
\r
94 priority and is effectively a deferred interrupt handler. The peripheral
\r
95 handling is deferred to a task to prevent the entire FIFO having to be read
\r
96 from within an ISR. */
\r
97 void vMACHandleTask( void *pvParameters );
\r
99 /*-----------------------------------------------------------*/
\r
101 /* The semaphore used to wake the uIP task when data arrives. */
\r
102 xSemaphoreHandle xEMACSemaphore = NULL;
\r
104 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
105 is processed at the task level to prevent the need to read the entire FIFO from
\r
106 within the ISR itself. */
\r
107 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
109 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
110 point to one of the Rx buffers. */
\r
111 unsigned portCHAR *uip_buf;
\r
113 /* Buffers into which Rx data is placed. */
\r
116 unsigned portLONG ulJustForAlignment;
\r
117 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
120 /* The length of the data within each of the Rx buffers. */
\r
121 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
123 /* Used to keep a track of the number of bytes to transmit. */
\r
124 static unsigned portLONG ulNextTxSpace;
\r
126 /*-----------------------------------------------------------*/
\r
128 portBASE_TYPE vInitEMAC( void )
\r
130 unsigned long ulTemp;
\r
131 portBASE_TYPE xReturn;
\r
133 /* Ensure all interrupts are disabled. */
\r
134 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
136 /* Clear any interrupts that were already pending. */
\r
137 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
138 EthernetIntClear( ETH_BASE, ulTemp );
\r
140 /* Initialise the MAC and connect. */
\r
141 EthernetInit( ETH_BASE );
\r
142 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
143 EthernetEnable( ETH_BASE );
\r
145 /* Mark each Rx buffer as empty. */
\r
146 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
148 ulRxLength[ ulTemp ] = 0;
\r
151 /* Create the queue and task used to defer the MAC processing to the
\r
153 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
154 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
155 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
156 vTaskDelay( macNEGOTIATE_DELAY );
\r
158 /* We are only interested in Rx interrupts. */
\r
159 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
160 IntEnable( INT_ETH );
\r
161 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
165 /*-----------------------------------------------------------*/
\r
167 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
169 static unsigned long ulNextRxBuffer = 0;
\r
172 iLen = ulRxLength[ ulNextRxBuffer ];
\r
176 /* Leave room for the size at the start of the buffer. */
\r
177 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
179 ulRxLength[ ulNextRxBuffer ] = 0;
\r
182 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
184 ulNextRxBuffer = 0;
\r
190 /*-----------------------------------------------------------*/
\r
192 void vInitialiseSend( void )
\r
194 /* Set the index to the first byte to send - skipping over the size
\r
198 /*-----------------------------------------------------------*/
\r
200 void vIncrementTxLength( unsigned portLONG ulLength )
\r
202 ulNextTxSpace += ulLength;
\r
204 /*-----------------------------------------------------------*/
\r
206 void vSendBufferToMAC( void )
\r
208 unsigned long *pulSource;
\r
209 unsigned portSHORT * pus;
\r
210 unsigned portLONG ulNextWord;
\r
212 /* Locate the data to be send. */
\r
213 pus = ( unsigned portSHORT * ) uip_buf;
\r
215 /* Add in the size of the data. */
\r
217 *pus = ulNextTxSpace;
\r
219 /* Wait for data to be sent if there is no space immediately. */
\r
220 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
222 vTaskDelay( macWAIT_SEND_TIME );
\r
225 pulSource = ( unsigned portLONG * ) pus;
\r
227 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
229 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
234 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
236 /*-----------------------------------------------------------*/
\r
238 void vEMAC_ISR( void )
\r
240 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
241 unsigned portLONG ulTemp;
\r
243 /* Clear the interrupt. */
\r
244 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
245 EthernetIntClear( ETH_BASE, ulTemp );
\r
247 /* Was it an Rx interrupt? */
\r
248 if( ulTemp & ETH_INT_RX )
\r
250 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
251 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
254 /* Switch to the uIP task. */
\r
255 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
257 /*-----------------------------------------------------------*/
\r
259 void vMACHandleTask( void *pvParameters )
\r
261 unsigned long i, ulInt;
\r
262 unsigned portLONG ulLength;
\r
263 unsigned long *pulBuffer;
\r
264 static unsigned portLONG ulNextRxBuffer = 0;
\r
268 /* Wait for something to do. */
\r
269 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
271 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
273 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
275 /* Leave room at the start of the buffer for the size. */
\r
276 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
277 *pulBuffer = ( ulLength >> 16 );
\r
279 /* Get the size of the data. */
\r
280 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
281 ulLength &= 0xFFFF;
\r
287 if( ulLength >= UIP_BUFSIZE )
\r
289 /* The data won't fit in our buffer. Ensure we don't
\r
290 try to write into the buffer. */
\r
294 /* Read out the data into our buffer. */
\r
295 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
297 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
301 /* Store the length of the data into the separate array. */
\r
302 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
304 /* Use the next buffer the next time through. */
\r
306 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
308 ulNextRxBuffer = 0;
\r
311 /* Ensure the uIP task is not blocked as data has arrived. */
\r
312 xSemaphoreGive( xEMACSemaphore );
\r
316 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r