]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_Eclipse/RTOSDemo/webserver/uIP_Task.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_Eclipse / RTOSDemo / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 /* Standard includes. */\r
65 #include <string.h>\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "semphr.h"\r
71 \r
72 #include "lcd_message.h"\r
73 \r
74 /* uip includes. */\r
75 #include "hw_types.h"\r
76 \r
77 #include "uip.h"\r
78 #include "uip_arp.h"\r
79 #include "httpd.h"\r
80 #include "timer.h"\r
81 #include "clock-arch.h"\r
82 #include "hw_ethernet.h"\r
83 #include "ethernet.h"\r
84 #include "hw_memmap.h"\r
85 #include "lmi_flash.h"\r
86 #include "sysctl.h"\r
87 \r
88 /* Demo includes. */\r
89 #include "emac.h"\r
90 #include "partest.h"\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* IP address configuration. */\r
95 #define uipIP_ADDR0             172\r
96 #define uipIP_ADDR1             25\r
97 #define uipIP_ADDR2             218\r
98 #define uipIP_ADDR3             19      \r
99 \r
100 /* How long to wait before attempting to connect the MAC again. */\r
101 #define uipINIT_WAIT    100\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 xSemaphoreHandle 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 )( 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 = cMessageForDisplay;\r
334         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
335     }\r
336 }\r
337 \r