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