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