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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
38 /* FreeRTOS+UDP includes. */
\r
39 #include "FreeRTOS_UDP_IP.h"
\r
40 #include "FreeRTOS_IP_Private.h"
\r
41 #include "FreeRTOS_Sockets.h"
\r
42 #include "NetworkBufferManagement.h"
\r
43 #include "lpc18xx_43xx_EMAC_LPCOpen.h"
\r
45 /* Demo includes. */
\r
46 #include "NetworkInterface.h"
\r
48 /* Library includes. */
\r
51 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY
\r
52 #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)
\r
55 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS
\r
56 #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors
\r
59 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS
\r
60 #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors
\r
63 /* If a packet cannot be sent immediately then the task performing the send
\r
64 operation will be held in the Blocked state (so other tasks can execute) for
\r
65 niTX_BUFFER_FREE_WAIT ticks. It will do this a maximum of niMAX_TX_ATTEMPTS
\r
66 before giving up. */
\r
67 #define niTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_RATE_MS )
\r
68 #define niMAX_TX_ATTEMPTS ( 5 )
\r
70 /*-----------------------------------------------------------*/
\r
73 * A deferred interrupt handler task that processes received frames.
\r
75 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );
\r
77 /*-----------------------------------------------------------*/
\r
79 /* The queue used to communicate Ethernet events to the IP task. */
\r
80 extern xQueueHandle xNetworkEventQueue;
\r
82 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
83 interrupt is received. */
\r
84 xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
86 /*-----------------------------------------------------------*/
\r
88 BaseType_t xNetworkInterfaceInitialise( void )
\r
91 extern uint8_t ucMACAddress[ 6 ];
\r
93 xReturn = xEMACInit( ucMACAddress );
\r
95 if( xReturn == pdPASS )
\r
97 /* Create the event semaphore if it has not already been created. */
\r
98 if( xEMACRxEventSemaphore == NULL )
\r
100 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
101 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
103 /* If the trace recorder code is included name the semaphore for
\r
104 viewing in FreeRTOS+Trace. */
\r
105 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );
\r
107 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
109 configASSERT( xEMACRxEventSemaphore );
\r
111 /* The Rx deferred interrupt handler task is created at the highest
\r
112 possible priority to ensure the interrupt handler can return directly to
\r
113 it no matter which task was running when the interrupt occurred. */
\r
114 xTaskCreate( prvEMACDeferredInterruptHandlerTask,/* The function that implements the task. */
\r
116 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (defined in words, not bytes). */
\r
117 NULL, /* The task parameter is not used. */
\r
118 configMAX_PRIORITIES - 1, /* The priority assigned to the task. */
\r
119 NULL ); /* The handle is not required, so NULL is passed. */
\r
125 /*-----------------------------------------------------------*/
\r
127 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
129 BaseType_t xReturn = pdFAIL;
\r
132 /* Attempt to obtain access to a Tx descriptor. */
\r
133 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
135 if( xEMACIsTxDescriptorAvailable() == TRUE )
\r
137 /* Assign the buffer being transmitted to the Tx descriptor. */
\r
138 vEMACAssignBufferToDescriptor( pxNetworkBuffer->pucEthernetBuffer );
\r
140 /* The EMAC now owns the buffer and will free it when it has been
\r
141 transmitted. Set pucBuffer to NULL to ensure the buffer is not
\r
142 freed when the network buffer structure is returned to the pool
\r
143 of network buffers. */
\r
144 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
146 /* Initiate the Tx. */
\r
147 vEMACStartNextTransmission( pxNetworkBuffer->xDataLength );
\r
148 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
150 /* The Tx has been initiated. */
\r
157 iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();
\r
158 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
162 /* Finished with the network buffer. */
\r
163 vNetworkBufferRelease( pxNetworkBuffer );
\r
167 /*-----------------------------------------------------------*/
\r
169 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )
\r
171 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
172 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
174 ( void ) pvParameters;
\r
175 configASSERT( xEMACRxEventSemaphore );
\r
179 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
180 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
181 set to 0 in FreeRTOSConfig.h. If INCLUDE_vTaskSuspend is set to 1
\r
182 then portMAX_DELAY would be an indefinite block time and
\r
183 xSemaphoreTake() would only return when the semaphore was actually
\r
185 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
187 /* At least one packet has been received. */
\r
188 while( xEMACRxDataAvailable() != FALSE )
\r
190 /* The buffer filled by the DMA is going to be passed into the IP
\r
191 stack. Allocate another buffer for the DMA descriptor. */
\r
192 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( TickType_t ) 0 );
\r
194 if( pxNetworkBuffer != NULL )
\r
196 /* Swap the buffer just allocated and referenced from the
\r
197 pxNetworkBuffer with the buffer that has already been filled by
\r
198 the DMA. pxNetworkBuffer will then hold a reference to the
\r
199 buffer that already contains the data without any data having
\r
200 been copied between buffers. */
\r
201 vEMACSwapEmptyBufferForRxedData( pxNetworkBuffer );
\r
203 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
\r
205 if( pxNetworkBuffer->xDataLength > 0 )
\r
207 /* If the frame would not be processed by the IP stack then
\r
208 don't even bother sending it to the IP stack. */
\r
209 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
\r
211 pxNetworkBuffer->xDataLength = 0;
\r
217 if( pxNetworkBuffer->xDataLength > 0 )
\r
219 /* Store a pointer to the network buffer structure in the
\r
220 padding space that was left in front of the Ethernet frame.
\r
221 The pointer is needed to ensure the network buffer structure
\r
222 can be located when it is time for it to be freed if the
\r
223 Ethernet frame gets used as a zero copy buffer. */
\r
224 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;
\r
226 /* Data was received and stored. Send it to the IP task
\r
228 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
229 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
\r
231 /* The buffer could not be sent to the IP task so the
\r
232 buffer must be released. */
\r
233 vNetworkBufferRelease( pxNetworkBuffer );
\r
234 iptraceETHERNET_RX_EVENT_LOST();
\r
238 iptraceNETWORK_INTERFACE_RECEIVE();
\r
243 /* The buffer does not contain any data so there is no
\r
244 point sending it to the IP task. Just release it. */
\r
245 vNetworkBufferRelease( pxNetworkBuffer );
\r
246 iptraceETHERNET_RX_EVENT_LOST();
\r
251 iptraceETHERNET_RX_EVENT_LOST();
\r
254 /* Release the descriptor. */
\r
255 vEMACReturnRxDescriptor();
\r
259 /*-----------------------------------------------------------*/
\r