]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_MPU_LM3Sxxxx_Rowley/webserver/uIP_Task.c
Update libdriver for Rowley demos.
[freertos] / Demo / CORTEX_MPU_LM3Sxxxx_Rowley / webserver / uIP_Task.c
1 /*\r
2         FreeRTOS V5.4.2 - 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         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 /* Standard includes. */\r
48 #include <string.h>\r
49 \r
50 /* Scheduler includes. */\r
51 #include "FreeRTOS.h"\r
52 #include "task.h"\r
53 #include "semphr.h"\r
54 \r
55 #include "lcd_message.h"\r
56 \r
57 /* uip includes. */\r
58 #include "hw_types.h"\r
59 \r
60 #include "uip.h"\r
61 #include "uip_arp.h"\r
62 #include "httpd.h"\r
63 #include "timer.h"\r
64 #include "clock-arch.h"\r
65 #include "hw_ethernet.h"\r
66 #include "ethernet.h"\r
67 #include "hw_memmap.h"\r
68 #include "lmi_flash.h"\r
69 #include "sysctl.h"\r
70 \r
71 /* Demo includes. */\r
72 #include "emac.h"\r
73 #include "partest.h"\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* IP address configuration. */\r
78 #define uipIP_ADDR0             192\r
79 #define uipIP_ADDR1             168\r
80 #define uipIP_ADDR2             0\r
81 #define uipIP_ADDR3             201\r
82 \r
83 /* Netmask configuration. */\r
84 #define uipNETMASK_0    255\r
85 #define uipNETMASK_1    255\r
86 #define uipNETMASK_2    255\r
87 #define uipNETMASK_3    0\r
88 \r
89 /* How long to wait before attempting to connect the MAC again. */\r
90 #define uipINIT_WAIT    100\r
91 \r
92 /* Shortcut to the header within the Rx buffer. */\r
93 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
94 \r
95 /* Standard constant. */\r
96 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /*\r
101  * Send the uIP buffer to the MAC.\r
102  */\r
103 static void prvENET_Send(void);\r
104 \r
105 /*\r
106  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
107  */\r
108 static void prvSetMACAddress( void );\r
109 \r
110 /*\r
111  * Port functions required by the uIP stack.\r
112  */\r
113 void clock_init( void );\r
114 clock_time_t clock_time( void );\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* The semaphore used by the ISR to wake the uIP task. */\r
119 extern xSemaphoreHandle xEMACSemaphore;\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void clock_init(void)\r
124 {\r
125         /* This is done when the scheduler starts. */\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 clock_time_t clock_time( void )\r
130 {\r
131         return xTaskGetTickCount();\r
132 }\r
133 \r
134 \r
135 void vuIP_Task( void *pvParameters )\r
136 {\r
137 portBASE_TYPE i;\r
138 uip_ipaddr_t xIPAddr;\r
139 struct timer periodic_timer, arp_timer;\r
140 extern void ( vEMAC_ISR )( void );\r
141 \r
142         /* Enable/Reset the Ethernet Controller */\r
143         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
144         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
145 \r
146         /* Create the semaphore used by the ISR to wake this task. */\r
147         vSemaphoreCreateBinary( xEMACSemaphore );\r
148 \r
149         /* Initialise the uIP stack. */\r
150         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
151         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
152         uip_init();\r
153         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
154         uip_sethostaddr( xIPAddr );\r
155         uip_ipaddr( xIPAddr, uipNETMASK_0, uipNETMASK_1, uipNETMASK_2, uipNETMASK_3 );\r
156     uip_setnetmask( xIPAddr );\r
157         httpd_init();\r
158 \r
159         while( vInitEMAC() != pdPASS )\r
160     {\r
161         vTaskDelay( uipINIT_WAIT );\r
162     }\r
163         prvSetMACAddress();\r
164 \r
165 \r
166         for( ;; )\r
167         {\r
168                 /* Is there received data ready to be processed? */\r
169                 uip_len = uiGetEMACRxData( uip_buf );\r
170 \r
171                 if( uip_len > 0 )\r
172                 {\r
173                         /* Standard uIP loop taken from the uIP manual. */\r
174 \r
175                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
176                         {\r
177                                 uip_arp_ipin();\r
178                                 uip_input();\r
179 \r
180                                 /* If the above function invocation resulted in data that\r
181                                 should be sent out on the network, the global variable\r
182                                 uip_len is set to a value > 0. */\r
183                                 if( uip_len > 0 )\r
184                                 {\r
185                                         uip_arp_out();\r
186                                         prvENET_Send();\r
187                                 }\r
188                         }\r
189                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
190                         {\r
191                                 uip_arp_arpin();\r
192 \r
193                                 /* If the above function invocation resulted in data that\r
194                                 should be sent out on the network, the global variable\r
195                                 uip_len is set to a value > 0. */\r
196                                 if( uip_len > 0 )\r
197                                 {\r
198                                         prvENET_Send();\r
199                                 }\r
200                         }\r
201                 }\r
202                 else\r
203                 {\r
204                         if( timer_expired( &periodic_timer ) )\r
205                         {\r
206                                 timer_reset( &periodic_timer );\r
207                                 for( i = 0; i < UIP_CONNS; i++ )\r
208                                 {\r
209                                         uip_periodic( i );\r
210 \r
211                                         /* If the above function invocation resulted in data that\r
212                                         should be sent out on the network, the global variable\r
213                                         uip_len is set to a value > 0. */\r
214                                         if( uip_len > 0 )\r
215                                         {\r
216                                                 uip_arp_out();\r
217                                                 prvENET_Send();\r
218                                         }\r
219                                 }\r
220 \r
221                                 /* Call the ARP timer function every 10 seconds. */\r
222                                 if( timer_expired( &arp_timer ) )\r
223                                 {\r
224                                         timer_reset( &arp_timer );\r
225                                         uip_arp_timer();\r
226                                 }\r
227                         }\r
228                         else\r
229                         {\r
230                                 /* We did not receive a packet, and there was no periodic\r
231                                 processing to perform.  Block for a fixed period.  If a packet\r
232                                 is received during this period we will be woken by the ISR\r
233                                 giving us the Semaphore. */\r
234                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
235                         }\r
236                 }\r
237         }\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 static void prvENET_Send(void)\r
242 {\r
243     vInitialiseSend();\r
244     vIncrementTxLength( uip_len );\r
245     vSendBufferToMAC();\r
246     vInitialiseSend();\r
247     vIncrementTxLength( uip_len );\r
248     vSendBufferToMAC();\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static void prvSetMACAddress( void )\r
253 {\r
254 unsigned portLONG ulUser0, ulUser1;\r
255 unsigned char pucMACArray[8];\r
256 struct uip_eth_addr xAddr;\r
257 \r
258         /* Get the device MAC address from flash */\r
259     FlashUserGet(&ulUser0, &ulUser1);\r
260 \r
261         /* Convert the MAC address from flash into sequence of bytes. */\r
262     pucMACArray[0] = ((ulUser0 >>  0) & 0xff);\r
263     pucMACArray[1] = ((ulUser0 >>  8) & 0xff);\r
264     pucMACArray[2] = ((ulUser0 >> 16) & 0xff);\r
265     pucMACArray[3] = ((ulUser1 >>  0) & 0xff);\r
266     pucMACArray[4] = ((ulUser1 >>  8) & 0xff);\r
267     pucMACArray[5] = ((ulUser1 >> 16) & 0xff);\r
268 \r
269         /* Program the MAC address. */\r
270     EthernetMACAddrSet(ETH_BASE, pucMACArray);\r
271 \r
272         xAddr.addr[ 0 ] = pucMACArray[0];\r
273         xAddr.addr[ 1 ] = pucMACArray[1];\r
274         xAddr.addr[ 2 ] = pucMACArray[2];\r
275         xAddr.addr[ 3 ] = pucMACArray[3];\r
276         xAddr.addr[ 4 ] = pucMACArray[4];\r
277         xAddr.addr[ 5 ] = pucMACArray[5];\r
278         uip_setethaddr( xAddr );\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void vApplicationProcessFormInput( portCHAR *pcInputString, portBASE_TYPE xInputLength )\r
283 {\r
284 char *c, *pcText;\r
285 static portCHAR cMessageForDisplay[ 32 ];\r
286 extern xQueueHandle xOLEDQueue;\r
287 xOLEDMessage xOLEDMessage;\r
288 \r
289         /* Process the form input sent by the IO page of the served HTML. */\r
290 \r
291         c = strstr( pcInputString, "?" );\r
292 \r
293     if( c )\r
294     {\r
295                 /* Turn LED's on or off in accordance with the check box status. */\r
296                 if( strstr( c, "LED0=1" ) != NULL )\r
297                 {\r
298                         vParTestSetLED( 0, 1 );\r
299                 }\r
300                 else\r
301                 {\r
302                         vParTestSetLED( 0, 0 );\r
303                 }\r
304 \r
305                 /* Find the start of the text to be displayed on the LCD. */\r
306         pcText = strstr( c, "LCD=" );\r
307         pcText += strlen( "LCD=" );\r
308 \r
309         /* Terminate the file name for further processing within uIP. */\r
310         *c = 0x00;\r
311 \r
312         /* Terminate the LCD string. */\r
313         c = strstr( pcText, " " );\r
314         if( c != NULL )\r
315         {\r
316             *c = 0x00;\r
317         }\r
318 \r
319         /* Add required spaces. */\r
320         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
321         {\r
322             *c = ' ';\r
323         }\r
324 \r
325         /* Write the message to the LCD. */\r
326                 strcpy( cMessageForDisplay, pcText );\r
327                 xOLEDMessage.pcMessage = cMessageForDisplay;\r
328         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
329     }\r
330 }\r
331 \r