2 * FreeRTOS+UDP V1.0.3 (C) 2014 Real Time Engineers ltd.
\r
3 * All rights reserved
\r
5 * This file is part of the FreeRTOS+UDP distribution. The FreeRTOS+UDP license
\r
6 * terms are different to the FreeRTOS license terms.
\r
8 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
9 * under a standard GPL open source license, or a commercial license. The
\r
10 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
11 * itself is distributed) requires that all software statically linked with
\r
12 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
13 * Details of both license options follow:
\r
15 * - Open source licensing -
\r
16 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
17 * distributed without charge provided the user adheres to version two of the
\r
18 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
19 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
20 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
22 * - Commercial licensing -
\r
23 * Businesses and individuals that for commercial or other reasons cannot comply
\r
24 * with the terms of the GPL V2 license must obtain a commercial license before
\r
25 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
26 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
27 * and do not require any source files to be changed.
\r
29 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
30 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
31 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
32 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
33 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
34 * implied, expressed, or statutory.
\r
36 * 1 tab == 4 spaces!
\r
38 * http://www.FreeRTOS.org
\r
39 * http://www.FreeRTOS.org/udp
\r
43 /* Standard includes. */
\r
46 /* FreeRTOS includes. */
\r
47 #include "FreeRTOS.h"
\r
52 /* Hardware abstraction. */
\r
53 #include "FreeRTOS_IO.h"
\r
55 /* FreeRTOS+UDP includes. */
\r
56 #include "FreeRTOS_UDP_IP.h"
\r
57 #include "FreeRTOS_IP_Private.h"
\r
58 #include "FreeRTOS_Sockets.h"
\r
59 #include "NetworkBufferManagement.h"
\r
61 /* Driver includes. */
\r
62 #include "lpc17xx_emac.h"
\r
63 #include "lpc17xx_pinsel.h"
\r
65 /* Demo includes. */
\r
66 #include "NetworkInterface.h"
\r
68 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
\r
69 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
\r
71 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
\r
74 /* When a packet is ready to be sent, if it cannot be sent immediately then the
\r
75 task performing the transmit will block for niTX_BUFFER_FREE_WAIT
\r
76 milliseconds. It will do this a maximum of niMAX_TX_ATTEMPTS before giving
\r
78 #define niTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
79 #define niMAX_TX_ATTEMPTS ( 5 )
\r
81 /* The length of the queue used to send interrupt status words from the
\r
82 interrupt handler to the deferred handler task. */
\r
83 #define niINTERRUPT_QUEUE_LENGTH ( 10 )
\r
85 /*-----------------------------------------------------------*/
\r
88 * A deferred interrupt handler task that processes
\r
90 static void prvEMACHandlerTask( void *pvParameters );
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The queue used to communicate Ethernet events with the IP task. */
\r
95 extern xQueueHandle xNetworkEventQueue;
\r
97 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
98 interrupt is received. */
\r
99 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
100 /*-----------------------------------------------------------*/
\r
102 portBASE_TYPE xNetworkInterfaceInitialise( void )
\r
104 EMAC_CFG_Type Emac_Config;
\r
105 PINSEL_CFG_Type xPinConfig;
\r
106 portBASE_TYPE xStatus, xReturn;
\r
107 extern uint8_t ucMACAddress[ 6 ];
\r
109 /* Enable Ethernet Pins */
\r
110 boardCONFIGURE_ENET_PINS( xPinConfig );
\r
112 Emac_Config.Mode = EMAC_MODE_AUTO;
\r
113 Emac_Config.pbEMAC_Addr = ucMACAddress;
\r
114 xStatus = EMAC_Init( &Emac_Config );
\r
116 LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );
\r
118 if( xStatus != ERROR )
\r
120 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
121 configASSERT( xEMACRxEventSemaphore );
\r
123 /* The handler task is created at the highest possible priority to
\r
124 ensure the interrupt handler can return directly to it. */
\r
125 xTaskCreate( prvEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
127 /* Enable the interrupt and set its priority to the minimum
\r
128 interrupt priority. */
\r
129 NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );
\r
130 NVIC_EnableIRQ( ENET_IRQn );
\r
139 configASSERT( xStatus != ERROR );
\r
143 /*-----------------------------------------------------------*/
\r
145 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
147 portBASE_TYPE xReturn = pdFAIL;
\r
149 extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );
\r
150 extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );
\r
153 /* Attempt to obtain access to a Tx buffer. */
\r
154 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
156 if( EMAC_CheckTransmitIndex() == TRUE )
\r
158 /* Will the data fit in the Tx buffer? */
\r
159 if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */
\r
161 /* Assign the buffer to the Tx descriptor that is now known to
\r
163 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );
\r
165 /* The EMAC now owns the buffer. */
\r
166 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
168 /* Initiate the Tx. */
\r
169 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );
\r
170 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
172 /* The Tx has been initiated. */
\r
179 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
183 /* Finished with the network buffer. */
\r
184 vNetworkBufferRelease( pxNetworkBuffer );
\r
188 /*-----------------------------------------------------------*/
\r
190 void ENET_IRQHandler( void )
\r
192 uint32_t ulInterruptCause;
\r
194 while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )
\r
196 /* Clear the interrupt. */
\r
197 LPC_EMAC->IntClear = ulInterruptCause;
\r
199 /* Clear fatal error conditions. NOTE: The driver does not clear all
\r
200 errors, only those actually experienced. For future reference, range
\r
201 errors are not actually errors so can be ignored. */
\r
202 if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )
\r
204 LPC_EMAC->Command |= EMAC_CR_TX_RES;
\r
207 /* Unblock the deferred interrupt handler task if the event was an
\r
209 if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )
\r
211 xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );
\r
215 /* ulInterruptCause is used for convenience here. A context switch is
\r
216 wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a
\r
217 compiler warning. */
\r
218 portEND_SWITCHING_ISR( ulInterruptCause );
\r
220 /*-----------------------------------------------------------*/
\r
222 static void prvEMACHandlerTask( void *pvParameters )
\r
224 size_t xDataLength;
\r
225 const uint16_t usCRCLength = 4;
\r
226 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
227 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
229 /* This is not included in the header file for some reason. */
\r
230 extern uint8_t *EMAC_NextPacketToRead( void );
\r
232 ( void ) pvParameters;
\r
233 configASSERT( xEMACRxEventSemaphore );
\r
237 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
238 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
239 set to 0 in FreeRTOSConfig.h. */
\r
240 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
242 /* At least one packet has been received. */
\r
243 while( EMAC_CheckReceiveIndex() != FALSE )
\r
245 /* Obtain the length, minus the CRC. The CRC is four bytes
\r
246 but the length is already minus 1. */
\r
247 xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );
\r
249 if( xDataLength > 0U )
\r
251 /* Obtain a network buffer to pass this data into the
\r
252 stack. No storage is required as the network buffer
\r
253 will point directly to the buffer that already holds
\r
254 the received data. */
\r
255 pxNetworkBuffer = pxNetworkBufferGet( 0, ( portTickType ) 0 );
\r
257 if( pxNetworkBuffer != NULL )
\r
259 pxNetworkBuffer->pucEthernetBuffer = EMAC_NextPacketToRead();
\r
260 pxNetworkBuffer->xDataLength = xDataLength;
\r
261 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
263 /* Data was received and stored. Send a message to the IP
\r
264 task to let it know. */
\r
265 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
267 vNetworkBufferRelease( pxNetworkBuffer );
\r
268 iptraceETHERNET_RX_EVENT_LOST();
\r
273 iptraceETHERNET_RX_EVENT_LOST();
\r
276 iptraceNETWORK_INTERFACE_RECEIVE();
\r
279 /* Release the frame. */
\r
280 EMAC_UpdateRxConsumeIndex();
\r
284 /*-----------------------------------------------------------*/
\r