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