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