]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/LPC18xx/Using_LPCOpen_Library/NetworkInterface.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / LPC18xx / Using_LPCOpen_Library / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.4\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <stdint.h>\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 #include "queue.h"\r
35 #include "semphr.h"\r
36 \r
37 /* FreeRTOS+UDP includes. */\r
38 #include "FreeRTOS_UDP_IP.h"\r
39 #include "FreeRTOS_IP_Private.h"\r
40 #include "FreeRTOS_Sockets.h"\r
41 #include "NetworkBufferManagement.h"\r
42 #include "lpc18xx_43xx_EMAC_LPCOpen.h"\r
43 \r
44 /* Demo includes. */\r
45 #include "NetworkInterface.h"\r
46 \r
47 /* Library includes. */\r
48 #include "board.h"\r
49 \r
50 #if configMAC_INTERRUPT_PRIORITY > configMAC_INTERRUPT_PRIORITY\r
51         #error configMAC_INTERRUPT_PRIORITY must be greater than or equal to configMAC_INTERRUPT_PRIORITY (higher numbers mean lower logical priority)\r
52 #endif\r
53 \r
54 #ifndef configNUM_RX_ETHERNET_DMA_DESCRIPTORS\r
55         #error configNUM_RX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of RX DMA descriptors\r
56 #endif\r
57 \r
58 #ifndef configNUM_TX_ETHERNET_DMA_DESCRIPTORS\r
59         #error configNUM_TX_ETHERNET_DMA_DESCRIPTORS must be defined in FreeRTOSConfig.h to set the number of TX DMA descriptors\r
60 #endif\r
61 \r
62 /* If a packet cannot be sent immediately then the task performing the send\r
63 operation will be held in the Blocked state (so other tasks can execute) for\r
64 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
65 before giving up. */\r
66 #define niTX_BUFFER_FREE_WAIT   ( ( TickType_t ) 2UL / portTICK_RATE_MS )\r
67 #define niMAX_TX_ATTEMPTS               ( 5 )\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /*\r
72  * A deferred interrupt handler task that processes received frames.\r
73  */\r
74 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters );\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 /* The queue used to communicate Ethernet events to the IP task. */\r
79 extern xQueueHandle xNetworkEventQueue;\r
80 \r
81 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
82 interrupt is received. */\r
83 xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 BaseType_t xNetworkInterfaceInitialise( void )\r
88 {\r
89 BaseType_t xReturn;\r
90 extern uint8_t ucMACAddress[ 6 ];\r
91 \r
92         xReturn = xEMACInit( ucMACAddress );\r
93 \r
94         if( xReturn == pdPASS )\r
95         {\r
96                 /* Create the event semaphore if it has not already been created. */\r
97                 if( xEMACRxEventSemaphore == NULL )\r
98                 {\r
99                         vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
100                         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
101                         {\r
102                                 /* If the trace recorder code is included name the semaphore for\r
103                                 viewing in FreeRTOS+Trace. */\r
104                                 vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" );\r
105                         }\r
106                         #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
107 \r
108                         configASSERT( xEMACRxEventSemaphore );\r
109 \r
110                         /* The Rx deferred interrupt handler task is created at the highest\r
111                         possible priority to ensure the interrupt handler can return directly to\r
112                         it no matter which task was running when the interrupt occurred. */\r
113                         xTaskCreate(    prvEMACDeferredInterruptHandlerTask,/* The function that implements the task. */\r
114                                                         "MACTsk",\r
115                                                         configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (defined in words, not bytes). */\r
116                                                         NULL,                                           /* The task parameter is not used. */\r
117                                                         configMAX_PRIORITIES - 1,       /* The priority assigned to the task. */\r
118                                                         NULL );                                         /* The handle is not required, so NULL is passed. */\r
119                 }\r
120         }\r
121 \r
122         return xReturn;\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
127 {\r
128 BaseType_t xReturn = pdFAIL;\r
129 int32_t x;\r
130 \r
131         /* Attempt to obtain access to a Tx descriptor. */\r
132         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
133         {\r
134                 if( xEMACIsTxDescriptorAvailable() == TRUE )\r
135                 {\r
136                         /* Assign the buffer being transmitted to the Tx descriptor. */\r
137                         vEMACAssignBufferToDescriptor( pxNetworkBuffer->pucEthernetBuffer );\r
138 \r
139                         /* The EMAC now owns the buffer and will free it when it has been\r
140                         transmitted.  Set pucBuffer to NULL to ensure the buffer is not\r
141                         freed when the network buffer structure is returned to the pool\r
142                         of network buffers. */\r
143                         pxNetworkBuffer->pucEthernetBuffer = NULL;\r
144 \r
145                         /* Initiate the Tx. */\r
146                         vEMACStartNextTransmission( pxNetworkBuffer->xDataLength );\r
147                         iptraceNETWORK_INTERFACE_TRANSMIT();\r
148 \r
149                         /* The Tx has been initiated. */\r
150                         xReturn = pdPASS;\r
151 \r
152                         break;\r
153                 }\r
154                 else\r
155                 {\r
156                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
157                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
158                 }\r
159         }\r
160 \r
161         /* Finished with the network buffer. */\r
162         vNetworkBufferRelease( pxNetworkBuffer );\r
163 \r
164         return xReturn;\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )\r
169 {\r
170 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
171 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
172 \r
173         ( void ) pvParameters;\r
174         configASSERT( xEMACRxEventSemaphore );\r
175 \r
176         for( ;; )\r
177         {\r
178                 /* Wait for the EMAC interrupt to indicate that another packet has been\r
179                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
180                 set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1\r
181                 then portMAX_DELAY would be an indefinite block time and\r
182                 xSemaphoreTake() would only return when the semaphore was actually\r
183                 obtained. */\r
184                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
185 \r
186                 /* At least one packet has been received. */\r
187                 while( xEMACRxDataAvailable() != FALSE )\r
188                 {\r
189                         /* The buffer filled by the DMA is going to be passed into the IP\r
190                         stack.  Allocate another buffer for the DMA descriptor. */\r
191                         pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( TickType_t ) 0 );\r
192 \r
193                         if( pxNetworkBuffer != NULL )\r
194                         {\r
195                                 /* Swap the buffer just allocated and referenced from the\r
196                                 pxNetworkBuffer with the buffer that has already been filled by\r
197                                 the DMA.  pxNetworkBuffer will then hold a reference to the\r
198                                 buffer that already contains the data without any data having\r
199                                 been copied between buffers. */\r
200                                 vEMACSwapEmptyBufferForRxedData( pxNetworkBuffer );\r
201 \r
202                                 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
203                                 {\r
204                                         if( pxNetworkBuffer->xDataLength > 0 )\r
205                                         {\r
206                                                 /* If the frame would not be processed by the IP stack then\r
207                                                 don't even bother sending it to the IP stack. */\r
208                                                 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
209                                                 {\r
210                                                         pxNetworkBuffer->xDataLength = 0;\r
211                                                 }\r
212                                         }\r
213                                 }\r
214                                 #endif\r
215 \r
216                                 if( pxNetworkBuffer->xDataLength > 0 )\r
217                                 {\r
218                                         /* Store a pointer to the network buffer structure in the\r
219                                         padding space that was left in front of the Ethernet frame.\r
220                                         The pointer     is needed to ensure the network buffer structure\r
221                                         can be located when it is time for it to be freed if the\r
222                                         Ethernet frame gets     used as a zero copy buffer. */\r
223                                         *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
224 \r
225                                         /* Data was received and stored.  Send it to the IP task\r
226                                         for processing. */\r
227                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
228                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
229                                         {\r
230                                                 /* The buffer could not be sent to the IP task so the\r
231                                                 buffer must be released. */\r
232                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
233                                                 iptraceETHERNET_RX_EVENT_LOST();\r
234                                         }\r
235                                         else\r
236                                         {\r
237                                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
238                                         }\r
239                                 }\r
240                                 else\r
241                                 {\r
242                                         /* The buffer does not contain any data so there is no\r
243                                         point sending it to the IP task.  Just release it. */\r
244                                         vNetworkBufferRelease( pxNetworkBuffer );\r
245                                         iptraceETHERNET_RX_EVENT_LOST();\r
246                                 }\r
247                         }\r
248                         else\r
249                         {\r
250                                 iptraceETHERNET_RX_EVENT_LOST();\r
251                         }\r
252 \r
253                         /* Release the descriptor. */\r
254                         vEMACReturnRxDescriptor();\r
255                 }\r
256         }\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r