]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LPC1768_GCC_Rowley/webserver/uIP_Task.c
0adb6fe6448c2533abd2a56581c0afc4be696104
[freertos] / Demo / CORTEX_LPC1768_GCC_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 \r
49 /* Standard includes. */\r
50 #include <string.h>\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 #include "semphr.h"\r
56 \r
57 /* uip includes. */\r
58 #include "uip.h"\r
59 #include "uip_arp.h"\r
60 #include "httpd.h"\r
61 #include "timer.h"\r
62 #include "clock-arch.h"\r
63 \r
64 /* Demo includes. */\r
65 #include "EthDev_LPC17xx.h"\r
66 #include "EthDev.h"\r
67 #include "ParTest.h"\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* How long to wait before attempting to connect the MAC again. */\r
72 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
73 \r
74 /* Shortcut to the header within the Rx buffer. */\r
75 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
76 \r
77 /* Standard constant. */\r
78 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /*\r
83  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
84  */\r
85 static void prvSetMACAddress( void );\r
86 \r
87 /*\r
88  * Port functions required by the uIP stack.\r
89  */\r
90 void clock_init( void );\r
91 clock_time_t clock_time( void );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* The semaphore used by the ISR to wake the uIP task. */\r
96 xSemaphoreHandle xEMACSemaphore = NULL;\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void clock_init(void)\r
101 {\r
102         /* This is done when the scheduler starts. */\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 clock_time_t clock_time( void )\r
107 {\r
108         return xTaskGetTickCount();\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vuIP_Task( void *pvParameters )\r
113 {\r
114 portBASE_TYPE i;\r
115 uip_ipaddr_t xIPAddr;\r
116 struct timer periodic_timer, arp_timer;\r
117 extern void ( vEMAC_ISR_Wrapper )( void );\r
118 \r
119         ( void ) pvParameters;\r
120 \r
121         /* Initialise the uIP stack. */\r
122         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
123         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
124         uip_init();\r
125         uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
126         uip_sethostaddr( xIPAddr );\r
127         uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
128         uip_setnetmask( xIPAddr );\r
129         httpd_init();\r
130 \r
131         /* Create the semaphore used to wake the uIP task. */\r
132         vSemaphoreCreateBinary( xEMACSemaphore );\r
133 \r
134         /* Initialise the MAC. */\r
135         while( lEMACInit() != pdPASS )\r
136     {\r
137         vTaskDelay( uipINIT_WAIT );\r
138     }\r
139 \r
140         portENTER_CRITICAL();\r
141         {\r
142                 EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );\r
143 \r
144                 /* Set the interrupt priority to the max permissible to cause some\r
145                 interrupt nesting. */\r
146                 NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );\r
147 \r
148                 /* Enable the interrupt. */\r
149                 NVIC_EnableIRQ( ENET_IRQn );\r
150                 prvSetMACAddress();\r
151         }\r
152         portEXIT_CRITICAL();\r
153 \r
154 \r
155         for( ;; )\r
156         {\r
157                 /* Is there received data ready to be processed? */\r
158                 uip_len = ulGetEMACRxData();\r
159 \r
160                 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
161                 {\r
162                         /* Standard uIP loop taken from the uIP manual. */\r
163                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
164                         {\r
165                                 uip_arp_ipin();\r
166                                 uip_input();\r
167 \r
168                                 /* If the above function invocation resulted in data that\r
169                                 should be sent out on the network, the global variable\r
170                                 uip_len is set to a value > 0. */\r
171                                 if( uip_len > 0 )\r
172                                 {\r
173                                         uip_arp_out();\r
174                                         vSendEMACTxData( uip_len );\r
175                                 }\r
176                         }\r
177                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
178                         {\r
179                                 uip_arp_arpin();\r
180 \r
181                                 /* If the above function invocation resulted in data that\r
182                                 should be sent out on the network, the global variable\r
183                                 uip_len is set to a value > 0. */\r
184                                 if( uip_len > 0 )\r
185                                 {\r
186                                         vSendEMACTxData( uip_len );\r
187                                 }\r
188                         }\r
189                 }\r
190                 else\r
191                 {\r
192                         if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )\r
193                         {\r
194                                 timer_reset( &periodic_timer );\r
195                                 for( i = 0; i < UIP_CONNS; i++ )\r
196                                 {\r
197                                         uip_periodic( i );\r
198 \r
199                                         /* If the above function invocation resulted in data that\r
200                                         should be sent out on the network, the global variable\r
201                                         uip_len is set to a value > 0. */\r
202                                         if( uip_len > 0 )\r
203                                         {\r
204                                                 uip_arp_out();\r
205                                                 vSendEMACTxData( uip_len );\r
206                                         }\r
207                                 }\r
208 \r
209                                 /* Call the ARP timer function every 10 seconds. */\r
210                                 if( timer_expired( &arp_timer ) )\r
211                                 {\r
212                                         timer_reset( &arp_timer );\r
213                                         uip_arp_timer();\r
214                                 }\r
215                         }\r
216                         else\r
217                         {\r
218                                 /* We did not receive a packet, and there was no periodic\r
219                                 processing to perform.  Block for a fixed period.  If a packet\r
220                                 is received during this period we will be woken by the ISR\r
221                                 giving us the Semaphore. */\r
222                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
223                         }\r
224                 }\r
225         }\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 static void prvSetMACAddress( void )\r
230 {\r
231 struct uip_eth_addr xAddr;\r
232 \r
233         /* Configure the MAC address in the uIP stack. */\r
234         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
235         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
236         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
237         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
238         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
239         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
240         uip_setethaddr( xAddr );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void vApplicationProcessFormInput( char *pcInputString )\r
245 {\r
246 char *c;\r
247 extern void vParTestSetLEDState( long lState );\r
248 \r
249         /* Process the form input sent by the IO page of the served HTML. */\r
250 \r
251         c = strstr( pcInputString, "?" );\r
252     if( c )\r
253     {\r
254                 /* Turn the FIO1 LED's on or off in accordance with the check box status. */\r
255                 if( strstr( c, "LED0=1" ) != NULL )\r
256                 {\r
257                         vParTestSetLEDState( pdTRUE );\r
258                 }\r
259                 else\r
260                 {\r
261                         vParTestSetLEDState( pdFALSE );\r
262                 }\r
263     }\r
264 }\r
265 \r