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