]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/uIP_Task.c
4518d1744f97e0fbf5e198ba10129500e5ffebc2
[freertos] / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 /* Standard includes. */\r
54 #include <string.h>\r
55 \r
56 /* Scheduler includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "semphr.h"\r
60 \r
61 /* uip includes. */\r
62 #include "hw_types.h"\r
63 #include "uip.h"\r
64 #include "uip_arp.h"\r
65 #include "httpd.h"\r
66 #include "timer.h"\r
67 #include "clock-arch.h"\r
68 #include "hw_ethernet.h"\r
69 #include "ethernet.h"\r
70 #include "hw_memmap.h"\r
71 #include "lmi_flash.h"\r
72 #include "sysctl.h"\r
73 \r
74 /* Demo includes. */\r
75 #include "emac.h"\r
76 #include "partest.h"\r
77 #include "lcd_message.h"\r
78 \r
79 struct timer {\r
80   clock_time_t start;\r
81   clock_time_t interval;\r
82 };\r
83 \r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* IP address configuration. */\r
88 #define uipIP_ADDR0             172\r
89 #define uipIP_ADDR1             25\r
90 #define uipIP_ADDR2             218\r
91 #define uipIP_ADDR3             19      \r
92 \r
93 /* How long to wait before attempting to connect the MAC again. */\r
94 #define uipINIT_WAIT    100\r
95 \r
96 /* Shortcut to the header within the Rx buffer. */\r
97 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
98 \r
99 /* Standard constant. */\r
100 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Send the uIP buffer to the MAC.\r
106  */\r
107 static void prvENET_Send(void);\r
108 \r
109 /*\r
110  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
111  */\r
112 static void prvSetMACAddress( void );\r
113 \r
114 /*\r
115  * Port functions required by the uIP stack.\r
116  */\r
117 void clock_init( void );\r
118 clock_time_t clock_time( void );\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* The semaphore used by the ISR to wake the uIP task. */\r
123 extern xSemaphoreHandle xEMACSemaphore;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void clock_init(void)\r
128 {\r
129         /* This is done when the scheduler starts. */\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* Define clock functions here to avoid header file name clash between uIP\r
134 and the Luminary Micro driver library. */\r
135 clock_time_t clock_time( void )\r
136 {\r
137         return xTaskGetTickCount();\r
138 }\r
139 extern void timer_set(struct timer *t, clock_time_t interval);\r
140 extern int timer_expired(struct timer *t);\r
141 extern void timer_reset(struct timer *t);\r
142 \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 )( void );\r
152 \r
153         /* Enable/Reset the Ethernet Controller */\r
154         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
155         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
156 \r
157         /* Create the semaphore used by the ISR to wake this task. */\r
158         vSemaphoreCreateBinary( xEMACSemaphore );\r
159         \r
160         /* Initialise the uIP stack. */\r
161         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
162         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
163         uip_init();\r
164         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
165         uip_sethostaddr( xIPAddr );\r
166         httpd_init();\r
167 \r
168         while( vInitEMAC() != pdPASS )\r
169     {\r
170         vTaskDelay( uipINIT_WAIT );\r
171     }\r
172         prvSetMACAddress();     \r
173         \r
174 \r
175         for( ;; )\r
176         {\r
177                 /* Is there received data ready to be processed? */\r
178                 uip_len = uiGetEMACRxData( uip_buf );\r
179                 \r
180                 if( uip_len > 0 )\r
181                 {\r
182                         /* Standard uIP loop taken from the uIP manual. */\r
183 \r
184                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
185                         {\r
186                                 uip_arp_ipin();\r
187                                 uip_input();\r
188 \r
189                                 /* If the above function invocation resulted in data that\r
190                                 should be sent out on the network, the global variable\r
191                                 uip_len is set to a value > 0. */\r
192                                 if( uip_len > 0 )\r
193                                 {\r
194                                         uip_arp_out();\r
195                                         prvENET_Send();\r
196                                 }\r
197                         }\r
198                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
199                         {\r
200                                 uip_arp_arpin();\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                                         prvENET_Send();\r
208                                 }\r
209                         }\r
210                 }\r
211                 else\r
212                 {\r
213                         if( timer_expired( &periodic_timer ) )\r
214                         {\r
215                                 timer_reset( &periodic_timer );\r
216                                 for( i = 0; i < UIP_CONNS; i++ )\r
217                                 {\r
218                                         uip_periodic( i );\r
219         \r
220                                         /* If the above function invocation resulted in data that\r
221                                         should be sent out on the network, the global variable\r
222                                         uip_len is set to a value > 0. */\r
223                                         if( uip_len > 0 )\r
224                                         {\r
225                                                 uip_arp_out();\r
226                                                 prvENET_Send();\r
227                                         }\r
228                                 }       \r
229         \r
230                                 /* Call the ARP timer function every 10 seconds. */\r
231                                 if( timer_expired( &arp_timer ) )\r
232                                 {\r
233                                         timer_reset( &arp_timer );\r
234                                         uip_arp_timer();\r
235                                 }\r
236                         }\r
237                         else\r
238                         {                       \r
239                                 /* We did not receive a packet, and there was no periodic\r
240                                 processing to perform.  Block for a fixed period.  If a packet\r
241                                 is received during this period we will be woken by the ISR\r
242                                 giving us the Semaphore. */\r
243                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );                       \r
244                         }\r
245                 }\r
246         }\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 static void prvENET_Send(void)\r
251 {\r
252         vInitialiseSend();\r
253         vIncrementTxLength( uip_len );\r
254         vSendBufferToMAC();\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvSetMACAddress( void )\r
259 {\r
260 unsigned portLONG ulUser0, ulUser1;\r
261 unsigned char pucMACArray[8];\r
262 struct uip_eth_addr xAddr;\r
263 \r
264         /* Get the device MAC address from flash */\r
265     FlashUserGet(&ulUser0, &ulUser1);\r
266 \r
267         /* Convert the MAC address from flash into sequence of bytes. */\r
268     pucMACArray[0] = ((ulUser0 >>  0) & 0xff);\r
269     pucMACArray[1] = ((ulUser0 >>  8) & 0xff);\r
270     pucMACArray[2] = ((ulUser0 >> 16) & 0xff);\r
271     pucMACArray[3] = ((ulUser1 >>  0) & 0xff);\r
272     pucMACArray[4] = ((ulUser1 >>  8) & 0xff);\r
273     pucMACArray[5] = ((ulUser1 >> 16) & 0xff);\r
274 \r
275         /* Program the MAC address. */\r
276     EthernetMACAddrSet(ETH_BASE, pucMACArray);\r
277 \r
278         xAddr.addr[ 0 ] = pucMACArray[0];\r
279         xAddr.addr[ 1 ] = pucMACArray[1];\r
280         xAddr.addr[ 2 ] = pucMACArray[2];\r
281         xAddr.addr[ 3 ] = pucMACArray[3];\r
282         xAddr.addr[ 4 ] = pucMACArray[4];\r
283         xAddr.addr[ 5 ] = pucMACArray[5];\r
284         uip_setethaddr( xAddr );\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 void vApplicationProcessFormInput( portCHAR *pcInputString, portBASE_TYPE xInputLength )\r
289 {\r
290 char *c, *pcText;\r
291 static portCHAR cMessageForDisplay[ 32 ];\r
292 extern xQueueHandle xOLEDQueue;\r
293 xOLEDMessage xOLEDMessage;\r
294 \r
295         /* Process the form input sent by the IO page of the served HTML. */\r
296 \r
297         c = strstr( pcInputString, "?" );\r
298 \r
299     if( c )\r
300     {\r
301                 /* Turn LED's on or off in accordance with the check box status. */\r
302                 if( strstr( c, "LED0=1" ) != NULL )\r
303                 {\r
304                         vParTestSetLED( 0, 1 );\r
305                 }\r
306                 else\r
307                 {\r
308                         vParTestSetLED( 0, 0 );\r
309                 }               \r
310                 \r
311                 /* Find the start of the text to be displayed on the LCD. */\r
312         pcText = strstr( c, "LCD=" );\r
313         pcText += strlen( "LCD=" );\r
314 \r
315         /* Terminate the file name for further processing within uIP. */\r
316         *c = 0x00;\r
317 \r
318         /* Terminate the LCD string. */\r
319         c = strstr( pcText, " " );\r
320         if( c != NULL )\r
321         {\r
322             *c = 0x00;\r
323         }\r
324 \r
325         /* Add required spaces. */\r
326         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
327         {\r
328             *c = ' ';\r
329         }\r
330 \r
331         /* Write the message to the LCD. */\r
332                 strcpy( cMessageForDisplay, pcText );\r
333                 xOLEDMessage.pcMessage = ( signed portCHAR * ) cMessageForDisplay;\r
334         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
335     }\r
336 }\r
337 \r