]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/LPC17xx/NetworkInterface.c
Change version numbers ready for V8.0.0 release candidate 1 tag.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / LPC17xx / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.3 (C) 2014 Real Time Engineers ltd.\r
3  * All rights reserved\r
4  *\r
5  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
6  * terms are different to the FreeRTOS license terms.\r
7  *\r
8  * FreeRTOS+UDP uses a dual license model that allows the software to be used \r
9  * under a standard GPL open source license, or a commercial license.  The \r
10  * standard GPL license (unlike the modified GPL license under which FreeRTOS \r
11  * itself is distributed) requires that all software statically linked with \r
12  * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.  \r
13  * Details of both license options follow:\r
14  *\r
15  * - Open source licensing -\r
16  * FreeRTOS+UDP is a free download and may be used, modified, evaluated and\r
17  * distributed without charge provided the user adheres to version two of the\r
18  * GNU General Public License (GPL) and does not remove the copyright notice or\r
19  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
20  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
21  *\r
22  * - Commercial licensing -\r
23  * Businesses and individuals that for commercial or other reasons cannot comply\r
24  * with the terms of the GPL V2 license must obtain a commercial license before \r
25  * incorporating FreeRTOS+UDP into proprietary software for distribution in any \r
26  * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp \r
27  * and do not require any source files to be changed.\r
28  *\r
29  * FreeRTOS+UDP is distributed in the hope that it will be useful.  You cannot\r
30  * use FreeRTOS+UDP unless you agree that you use the software 'as is'.\r
31  * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied\r
32  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
33  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
34  * implied, expressed, or statutory.\r
35  *\r
36  * 1 tab == 4 spaces!\r
37  *\r
38  * http://www.FreeRTOS.org\r
39  * http://www.FreeRTOS.org/udp\r
40  *\r
41  */\r
42 \r
43 /* Standard includes. */\r
44 #include <stdint.h>\r
45 \r
46 /* FreeRTOS includes. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 #include "queue.h"\r
50 #include "semphr.h"\r
51 \r
52 /* Hardware abstraction. */\r
53 #include "FreeRTOS_IO.h"\r
54 \r
55 /* FreeRTOS+UDP includes. */\r
56 #include "FreeRTOS_UDP_IP.h"\r
57 #include "FreeRTOS_IP_Private.h"\r
58 #include "FreeRTOS_Sockets.h"\r
59 #include "NetworkBufferManagement.h"\r
60 \r
61 /* Driver includes. */\r
62 #include "lpc17xx_emac.h"\r
63 #include "lpc17xx_pinsel.h"\r
64 \r
65 /* Demo includes. */\r
66 #include "NetworkInterface.h"\r
67 \r
68 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1\r
69         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer\r
70 #else\r
71         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )\r
72 #endif\r
73 \r
74 /* When a packet is ready to be sent, if it cannot be sent immediately then the\r
75 task performing the transmit will block for niTX_BUFFER_FREE_WAIT\r
76 milliseconds.  It will do this a maximum of niMAX_TX_ATTEMPTS before giving\r
77 up. */\r
78 #define niTX_BUFFER_FREE_WAIT   ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
79 #define niMAX_TX_ATTEMPTS               ( 5 )\r
80 \r
81 /* The length of the queue used to send interrupt status words from the\r
82 interrupt handler to the deferred handler task. */\r
83 #define niINTERRUPT_QUEUE_LENGTH        ( 10 )\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /*\r
88  * A deferred interrupt handler task that processes\r
89  */\r
90 static void prvEMACHandlerTask( void *pvParameters );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* The queue used to communicate Ethernet events with the IP task. */\r
95 extern xQueueHandle xNetworkEventQueue;\r
96 \r
97 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
98 interrupt is received. */\r
99 static xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 portBASE_TYPE xNetworkInterfaceInitialise( void )\r
103 {\r
104 EMAC_CFG_Type Emac_Config;\r
105 PINSEL_CFG_Type xPinConfig;\r
106 portBASE_TYPE xStatus, xReturn;\r
107 extern uint8_t ucMACAddress[ 6 ];\r
108 \r
109         /* Enable Ethernet Pins */\r
110         boardCONFIGURE_ENET_PINS( xPinConfig );\r
111 \r
112         Emac_Config.Mode = EMAC_MODE_AUTO;\r
113         Emac_Config.pbEMAC_Addr = ucMACAddress;\r
114         xStatus = EMAC_Init( &Emac_Config );\r
115 \r
116         LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );\r
117 \r
118         if( xStatus != ERROR )\r
119         {\r
120                 vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
121                 configASSERT( xEMACRxEventSemaphore );\r
122 \r
123                 /* The handler task is created at the highest possible priority to\r
124                 ensure the interrupt handler can return directly to it. */\r
125                 xTaskCreate( prvEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
126 \r
127                 /* Enable the interrupt and set its priority to the minimum\r
128                 interrupt priority.  */\r
129                 NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );\r
130                 NVIC_EnableIRQ( ENET_IRQn );\r
131 \r
132                 xReturn = pdPASS;\r
133         }\r
134         else\r
135         {\r
136                 xReturn = pdFAIL;\r
137         }\r
138 \r
139         configASSERT( xStatus != ERROR );\r
140 \r
141         return xReturn;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
146 {\r
147 portBASE_TYPE xReturn = pdFAIL;\r
148 int32_t x;\r
149 extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );\r
150 extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );\r
151 \r
152 \r
153         /* Attempt to obtain access to a Tx buffer. */\r
154         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
155         {\r
156                 if( EMAC_CheckTransmitIndex() == TRUE )\r
157                 {\r
158                         /* Will the data fit in the Tx buffer? */\r
159                         if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */\r
160                         {\r
161                                 /* Assign the buffer to the Tx descriptor that is now known to\r
162                                 be free. */\r
163                                 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );\r
164 \r
165                                 /* The EMAC now owns the buffer. */\r
166                                 pxNetworkBuffer->pucEthernetBuffer = NULL;\r
167 \r
168                                 /* Initiate the Tx. */\r
169                                 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );\r
170                                 iptraceNETWORK_INTERFACE_TRANSMIT();\r
171 \r
172                                 /* The Tx has been initiated. */\r
173                                 xReturn = pdPASS;\r
174                         }\r
175                         break;\r
176                 }\r
177                 else\r
178                 {\r
179                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
180                 }\r
181         }\r
182 \r
183         /* Finished with the network buffer. */\r
184         vNetworkBufferRelease( pxNetworkBuffer );\r
185 \r
186         return xReturn;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void ENET_IRQHandler( void )\r
191 {\r
192 uint32_t ulInterruptCause;\r
193 \r
194         while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )\r
195         {\r
196                 /* Clear the interrupt. */\r
197                 LPC_EMAC->IntClear = ulInterruptCause;\r
198 \r
199                 /* Clear fatal error conditions.  NOTE:  The driver does not clear all\r
200                 errors, only those actually experienced.  For future reference, range\r
201                 errors are not actually errors so can be ignored. */\r
202                 if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )\r
203                 {\r
204                         LPC_EMAC->Command |= EMAC_CR_TX_RES;\r
205                 }\r
206 \r
207                 /* Unblock the deferred interrupt handler task if the event was an\r
208                 Rx. */\r
209                 if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )\r
210                 {\r
211                         xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );\r
212                 }\r
213         }\r
214 \r
215         /* ulInterruptCause is used for convenience here.  A context switch is\r
216         wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a\r
217         compiler warning. */\r
218         portEND_SWITCHING_ISR( ulInterruptCause );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static void prvEMACHandlerTask( void *pvParameters )\r
223 {\r
224 size_t xDataLength;\r
225 const uint16_t usCRCLength = 4;\r
226 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
227 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
228 \r
229 /* This is not included in the header file for some reason. */\r
230 extern uint8_t *EMAC_NextPacketToRead( void );\r
231 \r
232         ( void ) pvParameters;\r
233         configASSERT( xEMACRxEventSemaphore );\r
234 \r
235         for( ;; )\r
236         {\r
237                 /* Wait for the EMAC interrupt to indicate that another packet has been\r
238                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
239                 set to 0 in FreeRTOSConfig.h. */\r
240                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
241 \r
242                 /* At least one packet has been received. */\r
243                 while( EMAC_CheckReceiveIndex() != FALSE )\r
244                 {\r
245                         /* Obtain the length, minus the CRC.  The CRC is four bytes\r
246                         but the length is already minus 1. */\r
247                         xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );\r
248 \r
249                         if( xDataLength > 0U )\r
250                         {\r
251                                 /* Obtain a network buffer to pass this data into the\r
252                                 stack.  No storage is required as the network buffer\r
253                                 will point directly to the buffer that already holds\r
254                                 the     received data. */\r
255                                 pxNetworkBuffer = pxNetworkBufferGet( 0, ( portTickType ) 0 );\r
256 \r
257                                 if( pxNetworkBuffer != NULL )\r
258                                 {\r
259                                         pxNetworkBuffer->pucEthernetBuffer = EMAC_NextPacketToRead();\r
260                                         pxNetworkBuffer->xDataLength = xDataLength;\r
261                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
262 \r
263                                         /* Data was received and stored.  Send a message to the IP\r
264                                         task to let it know. */\r
265                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )\r
266                                         {\r
267                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
268                                                 iptraceETHERNET_RX_EVENT_LOST();\r
269                                         }\r
270                                 }\r
271                                 else\r
272                                 {\r
273                                         iptraceETHERNET_RX_EVENT_LOST();\r
274                                 }\r
275 \r
276                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
277                         }\r
278 \r
279                         /* Release the frame. */\r
280                         EMAC_UpdateRxConsumeIndex();\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r