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