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