]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/LPC17xx/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 / LPC17xx / 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 /* Hardware abstraction. */\r
39 #include "FreeRTOS_IO.h"\r
40 \r
41 /* FreeRTOS+UDP includes. */\r
42 #include "FreeRTOS_UDP_IP.h"\r
43 #include "FreeRTOS_IP_Private.h"\r
44 #include "FreeRTOS_Sockets.h"\r
45 #include "NetworkBufferManagement.h"\r
46 \r
47 /* Driver includes. */\r
48 #include "lpc17xx_emac.h"\r
49 #include "lpc17xx_pinsel.h"\r
50 \r
51 /* Demo includes. */\r
52 #include "NetworkInterface.h"\r
53 \r
54 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1\r
55         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer\r
56 #else\r
57         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )\r
58 #endif\r
59 \r
60 /* When a packet is ready to be sent, if it cannot be sent immediately then the\r
61 task performing the transmit will block for niTX_BUFFER_FREE_WAIT\r
62 milliseconds.  It will do this a maximum of niMAX_TX_ATTEMPTS before giving\r
63 up. */\r
64 #define niTX_BUFFER_FREE_WAIT   ( ( TickType_t ) 2UL / portTICK_RATE_MS )\r
65 #define niMAX_TX_ATTEMPTS               ( 5 )\r
66 \r
67 /* The length of the queue used to send interrupt status words from the\r
68 interrupt handler to the deferred handler task. */\r
69 #define niINTERRUPT_QUEUE_LENGTH        ( 10 )\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*\r
74  * A deferred interrupt handler task that processes\r
75  */\r
76 static void prvEMACHandlerTask( void *pvParameters );\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* The queue used to communicate Ethernet events with the IP task. */\r
81 extern xQueueHandle xNetworkEventQueue;\r
82 \r
83 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
84 interrupt is received. */\r
85 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 BaseType_t xNetworkInterfaceInitialise( void )\r
89 {\r
90 EMAC_CFG_Type Emac_Config;\r
91 PINSEL_CFG_Type xPinConfig;\r
92 BaseType_t xStatus, xReturn;\r
93 extern uint8_t ucMACAddress[ 6 ];\r
94 \r
95         /* Enable Ethernet Pins */\r
96         boardCONFIGURE_ENET_PINS( xPinConfig );\r
97 \r
98         Emac_Config.Mode = EMAC_MODE_AUTO;\r
99         Emac_Config.pbEMAC_Addr = ucMACAddress;\r
100         xStatus = EMAC_Init( &Emac_Config );\r
101 \r
102         LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );\r
103 \r
104         if( xStatus != ERROR )\r
105         {\r
106                 vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
107                 configASSERT( xEMACRxEventSemaphore );\r
108 \r
109                 /* The handler task is created at the highest possible priority to\r
110                 ensure the interrupt handler can return directly to it. */\r
111                 xTaskCreate( prvEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
112 \r
113                 /* Enable the interrupt and set its priority to the minimum\r
114                 interrupt priority.  */\r
115                 NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );\r
116                 NVIC_EnableIRQ( ENET_IRQn );\r
117 \r
118                 xReturn = pdPASS;\r
119         }\r
120         else\r
121         {\r
122                 xReturn = pdFAIL;\r
123         }\r
124 \r
125         configASSERT( xStatus != ERROR );\r
126 \r
127         return xReturn;\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
132 {\r
133 BaseType_t xReturn = pdFAIL;\r
134 int32_t x;\r
135 extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );\r
136 extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );\r
137 \r
138 \r
139         /* Attempt to obtain access to a Tx buffer. */\r
140         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
141         {\r
142                 if( EMAC_CheckTransmitIndex() == TRUE )\r
143                 {\r
144                         /* Will the data fit in the Tx buffer? */\r
145                         if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */\r
146                         {\r
147                                 /* Assign the buffer to the Tx descriptor that is now known to\r
148                                 be free. */\r
149                                 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );\r
150 \r
151                                 /* The EMAC now owns the buffer. */\r
152                                 pxNetworkBuffer->pucEthernetBuffer = NULL;\r
153 \r
154                                 /* Initiate the Tx. */\r
155                                 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );\r
156                                 iptraceNETWORK_INTERFACE_TRANSMIT();\r
157 \r
158                                 /* The Tx has been initiated. */\r
159                                 xReturn = pdPASS;\r
160                         }\r
161                         break;\r
162                 }\r
163                 else\r
164                 {\r
165                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
166                 }\r
167         }\r
168 \r
169         /* Finished with the network buffer. */\r
170         vNetworkBufferRelease( pxNetworkBuffer );\r
171 \r
172         return xReturn;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void ENET_IRQHandler( void )\r
177 {\r
178 uint32_t ulInterruptCause;\r
179 \r
180         while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )\r
181         {\r
182                 /* Clear the interrupt. */\r
183                 LPC_EMAC->IntClear = ulInterruptCause;\r
184 \r
185                 /* Clear fatal error conditions.  NOTE:  The driver does not clear all\r
186                 errors, only those actually experienced.  For future reference, range\r
187                 errors are not actually errors so can be ignored. */\r
188                 if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )\r
189                 {\r
190                         LPC_EMAC->Command |= EMAC_CR_TX_RES;\r
191                 }\r
192 \r
193                 /* Unblock the deferred interrupt handler task if the event was an\r
194                 Rx. */\r
195                 if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )\r
196                 {\r
197                         xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );\r
198                 }\r
199         }\r
200 \r
201         /* ulInterruptCause is used for convenience here.  A context switch is\r
202         wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a\r
203         compiler warning. */\r
204         portEND_SWITCHING_ISR( ulInterruptCause );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvEMACHandlerTask( void *pvParameters )\r
209 {\r
210 size_t xDataLength;\r
211 const uint16_t usCRCLength = 4;\r
212 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
213 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
214 \r
215 /* This is not included in the header file for some reason. */\r
216 extern uint8_t *EMAC_NextPacketToRead( void );\r
217 \r
218         ( void ) pvParameters;\r
219         configASSERT( xEMACRxEventSemaphore );\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* Wait for the EMAC interrupt to indicate that another packet has been\r
224                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
225                 set to 0 in FreeRTOSConfig.h. */\r
226                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
227 \r
228                 /* At least one packet has been received. */\r
229                 while( EMAC_CheckReceiveIndex() != FALSE )\r
230                 {\r
231                         /* Obtain the length, minus the CRC.  The CRC is four bytes\r
232                         but the length is already minus 1. */\r
233                         xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );\r
234 \r
235                         if( xDataLength > 0U )\r
236                         {\r
237                                 /* Obtain a network buffer to pass this data into the\r
238                                 stack.  No storage is required as the network buffer\r
239                                 will point directly to the buffer that already holds\r
240                                 the     received data. */\r
241                                 pxNetworkBuffer = pxNetworkBufferGet( 0, ( TickType_t ) 0 );\r
242 \r
243                                 if( pxNetworkBuffer != NULL )\r
244                                 {\r
245                                         pxNetworkBuffer->pucEthernetBuffer = EMAC_NextPacketToRead();\r
246                                         pxNetworkBuffer->xDataLength = xDataLength;\r
247                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
248 \r
249                                         /* Data was received and stored.  Send a message to the IP\r
250                                         task to let it know. */\r
251                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
252                                         {\r
253                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
254                                                 iptraceETHERNET_RX_EVENT_LOST();\r
255                                         }\r
256                                 }\r
257                                 else\r
258                                 {\r
259                                         iptraceETHERNET_RX_EVENT_LOST();\r
260                                 }\r
261 \r
262                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
263                         }\r
264 \r
265                         /* Release the frame. */\r
266                         EMAC_UpdateRxConsumeIndex();\r
267                 }\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r