2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* Kernel includes. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo includes. */
\r
86 /* Hardware library includes. */
\r
87 #include "hw_types.h"
\r
88 #include "hw_memmap.h"
\r
89 #include "hw_ints.h"
\r
90 #include "hw_ethernet.h"
\r
91 #include "ethernet.h"
\r
92 #include "interrupt.h"
\r
94 #define emacNUM_RX_BUFFERS 5
\r
95 #define emacFRAM_SIZE_BYTES 2
\r
96 #define macNEGOTIATE_DELAY 2000
\r
97 #define macWAIT_SEND_TIME ( 10 )
\r
99 /* The task that handles the MAC peripheral. This is created at a high
\r
100 priority and is effectively a deferred interrupt handler. The peripheral
\r
101 handling is deferred to a task to prevent the entire FIFO having to be read
\r
102 from within an ISR. */
\r
103 void vMACHandleTask( void *pvParameters );
\r
105 /*-----------------------------------------------------------*/
\r
107 /* The semaphore used to wake the uIP task when data arrives. */
\r
108 xSemaphoreHandle xEMACSemaphore = NULL;
\r
110 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
111 is processed at the task level to prevent the need to read the entire FIFO from
\r
112 within the ISR itself. */
\r
113 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
115 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
116 point to one of the Rx buffers. */
\r
117 unsigned char *uip_buf;
\r
119 /* Buffers into which Rx data is placed. */
\r
120 static unsigned char ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ] __attribute__((aligned(4)));
\r
122 /* The length of the data within each of the Rx buffers. */
\r
123 static unsigned long ulRxLength[ emacNUM_RX_BUFFERS ];
\r
125 /* Used to keep a track of the number of bytes to transmit. */
\r
126 static unsigned long ulNextTxSpace;
\r
128 /*-----------------------------------------------------------*/
\r
130 portBASE_TYPE vInitEMAC( void )
\r
132 unsigned long ulTemp;
\r
133 portBASE_TYPE xReturn;
\r
135 /* Ensure all interrupts are disabled. */
\r
136 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
138 /* Clear any interrupts that were already pending. */
\r
139 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
140 EthernetIntClear( ETH_BASE, ulTemp );
\r
142 /* Initialise the MAC and connect. */
\r
143 EthernetInit( ETH_BASE );
\r
144 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
145 EthernetEnable( ETH_BASE );
\r
147 /* Mark each Rx buffer as empty. */
\r
148 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
150 ulRxLength[ ulTemp ] = 0;
\r
153 /* Create the queue and task used to defer the MAC processing to the
\r
155 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
156 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
157 xReturn = xTaskCreate( vMACHandleTask, ( signed char * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
158 vTaskDelay( macNEGOTIATE_DELAY );
\r
160 /* We are only interested in Rx interrupts. */
\r
161 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
162 IntEnable( INT_ETH );
\r
163 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
167 /*-----------------------------------------------------------*/
\r
169 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
171 static unsigned long ulNextRxBuffer = 0;
\r
174 iLen = ulRxLength[ ulNextRxBuffer ];
\r
178 /* Leave room for the size at the start of the buffer. */
\r
179 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
181 ulRxLength[ ulNextRxBuffer ] = 0;
\r
184 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
186 ulNextRxBuffer = 0;
\r
192 /*-----------------------------------------------------------*/
\r
194 void vInitialiseSend( void )
\r
196 /* Set the index to the first byte to send - skipping over the size
\r
200 /*-----------------------------------------------------------*/
\r
202 void vIncrementTxLength( unsigned long ulLength )
\r
204 ulNextTxSpace += ulLength;
\r
206 /*-----------------------------------------------------------*/
\r
208 void vSendBufferToMAC( void )
\r
210 unsigned long *pulSource;
\r
211 unsigned short * pus;
\r
212 unsigned long ulNextWord;
\r
214 /* Locate the data to be send. */
\r
215 pus = ( unsigned short * ) uip_buf;
\r
217 /* Add in the size of the data. */
\r
219 *pus = ulNextTxSpace;
\r
221 /* Wait for data to be sent if there is no space immediately. */
\r
222 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
224 vTaskDelay( macWAIT_SEND_TIME );
\r
227 pulSource = ( unsigned long * ) pus;
\r
229 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned long ) )
\r
231 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
236 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
238 /*-----------------------------------------------------------*/
\r
240 void vEMAC_ISR( void )
\r
242 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
243 unsigned long ulTemp;
\r
245 /* Clear the interrupt. */
\r
246 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
247 EthernetIntClear( ETH_BASE, ulTemp );
\r
249 /* Was it an Rx interrupt? */
\r
250 if( ulTemp & ETH_INT_RX )
\r
252 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
253 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
256 /* Switch to the uIP task. */
\r
257 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
259 /*-----------------------------------------------------------*/
\r
261 void vMACHandleTask( void *pvParameters )
\r
264 unsigned long ulLength, ulInt;
\r
265 unsigned long *pulBuffer;
\r
266 static unsigned long ulNextRxBuffer = 0;
\r
270 /* Wait for something to do. */
\r
271 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
273 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
275 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
277 /* Leave room at the start of the buffer for the size. */
\r
278 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
279 *pulBuffer = ( ulLength >> 16 );
\r
281 /* Get the size of the data. */
\r
282 pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
283 ulLength &= 0xFFFF;
\r
289 if( ulLength >= UIP_BUFSIZE )
\r
291 /* The data won't fit in our buffer. Ensure we don't
\r
292 try to write into the buffer. */
\r
296 /* Read out the data into our buffer. */
\r
297 for( i = 0; i < ulLength; i += sizeof( unsigned long ) )
\r
299 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
303 /* Store the length of the data into the separate array. */
\r
304 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
306 /* Use the next buffer the next time through. */
\r
308 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
310 ulNextRxBuffer = 0;
\r
313 /* Ensure the uIP task is not blocked as data has arrived. */
\r
314 xSemaphoreGive( xEMACSemaphore );
\r
318 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r