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