2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * FreeRTOS+UDP is an add-on component to FreeRTOS. It is not, in itself, part
\r
5 * of the FreeRTOS kernel. FreeRTOS+UDP is licensed separately from FreeRTOS,
\r
6 * and uses a different license to FreeRTOS. FreeRTOS+UDP uses a dual license
\r
7 * model, information on which is provided below:
\r
9 * - Open source licensing -
\r
10 * FreeRTOS+UDP is a free download and may be used, modified and distributed
\r
11 * without charge provided the user adheres to version two of the GNU General
\r
12 * Public license (GPL) and does not remove the copyright notice or this text.
\r
13 * The GPL V2 text is available on the gnu.org web site, and on the following
\r
14 * URL: http://www.FreeRTOS.org/gpl-2.0.txt
\r
16 * - Commercial licensing -
\r
17 * Businesses and individuals who wish to incorporate FreeRTOS+UDP into
\r
18 * proprietary software for redistribution in any form must first obtain a
\r
19 * (very) low cost commercial license - and in-so-doing support the maintenance,
\r
20 * support and further development of the FreeRTOS+UDP product. Commercial
\r
21 * licenses can be obtained from http://shop.freertos.org and do not require any
\r
22 * source files to be changed.
\r
24 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
25 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
26 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
27 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
28 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
29 * implied, expressed, or statutory.
\r
31 * 1 tab == 4 spaces!
\r
33 * http://www.FreeRTOS.org
\r
34 * http://www.FreeRTOS.org/udp
\r
38 /* Standard includes. */
\r
41 /* FreeRTOS includes. */
\r
42 #include "FreeRTOS.h"
\r
47 /* FreeRTOS+UDP includes. */
\r
48 #include "FreeRTOS_UDP_IP.h"
\r
49 #include "FreeRTOS_IP_Private.h"
\r
50 #include "FreeRTOS_Sockets.h"
\r
51 #include "NetworkBufferManagement.h"
\r
53 /* Driver includes. */
\r
54 #include "lpc18xx_emac.h"
\r
56 /* Demo includes. */
\r
57 #include "NetworkInterface.h"
\r
59 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY
\r
60 #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)
\r
63 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS
\r
64 #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors
\r
67 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS
\r
68 #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors
\r
71 /* If a packet cannot be sent immediately then the task performing the send
\r
72 operation will be held in the Blocked state (so other tasks can execute) for
\r
73 niTX_BUFFER_FREE_WAIT ticks. It will do this a maximum of niMAX_TX_ATTEMPTS
\r
74 before giving up. */
\r
75 #define niTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
76 #define niMAX_TX_ATTEMPTS ( 5 )
\r
78 /*-----------------------------------------------------------*/
\r
81 * A deferred interrupt handler task that processes received frames.
\r
83 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );
\r
85 /*-----------------------------------------------------------*/
\r
87 /* The queue used to communicate Ethernet events to the IP task. */
\r
88 extern xQueueHandle xNetworkEventQueue;
\r
90 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
91 interrupt is received. */
\r
92 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
94 /*-----------------------------------------------------------*/
\r
96 portBASE_TYPE xNetworkInterfaceInitialise( void )
\r
98 EMAC_CFG_Type Emac_Config;
\r
99 portBASE_TYPE xReturn;
\r
100 extern uint8_t ucMACAddress[ 6 ];
\r
102 Emac_Config.pbEMAC_Addr = ucMACAddress;
\r
103 xReturn = EMAC_Init( &Emac_Config );
\r
105 if( xReturn == pdPASS )
\r
107 LPC_ETHERNET->DMA_INT_EN = DMA_INT_NOR_SUM | DMA_INT_RECEIVE;
\r
109 /* Create the event semaphore if it has not already been created. */
\r
110 if( xEMACRxEventSemaphore == NULL )
\r
112 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
113 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
115 /* If the trace recorder code is included name the semaphore for
\r
116 viewing in FreeRTOS+Trace. */
\r
117 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );
\r
119 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
122 configASSERT( xEMACRxEventSemaphore );
\r
124 /* The Rx deferred interrupt handler task is created at the highest
\r
125 possible priority to ensure the interrupt handler can return directly to
\r
126 it no matter which task was running when the interrupt occurred. */
\r
127 xTaskCreate( prvEMACDeferredInterruptHandlerTask, /* The function that implements the task. */
\r
128 ( const signed char * const ) "MACTsk",
\r
129 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (defined in words, not bytes). */
\r
130 NULL, /* The task parameter is not used. */
\r
131 configMAX_PRIORITIES - 1, /* The priority assigned to the task. */
\r
132 NULL ); /* The handle is not required, so NULL is passed. */
\r
134 /* Enable the interrupt and set its priority as configured. THIS
\r
135 DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED, PREFERABLY
\r
136 IN FreeRTOSConfig.h. */
\r
137 NVIC_SetPriority( ETHERNET_IRQn, configMAC_INTERRUPT_PRIORITY );
\r
138 NVIC_EnableIRQ( ETHERNET_IRQn );
\r
143 /*-----------------------------------------------------------*/
\r
145 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
147 portBASE_TYPE xReturn = pdFAIL;
\r
150 /* Attempt to obtain access to a Tx descriptor. */
\r
151 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
153 if( EMAC_CheckTransmitIndex() == TRUE )
\r
155 /* Assign the buffer being transmitted to the Tx descriptor. */
\r
156 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );
\r
158 /* The EMAC now owns the buffer and will free it when it has been
\r
159 transmitted. Set pucBuffer to NULL to ensure the buffer is not
\r
160 freed when the network buffer structure is returned to the pool
\r
161 of network buffers. */
\r
162 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
164 /* Initiate the Tx. */
\r
165 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );
\r
166 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
168 /* The Tx has been initiated. */
\r
175 iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();
\r
176 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
180 /* Finished with the network buffer. */
\r
181 vNetworkBufferRelease( pxNetworkBuffer );
\r
185 /*-----------------------------------------------------------*/
\r
187 void ETH_IRQHandler( void )
\r
189 uint32_t ulInterruptCause;
\r
191 ulInterruptCause = LPC_ETHERNET->DMA_STAT ;
\r
193 /* Clear the interrupt. */
\r
194 LPC_ETHERNET->DMA_STAT |= ( DMA_INT_NOR_SUM | DMA_INT_RECEIVE );
\r
196 /* Clear fatal error conditions. NOTE: The driver does not clear all
\r
197 errors, only those actually experienced. For future reference, range
\r
198 errors are not actually errors so can be ignored. */
\r
199 if( ( ulInterruptCause & ( 1 << 13 ) ) != 0U )
\r
201 LPC_ETHERNET->DMA_STAT |= ( 1 << 13 );
\r
204 /* Unblock the deferred interrupt handler task if the event was an Rx. */
\r
205 if( ( ulInterruptCause & DMA_INT_RECEIVE ) != 0UL )
\r
207 xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );
\r
210 /* ulInterruptCause is used for convenience here. A context switch is
\r
211 wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a
\r
212 compiler warning. */
\r
213 portEND_SWITCHING_ISR( ulInterruptCause );
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )
\r
219 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
220 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
222 ( void ) pvParameters;
\r
223 configASSERT( xEMACRxEventSemaphore );
\r
227 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
228 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
229 set to 0 in FreeRTOSConfig.h. If INCLUDE_vTaskSuspend is set to 1
\r
230 then portMAX_DELAY would be an indefinite block time and
\r
231 xSemaphoreTake() would only return when the semaphore was actually
\r
233 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
235 /* At least one packet has been received. */
\r
236 while( EMAC_CheckReceiveIndex() != FALSE )
\r
238 /* The buffer filled by the DMA is going to be passed into the IP
\r
239 stack. Allocate another buffer for the DMA descriptor. */
\r
240 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( portTickType ) 0 );
\r
242 if( pxNetworkBuffer != NULL )
\r
244 /* Swap the buffer just allocated and referenced from the
\r
245 pxNetworkBuffer with the buffer that has already been filled by
\r
246 the DMA. pxNetworkBuffer will then hold a reference to the
\r
247 buffer that already contains the data without any data having
\r
248 been copied between buffers. */
\r
249 EMAC_NextPacketToRead( pxNetworkBuffer );
\r
251 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
\r
253 if( pxNetworkBuffer->xDataLength > 0 )
\r
255 /* If the frame would not be processed by the IP stack then
\r
256 don't even bother sending it to the IP stack. */
\r
257 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
\r
259 pxNetworkBuffer->xDataLength = 0;
\r
265 if( pxNetworkBuffer->xDataLength > 0 )
\r
267 /* Store a pointer to the network buffer structure in the
\r
268 padding space that was left in front of the Ethernet frame.
\r
269 The pointer is needed to ensure the network buffer structure
\r
270 can be located when it is time for it to be freed if the
\r
271 Ethernet frame gets used as a zero copy buffer. */
\r
272 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;
\r
274 /* Data was received and stored. Send it to the IP task
\r
276 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
277 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
279 /* The buffer could not be sent to the IP task so the
\r
280 buffer must be released. */
\r
281 vNetworkBufferRelease( pxNetworkBuffer );
\r
282 iptraceETHERNET_RX_EVENT_LOST();
\r
286 iptraceNETWORK_INTERFACE_RECEIVE();
\r
291 /* The buffer does not contain any data so there is no
\r
292 point sending it to the IP task. Just release it. */
\r
293 vNetworkBufferRelease( pxNetworkBuffer );
\r
294 iptraceETHERNET_RX_EVENT_LOST();
\r
299 iptraceETHERNET_RX_EVENT_LOST();
\r
302 /* Release the descriptor. */
\r
303 EMAC_UpdateRxConsumeIndex();
\r
307 /*-----------------------------------------------------------*/
\r