]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_Kinetis_K60_Tower_IAR/uIP_Task.c
Update version number ready for the V8.2.3 release.
[freertos] / FreeRTOS / Demo / CORTEX_Kinetis_K60_Tower_IAR / uIP_Task.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <string.h>\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "queue.h"\r
77 #include "timers.h"\r
78 \r
79 /* uip includes. */\r
80 #include "net/uip.h"\r
81 #include "net/uip_arp.h"\r
82 #include "apps/httpd/httpd.h"\r
83 #include "sys/timer.h"\r
84 #include "net/clock-arch.h"\r
85 #include "emac.h"\r
86 \r
87 /* Demo includes. */\r
88 #include "ParTest.h"\r
89 \r
90 /* The buffer used by the uIP stack to both receive and send.  In this case,\r
91 because the Ethernet driver is implemented to be zero copy - the uip_buf\r
92 variable is just a pointer to an Ethernet buffer, and not a buffer in its own\r
93 right. */\r
94 extern unsigned char *uip_buf;\r
95 \r
96 /* The ARP timer and the periodic timer share a callback function, so the\r
97 respective timer IDs are used to determine which timer actually expired.  These\r
98 constants are assigned to the timer IDs. */\r
99 #define uipARP_TIMER                            0\r
100 #define uipPERIODIC_TIMER                       1\r
101 \r
102 /* The length of the queue used to send events from timers or the Ethernet\r
103 driver to the uIP stack. */\r
104 #define uipEVENT_QUEUE_LENGTH           10\r
105 \r
106 /* A block time of zero simply means "don't block". */\r
107 #define uipDONT_BLOCK                           0UL\r
108 \r
109 /* Shortcut to the header within the Rx buffer. */\r
110 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
116  */\r
117 static void prvSetMACAddress( void );\r
118 \r
119 /*\r
120  * Perform any uIP initialisation required to ready the stack for http\r
121  * processing.\r
122  */\r
123 static void prvInitialise_uIP( void );\r
124 \r
125 /*\r
126  * The callback function that is assigned to both the periodic timer and the\r
127  * ARP timer.\r
128  */\r
129 static void prvUIPTimerCallback( TimerHandle_t xTimer );\r
130 \r
131 /*\r
132  * Port functions required by the uIP stack.\r
133  */\r
134 clock_time_t clock_time( void );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /* The queue used to send TCP/IP events to the uIP stack. */\r
139 QueueHandle_t xEMACEventQueue = NULL;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 clock_time_t clock_time( void )\r
144 {\r
145         return xTaskGetTickCount();\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vuIP_Task( void *pvParameters )\r
150 {\r
151 long i;\r
152 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;\r
153 unsigned short usPacketLength;\r
154 \r
155         /* Just to prevent compiler warnings about the unused parameter. */\r
156         ( void ) pvParameters;\r
157 \r
158         /* Initialise the uIP stack, configuring for web server usage. */\r
159         prvInitialise_uIP();\r
160 \r
161         /* Initialise the MAC and PHY. */\r
162         vEMACInit();\r
163 \r
164         for( ;; )\r
165         {\r
166                 /* Is there received data ready to be processed? */\r
167                 usPacketLength = usEMACRead();\r
168 \r
169                 /* Statements to be executed if data has been received on the Ethernet. */\r
170                 if( ( usPacketLength > 0U ) && ( uip_buf != NULL ) )\r
171                 {\r
172                         uip_len = usPacketLength;\r
173 \r
174                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
175                         {\r
176                                 uip_arp_ipin();\r
177                                 uip_input();\r
178 \r
179                                 /* If the above function invocation resulted in data that\r
180                                 should be sent out on the network, the global variable\r
181                                 uip_len is set to a value > 0. */\r
182                                 if( uip_len > 0 )\r
183                                 {\r
184                                         uip_arp_out();\r
185                                         vEMACWrite();\r
186                                 }\r
187                         }\r
188                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
189                         {\r
190                                 uip_arp_arpin();\r
191 \r
192                                 /* If the above function invocation resulted in data that\r
193                                 should be sent out on the network, the global variable\r
194                                 uip_len is set to a value > 0. */\r
195                                 if( uip_len > 0 )\r
196                                 {\r
197                                         vEMACWrite();\r
198                                 }\r
199                         }\r
200                 }\r
201                 else\r
202                 {\r
203                         /* Clear the RX event latched in ulUIP_Events - if one was latched. */\r
204                         ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
205                 }\r
206 \r
207                 /* Statements to be executed if the TCP/IP period timer has expired. */\r
208                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
209                 {\r
210                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
211 \r
212                         if( uip_buf != NULL )\r
213                         {\r
214                                 for( i = 0; i < UIP_CONNS; i++ )\r
215                                 {\r
216                                         uip_periodic( i );\r
217 \r
218                                         /* If the above function invocation resulted in data that\r
219                                         should be sent out on the network, the global variable\r
220                                         uip_len is set to a value > 0. */\r
221                                         if( uip_len > 0 )\r
222                                         {\r
223                                                 uip_arp_out();\r
224                                                 vEMACWrite();\r
225                                         }\r
226                                 }\r
227                         }\r
228                 }\r
229 \r
230                 /* Statements to be executed if the ARP timer has expired. */\r
231                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
232                 {\r
233                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
234                         uip_arp_timer();\r
235                 }\r
236 \r
237                 /* If all latched events have been cleared - block until another event\r
238                 occurs. */\r
239                 if( ulUIP_Events == pdFALSE )\r
240                 {\r
241                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
242                         ulUIP_Events |= ulNewEvent;\r
243                 }\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static void prvSetMACAddress( void )\r
249 {\r
250 struct uip_eth_addr xAddr;\r
251 \r
252         /* Configure the MAC address in the uIP stack. */\r
253         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
254         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
255         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
256         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
257         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
258         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
259         uip_setethaddr( xAddr );\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static void prvInitialise_uIP( void )\r
264 {\r
265 uip_ipaddr_t xIPAddr;\r
266 TimerHandle_t xARPTimer, xPeriodicTimer;\r
267 \r
268         uip_init();\r
269         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
270         uip_sethostaddr( &xIPAddr );\r
271         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
272         uip_setnetmask( &xIPAddr );\r
273         prvSetMACAddress();\r
274         httpd_init();\r
275 \r
276         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
277         xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );\r
278 \r
279         /* Create and start the uIP timers. */\r
280         xARPTimer = xTimerCreate(       "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
281                                                                 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */\r
282                                                                 pdTRUE, /* Autor-reload. */\r
283                                                                 ( void * ) uipARP_TIMER,\r
284                                                                 prvUIPTimerCallback\r
285                                                         );\r
286 \r
287         xPeriodicTimer = xTimerCreate(  "PeriodicTimer",\r
288                                                                         ( 500UL / portTICK_PERIOD_MS ),\r
289                                                                         pdTRUE, /* Autor-reload. */\r
290                                                                         ( void * ) uipPERIODIC_TIMER,\r
291                                                                         prvUIPTimerCallback\r
292                                                                 );\r
293 \r
294         /* Sanity check that the timers were indeed created. */\r
295         configASSERT( xARPTimer );\r
296         configASSERT( xPeriodicTimer );\r
297         configASSERT( xEMACEventQueue );\r
298 \r
299         /* These commands will block indefinitely until they succeed, so there is\r
300         no point in checking their return values. */\r
301         xTimerStart( xARPTimer, portMAX_DELAY );\r
302         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvUIPTimerCallback( TimerHandle_t xTimer )\r
307 {\r
308 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
309 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
310 \r
311         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
312         block.  As this callback is assigned to both the ARP and Periodic timers, the\r
313         first thing to do is ascertain which timer it was that actually expired. */\r
314         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
315         {\r
316                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
317                                                                         break;\r
318 \r
319                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
320                                                                         break;\r
321 \r
322                 default                                 :       /* Should not get here. */\r
323                                                                         break;\r
324         }\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 void vApplicationProcessFormInput( char *pcInputString )\r
329 {\r
330 char *c;\r
331 const unsigned long ulYellowLED = 2UL;\r
332 \r
333         /* Only interested in processing form input if this is the IO page. */\r
334         c = strstr( pcInputString, "io.shtml" );\r
335 \r
336         if( c )\r
337         {\r
338                 /* Is there a command in the string? */\r
339                 c = strstr( pcInputString, "?" );\r
340             if( c )\r
341             {\r
342                         /* Turn the LEDs on or off in accordance with the check box status. */\r
343                         if( strstr( c, "LED0=1" ) != NULL )\r
344                         {\r
345                                 /* Turn the LEDs on. */\r
346                                 vParTestSetLED( ulYellowLED, pdTRUE );\r
347                         }\r
348                         else\r
349                         {\r
350                                 /* Turn the LEDs off. */\r
351                                 vParTestSetLED( ulYellowLED, pdFALSE );\r
352                         }\r
353             }\r
354                 else\r
355                 {\r
356                         /* Some browsers will only imply that a check box is off. */\r
357                         vParTestSetLED( ulYellowLED, pdFALSE );\r
358                 }\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r