]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RDK_Renesas/RTOSDemo/uIP_Task.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / RX600_RX62N-RDK_Renesas / RTOSDemo / uIP_Task.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* Standard includes. */\r
76 #include <string.h>\r
77 \r
78 /* Scheduler includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "timers.h"\r
82 #include "queue.h"\r
83 \r
84 /* uip includes. */\r
85 #include "net/uip.h"\r
86 #include "net/uip_arp.h"\r
87 #include "apps/httpd/httpd.h"\r
88 #include "sys/timer.h"\r
89 #include "net/clock-arch.h"\r
90 #include "r_ether.h"\r
91 \r
92 /* Demo includes. */\r
93 #include "ParTest.h"\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* How long to wait before attempting to connect the MAC again. */\r
98 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
99 \r
100 /* Shortcut to the header within the Rx buffer. */\r
101 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
102 \r
103 /* Standard constant. */\r
104 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
105 \r
106 /* The ARP timer and the periodic timer share a callback function, so the\r
107 respective timer IDs are used to determine which timer actually expired.  These\r
108 constants are assigned to the timer IDs. */\r
109 #define uipARP_TIMER                            0\r
110 #define uipPERIODIC_TIMER                       1\r
111 \r
112 /* A block time of zero ticks simply means, "don't block". */\r
113 #define uipDONT_BLOCK                           0UL\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
119  */\r
120 static void prvSetMACAddress( void );\r
121 \r
122 /*\r
123  * Perform any uIP initialisation necessary. \r
124  */\r
125 static void prvInitialise_uIP( void );\r
126 \r
127 /*\r
128  * The callback function that is assigned to both the periodic timer and the\r
129  * ARP timer.\r
130  */\r
131 static void prvUIPTimerCallback( xTimerHandle xTimer );\r
132 \r
133 /*\r
134  * Port functions required by the uIP stack.\r
135  */\r
136 clock_time_t clock_time( void );\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /* The queue used to send TCP/IP events to the uIP stack. */\r
141 xQueueHandle xEMACEventQueue = NULL;\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 clock_time_t clock_time( void )\r
146 {\r
147         return xTaskGetTickCount();\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void vuIP_Task( void *pvParameters )\r
152 {\r
153 portBASE_TYPE i;\r
154 unsigned long ulNewEvent = 0UL;\r
155 unsigned long ulUIP_Events = 0UL;\r
156 \r
157         ( void ) pvParameters;\r
158         \r
159         /* Initialise the uIP stack. */\r
160         prvInitialise_uIP();\r
161 \r
162         /* Initialise the MAC. */\r
163         vInitEmac();\r
164 \r
165         while( lEMACWaitForLink() != pdPASS )\r
166     {\r
167         vTaskDelay( uipINIT_WAIT );\r
168     }\r
169 \r
170         for( ;; )\r
171         {\r
172                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )\r
173                 {               \r
174                         /* Is there received data ready to be processed? */\r
175                         uip_len = ( unsigned short ) ulEMACRead();\r
176                         \r
177                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
178                         {\r
179                                 /* Standard uIP loop taken from the uIP manual. */\r
180                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
181                                 {\r
182                                         uip_arp_ipin();\r
183                                         uip_input();\r
184 \r
185                                         /* If the above function invocation resulted in data that\r
186                                         should be sent out on the network, the global variable\r
187                                         uip_len is set to a value > 0. */\r
188                                         if( uip_len > 0 )\r
189                                         {\r
190                                                 uip_arp_out();\r
191                                                 vEMACWrite();\r
192                                         }\r
193                                 }\r
194                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
195                                 {\r
196                                         uip_arp_arpin();\r
197 \r
198                                         /* If the above function invocation resulted in data that\r
199                                         should be sent out on the network, the global variable\r
200                                         uip_len is set to a value > 0. */\r
201                                         if( uip_len > 0 )\r
202                                         {\r
203                                                 vEMACWrite();\r
204                                         }\r
205                                 }\r
206                         }\r
207                         else\r
208                         {\r
209                                 ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
210                         }\r
211                 }\r
212                 \r
213                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
214                 {\r
215                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
216                                         \r
217                         for( i = 0; i < UIP_CONNS; i++ )\r
218                         {\r
219                                 uip_periodic( i );\r
220 \r
221                                 /* If the above function invocation resulted in data that\r
222                                 should be sent out on the network, the global variable\r
223                                 uip_len is set to a value > 0. */\r
224                                 if( uip_len > 0 )\r
225                                 {\r
226                                         uip_arp_out();\r
227                                         vEMACWrite();\r
228                                 }\r
229                         }\r
230                 }\r
231                 \r
232                 /* Call the ARP timer function every 10 seconds. */\r
233                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
234                 {\r
235                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
236                         uip_arp_timer();\r
237                 }\r
238                         \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 prvInitialise_uIP( void )\r
249 {\r
250 xTimerHandle xARPTimer, xPeriodicTimer;\r
251 uip_ipaddr_t xIPAddr;\r
252 const unsigned long ul_uIPEventQueueLength = 10UL;\r
253 \r
254         /* Initialise the uIP stack. */\r
255         uip_init();\r
256         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
257         uip_sethostaddr( &xIPAddr );\r
258         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
259         uip_setnetmask( &xIPAddr );\r
260         prvSetMACAddress();\r
261         httpd_init();\r
262 \r
263         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
264         xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );\r
265 \r
266         /* Create and start the uIP timers. */\r
267         xARPTimer = xTimerCreate(       ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
268                                                                 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */\r
269                                                                 pdTRUE, /* Autor-reload. */\r
270                                                                 ( void * ) uipARP_TIMER,\r
271                                                                 prvUIPTimerCallback\r
272                                                         );\r
273 \r
274         xPeriodicTimer = xTimerCreate(  ( const signed char * const ) "PeriodicTimer",\r
275                                                                         ( 50 / portTICK_RATE_MS ),\r
276                                                                         pdTRUE, /* Autor-reload. */\r
277                                                                         ( void * ) uipPERIODIC_TIMER,\r
278                                                                         prvUIPTimerCallback\r
279                                                                 );\r
280 \r
281         configASSERT( xARPTimer );\r
282         configASSERT( xPeriodicTimer );\r
283 \r
284         xTimerStart( xARPTimer, portMAX_DELAY );\r
285         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static void prvUIPTimerCallback( xTimerHandle xTimer )\r
290 {\r
291 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
292 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
293 \r
294         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
295         block. */\r
296         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
297         {\r
298                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
299                                                                         break;\r
300 \r
301                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
302                                                                         break;\r
303 \r
304                 default                                 :       /* Should not get here. */\r
305                                                                         break;\r
306         }\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvSetMACAddress( void )\r
311 {\r
312 struct uip_eth_addr xAddr;\r
313 \r
314         /* Configure the MAC address in the uIP stack. */\r
315         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
316         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
317         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
318         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
319         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
320         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
321         uip_setethaddr( xAddr );\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationProcessFormInput( char *pcInputString )\r
326 {\r
327 char *c;\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 LED's 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( 7, 1 );\r
343                                 vParTestSetLED( 8, 1 );\r
344                                 vParTestSetLED( 9, 1 );\r
345                                 vParTestSetLED( 10, 1 );\r
346                         }\r
347                         else\r
348                         {\r
349                                 /* Turn the LEDs off. */\r
350                                 vParTestSetLED( 7, 0 );\r
351                                 vParTestSetLED( 8, 0 );\r
352                                 vParTestSetLED( 9, 0 );\r
353                                 vParTestSetLED( 10, 0 );\r
354                         }\r
355             }\r
356                 else\r
357                 {\r
358                         /* Commands to turn LEDs off are not always explicit. */\r
359                         vParTestSetLED( 7, 0 );\r
360                         vParTestSetLED( 8, 0 );\r
361                         vParTestSetLED( 9, 0 );\r
362                         vParTestSetLED( 10, 0 );\r
363                 }\r
364         }\r
365 }\r
366 \r