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