]> begriffs open source - freertos/blob - 20080217/Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/uIP_Task.c
Prepare Fujitsu ports for release.
[freertos] / 20080217 / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / uIP_Task.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 /* Standard includes. */\r
33 #include <string.h>\r
34 \r
35 /* Scheduler includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 #include "semphr.h"\r
39 \r
40 /* uip includes. */\r
41 #include "hw_types.h"\r
42 #include "uip.h"\r
43 #include "uip_arp.h"\r
44 #include "httpd.h"\r
45 #include "timer.h"\r
46 #include "clock-arch.h"\r
47 #include "hw_ethernet.h"\r
48 #include "ethernet.h"\r
49 #include "hw_memmap.h"\r
50 #include "lmi_flash.h"\r
51 #include "sysctl.h"\r
52 \r
53 /* Demo includes. */\r
54 #include "emac.h"\r
55 #include "partest.h"\r
56 #include "lcd_message.h"\r
57 \r
58 struct timer {\r
59   clock_time_t start;\r
60   clock_time_t interval;\r
61 };\r
62 \r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* IP address configuration. */\r
67 #define uipIP_ADDR0             172\r
68 #define uipIP_ADDR1             25\r
69 #define uipIP_ADDR2             218\r
70 #define uipIP_ADDR3             19      \r
71 \r
72 /* How long to wait before attempting to connect the MAC again. */\r
73 #define uipINIT_WAIT    100\r
74 \r
75 /* Shortcut to the header within the Rx buffer. */\r
76 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
77 \r
78 /* Standard constant. */\r
79 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /*\r
84  * Send the uIP buffer to the MAC.\r
85  */\r
86 static void prvENET_Send(void);\r
87 \r
88 /*\r
89  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
90  */\r
91 static void prvSetMACAddress( void );\r
92 \r
93 /*\r
94  * Port functions required by the uIP stack.\r
95  */\r
96 void clock_init( void );\r
97 clock_time_t clock_time( void );\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The semaphore used by the ISR to wake the uIP task. */\r
102 extern xSemaphoreHandle xEMACSemaphore;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void clock_init(void)\r
107 {\r
108         /* This is done when the scheduler starts. */\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Define clock functions here to avoid header file name clash between uIP\r
113 and the Luminary Micro driver library. */\r
114 clock_time_t clock_time( void )\r
115 {\r
116         return xTaskGetTickCount();\r
117 }\r
118 extern void timer_set(struct timer *t, clock_time_t interval);\r
119 extern int timer_expired(struct timer *t);\r
120 extern void timer_reset(struct timer *t);\r
121 \r
122 \r
123 \r
124 \r
125 void vuIP_Task( void *pvParameters )\r
126 {\r
127 portBASE_TYPE i;\r
128 uip_ipaddr_t xIPAddr;\r
129 struct timer periodic_timer, arp_timer;\r
130 extern void ( vEMAC_ISR )( void );\r
131 \r
132         /* Enable/Reset the Ethernet Controller */\r
133         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
134         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
135 \r
136         /* Create the semaphore used by the ISR to wake this task. */\r
137         vSemaphoreCreateBinary( xEMACSemaphore );\r
138         \r
139         /* Initialise the uIP stack. */\r
140         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
141         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
142         uip_init();\r
143         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
144         uip_sethostaddr( xIPAddr );\r
145         httpd_init();\r
146 \r
147         while( vInitEMAC() != pdPASS )\r
148     {\r
149         vTaskDelay( uipINIT_WAIT );\r
150     }\r
151         prvSetMACAddress();     \r
152         \r
153 \r
154         for( ;; )\r
155         {\r
156                 /* Is there received data ready to be processed? */\r
157                 uip_len = uiGetEMACRxData( uip_buf );\r
158                 \r
159                 if( uip_len > 0 )\r
160                 {\r
161                         /* Standard uIP loop taken from the uIP manual. */\r
162 \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                                         prvENET_Send();\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                                         prvENET_Send();\r
187                                 }\r
188                         }\r
189                 }\r
190                 else\r
191                 {\r
192                         if( timer_expired( &periodic_timer ) )\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                                                 prvENET_Send();\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 prvENET_Send(void)\r
230 {\r
231         vInitialiseSend();\r
232         vIncrementTxLength( uip_len );\r
233         vSendBufferToMAC();\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvSetMACAddress( void )\r
238 {\r
239 unsigned portLONG ulUser0, ulUser1;\r
240 unsigned char pucMACArray[8];\r
241 struct uip_eth_addr xAddr;\r
242 \r
243         /* Get the device MAC address from flash */\r
244     FlashUserGet(&ulUser0, &ulUser1);\r
245 \r
246         /* Convert the MAC address from flash into sequence of bytes. */\r
247     pucMACArray[0] = ((ulUser0 >>  0) & 0xff);\r
248     pucMACArray[1] = ((ulUser0 >>  8) & 0xff);\r
249     pucMACArray[2] = ((ulUser0 >> 16) & 0xff);\r
250     pucMACArray[3] = ((ulUser1 >>  0) & 0xff);\r
251     pucMACArray[4] = ((ulUser1 >>  8) & 0xff);\r
252     pucMACArray[5] = ((ulUser1 >> 16) & 0xff);\r
253 \r
254         /* Program the MAC address. */\r
255     EthernetMACAddrSet(ETH_BASE, pucMACArray);\r
256 \r
257         xAddr.addr[ 0 ] = pucMACArray[0];\r
258         xAddr.addr[ 1 ] = pucMACArray[1];\r
259         xAddr.addr[ 2 ] = pucMACArray[2];\r
260         xAddr.addr[ 3 ] = pucMACArray[3];\r
261         xAddr.addr[ 4 ] = pucMACArray[4];\r
262         xAddr.addr[ 5 ] = pucMACArray[5];\r
263         uip_setethaddr( xAddr );\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void vApplicationProcessFormInput( portCHAR *pcInputString, portBASE_TYPE xInputLength )\r
268 {\r
269 char *c, *pcText;\r
270 static portCHAR cMessageForDisplay[ 32 ];\r
271 extern xQueueHandle xOLEDQueue;\r
272 xOLEDMessage xOLEDMessage;\r
273 \r
274         /* Process the form input sent by the IO page of the served HTML. */\r
275 \r
276         c = strstr( pcInputString, "?" );\r
277 \r
278     if( c )\r
279     {\r
280                 /* Turn LED's on or off in accordance with the check box status. */\r
281                 if( strstr( c, "LED0=1" ) != NULL )\r
282                 {\r
283                         vParTestSetLED( 0, 1 );\r
284                 }\r
285                 else\r
286                 {\r
287                         vParTestSetLED( 0, 0 );\r
288                 }               \r
289                 \r
290                 /* Find the start of the text to be displayed on the LCD. */\r
291         pcText = strstr( c, "LCD=" );\r
292         pcText += strlen( "LCD=" );\r
293 \r
294         /* Terminate the file name for further processing within uIP. */\r
295         *c = 0x00;\r
296 \r
297         /* Terminate the LCD string. */\r
298         c = strstr( pcText, " " );\r
299         if( c != NULL )\r
300         {\r
301             *c = 0x00;\r
302         }\r
303 \r
304         /* Add required spaces. */\r
305         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
306         {\r
307             *c = ' ';\r
308         }\r
309 \r
310         /* Write the message to the LCD. */\r
311                 strcpy( cMessageForDisplay, pcText );\r
312                 xOLEDMessage.pcMessage = cMessageForDisplay;\r
313         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
314     }\r
315 }\r
316 \r