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