]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LPC1768_GCC_Rowley/webserver/uIP_Task.c
Continue work on emac driver.
[freertos] / Demo / CORTEX_LPC1768_GCC_Rowley / webserver / uIP_Task.c
1 /*\r
2         FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the\r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* Standard includes. */\r
53 #include <string.h>\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "task.h"\r
58 #include "semphr.h"\r
59 \r
60 /* uip includes. */\r
61 #include "uip.h"\r
62 #include "uip_arp.h"\r
63 #include "httpd.h"\r
64 #include "timer.h"\r
65 #include "clock-arch.h"\r
66 \r
67 /* Demo includes. */\r
68 #include "EthDev_LPC17xx.h"\r
69 #include "LED.h"\r
70 \r
71 #include "LPC17xx.h"\r
72 #include "core_cm3.h"\r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* How long to wait before attempting to connect the MAC again. */\r
76 #define uipINIT_WAIT    100\r
77 \r
78 /* Shortcut to the header within the Rx buffer. */\r
79 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
80 \r
81 /* Standard constant. */\r
82 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * Send the uIP buffer to the MAC.\r
88  */\r
89 static void prvENET_Send(void);\r
90 \r
91 /*\r
92  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
93  */\r
94 static void prvSetMACAddress( void );\r
95 \r
96 /*\r
97  * Port functions required by the uIP stack.\r
98  */\r
99 void clock_init( void );\r
100 clock_time_t clock_time( void );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /* The semaphore used by the ISR to wake the uIP task. */\r
105 extern xSemaphoreHandle xEMACSemaphore;\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void clock_init(void)\r
110 {\r
111         /* This is done when the scheduler starts. */\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 clock_time_t clock_time( void )\r
116 {\r
117         return xTaskGetTickCount();\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void vuIP_Task( void *pvParameters )\r
122 {\r
123 portBASE_TYPE i;\r
124 uip_ipaddr_t xIPAddr;\r
125 struct timer periodic_timer, arp_timer;\r
126 extern void ( vEMAC_ISR_Wrapper )( void );\r
127 \r
128         ( void ) pvParameters;\r
129 \r
130         /* Create the semaphore used by the ISR to wake this task. */\r
131         vSemaphoreCreateBinary( xEMACSemaphore );\r
132 \r
133         /* Initialise the uIP stack. */\r
134         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
135         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
136         uip_init();\r
137         uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
138         uip_sethostaddr( xIPAddr );\r
139         uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
140         uip_setnetmask( xIPAddr );\r
141         httpd_init();\r
142 \r
143         /* Initialise the MAC. */\r
144         while( Init_EMAC() != pdPASS )\r
145     {\r
146         vTaskDelay( uipINIT_WAIT );\r
147     }\r
148 \r
149         portENTER_CRITICAL();\r
150         {\r
151                 ETH_INTENABLE = INT_RX_DONE;\r
152                 /* set the interrupt priority */\r
153                 NVIC_SetPriority( ENET_IRQn, configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
154                 /* enable the interrupt */\r
155                 NVIC_EnableIRQ( ENET_IRQn );\r
156                 prvSetMACAddress();\r
157         }\r
158         portEXIT_CRITICAL();\r
159 \r
160 \r
161         for( ;; )\r
162         {\r
163                 /* Is there received data ready to be processed? */\r
164                 uip_len = uiGetEMACRxData( uip_buf );\r
165 \r
166                 if( uip_len > 0 )\r
167                 {\r
168                         /* Standard uIP loop taken from the uIP manual. */\r
169                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
170                         {\r
171                                 uip_arp_ipin();\r
172                                 uip_input();\r
173 \r
174                                 /* If the above function invocation resulted in data that\r
175                                 should be sent out on the network, the global variable\r
176                                 uip_len is set to a value > 0. */\r
177                                 if( uip_len > 0 )\r
178                                 {\r
179                                         uip_arp_out();\r
180                                         prvENET_Send();\r
181                                 }\r
182                         }\r
183                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
184                         {\r
185                                 uip_arp_arpin();\r
186 \r
187                                 /* If the above function invocation resulted in data that\r
188                                 should be sent out on the network, the global variable\r
189                                 uip_len is set to a value > 0. */\r
190                                 if( uip_len > 0 )\r
191                                 {\r
192                                         prvENET_Send();\r
193                                 }\r
194                         }\r
195                 }\r
196                 else\r
197                 {\r
198                         if( timer_expired( &periodic_timer ) )\r
199                         {\r
200                                 timer_reset( &periodic_timer );\r
201                                 for( i = 0; i < UIP_CONNS; i++ )\r
202                                 {\r
203                                         uip_periodic( i );\r
204 \r
205                                         /* If the above function invocation resulted in data that\r
206                                         should be sent out on the network, the global variable\r
207                                         uip_len is set to a value > 0. */\r
208                                         if( uip_len > 0 )\r
209                                         {\r
210                                                 uip_arp_out();\r
211                                                 prvENET_Send();\r
212                                         }\r
213                                 }\r
214 \r
215                                 /* Call the ARP timer function every 10 seconds. */\r
216                                 if( timer_expired( &arp_timer ) )\r
217                                 {\r
218                                         timer_reset( &arp_timer );\r
219                                         uip_arp_timer();\r
220                                 }\r
221                         }\r
222                         else\r
223                         {\r
224                                 /* We did not receive a packet, and there was no periodic\r
225                                 processing to perform.  Block for a fixed period.  If a packet\r
226                                 is received during this period we will be woken by the ISR\r
227                                 giving us the Semaphore. */\r
228                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
229                         }\r
230                 }\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void prvENET_Send(void)\r
236 {\r
237     RequestSend();\r
238 \r
239     /* Copy the header into the Tx buffer. */\r
240     CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
241     if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
242     {\r
243         CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
244     }\r
245 \r
246     DoSend_EMAC( uip_len );\r
247 \r
248     RequestSend();\r
249 \r
250     /* Copy the header into the Tx buffer. */\r
251     CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
252     if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
253     {\r
254         CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
255     }\r
256 \r
257     DoSend_EMAC( uip_len );\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvSetMACAddress( void )\r
262 {\r
263 struct uip_eth_addr xAddr;\r
264 \r
265         /* Configure the MAC address in the uIP stack. */\r
266         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
267         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
268         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
269         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
270         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
271         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
272         uip_setethaddr( xAddr );\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 void vApplicationProcessFormInput( portCHAR *pcInputString )\r
277 {\r
278 char *c, *pcText;\r
279 static portCHAR cMessageForDisplay[ 32 ];\r
280 extern xQueueHandle xLCDQueue;\r
281 xLCDMessage xLCDMessage;\r
282 \r
283         /* Process the form input sent by the IO page of the served HTML. */\r
284 \r
285         c = strstr( pcInputString, "?" );\r
286     if( c )\r
287     {\r
288                 /* Turn LED's on or off in accordance with the check box status. */\r
289                 if( strstr( c, "LED0=1" ) != NULL )\r
290                 {\r
291                         /* Set LED7. */\r
292                         vParTestSetLED( 1 << 7, 1 );\r
293                 }\r
294                 else\r
295                 {\r
296                         /* Clear LED7. */\r
297                         vParTestSetLED( 1 << 7, 0 );\r
298                 }\r
299 \r
300                 /* Find the start of the text to be displayed on the LCD. */\r
301         pcText = strstr( c, "LCD=" );\r
302         pcText += strlen( "LCD=" );\r
303 \r
304         /* Terminate the file name for further processing within uIP. */\r
305         *c = 0x00;\r
306 \r
307         /* Terminate the LCD string. */\r
308         c = strstr( pcText, " " );\r
309         if( c != NULL )\r
310         {\r
311             *c = 0x00;\r
312         }\r
313 \r
314         /* Add required spaces. */\r
315         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
316         {\r
317             *c = ' ';\r
318         }\r
319 \r
320         /* Write the message to the LCD. */\r
321                 strcpy( cMessageForDisplay, pcText );\r
322                 xLCDMessage.pcMessage = cMessageForDisplay;\r
323         xQueueSend( xLCDQueue, &xLCDMessage, portMAX_DELAY );\r
324     }\r
325 }\r
326 \r