]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/LPC18xx/Using_LPCOpen_Library/NetworkInterface.c
63bd2a44d9f610e7b0d59c8105336c8d9d28519b
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / LPC18xx / Using_LPCOpen_Library / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.3 (C) 2014 Real Time Engineers ltd.\r
3  * All rights reserved\r
4  *\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
7  *\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
14  *\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
21  *\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
28  *\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
35  *\r
36  * 1 tab == 4 spaces!\r
37  *\r
38  * http://www.FreeRTOS.org\r
39  * http://www.FreeRTOS.org/udp\r
40  *\r
41  */\r
42 \r
43 /* Standard includes. */\r
44 #include <stdint.h>\r
45 \r
46 /* FreeRTOS includes. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 #include "queue.h"\r
50 #include "semphr.h"\r
51 \r
52 /* FreeRTOS+UDP includes. */\r
53 #include "FreeRTOS_UDP_IP.h"\r
54 #include "FreeRTOS_IP_Private.h"\r
55 #include "FreeRTOS_Sockets.h"\r
56 #include "NetworkBufferManagement.h"\r
57 #include "lpc18xx_43xx_EMAC_LPCOpen.h"\r
58 \r
59 /* Demo includes. */\r
60 #include "NetworkInterface.h"\r
61 \r
62 /* Library includes. */\r
63 #include "board.h"\r
64 \r
65 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY\r
66         #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)\r
67 #endif\r
68 \r
69 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS\r
70         #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors\r
71 #endif\r
72 \r
73 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS\r
74         #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors\r
75 #endif\r
76 \r
77 /* If a packet cannot be sent immediately then the task performing the send\r
78 operation will be held in the Blocked state (so other tasks can execute) for\r
79 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
80 before giving up. */\r
81 #define niTX_BUFFER_FREE_WAIT   ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
82 #define niMAX_TX_ATTEMPTS               ( 5 )\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * A deferred interrupt handler task that processes received frames.\r
88  */\r
89 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* The queue used to communicate Ethernet events to the IP task. */\r
94 extern xQueueHandle xNetworkEventQueue;\r
95 \r
96 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
97 interrupt is received. */\r
98 xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 portBASE_TYPE xNetworkInterfaceInitialise( void )\r
103 {\r
104 portBASE_TYPE xReturn;\r
105 extern uint8_t ucMACAddress[ 6 ];\r
106 \r
107         xReturn = xEMACInit( ucMACAddress );\r
108 \r
109         if( xReturn == pdPASS )\r
110         {\r
111                 /* Create the event semaphore if it has not already been created. */\r
112                 if( xEMACRxEventSemaphore == NULL )\r
113                 {\r
114                         vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
115                         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
116                         {\r
117                                 /* If the trace recorder code is included name the semaphore for\r
118                                 viewing in FreeRTOS+Trace. */\r
119                                 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );\r
120                         }\r
121                         #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
122 \r
123                         configASSERT( xEMACRxEventSemaphore );\r
124 \r
125                         /* The Rx deferred interrupt handler task is created at the highest\r
126                         possible priority to ensure the interrupt handler can return directly to\r
127                         it no matter which task was running when the interrupt occurred. */\r
128                         xTaskCreate(    prvEMACDeferredInterruptHandlerTask,/* The function that implements the task. */\r
129                                                         "MACTsk",\r
130                                                         configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (defined in words, not bytes). */\r
131                                                         NULL,                                           /* The task parameter is not used. */\r
132                                                         configMAX_PRIORITIES - 1,       /* The priority assigned to the task. */\r
133                                                         NULL );                                         /* The handle is not required, so NULL is passed. */\r
134                 }\r
135         }\r
136 \r
137         return xReturn;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
142 {\r
143 portBASE_TYPE xReturn = pdFAIL;\r
144 int32_t x;\r
145 \r
146         /* Attempt to obtain access to a Tx descriptor. */\r
147         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
148         {\r
149                 if( xEMACIsTxDescriptorAvailable() == TRUE )\r
150                 {\r
151                         /* Assign the buffer being transmitted to the Tx descriptor. */\r
152                         vEMACAssignBufferToDescriptor( pxNetworkBuffer->pucEthernetBuffer );\r
153 \r
154                         /* The EMAC now owns the buffer and will free it when it has been\r
155                         transmitted.  Set pucBuffer to NULL to ensure the buffer is not\r
156                         freed when the network buffer structure is returned to the pool\r
157                         of network buffers. */\r
158                         pxNetworkBuffer->pucEthernetBuffer = NULL;\r
159 \r
160                         /* Initiate the Tx. */\r
161                         vEMACStartNextTransmission( pxNetworkBuffer->xDataLength );\r
162                         iptraceNETWORK_INTERFACE_TRANSMIT();\r
163 \r
164                         /* The Tx has been initiated. */\r
165                         xReturn = pdPASS;\r
166 \r
167                         break;\r
168                 }\r
169                 else\r
170                 {\r
171                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
172                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
173                 }\r
174         }\r
175 \r
176         /* Finished with the network buffer. */\r
177         vNetworkBufferRelease( pxNetworkBuffer );\r
178 \r
179         return xReturn;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )\r
184 {\r
185 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
186 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
187 \r
188         ( void ) pvParameters;\r
189         configASSERT( xEMACRxEventSemaphore );\r
190 \r
191         for( ;; )\r
192         {\r
193                 /* Wait for the EMAC interrupt to indicate that another packet has been\r
194                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
195                 set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1\r
196                 then portMAX_DELAY would be an indefinite block time and\r
197                 xSemaphoreTake() would only return when the semaphore was actually\r
198                 obtained. */\r
199                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
200 \r
201                 /* At least one packet has been received. */\r
202                 while( xEMACRxDataAvailable() != FALSE )\r
203                 {\r
204                         /* The buffer filled by the DMA is going to be passed into the IP\r
205                         stack.  Allocate another buffer for the DMA descriptor. */\r
206                         pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( portTickType ) 0 );\r
207 \r
208                         if( pxNetworkBuffer != NULL )\r
209                         {\r
210                                 /* Swap the buffer just allocated and referenced from the\r
211                                 pxNetworkBuffer with the buffer that has already been filled by\r
212                                 the DMA.  pxNetworkBuffer will then hold a reference to the\r
213                                 buffer that already contains the data without any data having\r
214                                 been copied between buffers. */\r
215                                 vEMACSwapEmptyBufferForRxedData( pxNetworkBuffer );\r
216 \r
217                                 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
218                                 {\r
219                                         if( pxNetworkBuffer->xDataLength > 0 )\r
220                                         {\r
221                                                 /* If the frame would not be processed by the IP stack then\r
222                                                 don't even bother sending it to the IP stack. */\r
223                                                 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
224                                                 {\r
225                                                         pxNetworkBuffer->xDataLength = 0;\r
226                                                 }\r
227                                         }\r
228                                 }\r
229                                 #endif\r
230 \r
231                                 if( pxNetworkBuffer->xDataLength > 0 )\r
232                                 {\r
233                                         /* Store a pointer to the network buffer structure in the\r
234                                         padding space that was left in front of the Ethernet frame.\r
235                                         The pointer     is needed to ensure the network buffer structure\r
236                                         can be located when it is time for it to be freed if the\r
237                                         Ethernet frame gets     used as a zero copy buffer. */\r
238                                         *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
239 \r
240                                         /* Data was received and stored.  Send it to the IP task\r
241                                         for processing. */\r
242                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
243                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )\r
244                                         {\r
245                                                 /* The buffer could not be sent to the IP task so the\r
246                                                 buffer must be released. */\r
247                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
248                                                 iptraceETHERNET_RX_EVENT_LOST();\r
249                                         }\r
250                                         else\r
251                                         {\r
252                                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
253                                         }\r
254                                 }\r
255                                 else\r
256                                 {\r
257                                         /* The buffer does not contain any data so there is no\r
258                                         point sending it to the IP task.  Just release it. */\r
259                                         vNetworkBufferRelease( pxNetworkBuffer );\r
260                                         iptraceETHERNET_RX_EVENT_LOST();\r
261                                 }\r
262                         }\r
263                         else\r
264                         {\r
265                                 iptraceETHERNET_RX_EVENT_LOST();\r
266                         }\r
267 \r
268                         /* Release the descriptor. */\r
269                         vEMACReturnRxDescriptor();\r
270                 }\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r