2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* Kernel includes. */
\r
68 #include "FreeRTOS.h"
\r
72 /* Demo includes. */
\r
78 /* Hardware library includes. */
\r
79 #include "hw_types.h"
\r
80 #include "hw_memmap.h"
\r
81 #include "hw_ints.h"
\r
82 #include "hw_ethernet.h"
\r
83 #include "ethernet.h"
\r
84 #include "interrupt.h"
\r
86 #define emacNUM_RX_BUFFERS 5
\r
87 #define emacFRAM_SIZE_BYTES 2
\r
88 #define macNEGOTIATE_DELAY 2000
\r
89 #define macWAIT_SEND_TIME ( 10 )
\r
91 /* The task that handles the MAC peripheral. This is created at a high
\r
92 priority and is effectively a deferred interrupt handler. The peripheral
\r
93 handling is deferred to a task to prevent the entire FIFO having to be read
\r
94 from within an ISR. */
\r
95 void vMACHandleTask( void *pvParameters );
\r
97 /*-----------------------------------------------------------*/
\r
99 /* The semaphore used to wake the uIP task when data arrives. */
\r
100 xSemaphoreHandle xEMACSemaphore = NULL;
\r
102 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
103 is processed at the task level to prevent the need to read the entire FIFO from
\r
104 within the ISR itself. */
\r
105 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
107 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
108 point to one of the Rx buffers. */
\r
109 unsigned char *uip_buf;
\r
111 /* Buffers into which Rx data is placed. */
\r
112 static unsigned char ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ] __attribute__((aligned(4)));
\r
114 /* The length of the data within each of the Rx buffers. */
\r
115 static unsigned long ulRxLength[ emacNUM_RX_BUFFERS ];
\r
117 /* Used to keep a track of the number of bytes to transmit. */
\r
118 static unsigned long ulNextTxSpace;
\r
120 /*-----------------------------------------------------------*/
\r
122 portBASE_TYPE vInitEMAC( void )
\r
124 unsigned long ulTemp;
\r
125 portBASE_TYPE xReturn;
\r
127 /* Ensure all interrupts are disabled. */
\r
128 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
130 /* Clear any interrupts that were already pending. */
\r
131 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
132 EthernetIntClear( ETH_BASE, ulTemp );
\r
134 /* Initialise the MAC and connect. */
\r
135 EthernetInit( ETH_BASE );
\r
136 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
137 EthernetEnable( ETH_BASE );
\r
139 /* Mark each Rx buffer as empty. */
\r
140 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
142 ulRxLength[ ulTemp ] = 0;
\r
145 /* Create the queue and task used to defer the MAC processing to the
\r
147 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
148 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
149 xReturn = xTaskCreate( vMACHandleTask, ( signed char * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
150 vTaskDelay( macNEGOTIATE_DELAY );
\r
152 /* We are only interested in Rx interrupts. */
\r
153 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
154 IntEnable( INT_ETH );
\r
155 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
159 /*-----------------------------------------------------------*/
\r
161 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
163 static unsigned long ulNextRxBuffer = 0;
\r
166 iLen = ulRxLength[ ulNextRxBuffer ];
\r
170 /* Leave room for the size at the start of the buffer. */
\r
171 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
173 ulRxLength[ ulNextRxBuffer ] = 0;
\r
176 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
178 ulNextRxBuffer = 0;
\r
184 /*-----------------------------------------------------------*/
\r
186 void vInitialiseSend( void )
\r
188 /* Set the index to the first byte to send - skipping over the size
\r
192 /*-----------------------------------------------------------*/
\r
194 void vIncrementTxLength( unsigned long ulLength )
\r
196 ulNextTxSpace += ulLength;
\r
198 /*-----------------------------------------------------------*/
\r
200 void vSendBufferToMAC( void )
\r
202 unsigned long *pulSource;
\r
203 unsigned short * pus;
\r
204 unsigned long ulNextWord;
\r
206 /* Locate the data to be send. */
\r
207 pus = ( unsigned short * ) uip_buf;
\r
209 /* Add in the size of the data. */
\r
211 *pus = ulNextTxSpace;
\r
213 /* Wait for data to be sent if there is no space immediately. */
\r
214 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
216 vTaskDelay( macWAIT_SEND_TIME );
\r
219 pulSource = ( unsigned long * ) pus;
\r
221 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned long ) )
\r
223 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
228 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
230 /*-----------------------------------------------------------*/
\r
232 void vEMAC_ISR( void )
\r
234 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
235 unsigned long ulTemp;
\r
237 /* Clear the interrupt. */
\r
238 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
239 EthernetIntClear( ETH_BASE, ulTemp );
\r
241 /* Was it an Rx interrupt? */
\r
242 if( ulTemp & ETH_INT_RX )
\r
244 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
245 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
248 /* Switch to the uIP task. */
\r
249 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
251 /*-----------------------------------------------------------*/
\r
253 void vMACHandleTask( void *pvParameters )
\r
255 unsigned long ulLen = 0, i;
\r
256 unsigned long ulLength, ulInt;
\r
257 unsigned long *pulBuffer;
\r
258 static unsigned long ulNextRxBuffer = 0;
\r
259 portBASE_TYPE xSwitchRequired = pdFALSE;
\r
263 /* Wait for something to do. */
\r
264 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
266 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
268 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
270 /* Leave room at the start of the buffer for the size. */
\r
271 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
272 *pulBuffer = ( ulLength >> 16 );
\r
274 /* Get the size of the data. */
\r
275 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
276 ulLength &= 0xFFFF;
\r
282 if( ulLength >= UIP_BUFSIZE )
\r
284 /* The data won't fit in our buffer. Ensure we don't
\r
285 try to write into the buffer. */
\r
289 /* Read out the data into our buffer. */
\r
290 for( i = 0; i < ulLength; i += sizeof( unsigned long ) )
\r
292 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
296 /* Store the length of the data into the separate array. */
\r
297 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
299 /* Use the next buffer the next time through. */
\r
301 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
303 ulNextRxBuffer = 0;
\r
306 /* Ensure the uIP task is not blocked as data has arrived. */
\r
307 xSemaphoreGive( xEMACSemaphore );
\r
311 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r