]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RDK_IAR/uIP_Task.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / RX600_RX62N-RDK_IAR / uIP_Task.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 "timers.h"\r
73 #include "queue.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 "r_ether.h"\r
82 \r
83 /* Demo includes. */\r
84 #include "ParTest.h"\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* How long to wait before attempting to connect the MAC again. */\r
89 #define uipINIT_WAIT    ( 100 / portTICK_PERIOD_MS )\r
90 \r
91 /* Shortcut to the header within the Rx buffer. */\r
92 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
93 \r
94 /* Standard constant. */\r
95 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
96 \r
97 /* The ARP timer and the periodic timer share a callback function, so the\r
98 respective timer IDs are used to determine which timer actually expired.  These\r
99 constants are assigned to the timer IDs. */\r
100 #define uipARP_TIMER                            0\r
101 #define uipPERIODIC_TIMER                       1\r
102 \r
103 /* A block time of zero ticks simply means, "don't block". */\r
104 #define uipDONT_BLOCK                           0UL\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
110  */\r
111 static void prvSetMACAddress( void );\r
112 \r
113 /*\r
114  * Perform any uIP initialisation necessary.\r
115  */\r
116 static void prvInitialise_uIP( void );\r
117 \r
118 /*\r
119  * The callback function that is assigned to both the periodic timer and the\r
120  * ARP timer.\r
121  */\r
122 static void prvUIPTimerCallback( TimerHandle_t xTimer );\r
123 \r
124 /*\r
125  * Port functions required by the uIP stack.\r
126  */\r
127 clock_time_t clock_time( void );\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* The queue used to send TCP/IP events to the uIP stack. */\r
132 QueueHandle_t xEMACEventQueue = NULL;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 clock_time_t clock_time( void )\r
137 {\r
138         return xTaskGetTickCount();\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vuIP_Task( void *pvParameters )\r
143 {\r
144 portBASE_TYPE i;\r
145 unsigned long ulNewEvent = 0UL;\r
146 unsigned long ulUIP_Events = 0UL;\r
147 \r
148         ( void ) pvParameters;\r
149         \r
150         /* Initialise the uIP stack. */\r
151         prvInitialise_uIP();\r
152 \r
153         /* Initialise the MAC. */\r
154         vInitEmac();\r
155 \r
156         while( lEMACWaitForLink() != pdPASS )\r
157     {\r
158         vTaskDelay( uipINIT_WAIT );\r
159     }\r
160 \r
161         for( ;; )\r
162         {\r
163                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )\r
164                 {               \r
165                         /* Is there received data ready to be processed? */\r
166                         uip_len = ( unsigned short ) ulEMACRead();\r
167                         \r
168                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
169                         {\r
170                                 /* Standard uIP loop taken from the uIP manual. */\r
171                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
172                                 {\r
173                                         uip_arp_ipin();\r
174                                         uip_input();\r
175 \r
176                                         /* If the above function invocation resulted in data that\r
177                                         should be sent out on the network, the global variable\r
178                                         uip_len is set to a value > 0. */\r
179                                         if( uip_len > 0 )\r
180                                         {\r
181                                                 uip_arp_out();\r
182                                                 vEMACWrite();\r
183                                         }\r
184                                 }\r
185                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
186                                 {\r
187                                         uip_arp_arpin();\r
188 \r
189                                         /* If the above function invocation resulted in data that\r
190                                         should be sent out on the network, the global variable\r
191                                         uip_len is set to a value > 0. */\r
192                                         if( uip_len > 0 )\r
193                                         {\r
194                                                 vEMACWrite();\r
195                                         }\r
196                                 }\r
197                         }\r
198                         else\r
199                         {\r
200                                 ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
201                         }\r
202                 }\r
203                 \r
204                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
205                 {\r
206                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
207                                         \r
208                         for( i = 0; i < UIP_CONNS; i++ )\r
209                         {\r
210                                 uip_periodic( i );\r
211 \r
212                                 /* If the above function invocation resulted in data that\r
213                                 should be sent out on the network, the global variable\r
214                                 uip_len is set to a value > 0. */\r
215                                 if( uip_len > 0 )\r
216                                 {\r
217                                         uip_arp_out();\r
218                                         vEMACWrite();\r
219                                 }\r
220                         }\r
221                 }\r
222                 \r
223                 /* Call the ARP timer function every 10 seconds. */\r
224                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
225                 {\r
226                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
227                         uip_arp_timer();\r
228                 }\r
229                         \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 prvInitialise_uIP( void )\r
240 {\r
241 TimerHandle_t xARPTimer, xPeriodicTimer;\r
242 uip_ipaddr_t xIPAddr;\r
243 const unsigned long ul_uIPEventQueueLength = 10UL;\r
244 \r
245         /* Initialise the uIP stack. */\r
246         uip_init();\r
247         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
248         uip_sethostaddr( &xIPAddr );\r
249         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
250         uip_setnetmask( &xIPAddr );\r
251         prvSetMACAddress();\r
252         httpd_init();\r
253 \r
254         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
255         xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );\r
256 \r
257         /* Create and start the uIP timers. */\r
258         xARPTimer = xTimerCreate(       "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
259                                                                 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */\r
260                                                                 pdTRUE, /* Autor-reload. */\r
261                                                                 ( void * ) uipARP_TIMER,\r
262                                                                 prvUIPTimerCallback\r
263                                                         );\r
264 \r
265         xPeriodicTimer = xTimerCreate(  "PeriodicTimer",\r
266                                                                         ( 50 / portTICK_PERIOD_MS ),\r
267                                                                         pdTRUE, /* Autor-reload. */\r
268                                                                         ( void * ) uipPERIODIC_TIMER,\r
269                                                                         prvUIPTimerCallback\r
270                                                                 );\r
271 \r
272         configASSERT( xARPTimer );\r
273         configASSERT( xPeriodicTimer );\r
274 \r
275         xTimerStart( xARPTimer, portMAX_DELAY );\r
276         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvUIPTimerCallback( TimerHandle_t xTimer )\r
281 {\r
282 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
283 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
284 \r
285         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
286         block. */\r
287         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
288         {\r
289                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
290                                                                         break;\r
291 \r
292                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
293                                                                         break;\r
294 \r
295                 default                                 :       /* Should not get here. */\r
296                                                                         break;\r
297         }\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 static void prvSetMACAddress( void )\r
302 {\r
303 struct uip_eth_addr xAddr;\r
304 \r
305         /* Configure the MAC address in the uIP stack. */\r
306         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
307         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
308         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
309         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
310         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
311         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
312         uip_setethaddr( xAddr );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 void vApplicationProcessFormInput( char *pcInputString )\r
317 {\r
318 char *c;\r
319 \r
320         /* Only interested in processing form input if this is the IO page. */\r
321         c = strstr( pcInputString, "io.shtml" );\r
322         \r
323         if( c )\r
324         {\r
325                 /* Is there a command in the string? */\r
326                 c = strstr( pcInputString, "?" );\r
327             if( c )\r
328             {\r
329                         /* Turn the LED's on or off in accordance with the check box status. */\r
330                         if( strstr( c, "LED0=1" ) != NULL )\r
331                         {\r
332                                 /* Turn the LEDs on. */\r
333                                 vParTestSetLED( 7, 1 );\r
334                                 vParTestSetLED( 8, 1 );\r
335                                 vParTestSetLED( 9, 1 );\r
336                                 vParTestSetLED( 10, 1 );\r
337                         }\r
338                         else\r
339                         {\r
340                                 /* Turn the LEDs off. */\r
341                                 vParTestSetLED( 7, 0 );\r
342                                 vParTestSetLED( 8, 0 );\r
343                                 vParTestSetLED( 9, 0 );\r
344                                 vParTestSetLED( 10, 0 );\r
345                         }\r
346             }\r
347                 else\r
348                 {\r
349                         /* Commands to turn LEDs off are not always explicit. */\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 }\r
357 \r