]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_A2F200_IAR_and_Keil/uIP_Task.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1391 1d2547de-c912-0410...
[freertos] / Demo / CORTEX_A2F200_IAR_and_Keil / uIP_Task.c
1 /*\r
2     FreeRTOS V7.0.0 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include <string.h>\r
56 \r
57 /* Scheduler includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "queue.h"\r
61 #include "timers.h"\r
62 \r
63 /* uip includes. */\r
64 #include "net/uip.h"\r
65 #include "net/uip_arp.h"\r
66 #include "apps/httpd/httpd.h"\r
67 #include "sys/timer.h"\r
68 #include "net/clock-arch.h"\r
69 \r
70 /* Demo includes. */\r
71 #include "ParTest.h"\r
72 \r
73 /* Hardware driver includes. */\r
74 #include "mss_ethernet_mac_regs.h"\r
75 #include "mss_ethernet_mac.h"\r
76 \r
77 /* The buffer used by the uIP stack to both receive and send.  In this case,\r
78 because the Ethernet driver has been modified to be zero copy - the uip_buf\r
79 variable is just a pointer to an Ethernet buffer, and not a buffer in its own\r
80 right. */\r
81 extern unsigned char *uip_buf;\r
82 \r
83 /* The ARP timer and the periodic timer share a callback function, so the\r
84 respective timer IDs are used to determine which timer actually expired.  These\r
85 constants are assigned to the timer IDs. */\r
86 #define uipARP_TIMER                            0\r
87 #define uipPERIODIC_TIMER                       1\r
88 \r
89 /* The length of the queue used to send events from timers or the Ethernet\r
90 driver to the uIP stack. */\r
91 #define uipEVENT_QUEUE_LENGTH           10\r
92 \r
93 /* A block time of zero simply means "don't block". */\r
94 #define uipDONT_BLOCK                           0UL\r
95 \r
96 /* How long to wait before attempting to connect the MAC again. */\r
97 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
98 \r
99 /* Shortcut to the header within the Rx buffer. */\r
100 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
101 \r
102 /* Standard constant. */\r
103 #define uipTOTAL_FRAME_HEADER_SIZE      54\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 required to ready the stack for http\r
114  * processing.\r
115  */\r
116 static void prvInitialise_uIP( void );\r
117 \r
118 /*\r
119  * Handles Ethernet interrupt events.\r
120  */\r
121 static void prvEMACEventListener( unsigned long ulISREvents );\r
122 \r
123 /*\r
124  * The callback function that is assigned to both the periodic timer and the\r
125  * ARP timer.\r
126  */\r
127 static void prvUIPTimerCallback( xTimerHandle xTimer );\r
128 \r
129 /*\r
130  * Initialise the MAC hardware.\r
131  */\r
132 static void prvInitEmac( void );\r
133 \r
134 /*\r
135  * Write data to the Ethener.  Note that this actually writes data twice for the\r
136  * to get around delayed ack issues when communicating with a non real-time\r
137  * peer (for example, a Windows machine).\r
138  */\r
139 void vEMACWrite( void );\r
140 \r
141 /*\r
142  * Port functions required by the uIP stack.\r
143  */\r
144 clock_time_t clock_time( void );\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 /* The queue used to send TCP/IP events to the uIP stack. */\r
149 xQueueHandle xEMACEventQueue = NULL;\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 clock_time_t clock_time( void )\r
154 {\r
155         return xTaskGetTickCount();\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vuIP_Task( void *pvParameters )\r
160 {\r
161 portBASE_TYPE i;\r
162 unsigned long ulNewEvent = 0UL;\r
163 unsigned long ulUIP_Events = 0UL;\r
164 \r
165         /* Just to prevent compiler warnings about the unused parameter. */\r
166         ( void ) pvParameters;\r
167 \r
168         /* Initialise the uIP stack, configuring for web server usage. */\r
169         prvInitialise_uIP();\r
170 \r
171         /* Initialise the MAC and PHY. */\r
172         prvInitEmac();\r
173 \r
174         for( ;; )\r
175         {\r
176                 /* Is there received data ready to be processed? */\r
177                 uip_len = MSS_MAC_rx_packet();\r
178 \r
179                 /* Statements to be executed if data has been received on the Ethernet. */\r
180                 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
181                 {\r
182                         /* Standard uIP loop taken from the uIP manual. */\r
183                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
184                         {\r
185                                 uip_arp_ipin();\r
186                                 uip_input();\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                                         uip_arp_out();\r
194                                         vEMACWrite();\r
195                                 }\r
196                         }\r
197                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
198                         {\r
199                                 uip_arp_arpin();\r
200 \r
201                                 /* If the above function invocation resulted in data that\r
202                                 should be sent out on the network, the global variable\r
203                                 uip_len is set to a value > 0. */\r
204                                 if( uip_len > 0 )\r
205                                 {\r
206                                         vEMACWrite();\r
207                                 }\r
208                         }\r
209                 }\r
210                 else\r
211                 {\r
212                         /* Clear the RX event latched in ulUIP_Events - if one was latched. */\r
213                         ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
214                 }\r
215 \r
216                 /* Statements to be executed if the TCP/IP period timer has expired. */\r
217                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
218                 {\r
219                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
220 \r
221                         if( uip_buf != NULL )\r
222                         {\r
223                                 for( i = 0; i < UIP_CONNS; i++ )\r
224                                 {\r
225                                         uip_periodic( i );\r
226         \r
227                                         /* If the above function invocation resulted in data that\r
228                                         should be sent out on the network, the global variable\r
229                                         uip_len is set to a value > 0. */\r
230                                         if( uip_len > 0 )\r
231                                         {\r
232                                                 uip_arp_out();\r
233                                                 vEMACWrite();\r
234                                         }\r
235                                 }\r
236                         }\r
237                 }\r
238 \r
239                 /* Statements to be executed if the ARP timer has expired. */\r
240                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
241                 {\r
242                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
243                         uip_arp_timer();\r
244                 }\r
245 \r
246                 /* If all latched events have been cleared - block until another event\r
247                 occurs. */\r
248                 if( ulUIP_Events == pdFALSE )\r
249                 {\r
250                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
251                         ulUIP_Events |= ulNewEvent;\r
252                 }\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvSetMACAddress( void )\r
258 {\r
259 struct uip_eth_addr xAddr;\r
260 \r
261         /* Configure the MAC address in the uIP stack. */\r
262         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
263         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
264         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
265         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
266         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
267         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
268         uip_setethaddr( xAddr );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 void vApplicationProcessFormInput( char *pcInputString )\r
273 {\r
274 char *c;\r
275 \r
276         /* Only interested in processing form input if this is the IO page. */\r
277         c = strstr( pcInputString, "io.shtml" );\r
278         \r
279         if( c )\r
280         {\r
281                 /* Is there a command in the string? */\r
282                 c = strstr( pcInputString, "?" );\r
283             if( c )\r
284             {\r
285                         /* Turn the LED's on or off in accordance with the check box status. */\r
286                         if( strstr( c, "LED0=1" ) != NULL )\r
287                         {\r
288                                 /* Turn the LEDs on. */\r
289                                 vParTestSetLED( 7, 1 );\r
290                                 vParTestSetLED( 8, 1 );\r
291                                 vParTestSetLED( 9, 1 );\r
292                                 vParTestSetLED( 10, 1 );\r
293                         }\r
294                         else\r
295                         {\r
296                                 /* Turn the LEDs off. */\r
297                                 vParTestSetLED( 7, 0 );\r
298                                 vParTestSetLED( 8, 0 );\r
299                                 vParTestSetLED( 9, 0 );\r
300                                 vParTestSetLED( 10, 0 );\r
301                         }\r
302             }\r
303                 else\r
304                 {\r
305                         /* Commands to turn LEDs off are not always explicit. */\r
306                         vParTestSetLED( 7, 0 );\r
307                         vParTestSetLED( 8, 0 );\r
308                         vParTestSetLED( 9, 0 );\r
309                         vParTestSetLED( 10, 0 );\r
310                 }\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvInitialise_uIP( void )\r
316 {\r
317 uip_ipaddr_t xIPAddr;\r
318 xTimerHandle xARPTimer, xPeriodicTimer;\r
319 \r
320         uip_init();\r
321         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
322         uip_sethostaddr( &xIPAddr );\r
323         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
324         uip_setnetmask( &xIPAddr );\r
325         prvSetMACAddress();\r
326         httpd_init();\r
327 \r
328         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
329         xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );\r
330 \r
331         /* Create and start the uIP timers. */\r
332         xARPTimer = xTimerCreate(       ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
333                                                                 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */\r
334                                                                 pdTRUE, /* Autor-reload. */\r
335                                                                 ( void * ) uipARP_TIMER,\r
336                                                                 prvUIPTimerCallback\r
337                                                         );\r
338 \r
339         xPeriodicTimer = xTimerCreate(  ( const signed char * const ) "PeriodicTimer",\r
340                                                                         ( 500UL / portTICK_RATE_MS ),\r
341                                                                         pdTRUE, /* Autor-reload. */\r
342                                                                         ( void * ) uipPERIODIC_TIMER,\r
343                                                                         prvUIPTimerCallback\r
344                                                                 );\r
345 \r
346         /* Sanity check that the timers were indeed created. */\r
347         configASSERT( xARPTimer );\r
348         configASSERT( xPeriodicTimer );\r
349 \r
350         /* These commands will block indefinitely until they succeed, so there is\r
351         no point in checking their return values. */\r
352         xTimerStart( xARPTimer, portMAX_DELAY );\r
353         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 static void prvEMACEventListener( unsigned long ulISREvents )\r
358 {\r
359 long lHigherPriorityTaskWoken = pdFALSE;\r
360 unsigned long ulUIPEvents = 0UL;\r
361 \r
362         /* Sanity check that the event queue was indeed created. */\r
363         configASSERT( xEMACEventQueue );\r
364 \r
365         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )\r
366         {\r
367                 /* An Ethernet Tx event has occurred. */\r
368                 MSS_MAC_CheckTxBufferStatus();\r
369         }\r
370 \r
371         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )\r
372         {\r
373                 /* An Ethernet Rx event has occurred. */\r
374                 ulUIPEvents |= uipETHERNET_RX_EVENT;\r
375         }\r
376 \r
377         if( ulUIPEvents != 0UL )\r
378         {\r
379                 /* Send any events that have occurred to the uIP stack (the uIP task in\r
380                 this case). */\r
381                 xQueueSendFromISR( xEMACEventQueue, &ulUIPEvents, &lHigherPriorityTaskWoken );\r
382         }\r
383 \r
384         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r
388 static void prvInitEmac( void )\r
389 {\r
390 const unsigned char ucPHYAddress = 1;\r
391 \r
392         /* Initialise the MAC and PHY hardware. */\r
393         MSS_MAC_init( ucPHYAddress );\r
394 \r
395         /* Register the event listener.  The Ethernet interrupt handler will call\r
396         this listener whenever an Rx or a Tx interrupt occurs. */\r
397         MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );\r
398 \r
399     /* Setup the EMAC and the NVIC for MAC interrupts. */\r
400     NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
401     NVIC_EnableIRQ( EthernetMAC_IRQn );\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 void vEMACWrite( void )\r
406 {\r
407 const long lMaxAttempts = 10;\r
408 long lAttempt;\r
409 const portTickType xShortDelay = ( 10 / portTICK_RATE_MS );\r
410 \r
411         /* Try to send data to the Ethernet.  Keep trying for a while if data cannot\r
412         be sent immediately.  Note that this will actually cause the data to be sent\r
413         twice to get around delayed ACK problems when communicating with non real-\r
414         time TCP/IP stacks (such as a Windows machine). */\r
415         for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )\r
416         {\r
417                 if( MSS_MAC_tx_packet( uip_len ) != 0 )\r
418                 {\r
419                         break;\r
420                 }\r
421                 else\r
422                 {\r
423                         vTaskDelay( xShortDelay );\r
424                 }\r
425         }\r
426 }\r
427 /*-----------------------------------------------------------*/\r
428 \r
429 static void prvUIPTimerCallback( xTimerHandle xTimer )\r
430 {\r
431 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
432 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
433 \r
434         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
435         block.  As this callback is assigned to both the ARP and Periodic timers, the\r
436         first thing to do is ascertain which timer it was that actually expired. */\r
437         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
438         {\r
439                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
440                                                                         break;\r
441 \r
442                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
443                                                                         break;\r
444 \r
445                 default                                 :       /* Should not get here. */\r
446                                                                         break;\r
447         }\r
448 }\r
449 /*-----------------------------------------------------------*/\r