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