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