]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_Kinetis_K60_Tower_IAR/uIP_Task.c
2172f0dfdc3feb7ab3814a8db2d21c2461a440f9
[freertos] / FreeRTOS / Demo / CORTEX_Kinetis_K60_Tower_IAR / 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 \r
65 /* Standard includes. */\r
66 #include <string.h>\r
67 \r
68 /* Scheduler includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 #include "queue.h"\r
72 #include "timers.h"\r
73 \r
74 /* uip includes. */\r
75 #include "net/uip.h"\r
76 #include "net/uip_arp.h"\r
77 #include "apps/httpd/httpd.h"\r
78 #include "sys/timer.h"\r
79 #include "net/clock-arch.h"\r
80 #include "emac.h"\r
81 \r
82 /* Demo includes. */\r
83 #include "ParTest.h"\r
84 \r
85 /* The buffer used by the uIP stack to both receive and send.  In this case,\r
86 because the Ethernet driver is implemented to be zero copy - the uip_buf\r
87 variable is just a pointer to an Ethernet buffer, and not a buffer in its own\r
88 right. */\r
89 extern unsigned char *uip_buf;\r
90 \r
91 /* The ARP timer and the periodic timer share a callback function, so the\r
92 respective timer IDs are used to determine which timer actually expired.  These\r
93 constants are assigned to the timer IDs. */\r
94 #define uipARP_TIMER                            0\r
95 #define uipPERIODIC_TIMER                       1\r
96 \r
97 /* The length of the queue used to send events from timers or the Ethernet\r
98 driver to the uIP stack. */\r
99 #define uipEVENT_QUEUE_LENGTH           10\r
100 \r
101 /* A block time of zero simply means "don't block". */\r
102 #define uipDONT_BLOCK                           0UL\r
103 \r
104 /* Shortcut to the header within the Rx buffer. */\r
105 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
106 \r
107 /*-----------------------------------------------------------*/\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  * Perform any uIP initialisation required to ready the stack for http\r
116  * processing.\r
117  */\r
118 static void prvInitialise_uIP( void );\r
119 \r
120 /*\r
121  * The callback function that is assigned to both the periodic timer and the\r
122  * ARP timer.\r
123  */\r
124 static void prvUIPTimerCallback( xTimerHandle xTimer );\r
125 \r
126 /*\r
127  * Port functions required by the uIP stack.\r
128  */\r
129 clock_time_t clock_time( void );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The queue used to send TCP/IP events to the uIP stack. */\r
134 xQueueHandle xEMACEventQueue = NULL;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 clock_time_t clock_time( void )\r
139 {\r
140         return xTaskGetTickCount();\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vuIP_Task( void *pvParameters )\r
145 {\r
146 long i;\r
147 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;\r
148 unsigned short usPacketLength;\r
149 \r
150         /* Just to prevent compiler warnings about the unused parameter. */\r
151         ( void ) pvParameters;\r
152 \r
153         /* Initialise the uIP stack, configuring for web server usage. */\r
154         prvInitialise_uIP();\r
155 \r
156         /* Initialise the MAC and PHY. */\r
157         vEMACInit();\r
158 \r
159         for( ;; )\r
160         {\r
161                 /* Is there received data ready to be processed? */\r
162                 usPacketLength = usEMACRead();\r
163 \r
164                 /* Statements to be executed if data has been received on the Ethernet. */\r
165                 if( ( usPacketLength > 0U ) && ( uip_buf != NULL ) )\r
166                 {\r
167                         uip_len = usPacketLength;\r
168                         \r
169                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
170                         {\r
171                                 uip_arp_ipin();\r
172                                 uip_input();\r
173 \r
174                                 /* If the above function invocation resulted in data that\r
175                                 should be sent out on the network, the global variable\r
176                                 uip_len is set to a value > 0. */\r
177                                 if( uip_len > 0 )\r
178                                 {\r
179                                         uip_arp_out();\r
180                                         vEMACWrite();\r
181                                 }\r
182                         }\r
183                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
184                         {\r
185                                 uip_arp_arpin();\r
186 \r
187                                 /* If the above function invocation resulted in data that\r
188                                 should be sent out on the network, the global variable\r
189                                 uip_len is set to a value > 0. */\r
190                                 if( uip_len > 0 )\r
191                                 {\r
192                                         vEMACWrite();\r
193                                 }\r
194                         }\r
195                 }\r
196                 else\r
197                 {\r
198                         /* Clear the RX event latched in ulUIP_Events - if one was latched. */\r
199                         ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
200                 }\r
201 \r
202                 /* Statements to be executed if the TCP/IP period timer has expired. */\r
203                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
204                 {\r
205                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
206 \r
207                         if( uip_buf != NULL )\r
208                         {\r
209                                 for( i = 0; i < UIP_CONNS; i++ )\r
210                                 {\r
211                                         uip_periodic( i );\r
212         \r
213                                         /* If the above function invocation resulted in data that\r
214                                         should be sent out on the network, the global variable\r
215                                         uip_len is set to a value > 0. */\r
216                                         if( uip_len > 0 )\r
217                                         {\r
218                                                 uip_arp_out();\r
219                                                 vEMACWrite();\r
220                                         }\r
221                                 }\r
222                         }\r
223                 }\r
224 \r
225                 /* Statements to be executed if the ARP timer has expired. */\r
226                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
227                 {\r
228                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
229                         uip_arp_timer();\r
230                 }\r
231 \r
232                 /* If all latched events have been cleared - block until another event\r
233                 occurs. */\r
234                 if( ulUIP_Events == pdFALSE )\r
235                 {\r
236                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
237                         ulUIP_Events |= ulNewEvent;\r
238                 }\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 static void prvSetMACAddress( void )\r
244 {\r
245 struct uip_eth_addr xAddr;\r
246 \r
247         /* Configure the MAC address in the uIP stack. */\r
248         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
249         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
250         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
251         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
252         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
253         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
254         uip_setethaddr( xAddr );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvInitialise_uIP( void )\r
259 {\r
260 uip_ipaddr_t xIPAddr;\r
261 xTimerHandle xARPTimer, xPeriodicTimer;\r
262 \r
263         uip_init();\r
264         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
265         uip_sethostaddr( &xIPAddr );\r
266         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
267         uip_setnetmask( &xIPAddr );\r
268         prvSetMACAddress();\r
269         httpd_init();\r
270 \r
271         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
272         xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );\r
273 \r
274         /* Create and start the uIP timers. */\r
275         xARPTimer = xTimerCreate(       ( signed char * ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
276                                                                 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */\r
277                                                                 pdTRUE, /* Autor-reload. */\r
278                                                                 ( void * ) uipARP_TIMER,\r
279                                                                 prvUIPTimerCallback\r
280                                                         );\r
281 \r
282         xPeriodicTimer = xTimerCreate(  ( signed char * ) "PeriodicTimer",\r
283                                                                         ( 500UL / portTICK_RATE_MS ),\r
284                                                                         pdTRUE, /* Autor-reload. */\r
285                                                                         ( void * ) uipPERIODIC_TIMER,\r
286                                                                         prvUIPTimerCallback\r
287                                                                 );\r
288 \r
289         /* Sanity check that the timers were indeed created. */\r
290         configASSERT( xARPTimer );\r
291         configASSERT( xPeriodicTimer );\r
292         configASSERT( xEMACEventQueue );\r
293 \r
294         /* These commands will block indefinitely until they succeed, so there is\r
295         no point in checking their return values. */\r
296         xTimerStart( xARPTimer, portMAX_DELAY );\r
297         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 static void prvUIPTimerCallback( xTimerHandle xTimer )\r
302 {\r
303 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
304 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
305 \r
306         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
307         block.  As this callback is assigned to both the ARP and Periodic timers, the\r
308         first thing to do is ascertain which timer it was that actually expired. */\r
309         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
310         {\r
311                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
312                                                                         break;\r
313 \r
314                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
315                                                                         break;\r
316 \r
317                 default                                 :       /* Should not get here. */\r
318                                                                         break;\r
319         }\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 void vApplicationProcessFormInput( char *pcInputString )\r
324 {\r
325 char *c;\r
326 const unsigned long ulYellowLED = 2UL;\r
327 \r
328         /* Only interested in processing form input if this is the IO page. */\r
329         c = strstr( pcInputString, "io.shtml" );\r
330         \r
331         if( c )\r
332         {\r
333                 /* Is there a command in the string? */\r
334                 c = strstr( pcInputString, "?" );\r
335             if( c )\r
336             {\r
337                         /* Turn the LEDs on or off in accordance with the check box status. */\r
338                         if( strstr( c, "LED0=1" ) != NULL )\r
339                         {\r
340                                 /* Turn the LEDs on. */\r
341                                 vParTestSetLED( ulYellowLED, pdTRUE );\r
342                         }\r
343                         else\r
344                         {\r
345                                 /* Turn the LEDs off. */\r
346                                 vParTestSetLED( ulYellowLED, pdFALSE );\r
347                         }\r
348             }\r
349                 else\r
350                 {\r
351                         /* Some browsers will only imply that a check box is off. */\r
352                         vParTestSetLED( ulYellowLED, pdFALSE );\r
353                 }\r
354         }\r
355 }\r
356 /*-----------------------------------------------------------*/\r