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