]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_LPC2368_Rowley/webserver/uIP_Task.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / ARM7_LPC2368_Rowley / 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             192\r
96 #define uipIP_ADDR1             168\r
97 #define uipIP_ADDR2             0\r
98 #define uipIP_ADDR3             200     \r
99 \r
100 /* How long to wait before attempting to connect the MAC again. */\r
101 #define uipINIT_WAIT    200\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         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     RequestSend();\r
268 \r
269     /* Copy the header into the Tx buffer. */\r
270     CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
271     if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
272     {\r
273         CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
274     }\r
275 \r
276     DoSend_EMAC( uip_len );\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvSetMACAddress( void )\r
281 {\r
282 struct uip_eth_addr xAddr;\r
283 \r
284         /* Configure the MAC address in the uIP stack. */\r
285         xAddr.addr[ 0 ] = uipMAC_ADDR0;\r
286         xAddr.addr[ 1 ] = uipMAC_ADDR1;\r
287         xAddr.addr[ 2 ] = uipMAC_ADDR2;\r
288         xAddr.addr[ 3 ] = uipMAC_ADDR3;\r
289         xAddr.addr[ 4 ] = uipMAC_ADDR4;\r
290         xAddr.addr[ 5 ] = uipMAC_ADDR5;\r
291         uip_setethaddr( xAddr );\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 void vApplicationProcessFormInput( char *pcInputString, portBASE_TYPE xInputLength )\r
296 {\r
297 char *c, *pcText;\r
298 static char cMessageForDisplay[ 32 ];\r
299 extern QueueHandle_t xLCDQueue;\r
300 xLCDMessage xLCDMessage;\r
301 \r
302         /* Process the form input sent by the IO page of the served HTML. */\r
303 \r
304         c = strstr( pcInputString, "?" );\r
305     if( c )\r
306     {\r
307                 /* Turn LED's on or off in accordance with the check box status. */\r
308                 if( strstr( c, "LED0=1" ) != NULL )\r
309                 {\r
310                         vParTestSetLED( 5, 0 );\r
311                 }\r
312                 else\r
313                 {\r
314                         vParTestSetLED( 5, 1 );\r
315                 }               \r
316                 \r
317                 if( strstr( c, "LED1=1" ) != NULL )\r
318                 {\r
319                         vParTestSetLED( 6, 0 );\r
320                 }\r
321                 else\r
322                 {\r
323                         vParTestSetLED( 6, 1 );\r
324                 }               \r
325 \r
326                 if( strstr( c, "LED2=1" ) != NULL )\r
327                 {\r
328                         vParTestSetLED( 7, 0 );\r
329                 }\r
330                 else\r
331                 {\r
332                         vParTestSetLED( 7, 1 );\r
333                 }\r
334 \r
335                 /* Find the start of the text to be displayed on the LCD. */\r
336         pcText = strstr( c, "LCD=" );\r
337         pcText += strlen( "LCD=" );\r
338 \r
339         /* Terminate the file name for further processing within uIP. */\r
340         *c = 0x00;\r
341 \r
342         /* Terminate the LCD string. */\r
343         c = strstr( pcText, " " );\r
344         if( c != NULL )\r
345         {\r
346             *c = 0x00;\r
347         }\r
348 \r
349         /* Add required spaces. */\r
350         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
351         {\r
352             *c = ' ';\r
353         }\r
354     \r
355         /* Write the message to the LCD. */\r
356                 strcpy( cMessageForDisplay, pcText );\r
357                 xLCDMessage.xColumn = 0;\r
358                 xLCDMessage.pcMessage = cMessageForDisplay;\r
359         xQueueSend( xLCDQueue, &xLCDMessage, portMAX_DELAY );\r
360     }\r
361 }\r
362 \r