]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2368_Eclipse/RTOSDemo/webserver/uIP_Task.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / ARM7_LPC2368_Eclipse / RTOSDemo / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 /* Standard includes. */\r
65 #include <string.h>\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "semphr.h"\r
71 \r
72 /* uip includes. */\r
73 #include "uip.h"\r
74 #include "uip_arp.h"\r
75 #include "httpd.h"\r
76 #include "timer.h"\r
77 #include "clock-arch.h"\r
78 \r
79 /* Demo includes. */\r
80 #include "emac.h"\r
81 #include "partest.h"\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* MAC address configuration. */\r
86 #define uipMAC_ADDR0    0x00\r
87 #define uipMAC_ADDR1    0x12\r
88 #define uipMAC_ADDR2    0x13\r
89 #define uipMAC_ADDR3    0x10\r
90 #define uipMAC_ADDR4    0x15\r
91 #define uipMAC_ADDR5    0x11\r
92 \r
93 /* IP address configuration. */\r
94 #define uipIP_ADDR0             172\r
95 #define uipIP_ADDR1             25\r
96 #define uipIP_ADDR2             218\r
97 #define uipIP_ADDR3             16      \r
98 \r
99 /* How long to wait before attempting to connect the MAC again. */\r
100 #define uipINIT_WAIT    100\r
101 \r
102 /* Shortcut to the header within the Rx buffer. */\r
103 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
104 \r
105 /* Standard constant. */\r
106 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* \r
111  * Send the uIP buffer to the MAC. \r
112  */\r
113 static void prvENET_Send(void);\r
114 \r
115 /*\r
116  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
117  */\r
118 static void prvSetMACAddress( void );\r
119 \r
120 /*\r
121  * Port functions required by the uIP stack.\r
122  */\r
123 void clock_init( void );\r
124 clock_time_t clock_time( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* The semaphore used by the ISR to wake the uIP task. */\r
129 extern xSemaphoreHandle xEMACSemaphore;\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void clock_init(void)\r
134 {\r
135         /* This is done when the scheduler starts. */\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 clock_time_t clock_time( void )\r
140 {\r
141         return xTaskGetTickCount();\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vuIP_Task( void *pvParameters )\r
146 {\r
147 portBASE_TYPE i;\r
148 uip_ipaddr_t xIPAddr;\r
149 struct timer periodic_timer, arp_timer;\r
150 extern void ( vEMAC_ISR_Wrapper )( void );\r
151 \r
152         /* Create the semaphore used by the ISR to wake this task. */\r
153         vSemaphoreCreateBinary( xEMACSemaphore );\r
154         \r
155         /* Initialise the uIP stack. */\r
156         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
157         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
158         uip_init();\r
159         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
160         uip_sethostaddr( xIPAddr );\r
161         httpd_init();\r
162 \r
163         /* Initialise the MAC. */\r
164         while( Init_EMAC() != pdPASS )\r
165     {\r
166         vTaskDelay( uipINIT_WAIT );\r
167     }\r
168 \r
169         portENTER_CRITICAL();\r
170         {\r
171                 MAC_INTENABLE = INT_RX_DONE;\r
172         VICIntEnable |= 0x00200000;\r
173         VICVectAddr21 = ( portLONG ) vEMAC_ISR_Wrapper;\r
174                 prvSetMACAddress();\r
175         }\r
176         portEXIT_CRITICAL();\r
177         \r
178 \r
179         for( ;; )\r
180         {\r
181                 /* Is there received data ready to be processed? */\r
182                 uip_len = uiGetEMACRxData( uip_buf );\r
183                 \r
184                 if( uip_len > 0 )\r
185                 {\r
186                         /* Standard uIP loop taken from the uIP manual. */\r
187                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
188                         {\r
189                                 uip_arp_ipin();\r
190                                 uip_input();\r
191 \r
192                                 /* If the above function invocation resulted in data that \r
193                                 should be sent out on the network, the global variable \r
194                                 uip_len is set to a value > 0. */\r
195                                 if( uip_len > 0 )\r
196                                 {\r
197                                         uip_arp_out();\r
198                                         prvENET_Send();\r
199                                 }\r
200                         }\r
201                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
202                         {\r
203                                 uip_arp_arpin();\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                                         prvENET_Send();\r
211                                 }\r
212                         }\r
213                 }\r
214                 else\r
215                 {\r
216                         if( timer_expired( &periodic_timer ) )\r
217                         {\r
218                                 timer_reset( &periodic_timer );\r
219                                 for( i = 0; i < UIP_CONNS; i++ )\r
220                                 {\r
221                                         uip_periodic( i );\r
222         \r
223                                         /* If the above function invocation resulted in data that \r
224                                         should be sent out on the network, the global variable \r
225                                         uip_len is set to a value > 0. */\r
226                                         if( uip_len > 0 )\r
227                                         {\r
228                                                 uip_arp_out();\r
229                                                 prvENET_Send();\r
230                                         }\r
231                                 }       \r
232         \r
233                                 /* Call the ARP timer function every 10 seconds. */\r
234                                 if( timer_expired( &arp_timer ) )\r
235                                 {\r
236                                         timer_reset( &arp_timer );\r
237                                         uip_arp_timer();\r
238                                 }\r
239                         }\r
240                         else\r
241                         {                       \r
242                                 /* We did not receive a packet, and there was no periodic\r
243                                 processing to perform.  Block for a fixed period.  If a packet\r
244                                 is received during this period we will be woken by the ISR\r
245                                 giving us the Semaphore. */\r
246                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );                       \r
247                         }\r
248                 }\r
249         }\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvENET_Send(void)\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