]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RSK_GNURX/RTOSDemo/uIP_Task.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / RX600_RX62N-RSK_GNURX / RTOSDemo / uIP_Task.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Standard includes. */\r
68 #include <string.h>\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "timers.h"\r
74 #include "queue.h"\r
75 \r
76 /* uip includes. */\r
77 #include "net/uip.h"\r
78 #include "net/uip_arp.h"\r
79 #include "apps/httpd/httpd.h"\r
80 #include "sys/timer.h"\r
81 #include "net/clock-arch.h"\r
82 #include "r_ether.h"\r
83 \r
84 /* Demo includes. */\r
85 #include "ParTest.h"\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* How long to wait before attempting to connect the MAC again. */\r
90 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
91 \r
92 /* Shortcut to the header within the Rx buffer. */\r
93 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
94 \r
95 /* Standard constant. */\r
96 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
97 \r
98 /* The ARP timer and the periodic timer share a callback function, so the\r
99 respective timer IDs are used to determine which timer actually expired.  These\r
100 constants are assigned to the timer IDs. */\r
101 #define uipARP_TIMER                            0\r
102 #define uipPERIODIC_TIMER                       1\r
103 \r
104 /* A block time of zero ticks simply means, "don't block". */\r
105 #define uipDONT_BLOCK                           0UL\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 necessary. \r
116  */\r
117 static void prvInitialise_uIP( void );\r
118 \r
119 /*\r
120  * The callback function that is assigned to both the periodic timer and the\r
121  * ARP timer.\r
122  */\r
123 static void prvUIPTimerCallback( xTimerHandle xTimer );\r
124 \r
125 /*\r
126  * Port functions required by the uIP stack.\r
127  */\r
128 clock_time_t clock_time( void );\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* The queue used to send TCP/IP events to the uIP stack. */\r
133 xQueueHandle xEMACEventQueue = NULL;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 clock_time_t clock_time( void )\r
138 {\r
139         return xTaskGetTickCount();\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vuIP_Task( void *pvParameters )\r
144 {\r
145 portBASE_TYPE i;\r
146 unsigned long ulNewEvent = 0UL;\r
147 unsigned long ulUIP_Events = 0UL;\r
148 \r
149         ( void ) pvParameters;\r
150         \r
151         /* Initialise the uIP stack. */\r
152         prvInitialise_uIP();\r
153 \r
154         /* Initialise the MAC. */\r
155         vInitEmac();\r
156 \r
157         while( lEMACWaitForLink() != pdPASS )\r
158     {\r
159         vTaskDelay( uipINIT_WAIT );\r
160     }\r
161 \r
162         for( ;; )\r
163         {\r
164                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )\r
165                 {               \r
166                         /* Is there received data ready to be processed? */\r
167                         uip_len = ( unsigned short ) ulEMACRead();\r
168                         \r
169                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
170                         {\r
171                                 /* Standard uIP loop taken from the uIP manual. */\r
172                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
173                                 {\r
174                                         uip_arp_ipin();\r
175                                         uip_input();\r
176 \r
177                                         /* If the above function invocation resulted in data that\r
178                                         should be sent out on the network, the global variable\r
179                                         uip_len is set to a value > 0. */\r
180                                         if( uip_len > 0 )\r
181                                         {\r
182                                                 uip_arp_out();\r
183                                                 vEMACWrite();\r
184                                         }\r
185                                 }\r
186                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
187                                 {\r
188                                         uip_arp_arpin();\r
189 \r
190                                         /* If the above function invocation resulted in data that\r
191                                         should be sent out on the network, the global variable\r
192                                         uip_len is set to a value > 0. */\r
193                                         if( uip_len > 0 )\r
194                                         {\r
195                                                 vEMACWrite();\r
196                                         }\r
197                                 }\r
198                         }\r
199                         else\r
200                         {\r
201                                 ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
202                         }\r
203                 }\r
204                 \r
205                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
206                 {\r
207                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\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                 /* Call the ARP timer function every 10 seconds. */\r
225                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
226                 {\r
227                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
228                         uip_arp_timer();\r
229                 }\r
230                         \r
231                 if( ulUIP_Events == pdFALSE )\r
232                 {\r
233                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
234                         ulUIP_Events |= ulNewEvent;\r
235                 }\r
236         }\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void prvInitialise_uIP( void )\r
241 {\r
242 xTimerHandle xARPTimer, xPeriodicTimer;\r
243 uip_ipaddr_t xIPAddr;\r
244 const unsigned long ul_uIPEventQueueLength = 10UL;\r
245 \r
246         /* Initialise the uIP stack. */\r
247         uip_init();\r
248         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
249         uip_sethostaddr( &xIPAddr );\r
250         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
251         uip_setnetmask( &xIPAddr );\r
252         prvSetMACAddress();\r
253         httpd_init();\r
254 \r
255         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
256         xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );\r
257 \r
258         /* Create and start the uIP timers. */\r
259         xARPTimer = xTimerCreate(       ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
260                                                                 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */\r
261                                                                 pdTRUE, /* Autor-reload. */\r
262                                                                 ( void * ) uipARP_TIMER,\r
263                                                                 prvUIPTimerCallback\r
264                                                         );\r
265 \r
266         xPeriodicTimer = xTimerCreate(  ( const signed char * const ) "PeriodicTimer",\r
267                                                                         ( 50 / portTICK_RATE_MS ),\r
268                                                                         pdTRUE, /* Autor-reload. */\r
269                                                                         ( void * ) uipPERIODIC_TIMER,\r
270                                                                         prvUIPTimerCallback\r
271                                                                 );\r
272 \r
273         configASSERT( xARPTimer );\r
274         configASSERT( xPeriodicTimer );\r
275 \r
276         xTimerStart( xARPTimer, portMAX_DELAY );\r
277         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void prvUIPTimerCallback( xTimerHandle xTimer )\r
282 {\r
283 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
284 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
285 \r
286         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
287         block. */\r
288         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
289         {\r
290                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
291                                                                         break;\r
292 \r
293                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
294                                                                         break;\r
295 \r
296                 default                                 :       /* Should not get here. */\r
297                                                                         break;\r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 static void prvSetMACAddress( void )\r
303 {\r
304 struct uip_eth_addr xAddr;\r
305 \r
306         /* Configure the MAC address in the uIP stack. */\r
307         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
308         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
309         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
310         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
311         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
312         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
313         uip_setethaddr( xAddr );\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vApplicationProcessFormInput( char *pcInputString )\r
318 {\r
319 char *c;\r
320 \r
321         /* Only interested in processing form input if this is the IO page. */\r
322         c = strstr( pcInputString, "io.shtml" );\r
323         \r
324         if( c )\r
325         {\r
326                 /* Is there a command in the string? */\r
327                 c = strstr( pcInputString, "?" );\r
328             if( c )\r
329             {\r
330                         /* Turn the LED's on or off in accordance with the check box status. */\r
331                         if( strstr( c, "LED0=1" ) != NULL )\r
332                         {\r
333                                 /* Turn the LEDs on. */\r
334                                 vParTestSetLED( 7, 1 );\r
335                                 vParTestSetLED( 8, 1 );\r
336                                 vParTestSetLED( 9, 1 );\r
337                                 vParTestSetLED( 10, 1 );\r
338                         }\r
339                         else\r
340                         {\r
341                                 /* Turn the LEDs off. */\r
342                                 vParTestSetLED( 7, 0 );\r
343                                 vParTestSetLED( 8, 0 );\r
344                                 vParTestSetLED( 9, 0 );\r
345                                 vParTestSetLED( 10, 0 );\r
346                         }\r
347             }\r
348                 else\r
349                 {\r
350                         /* Commands to turn LEDs off are not always explicit. */\r
351                         vParTestSetLED( 7, 0 );\r
352                         vParTestSetLED( 8, 0 );\r
353                         vParTestSetLED( 9, 0 );\r
354                         vParTestSetLED( 10, 0 );\r
355                 }\r
356         }\r
357 }\r
358 \r