2 * FreeRTOS+UDP V1.0.4
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\r
37 /* FreeRTOS+UDP includes. */
\r
38 #include "FreeRTOS_UDP_IP.h"
\r
39 #include "FreeRTOS_IP_Private.h"
\r
40 #include "FreeRTOS_Sockets.h"
\r
41 #include "NetworkBufferManagement.h"
\r
42 #include "lpc18xx_43xx_EMAC_LPCOpen.h"
\r
44 /* Demo includes. */
\r
45 #include "NetworkInterface.h"
\r
47 /* Library includes. */
\r
50 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY
\r
51 #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)
\r
54 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS
\r
55 #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors
\r
58 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS
\r
59 #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors
\r
62 /* If a packet cannot be sent immediately then the task performing the send
\r
63 operation will be held in the Blocked state (so other tasks can execute) for
\r
64 niTX_BUFFER_FREE_WAIT ticks. It will do this a maximum of niMAX_TX_ATTEMPTS
\r
65 before giving up. */
\r
66 #define niTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_RATE_MS )
\r
67 #define niMAX_TX_ATTEMPTS ( 5 )
\r
69 /*-----------------------------------------------------------*/
\r
72 * A deferred interrupt handler task that processes received frames.
\r
74 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );
\r
76 /*-----------------------------------------------------------*/
\r
78 /* The queue used to communicate Ethernet events to the IP task. */
\r
79 extern xQueueHandle xNetworkEventQueue;
\r
81 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
82 interrupt is received. */
\r
83 xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
85 /*-----------------------------------------------------------*/
\r
87 BaseType_t xNetworkInterfaceInitialise( void )
\r
90 extern uint8_t ucMACAddress[ 6 ];
\r
92 xReturn = xEMACInit( ucMACAddress );
\r
94 if( xReturn == pdPASS )
\r
96 /* Create the event semaphore if it has not already been created. */
\r
97 if( xEMACRxEventSemaphore == NULL )
\r
99 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
100 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
102 /* If the trace recorder code is included name the semaphore for
\r
103 viewing in FreeRTOS+Trace. */
\r
104 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );
\r
106 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
108 configASSERT( xEMACRxEventSemaphore );
\r
110 /* The Rx deferred interrupt handler task is created at the highest
\r
111 possible priority to ensure the interrupt handler can return directly to
\r
112 it no matter which task was running when the interrupt occurred. */
\r
113 xTaskCreate( prvEMACDeferredInterruptHandlerTask,/* The function that implements the task. */
\r
115 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (defined in words, not bytes). */
\r
116 NULL, /* The task parameter is not used. */
\r
117 configMAX_PRIORITIES - 1, /* The priority assigned to the task. */
\r
118 NULL ); /* The handle is not required, so NULL is passed. */
\r
124 /*-----------------------------------------------------------*/
\r
126 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
128 BaseType_t xReturn = pdFAIL;
\r
131 /* Attempt to obtain access to a Tx descriptor. */
\r
132 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
134 if( xEMACIsTxDescriptorAvailable() == TRUE )
\r
136 /* Assign the buffer being transmitted to the Tx descriptor. */
\r
137 vEMACAssignBufferToDescriptor( pxNetworkBuffer->pucEthernetBuffer );
\r
139 /* The EMAC now owns the buffer and will free it when it has been
\r
140 transmitted. Set pucBuffer to NULL to ensure the buffer is not
\r
141 freed when the network buffer structure is returned to the pool
\r
142 of network buffers. */
\r
143 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
145 /* Initiate the Tx. */
\r
146 vEMACStartNextTransmission( pxNetworkBuffer->xDataLength );
\r
147 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
149 /* The Tx has been initiated. */
\r
156 iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();
\r
157 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
161 /* Finished with the network buffer. */
\r
162 vNetworkBufferRelease( pxNetworkBuffer );
\r
166 /*-----------------------------------------------------------*/
\r
168 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )
\r
170 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
171 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
173 ( void ) pvParameters;
\r
174 configASSERT( xEMACRxEventSemaphore );
\r
178 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
179 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
180 set to 0 in FreeRTOSConfig.h. If INCLUDE_vTaskSuspend is set to 1
\r
181 then portMAX_DELAY would be an indefinite block time and
\r
182 xSemaphoreTake() would only return when the semaphore was actually
\r
184 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
186 /* At least one packet has been received. */
\r
187 while( xEMACRxDataAvailable() != FALSE )
\r
189 /* The buffer filled by the DMA is going to be passed into the IP
\r
190 stack. Allocate another buffer for the DMA descriptor. */
\r
191 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( TickType_t ) 0 );
\r
193 if( pxNetworkBuffer != NULL )
\r
195 /* Swap the buffer just allocated and referenced from the
\r
196 pxNetworkBuffer with the buffer that has already been filled by
\r
197 the DMA. pxNetworkBuffer will then hold a reference to the
\r
198 buffer that already contains the data without any data having
\r
199 been copied between buffers. */
\r
200 vEMACSwapEmptyBufferForRxedData( pxNetworkBuffer );
\r
202 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
\r
204 if( pxNetworkBuffer->xDataLength > 0 )
\r
206 /* If the frame would not be processed by the IP stack then
\r
207 don't even bother sending it to the IP stack. */
\r
208 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
\r
210 pxNetworkBuffer->xDataLength = 0;
\r
216 if( pxNetworkBuffer->xDataLength > 0 )
\r
218 /* Store a pointer to the network buffer structure in the
\r
219 padding space that was left in front of the Ethernet frame.
\r
220 The pointer is needed to ensure the network buffer structure
\r
221 can be located when it is time for it to be freed if the
\r
222 Ethernet frame gets used as a zero copy buffer. */
\r
223 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;
\r
225 /* Data was received and stored. Send it to the IP task
\r
227 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
228 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
\r
230 /* The buffer could not be sent to the IP task so the
\r
231 buffer must be released. */
\r
232 vNetworkBufferRelease( pxNetworkBuffer );
\r
233 iptraceETHERNET_RX_EVENT_LOST();
\r
237 iptraceNETWORK_INTERFACE_RECEIVE();
\r
242 /* The buffer does not contain any data so there is no
\r
243 point sending it to the IP task. Just release it. */
\r
244 vNetworkBufferRelease( pxNetworkBuffer );
\r
245 iptraceETHERNET_RX_EVENT_LOST();
\r
250 iptraceETHERNET_RX_EVENT_LOST();
\r
253 /* Release the descriptor. */
\r
254 vEMACReturnRxDescriptor();
\r
258 /*-----------------------------------------------------------*/
\r