2 FreeRTOS V7.1.0 - Copyright (C) 2011 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 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
101 unsigned portLONG ulJustForAlignment;
\r
102 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
105 /* The length of the data within each of the Rx buffers. */
\r
106 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
108 /* Used to keep a track of the number of bytes to transmit. */
\r
109 static unsigned portLONG ulNextTxSpace;
\r
111 /*-----------------------------------------------------------*/
\r
113 portBASE_TYPE vInitEMAC( void )
\r
115 unsigned long ulTemp;
\r
116 portBASE_TYPE xReturn;
\r
118 /* Ensure all interrupts are disabled. */
\r
119 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
121 /* Clear any interrupts that were already pending. */
\r
122 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
123 EthernetIntClear( ETH_BASE, ulTemp );
\r
125 /* Initialise the MAC and connect. */
\r
126 EthernetInit( ETH_BASE );
\r
127 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
128 EthernetEnable( ETH_BASE );
\r
130 /* Mark each Rx buffer as empty. */
\r
131 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
133 ulRxLength[ ulTemp ] = 0;
\r
136 /* Create the queue and task used to defer the MAC processing to the
\r
138 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
139 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
140 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
141 vTaskDelay( macNEGOTIATE_DELAY );
\r
143 /* We are only interested in Rx interrupts. */
\r
144 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
145 IntEnable( INT_ETH );
\r
146 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
150 /*-----------------------------------------------------------*/
\r
152 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
154 static unsigned long ulNextRxBuffer = 0;
\r
157 iLen = ulRxLength[ ulNextRxBuffer ];
\r
161 /* Leave room for the size at the start of the buffer. */
\r
162 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
164 ulRxLength[ ulNextRxBuffer ] = 0;
\r
167 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
169 ulNextRxBuffer = 0;
\r
175 /*-----------------------------------------------------------*/
\r
177 void vInitialiseSend( void )
\r
179 /* Set the index to the first byte to send - skipping over the size
\r
183 /*-----------------------------------------------------------*/
\r
185 void vIncrementTxLength( unsigned portLONG ulLength )
\r
187 ulNextTxSpace += ulLength;
\r
189 /*-----------------------------------------------------------*/
\r
191 void vSendBufferToMAC( void )
\r
193 unsigned long *pulSource;
\r
194 unsigned portSHORT * pus;
\r
195 unsigned portLONG ulNextWord;
\r
197 /* Locate the data to be send. */
\r
198 pus = ( unsigned portSHORT * ) uip_buf;
\r
200 /* Add in the size of the data. */
\r
202 *pus = ulNextTxSpace;
\r
204 /* Wait for data to be sent if there is no space immediately. */
\r
205 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
207 vTaskDelay( macWAIT_SEND_TIME );
\r
210 pulSource = ( unsigned portLONG * ) pus;
\r
212 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
214 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
219 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
221 /*-----------------------------------------------------------*/
\r
223 void vEMAC_ISR( void )
\r
225 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
226 unsigned portLONG ulTemp;
\r
228 /* Clear the interrupt. */
\r
229 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
230 EthernetIntClear( ETH_BASE, ulTemp );
\r
232 /* Was it an Rx interrupt? */
\r
233 if( ulTemp & ETH_INT_RX )
\r
235 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
236 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
239 /* Switch to the uIP task. */
\r
240 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
242 /*-----------------------------------------------------------*/
\r
244 void vMACHandleTask( void *pvParameters )
\r
246 unsigned long i, ulInt;
\r
247 unsigned portLONG ulLength;
\r
248 unsigned long *pulBuffer;
\r
249 static unsigned portLONG ulNextRxBuffer = 0;
\r
253 /* Wait for something to do. */
\r
254 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
256 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
258 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
260 /* Leave room at the start of the buffer for the size. */
\r
261 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
262 *pulBuffer = ( ulLength >> 16 );
\r
264 /* Get the size of the data. */
\r
265 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
266 ulLength &= 0xFFFF;
\r
272 if( ulLength >= UIP_BUFSIZE )
\r
274 /* The data won't fit in our buffer. Ensure we don't
\r
275 try to write into the buffer. */
\r
279 /* Read out the data into our buffer. */
\r
280 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
282 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
286 /* Store the length of the data into the separate array. */
\r
287 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
289 /* Use the next buffer the next time through. */
\r
291 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
293 ulNextRxBuffer = 0;
\r
296 /* Ensure the uIP task is not blocked as data has arrived. */
\r
297 xSemaphoreGive( xEMACSemaphore );
\r
301 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r