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