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