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