2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Kernel includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Demo includes. */
\r
76 /* Hardware library includes. */
\r
77 #include "hw_types.h"
\r
78 #include "hw_memmap.h"
\r
79 #include "hw_ints.h"
\r
80 #include "hw_ethernet.h"
\r
81 #include "ethernet.h"
\r
82 #include "interrupt.h"
\r
84 #define emacNUM_RX_BUFFERS 5
\r
85 #define emacFRAM_SIZE_BYTES 2
\r
86 #define macNEGOTIATE_DELAY 2000
\r
87 #define macWAIT_SEND_TIME ( 10 )
\r
89 /* The task that handles the MAC peripheral. This is created at a high
\r
90 priority and is effectively a deferred interrupt handler. The peripheral
\r
91 handling is deferred to a task to prevent the entire FIFO having to be read
\r
92 from within an ISR. */
\r
93 void vMACHandleTask( void *pvParameters );
\r
95 /*-----------------------------------------------------------*/
\r
97 /* The semaphore used to wake the uIP task when data arrives. */
\r
98 xSemaphoreHandle xEMACSemaphore = NULL;
\r
100 /* The semaphore used to wake the interrupt handler task. The peripheral
\r
101 is processed at the task level to prevent the need to read the entire FIFO from
\r
102 within the ISR itself. */
\r
103 xSemaphoreHandle xMACInterruptSemaphore = NULL;
\r
105 /* The buffer used by the uIP stack. In this case the pointer is used to
\r
106 point to one of the Rx buffers. */
\r
107 unsigned portCHAR *uip_buf;
\r
109 /* Buffers into which Rx data is placed. */
\r
112 unsigned portLONG ulJustForAlignment;
\r
113 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];
\r
116 /* The length of the data within each of the Rx buffers. */
\r
117 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];
\r
119 /* Used to keep a track of the number of bytes to transmit. */
\r
120 static unsigned portLONG ulNextTxSpace;
\r
122 /*-----------------------------------------------------------*/
\r
124 portBASE_TYPE vInitEMAC( void )
\r
126 unsigned long ulTemp;
\r
127 portBASE_TYPE xReturn;
\r
129 /* Ensure all interrupts are disabled. */
\r
130 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
132 /* Clear any interrupts that were already pending. */
\r
133 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
134 EthernetIntClear( ETH_BASE, ulTemp );
\r
136 /* Initialise the MAC and connect. */
\r
137 EthernetInit( ETH_BASE );
\r
138 EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );
\r
139 EthernetEnable( ETH_BASE );
\r
141 /* Mark each Rx buffer as empty. */
\r
142 for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )
\r
144 ulRxLength[ ulTemp ] = 0;
\r
147 /* Create the queue and task used to defer the MAC processing to the
\r
149 vSemaphoreCreateBinary( xMACInterruptSemaphore );
\r
150 xSemaphoreTake( xMACInterruptSemaphore, 0 );
\r
151 xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
152 vTaskDelay( macNEGOTIATE_DELAY );
\r
154 /* We are only interested in Rx interrupts. */
\r
155 IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );
\r
156 IntEnable( INT_ETH );
\r
157 EthernetIntEnable(ETH_BASE, ETH_INT_RX);
\r
161 /*-----------------------------------------------------------*/
\r
163 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
\r
165 static unsigned long ulNextRxBuffer = 0;
\r
168 iLen = ulRxLength[ ulNextRxBuffer ];
\r
172 /* Leave room for the size at the start of the buffer. */
\r
173 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
175 ulRxLength[ ulNextRxBuffer ] = 0;
\r
178 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
180 ulNextRxBuffer = 0;
\r
186 /*-----------------------------------------------------------*/
\r
188 void vInitialiseSend( void )
\r
190 /* Set the index to the first byte to send - skipping over the size
\r
194 /*-----------------------------------------------------------*/
\r
196 void vIncrementTxLength( unsigned portLONG ulLength )
\r
198 ulNextTxSpace += ulLength;
\r
200 /*-----------------------------------------------------------*/
\r
202 void vSendBufferToMAC( void )
\r
204 unsigned long *pulSource;
\r
205 unsigned portSHORT * pus;
\r
206 unsigned portLONG ulNextWord;
\r
208 /* Locate the data to be send. */
\r
209 pus = ( unsigned portSHORT * ) uip_buf;
\r
211 /* Add in the size of the data. */
\r
213 *pus = ulNextTxSpace;
\r
215 /* Wait for data to be sent if there is no space immediately. */
\r
216 while( !EthernetSpaceAvail( ETH_BASE ) )
\r
218 vTaskDelay( macWAIT_SEND_TIME );
\r
221 pulSource = ( unsigned portLONG * ) pus;
\r
223 for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )
\r
225 HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;
\r
230 HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;
\r
232 /*-----------------------------------------------------------*/
\r
234 void vEMAC_ISR( void )
\r
236 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
237 unsigned portLONG ulTemp;
\r
239 /* Clear the interrupt. */
\r
240 ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );
\r
241 EthernetIntClear( ETH_BASE, ulTemp );
\r
243 /* Was it an Rx interrupt? */
\r
244 if( ulTemp & ETH_INT_RX )
\r
246 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );
\r
247 EthernetIntDisable( ETH_BASE, ETH_INT_RX );
\r
250 /* Switch to the uIP task. */
\r
251 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
253 /*-----------------------------------------------------------*/
\r
255 void vMACHandleTask( void *pvParameters )
\r
257 unsigned long i, ulInt;
\r
258 unsigned portLONG ulLength;
\r
259 unsigned long *pulBuffer;
\r
260 static unsigned portLONG ulNextRxBuffer = 0;
\r
264 /* Wait for something to do. */
\r
265 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );
\r
267 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )
\r
269 ulLength = HWREG( ETH_BASE + MAC_O_DATA );
\r
271 /* Leave room at the start of the buffer for the size. */
\r
272 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );
\r
273 *pulBuffer = ( ulLength >> 16 );
\r
275 /* Get the size of the data. */
\r
276 pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );
\r
277 ulLength &= 0xFFFF;
\r
283 if( ulLength >= UIP_BUFSIZE )
\r
285 /* The data won't fit in our buffer. Ensure we don't
\r
286 try to write into the buffer. */
\r
290 /* Read out the data into our buffer. */
\r
291 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )
\r
293 *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );
\r
297 /* Store the length of the data into the separate array. */
\r
298 ulRxLength[ ulNextRxBuffer ] = ulLength;
\r
300 /* Use the next buffer the next time through. */
\r
302 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )
\r
304 ulNextRxBuffer = 0;
\r
307 /* Ensure the uIP task is not blocked as data has arrived. */
\r
308 xSemaphoreGive( xEMACSemaphore );
\r
312 EthernetIntEnable( ETH_BASE, ETH_INT_RX );
\r