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 /* Hardware abstraction. */
\r
48 #include "FreeRTOS_IO.h"
\r
50 /* FreeRTOS+UDP includes. */
\r
51 #include "FreeRTOS_UDP_IP.h"
\r
52 #include "FreeRTOS_Sockets.h"
\r
53 #include "NetworkBufferManagement.h"
\r
55 /* Driver includes. */
\r
56 #include "lpc17xx_emac.h"
\r
57 #include "lpc17xx_pinsel.h"
\r
59 /* Demo includes. */
\r
60 #include "NetworkInterface.h"
\r
62 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
\r
63 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
\r
65 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
\r
68 /* When a packet is ready to be sent, if it cannot be sent immediately then the
\r
69 task performing the transmit will block for niTX_BUFFER_FREE_WAIT
\r
70 milliseconds. It will do this a maximum of niMAX_TX_ATTEMPTS before giving
\r
72 #define niTX_BUFFER_FREE_WAIT ( ( portTickType ) 2UL / portTICK_RATE_MS )
\r
73 #define niMAX_TX_ATTEMPTS ( 5 )
\r
75 /* The length of the queue used to send interrupt status words from the
\r
76 interrupt handler to the deferred handler task. */
\r
77 #define niINTERRUPT_QUEUE_LENGTH ( 10 )
\r
79 /*-----------------------------------------------------------*/
\r
82 * A deferred interrupt handler task that processes
\r
84 static void prvEMACHandlerTask( void *pvParameters );
\r
86 /*-----------------------------------------------------------*/
\r
88 /* The queue used to communicate Ethernet events with the IP task. */
\r
89 extern xQueueHandle xNetworkEventQueue;
\r
91 /* The semaphore used to wake the deferred interrupt handler task when an Rx
\r
92 interrupt is received. */
\r
93 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;
\r
94 /*-----------------------------------------------------------*/
\r
96 portBASE_TYPE xNetworkInterfaceInitialise( void )
\r
98 EMAC_CFG_Type Emac_Config;
\r
99 PINSEL_CFG_Type xPinConfig;
\r
100 portBASE_TYPE xStatus, xReturn;
\r
101 extern uint8_t ucMACAddress[ 6 ];
\r
103 /* Enable Ethernet Pins */
\r
104 boardCONFIGURE_ENET_PINS( xPinConfig );
\r
106 Emac_Config.Mode = EMAC_MODE_AUTO;
\r
107 Emac_Config.pbEMAC_Addr = ucMACAddress;
\r
108 xStatus = EMAC_Init( &Emac_Config );
\r
110 LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );
\r
112 if( xStatus != ERROR )
\r
114 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
\r
115 configASSERT( xEMACRxEventSemaphore );
\r
117 /* The handler task is created at the highest possible priority to
\r
118 ensure the interrupt handler can return directly to it. */
\r
119 xTaskCreate( prvEMACHandlerTask, ( const signed char * const ) "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
121 /* Enable the interrupt and set its priority to the minimum
\r
122 interrupt priority. */
\r
123 NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );
\r
124 NVIC_EnableIRQ( ENET_IRQn );
\r
133 configASSERT( xStatus != ERROR );
\r
137 /*-----------------------------------------------------------*/
\r
139 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
141 portBASE_TYPE xReturn = pdFAIL;
\r
143 extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );
\r
144 extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );
\r
147 /* Attempt to obtain access to a Tx buffer. */
\r
148 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
150 if( EMAC_CheckTransmitIndex() == TRUE )
\r
152 /* Will the data fit in the Tx buffer? */
\r
153 if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */
\r
155 /* Assign the buffer to the Tx descriptor that is now known to
\r
157 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucBuffer );
\r
159 /* The EMAC now owns the buffer. */
\r
160 pxNetworkBuffer->pucBuffer = NULL;
\r
162 /* Initiate the Tx. */
\r
163 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );
\r
164 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
166 /* The Tx has been initiated. */
\r
173 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
177 /* Finished with the network buffer. */
\r
178 vNetworkBufferRelease( pxNetworkBuffer );
\r
182 /*-----------------------------------------------------------*/
\r
184 void ENET_IRQHandler( void )
\r
186 uint32_t ulInterruptCause;
\r
188 while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )
\r
190 /* Clear the interrupt. */
\r
191 LPC_EMAC->IntClear = ulInterruptCause;
\r
193 /* Clear fatal error conditions. NOTE: The driver does not clear all
\r
194 errors, only those actually experienced. For future reference, range
\r
195 errors are not actually errors so can be ignored. */
\r
196 if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )
\r
198 LPC_EMAC->Command |= EMAC_CR_TX_RES;
\r
201 /* Unblock the deferred interrupt handler task if the event was an
\r
203 if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )
\r
205 xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );
\r
209 /* ulInterruptCause is used for convenience here. A context switch is
\r
210 wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a
\r
211 compiler warning. */
\r
212 portEND_SWITCHING_ISR( ulInterruptCause );
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvEMACHandlerTask( void *pvParameters )
\r
218 size_t xDataLength;
\r
219 const uint16_t usCRCLength = 4;
\r
220 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
221 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
223 /* This is not included in the header file for some reason. */
\r
224 extern uint8_t *EMAC_NextPacketToRead( void );
\r
226 ( void ) pvParameters;
\r
227 configASSERT( xEMACRxEventSemaphore );
\r
231 /* Wait for the EMAC interrupt to indicate that another packet has been
\r
232 received. The while() loop is only needed if INCLUDE_vTaskSuspend is
\r
233 set to 0 in FreeRTOSConfig.h. */
\r
234 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
\r
236 /* At least one packet has been received. */
\r
237 while( EMAC_CheckReceiveIndex() != FALSE )
\r
239 /* Obtain the length, minus the CRC. The CRC is four bytes
\r
240 but the length is already minus 1. */
\r
241 xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );
\r
243 if( xDataLength > 0U )
\r
245 /* Obtain a network buffer to pass this data into the
\r
246 stack. No storage is required as the network buffer
\r
247 will point directly to the buffer that already holds
\r
248 the received data. */
\r
249 pxNetworkBuffer = pxNetworkBufferGet( 0, ( portTickType ) 0 );
\r
251 if( pxNetworkBuffer != NULL )
\r
253 pxNetworkBuffer->pucBuffer = EMAC_NextPacketToRead();
\r
254 pxNetworkBuffer->xDataLength = xDataLength;
\r
255 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
257 /* Data was received and stored. Send a message to the IP
\r
258 task to let it know. */
\r
259 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
261 vNetworkBufferRelease( pxNetworkBuffer );
\r
262 iptraceETHERNET_RX_EVENT_LOST();
\r
267 iptraceETHERNET_RX_EVENT_LOST();
\r
270 iptraceNETWORK_INTERFACE_RECEIVE();
\r
273 /* Release the frame. */
\r
274 EMAC_UpdateRxConsumeIndex();
\r
278 /*-----------------------------------------------------------*/
\r