]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LPC1768_GCC_Rowley/webserver/uIP_Task.c
Prepare for release.
[freertos] / Demo / CORTEX_LPC1768_GCC_Rowley / webserver / uIP_Task.c
1 /*\r
2         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the 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.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the\r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* Standard includes. */\r
53 #include <string.h>\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "task.h"\r
58 #include "semphr.h"\r
59 \r
60 /* uip includes. */\r
61 #include "uip.h"\r
62 #include "uip_arp.h"\r
63 #include "httpd.h"\r
64 #include "timer.h"\r
65 #include "clock-arch.h"\r
66 \r
67 /* Demo includes. */\r
68 #include "EthDev_LPC17xx.h"\r
69 #include "EthDev.h"\r
70 #include "ParTest.h"\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* How long to wait before attempting to connect the MAC again. */\r
75 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
76 \r
77 /* Shortcut to the header within the Rx buffer. */\r
78 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
79 \r
80 /* Standard constant. */\r
81 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /*\r
86  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
87  */\r
88 static void prvSetMACAddress( void );\r
89 \r
90 /*\r
91  * Port functions required by the uIP stack.\r
92  */\r
93 void clock_init( void );\r
94 clock_time_t clock_time( void );\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* The semaphore used by the ISR to wake the uIP task. */\r
99 xSemaphoreHandle xEMACSemaphore = NULL;\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void clock_init(void)\r
104 {\r
105         /* This is done when the scheduler starts. */\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 clock_time_t clock_time( void )\r
110 {\r
111         return xTaskGetTickCount();\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vuIP_Task( void *pvParameters )\r
116 {\r
117 portBASE_TYPE i;\r
118 uip_ipaddr_t xIPAddr;\r
119 struct timer periodic_timer, arp_timer;\r
120 extern void ( vEMAC_ISR_Wrapper )( void );\r
121 \r
122         ( void ) pvParameters;\r
123 \r
124         /* Initialise the uIP stack. */\r
125         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
126         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
127         uip_init();\r
128         uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
129         uip_sethostaddr( xIPAddr );\r
130         uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
131         uip_setnetmask( xIPAddr );\r
132         httpd_init();\r
133 \r
134         /* Create the semaphore used to wake the uIP task. */\r
135         vSemaphoreCreateBinary( xEMACSemaphore );\r
136 \r
137         /* Initialise the MAC. */\r
138         while( lEMACInit() != pdPASS )\r
139     {\r
140         vTaskDelay( uipINIT_WAIT );\r
141     }\r
142 \r
143         portENTER_CRITICAL();\r
144         {\r
145                 EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );\r
146 \r
147                 /* Set the interrupt priority to the max permissible to cause some\r
148                 interrupt nesting. */\r
149                 NVIC_SetPriority( ENET_IRQn, configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
150 \r
151                 /* Enable the interrupt. */\r
152                 NVIC_EnableIRQ( ENET_IRQn );\r
153                 prvSetMACAddress();\r
154         }\r
155         portEXIT_CRITICAL();\r
156 \r
157 \r
158         for( ;; )\r
159         {\r
160                 /* Is there received data ready to be processed? */\r
161                 uip_len = ulGetEMACRxData();\r
162 \r
163                 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
164                 {\r
165                         /* Standard uIP loop taken from the uIP manual. */\r
166                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
167                         {\r
168                                 uip_arp_ipin();\r
169                                 uip_input();\r
170 \r
171                                 /* If the above function invocation resulted in data that\r
172                                 should be sent out on the network, the global variable\r
173                                 uip_len is set to a value > 0. */\r
174                                 if( uip_len > 0 )\r
175                                 {\r
176                                         uip_arp_out();\r
177                                         vSendEMACTxData( uip_len );\r
178                                 }\r
179                         }\r
180                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
181                         {\r
182                                 uip_arp_arpin();\r
183 \r
184                                 /* If the above function invocation resulted in data that\r
185                                 should be sent out on the network, the global variable\r
186                                 uip_len is set to a value > 0. */\r
187                                 if( uip_len > 0 )\r
188                                 {\r
189                                         vSendEMACTxData( uip_len );\r
190                                 }\r
191                         }\r
192                 }\r
193                 else\r
194                 {\r
195                         if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )\r
196                         {\r
197                                 timer_reset( &periodic_timer );\r
198                                 for( i = 0; i < UIP_CONNS; i++ )\r
199                                 {\r
200                                         uip_periodic( i );\r
201 \r
202                                         /* If the above function invocation resulted in data that\r
203                                         should be sent out on the network, the global variable\r
204                                         uip_len is set to a value > 0. */\r
205                                         if( uip_len > 0 )\r
206                                         {\r
207                                                 uip_arp_out();\r
208                                                 vSendEMACTxData( uip_len );\r
209                                         }\r
210                                 }\r
211 \r
212                                 /* Call the ARP timer function every 10 seconds. */\r
213                                 if( timer_expired( &arp_timer ) )\r
214                                 {\r
215                                         timer_reset( &arp_timer );\r
216                                         uip_arp_timer();\r
217                                 }\r
218                         }\r
219                         else\r
220                         {\r
221                                 /* We did not receive a packet, and there was no periodic\r
222                                 processing to perform.  Block for a fixed period.  If a packet\r
223                                 is received during this period we will be woken by the ISR\r
224                                 giving us the Semaphore. */\r
225                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
226                         }\r
227                 }\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvSetMACAddress( void )\r
233 {\r
234 struct uip_eth_addr xAddr;\r
235 \r
236         /* Configure the MAC address in the uIP stack. */\r
237         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
238         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
239         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
240         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
241         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
242         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
243         uip_setethaddr( xAddr );\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 void vApplicationProcessFormInput( portCHAR *pcInputString )\r
248 {\r
249 char *c;\r
250 extern void vParTestSetLEDState( long lState );\r
251 \r
252         /* Process the form input sent by the IO page of the served HTML. */\r
253 \r
254         c = strstr( pcInputString, "?" );\r
255     if( c )\r
256     {\r
257                 /* Turn the FIO1 LED's on or off in accordance with the check box status. */\r
258                 if( strstr( c, "LED0=1" ) != NULL )\r
259                 {\r
260                         vParTestSetLEDState( pdTRUE );\r
261                 }\r
262                 else\r
263                 {\r
264                         vParTestSetLEDState( pdFALSE );\r
265                 }\r
266     }\r
267 }\r
268 \r