]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/LPC18xx/NetworkInterface.c
Change version number in common files within the FreeRTOS-plus directory and check...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / LPC18xx / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.\r
3  *\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
8  *\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
15  *\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
23  *\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
30  *\r
31  * 1 tab == 4 spaces!\r
32  *\r
33  * http://www.FreeRTOS.org\r
34  * http://www.FreeRTOS.org/udp\r
35  *\r
36  */\r
37 \r
38 /* Standard includes. */\r
39 #include <stdint.h>\r
40 \r
41 /* FreeRTOS includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 #include "queue.h"\r
45 #include "semphr.h"\r
46 \r
47 /* FreeRTOS+UDP includes. */\r
48 #include "FreeRTOS_UDP_IP.h"\r
49 #include "FreeRTOS_IP_Private.h"\r
50 #include "FreeRTOS_Sockets.h"\r
51 #include "NetworkBufferManagement.h"\r
52 \r
53 /* Driver includes. */\r
54 #include "lpc18xx_emac.h"\r
55 \r
56 /* Demo includes. */\r
57 #include "NetworkInterface.h"\r
58 \r
59 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY\r
60         #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)\r
61 #endif\r
62 \r
63 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS\r
64         #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors\r
65 #endif\r
66 \r
67 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS\r
68         #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors\r
69 #endif\r
70 \r
71 /* If a packet cannot be sent immediately then the task performing the send\r
72 operation will be held in the Blocked state (so other tasks can execute) for\r
73 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
74 before giving up. */\r
75 #define niTX_BUFFER_FREE_WAIT   ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
76 #define niMAX_TX_ATTEMPTS               ( 5 )\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /*\r
81  * A deferred interrupt handler task that processes received frames.\r
82  */\r
83 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* The queue used to communicate Ethernet events to the IP task. */\r
88 extern xQueueHandle xNetworkEventQueue;\r
89 \r
90 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
91 interrupt is received. */\r
92 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 portBASE_TYPE xNetworkInterfaceInitialise( void )\r
97 {\r
98 EMAC_CFG_Type Emac_Config;\r
99 portBASE_TYPE xReturn;\r
100 extern uint8_t ucMACAddress[ 6 ];\r
101 \r
102         Emac_Config.pbEMAC_Addr = ucMACAddress;\r
103         xReturn = EMAC_Init( &Emac_Config );\r
104 \r
105         if( xReturn == pdPASS )\r
106         {\r
107                 LPC_ETHERNET->DMA_INT_EN =  DMA_INT_NOR_SUM | DMA_INT_RECEIVE;\r
108 \r
109                 /* Create the event semaphore if it has not already been created. */\r
110                 if( xEMACRxEventSemaphore == NULL )\r
111                 {\r
112                         vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
113                         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
114                         {\r
115                                 /* If the trace recorder code is included name the semaphore for\r
116                                 viewing in FreeRTOS+Trace. */\r
117                                 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );\r
118                         }\r
119                         #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
120                 }\r
121 \r
122                 configASSERT( xEMACRxEventSemaphore );\r
123 \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
133 \r
134                 /* Enable the interrupt and set its priority as configured.  THIS\r
135                 DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED, PREFERABLY\r
136                 IN FreeRTOSConfig.h. */\r
137                 NVIC_SetPriority( ETHERNET_IRQn, configMAC_INTERRUPT_PRIORITY );\r
138                 NVIC_EnableIRQ( ETHERNET_IRQn );\r
139         }\r
140 \r
141         return xReturn;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
146 {\r
147 portBASE_TYPE xReturn = pdFAIL;\r
148 int32_t x;\r
149 \r
150         /* Attempt to obtain access to a Tx descriptor. */\r
151         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
152         {\r
153                 if( EMAC_CheckTransmitIndex() == TRUE )\r
154                 {\r
155                         /* Assign the buffer being transmitted to the Tx descriptor. */\r
156                         EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );\r
157 \r
158                         /* The EMAC now owns the buffer and will free it when it has been\r
159                         transmitted.  Set pucBuffer to NULL to ensure the buffer is not\r
160                         freed when the network buffer structure is returned to the pool\r
161                         of network buffers. */\r
162                         pxNetworkBuffer->pucEthernetBuffer = NULL;\r
163 \r
164                         /* Initiate the Tx. */\r
165                         EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );\r
166                         iptraceNETWORK_INTERFACE_TRANSMIT();\r
167 \r
168                         /* The Tx has been initiated. */\r
169                         xReturn = pdPASS;\r
170 \r
171                         break;\r
172                 }\r
173                 else\r
174                 {\r
175                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
176                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
177                 }\r
178         }\r
179 \r
180         /* Finished with the network buffer. */\r
181         vNetworkBufferRelease( pxNetworkBuffer );\r
182 \r
183         return xReturn;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void ETH_IRQHandler( void )\r
188 {\r
189 uint32_t ulInterruptCause;\r
190 \r
191         ulInterruptCause = LPC_ETHERNET->DMA_STAT ;\r
192 \r
193         /* Clear the interrupt. */\r
194         LPC_ETHERNET->DMA_STAT |= ( DMA_INT_NOR_SUM | DMA_INT_RECEIVE );\r
195 \r
196         /* Clear fatal error conditions.  NOTE:  The driver does not clear all\r
197         errors, only those actually experienced.  For future reference, range\r
198         errors are not actually errors so can be ignored. */\r
199         if( ( ulInterruptCause & ( 1 << 13 ) ) != 0U )\r
200         {\r
201                 LPC_ETHERNET->DMA_STAT |= ( 1 << 13 );\r
202         }\r
203 \r
204         /* Unblock the deferred interrupt handler task if the event was an Rx. */\r
205         if( ( ulInterruptCause & DMA_INT_RECEIVE ) != 0UL )\r
206         {\r
207                 xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );\r
208         }\r
209 \r
210         /* ulInterruptCause is used for convenience here.  A context switch is\r
211         wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a\r
212         compiler warning. */\r
213         portEND_SWITCHING_ISR( ulInterruptCause );\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )\r
218 {\r
219 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
220 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
221 \r
222         ( void ) pvParameters;\r
223         configASSERT( xEMACRxEventSemaphore );\r
224 \r
225         for( ;; )\r
226         {\r
227                 /* Wait for the EMAC interrupt to indicate that another packet has been\r
228                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
229                 set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1\r
230                 then portMAX_DELAY would be an indefinite block time and\r
231                 xSemaphoreTake() would only return when the semaphore was actually\r
232                 obtained. */\r
233                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
234 \r
235                 /* At least one packet has been received. */\r
236                 while( EMAC_CheckReceiveIndex() != FALSE )\r
237                 {\r
238                         /* The buffer filled by the DMA is going to be passed into the IP\r
239                         stack.  Allocate another buffer for the DMA descriptor. */\r
240                         pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( portTickType ) 0 );\r
241 \r
242                         if( pxNetworkBuffer != NULL )\r
243                         {\r
244                                 /* Swap the buffer just allocated and referenced from the\r
245                                 pxNetworkBuffer with the buffer that has already been filled by\r
246                                 the DMA.  pxNetworkBuffer will then hold a reference to the\r
247                                 buffer that already contains the data without any data having\r
248                                 been copied between buffers. */\r
249                                 EMAC_NextPacketToRead( pxNetworkBuffer );\r
250 \r
251                                 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
252                                 {\r
253                                         if( pxNetworkBuffer->xDataLength > 0 )\r
254                                         {\r
255                                                 /* If the frame would not be processed by the IP stack then\r
256                                                 don't even bother sending it to the IP stack. */\r
257                                                 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
258                                                 {\r
259                                                         pxNetworkBuffer->xDataLength = 0;\r
260                                                 }\r
261                                         }\r
262                                 }\r
263                                 #endif\r
264 \r
265                                 if( pxNetworkBuffer->xDataLength > 0 )\r
266                                 {\r
267                                         /* Store a pointer to the network buffer structure in the\r
268                                         padding space that was left in front of the Ethernet frame.\r
269                                         The pointer     is needed to ensure the network buffer structure\r
270                                         can be located when it is time for it to be freed if the\r
271                                         Ethernet frame gets     used as a zero copy buffer. */\r
272                                         *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
273 \r
274                                         /* Data was received and stored.  Send it to the IP task\r
275                                         for processing. */\r
276                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
277                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )\r
278                                         {\r
279                                                 /* The buffer could not be sent to the IP task so the\r
280                                                 buffer must be released. */\r
281                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
282                                                 iptraceETHERNET_RX_EVENT_LOST();\r
283                                         }\r
284                                         else\r
285                                         {\r
286                                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
287                                         }\r
288                                 }\r
289                                 else\r
290                                 {\r
291                                         /* The buffer does not contain any data so there is no\r
292                                         point sending it to the IP task.  Just release it. */\r
293                                         vNetworkBufferRelease( pxNetworkBuffer );\r
294                                         iptraceETHERNET_RX_EVENT_LOST();\r
295                                 }\r
296                         }\r
297                         else\r
298                         {\r
299                                 iptraceETHERNET_RX_EVENT_LOST();\r
300                         }\r
301 \r
302                         /* Release the descriptor. */\r
303                         EMAC_UpdateRxConsumeIndex();\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r