2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * This file is part of the FreeRTOS+UDP distribution. The FreeRTOS+UDP license
\r
5 * terms are different to the FreeRTOS license terms.
\r
7 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
8 * under a standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals that for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a commercial license before
\r
24 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
25 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
26 * and do not require any source files to be changed.
\r
28 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
29 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
30 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
31 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
32 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
33 * implied, expressed, or statutory.
\r
35 * 1 tab == 4 spaces!
\r
37 * http://www.FreeRTOS.org
\r
38 * http://www.FreeRTOS.org/udp
\r
42 /* Standard includes. */
\r
45 /* FreeRTOS includes. */
\r
46 #include "FreeRTOS.h"
\r
51 /* FreeRTOS+UDP includes. */
\r
52 #include "FreeRTOS_UDP_IP.h"
\r
53 #include "FreeRTOS_IP_Private.h"
\r
54 #include "FreeRTOS_Sockets.h"
\r
55 #include "NetworkBufferManagement.h"
\r
56 #include "lpc18xx_43xx_EMAC_LPCOpen.h"
\r
58 /* Demo includes. */
\r
59 #include "NetworkInterface.h"
\r
61 /* Library includes. */
\r
64 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY
\r
65 #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)
\r
68 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS
\r
69 #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors
\r
72 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS
\r
73 #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors
\r
76 /* If a packet cannot be sent immediately then the task performing the send
\r
77 operation will be held in the Blocked state (so other tasks can execute) for
\r
78 niTX_BUFFER_FREE_WAIT ticks. It will do this a maximum of niMAX_TX_ATTEMPTS
\r
79 before giving up. */
\r
80 #define niTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
81 #define niMAX_TX_ATTEMPTS ( 5 )
\r
83 /*-----------------------------------------------------------*/
\r
86 * A deferred interrupt handler task that processes received frames.
\r
88 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );
\r
90 /*-----------------------------------------------------------*/
\r
92 /* The queue used to communicate Ethernet events to the IP task. */
\r
93 extern xQueueHandle xNetworkEventQueue;
\r
95 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
96 interrupt is received. */
\r
97 xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
99 /*-----------------------------------------------------------*/
\r
101 portBASE_TYPE xNetworkInterfaceInitialise( void )
\r
103 portBASE_TYPE xReturn;
\r
104 extern uint8_t ucMACAddress[ 6 ];
\r
106 xReturn = xEMACInit( ucMACAddress );
\r
108 if( xReturn == pdPASS )
\r
110 /* Create the event semaphore if it has not already been created. */
\r
111 if( xEMACRxEventSemaphore == NULL )
\r
113 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
114 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
116 /* If the trace recorder code is included name the semaphore for
\r
117 viewing in FreeRTOS+Trace. */
\r
118 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );
\r
120 #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
138 /*-----------------------------------------------------------*/
\r
140 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
142 portBASE_TYPE xReturn = pdFAIL;
\r
145 /* Attempt to obtain access to a Tx descriptor. */
\r
146 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
148 if( xEMACIsTxDescriptorAvailable() == TRUE )
\r
150 /* Assign the buffer being transmitted to the Tx descriptor. */
\r
151 vEMACAssignBufferToDescriptor( pxNetworkBuffer->pucEthernetBuffer );
\r
153 /* The EMAC now owns the buffer and will free it when it has been
\r
154 transmitted. Set pucBuffer to NULL to ensure the buffer is not
\r
155 freed when the network buffer structure is returned to the pool
\r
156 of network buffers. */
\r
157 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
159 /* Initiate the Tx. */
\r
160 vEMACStartNextTransmission( pxNetworkBuffer->xDataLength );
\r
161 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
163 /* The Tx has been initiated. */
\r
170 iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();
\r
171 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
175 /* Finished with the network buffer. */
\r
176 vNetworkBufferRelease( pxNetworkBuffer );
\r
180 /*-----------------------------------------------------------*/
\r
182 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )
\r
184 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
185 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
187 ( void ) pvParameters;
\r
188 configASSERT( xEMACRxEventSemaphore );
\r
192 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
193 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
194 set to 0 in FreeRTOSConfig.h. If INCLUDE_vTaskSuspend is set to 1
\r
195 then portMAX_DELAY would be an indefinite block time and
\r
196 xSemaphoreTake() would only return when the semaphore was actually
\r
198 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
200 /* At least one packet has been received. */
\r
201 while( xEMACRxDataAvailable() != FALSE )
\r
203 /* The buffer filled by the DMA is going to be passed into the IP
\r
204 stack. Allocate another buffer for the DMA descriptor. */
\r
205 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( portTickType ) 0 );
\r
207 if( pxNetworkBuffer != NULL )
\r
209 /* Swap the buffer just allocated and referenced from the
\r
210 pxNetworkBuffer with the buffer that has already been filled by
\r
211 the DMA. pxNetworkBuffer will then hold a reference to the
\r
212 buffer that already contains the data without any data having
\r
213 been copied between buffers. */
\r
214 vEMACSwapEmptyBufferForRxedData( pxNetworkBuffer );
\r
216 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
\r
218 if( pxNetworkBuffer->xDataLength > 0 )
\r
220 /* If the frame would not be processed by the IP stack then
\r
221 don't even bother sending it to the IP stack. */
\r
222 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
\r
224 pxNetworkBuffer->xDataLength = 0;
\r
230 if( pxNetworkBuffer->xDataLength > 0 )
\r
232 /* Store a pointer to the network buffer structure in the
\r
233 padding space that was left in front of the Ethernet frame.
\r
234 The pointer is needed to ensure the network buffer structure
\r
235 can be located when it is time for it to be freed if the
\r
236 Ethernet frame gets used as a zero copy buffer. */
\r
237 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;
\r
239 /* Data was received and stored. Send it to the IP task
\r
241 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
242 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
244 /* The buffer could not be sent to the IP task so the
\r
245 buffer must be released. */
\r
246 vNetworkBufferRelease( pxNetworkBuffer );
\r
247 iptraceETHERNET_RX_EVENT_LOST();
\r
251 iptraceNETWORK_INTERFACE_RECEIVE();
\r
256 /* The buffer does not contain any data so there is no
\r
257 point sending it to the IP task. Just release it. */
\r
258 vNetworkBufferRelease( pxNetworkBuffer );
\r
259 iptraceETHERNET_RX_EVENT_LOST();
\r
264 iptraceETHERNET_RX_EVENT_LOST();
\r
267 /* Release the descriptor. */
\r
268 vEMACReturnRxDescriptor();
\r
272 /*-----------------------------------------------------------*/
\r