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 portCHAR *uip_buf;
\r
111 /* Buffers into which Rx data is placed. */
\r
114 unsigned portLONG ulJustForAlignment;
\r
115 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
118 /* The length of the data within each of the Rx buffers. */
\r
119 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
121 /* Used to keep a track of the number of bytes to transmit. */
\r
122 static unsigned portLONG ulNextTxSpace;
\r
124 /*-----------------------------------------------------------*/
\r
126 portBASE_TYPE vInitEMAC( void )
\r
128 unsigned long ulTemp;
\r
129 portBASE_TYPE xReturn;
\r
131 /* Ensure all interrupts are disabled. */
\r
132 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
134 /* Clear any interrupts that were already pending. */
\r
135 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
136 EthernetIntClear( ETH_BASE, ulTemp );
\r
138 /* Initialise the MAC and connect. */
\r
139 EthernetInit( ETH_BASE );
\r
140 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
141 EthernetEnable( ETH_BASE );
\r
143 /* Mark each Rx buffer as empty. */
\r
144 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
146 ulRxLength[ ulTemp ] = 0;
\r
149 /* Create the queue and task used to defer the MAC processing to the
\r
151 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
152 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
153 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
154 vTaskDelay( macNEGOTIATE_DELAY );
\r
156 /* We are only interested in Rx interrupts. */
\r
157 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
158 IntEnable( INT_ETH );
\r
159 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
163 /*-----------------------------------------------------------*/
\r
165 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
167 static unsigned long ulNextRxBuffer = 0;
\r
170 iLen = ulRxLength[ ulNextRxBuffer ];
\r
174 /* Leave room for the size at the start of the buffer. */
\r
175 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
177 ulRxLength[ ulNextRxBuffer ] = 0;
\r
180 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
182 ulNextRxBuffer = 0;
\r
188 /*-----------------------------------------------------------*/
\r
190 void vInitialiseSend( void )
\r
192 /* Set the index to the first byte to send - skipping over the size
\r
196 /*-----------------------------------------------------------*/
\r
198 void vIncrementTxLength( unsigned portLONG ulLength )
\r
200 ulNextTxSpace += ulLength;
\r
202 /*-----------------------------------------------------------*/
\r
204 void vSendBufferToMAC( void )
\r
206 unsigned long *pulSource;
\r
207 unsigned portSHORT * pus;
\r
208 unsigned portLONG ulNextWord;
\r
210 /* Locate the data to be send. */
\r
211 pus = ( unsigned portSHORT * ) uip_buf;
\r
213 /* Add in the size of the data. */
\r
215 *pus = ulNextTxSpace;
\r
217 /* Wait for data to be sent if there is no space immediately. */
\r
218 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
220 vTaskDelay( macWAIT_SEND_TIME );
\r
223 pulSource = ( unsigned portLONG * ) pus;
\r
225 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
227 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
232 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
234 /*-----------------------------------------------------------*/
\r
236 void vEMAC_ISR( void )
\r
238 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
239 unsigned portLONG ulTemp;
\r
241 /* Clear the interrupt. */
\r
242 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
243 EthernetIntClear( ETH_BASE, ulTemp );
\r
245 /* Was it an Rx interrupt? */
\r
246 if( ulTemp & ETH_INT_RX )
\r
248 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
249 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
252 /* Switch to the uIP task. */
\r
253 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
255 /*-----------------------------------------------------------*/
\r
257 void vMACHandleTask( void *pvParameters )
\r
259 unsigned long i, ulInt;
\r
260 unsigned portLONG ulLength;
\r
261 unsigned long *pulBuffer;
\r
262 static unsigned portLONG ulNextRxBuffer = 0;
\r
266 /* Wait for something to do. */
\r
267 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
269 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
271 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
273 /* Leave room at the start of the buffer for the size. */
\r
274 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
275 *pulBuffer = ( ulLength >> 16 );
\r
277 /* Get the size of the data. */
\r
278 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
279 ulLength &= 0xFFFF;
\r
285 if( ulLength >= UIP_BUFSIZE )
\r
287 /* The data won't fit in our buffer. Ensure we don't
\r
288 try to write into the buffer. */
\r
292 /* Read out the data into our buffer. */
\r
293 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
295 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
299 /* Store the length of the data into the separate array. */
\r
300 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
302 /* Use the next buffer the next time through. */
\r
304 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
306 ulNextRxBuffer = 0;
\r
309 /* Ensure the uIP task is not blocked as data has arrived. */
\r
310 xSemaphoreGive( xEMACSemaphore );
\r
314 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r