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