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