]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_Rowley/webserver/uIP_Task.c
Added BSP generation files to MicroBlaze directory.
[freertos] / Demo / CORTEX_LM3Sxxxx_Rowley / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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 #include "lcd_message.h"\r
62 \r
63 /* uip includes. */\r
64 #include "hw_types.h"\r
65 \r
66 #include "uip.h"\r
67 #include "uip_arp.h"\r
68 #include "httpd.h"\r
69 #include "timer.h"\r
70 #include "clock-arch.h"\r
71 #include "hw_ethernet.h"\r
72 #include "ethernet.h"\r
73 #include "hw_memmap.h"\r
74 #include "lmi_flash.h"\r
75 #include "sysctl.h"\r
76 \r
77 /* Demo includes. */\r
78 #include "emac.h"\r
79 #include "partest.h"\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* IP address configuration. */\r
84 #define uipIP_ADDR0             192\r
85 #define uipIP_ADDR1             168\r
86 #define uipIP_ADDR2             0\r
87 #define uipIP_ADDR3             201\r
88 \r
89 /* Netmask configuration. */\r
90 #define uipNETMASK_0    255\r
91 #define uipNETMASK_1    255\r
92 #define uipNETMASK_2    255\r
93 #define uipNETMASK_3    0\r
94 \r
95 /* How long to wait before attempting to connect the MAC again. */\r
96 #define uipINIT_WAIT    100\r
97 \r
98 /* Shortcut to the header within the Rx buffer. */\r
99 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
100 \r
101 /* Standard constant. */\r
102 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * Send the uIP buffer to the MAC.\r
108  */\r
109 static void prvENET_Send(void);\r
110 \r
111 /*\r
112  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
113  */\r
114 static void prvSetMACAddress( void );\r
115 \r
116 /*\r
117  * Port functions required by the uIP stack.\r
118  */\r
119 void clock_init( void );\r
120 clock_time_t clock_time( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /* The semaphore used by the ISR to wake the uIP task. */\r
125 extern xSemaphoreHandle xEMACSemaphore;\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 void clock_init(void)\r
130 {\r
131         /* This is done when the scheduler starts. */\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 clock_time_t clock_time( void )\r
136 {\r
137         return xTaskGetTickCount();\r
138 }\r
139 \r
140 \r
141 void vuIP_Task( void *pvParameters )\r
142 {\r
143 portBASE_TYPE i;\r
144 uip_ipaddr_t xIPAddr;\r
145 struct timer periodic_timer, arp_timer;\r
146 extern void ( vEMAC_ISR )( void );\r
147 \r
148         /* Enable/Reset the Ethernet Controller */\r
149         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
150         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
151 \r
152         /* Create the semaphore used by the ISR to wake this task. */\r
153         vSemaphoreCreateBinary( xEMACSemaphore );\r
154 \r
155         /* Initialise the uIP stack. */\r
156         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
157         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
158         uip_init();\r
159         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
160         uip_sethostaddr( xIPAddr );\r
161         uip_ipaddr( xIPAddr, uipNETMASK_0, uipNETMASK_1, uipNETMASK_2, uipNETMASK_3 );\r
162     uip_setnetmask( xIPAddr );\r
163         httpd_init();\r
164 \r
165         while( vInitEMAC() != pdPASS )\r
166     {\r
167         vTaskDelay( uipINIT_WAIT );\r
168     }\r
169         prvSetMACAddress();\r
170 \r
171 \r
172         for( ;; )\r
173         {\r
174                 /* Is there received data ready to be processed? */\r
175                 uip_len = uiGetEMACRxData( uip_buf );\r
176 \r
177                 if( uip_len > 0 )\r
178                 {\r
179                         /* Standard uIP loop taken from the uIP manual. */\r
180 \r
181                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
182                         {\r
183                                 uip_arp_ipin();\r
184                                 uip_input();\r
185 \r
186                                 /* If the above function invocation resulted in data that\r
187                                 should be sent out on the network, the global variable\r
188                                 uip_len is set to a value > 0. */\r
189                                 if( uip_len > 0 )\r
190                                 {\r
191                                         uip_arp_out();\r
192                                         prvENET_Send();\r
193                                 }\r
194                         }\r
195                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
196                         {\r
197                                 uip_arp_arpin();\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                                         prvENET_Send();\r
205                                 }\r
206                         }\r
207                 }\r
208                 else\r
209                 {\r
210                         if( timer_expired( &periodic_timer ) )\r
211                         {\r
212                                 timer_reset( &periodic_timer );\r
213                                 for( i = 0; i < UIP_CONNS; i++ )\r
214                                 {\r
215                                         uip_periodic( i );\r
216 \r
217                                         /* If the above function invocation resulted in data that\r
218                                         should be sent out on the network, the global variable\r
219                                         uip_len is set to a value > 0. */\r
220                                         if( uip_len > 0 )\r
221                                         {\r
222                                                 uip_arp_out();\r
223                                                 prvENET_Send();\r
224                                         }\r
225                                 }\r
226 \r
227                                 /* Call the ARP timer function every 10 seconds. */\r
228                                 if( timer_expired( &arp_timer ) )\r
229                                 {\r
230                                         timer_reset( &arp_timer );\r
231                                         uip_arp_timer();\r
232                                 }\r
233                         }\r
234                         else\r
235                         {\r
236                                 /* We did not receive a packet, and there was no periodic\r
237                                 processing to perform.  Block for a fixed period.  If a packet\r
238                                 is received during this period we will be woken by the ISR\r
239                                 giving us the Semaphore. */\r
240                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
241                         }\r
242                 }\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvENET_Send(void)\r
248 {\r
249     vInitialiseSend();\r
250     vIncrementTxLength( uip_len );\r
251     vSendBufferToMAC();\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 long 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( char *pcInputString, portBASE_TYPE xInputLength )\r
289 {\r
290 char *c, *pcText;\r
291 static char 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