2 FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
33 /* Kernel includes. */
\r
34 #include "FreeRTOS.h"
\r
38 /* Demo includes. */
\r
44 /* Hardware library includes. */
\r
45 #include "hw_types.h"
\r
46 #include "hw_memmap.h"
\r
47 #include "hw_ints.h"
\r
48 #include "hw_ethernet.h"
\r
49 #include "ethernet.h"
\r
50 #include "interrupt.h"
\r
52 #define emacNUM_RX_BUFFERS 5
\r
53 #define emacFRAM_SIZE_BYTES 2
\r
54 #define macNEGOTIATE_DELAY 2000
\r
55 #define macWAIT_SEND_TIME ( 10 )
\r
57 /* The task that handles the MAC peripheral. This is created at a high
\r
58 priority and is effectively a deferred interrupt handler. The peripheral
\r
59 handling is deferred to a task to prevent the entire FIFO having to be read
\r
60 from within an ISR. */
\r
61 void vMACHandleTask( void *pvParameters );
\r
63 /*-----------------------------------------------------------*/
\r
65 /* The semaphore used to wake the uIP task when data arrives. */
\r
66 xSemaphoreHandle xEMACSemaphore = NULL;
\r
68 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
69 is processed at the task level to prevent the need to read the entire FIFO from
\r
70 within the ISR itself. */
\r
71 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
73 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
74 point to one of the Rx buffers. */
\r
75 unsigned portCHAR *uip_buf;
\r
77 /* Buffers into which Rx data is placed. */
\r
80 unsigned portLONG ulJustForAlignment;
\r
81 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
84 /* The length of the data within each of the Rx buffers. */
\r
85 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
87 /* Used to keep a track of the number of bytes to transmit. */
\r
88 static unsigned portLONG ulNextTxSpace;
\r
90 /*-----------------------------------------------------------*/
\r
92 portBASE_TYPE vInitEMAC( void )
\r
94 unsigned long ulTemp;
\r
95 portBASE_TYPE xReturn;
\r
97 /* Ensure all interrupts are disabled. */
\r
98 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
100 /* Clear any interrupts that were already pending. */
\r
101 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
102 EthernetIntClear( ETH_BASE, ulTemp );
\r
104 /* Initialise the MAC and connect. */
\r
105 EthernetInit( ETH_BASE );
\r
106 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
107 EthernetEnable( ETH_BASE );
\r
109 /* Mark each Rx buffer as empty. */
\r
110 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
112 ulRxLength[ ulTemp ] = 0;
\r
115 /* Create the queue and task used to defer the MAC processing to the
\r
117 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
118 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
119 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
120 vTaskDelay( macNEGOTIATE_DELAY );
\r
122 /* We are only interested in Rx interrupts. */
\r
123 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
124 IntEnable( INT_ETH );
\r
125 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
129 /*-----------------------------------------------------------*/
\r
131 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
133 static unsigned long ulNextRxBuffer = 0;
\r
136 iLen = ulRxLength[ ulNextRxBuffer ];
\r
140 /* Leave room for the size at the start of the buffer. */
\r
141 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
143 ulRxLength[ ulNextRxBuffer ] = 0;
\r
146 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
148 ulNextRxBuffer = 0;
\r
154 /*-----------------------------------------------------------*/
\r
156 void vInitialiseSend( void )
\r
158 /* Set the index to the first byte to send - skipping over the size
\r
162 /*-----------------------------------------------------------*/
\r
164 void vIncrementTxLength( unsigned portLONG ulLength )
\r
166 ulNextTxSpace += ulLength;
\r
168 /*-----------------------------------------------------------*/
\r
170 void vSendBufferToMAC( void )
\r
172 unsigned long *pulSource;
\r
173 unsigned portSHORT * pus;
\r
174 unsigned portLONG ulNextWord;
\r
176 /* Locate the data to be send. */
\r
177 pus = ( unsigned portSHORT * ) uip_buf;
\r
179 /* Add in the size of the data. */
\r
181 *pus = ulNextTxSpace;
\r
183 /* Wait for data to be sent if there is no space immediately. */
\r
184 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
186 vTaskDelay( macWAIT_SEND_TIME );
\r
189 pulSource = ( unsigned portLONG * ) pus;
\r
191 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
193 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
198 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
200 /*-----------------------------------------------------------*/
\r
202 void vEMAC_ISR( void )
\r
204 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
205 unsigned portLONG ulTemp;
\r
207 /* Clear the interrupt. */
\r
208 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
209 EthernetIntClear( ETH_BASE, ulTemp );
\r
211 /* Was it an Rx interrupt? */
\r
212 if( ulTemp & ETH_INT_RX )
\r
214 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
215 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
218 /* Switch to the uIP task. */
\r
219 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
221 /*-----------------------------------------------------------*/
\r
223 void vMACHandleTask( void *pvParameters )
\r
225 unsigned long i, ulInt;
\r
226 unsigned portLONG ulLength;
\r
227 unsigned long *pulBuffer;
\r
228 static unsigned portLONG ulNextRxBuffer = 0;
\r
232 /* Wait for something to do. */
\r
233 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
235 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
237 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
239 /* Leave room at the start of the buffer for the size. */
\r
240 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
241 *pulBuffer = ( ulLength >> 16 );
\r
243 /* Get the size of the data. */
\r
244 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
245 ulLength &= 0xFFFF;
\r
251 if( ulLength >= UIP_BUFSIZE )
\r
253 /* The data won't fit in our buffer. Ensure we don't
\r
254 try to write into the buffer. */
\r
258 /* Read out the data into our buffer. */
\r
259 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
261 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
265 /* Store the length of the data into the separate array. */
\r
266 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
268 /* Use the next buffer the next time through. */
\r
270 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
272 ulNextRxBuffer = 0;
\r
275 /* Ensure the uIP task is not blocked as data has arrived. */
\r
276 xSemaphoreGive( xEMACSemaphore );
\r
280 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r