]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_A2F200_IAR_and_Keil/uIP_Task.c
Create directory structure to hold the (not yet created) Keil and IAR demo projects...
[freertos] / Demo / CORTEX_A2F200_IAR_and_Keil / uIP_Task.c
1 unsigned long ulRxed = 0, ulRxISR = 0, ulTxed = 0, ulTxISR = 0;\r
2 /*\r
3     FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
4         \r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
9      *    Complete, revised, and edited pdf reference manuals are also       *\r
10      *    available.                                                         *\r
11      *                                                                       *\r
12      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
13      *    ensuring you get running as quickly as possible and with an        *\r
14      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
15      *    the FreeRTOS project to continue with its mission of providing     *\r
16      *    professional grade, cross platform, de facto standard solutions    *\r
17      *    for microcontrollers - completely free of charge!                  *\r
18      *                                                                       *\r
19      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
20      *                                                                       *\r
21      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
22      *                                                                       *\r
23     ***************************************************************************\r
24 \r
25 \r
26     This file is part of the FreeRTOS distribution.\r
27 \r
28     FreeRTOS is free software; you can redistribute it and/or modify it under\r
29     the terms of the GNU General Public License (version 2) as published by the\r
30     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
31     >>>NOTE<<< The modification to the GPL is included to allow you to\r
32     distribute a combined work that includes FreeRTOS without being obliged to\r
33     provide the source code for proprietary components outside of the FreeRTOS\r
34     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
35     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
36     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
37     more details. You should have received a copy of the GNU General Public\r
38     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
39     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
40     by writing to Richard Barry, contact details for whom are available on the\r
41     FreeRTOS WEB site.\r
42 \r
43     1 tab == 4 spaces!\r
44 \r
45     http://www.FreeRTOS.org - Documentation, latest information, license and\r
46     contact details.\r
47 \r
48     http://www.SafeRTOS.com - A version that is certified for use in safety\r
49     critical systems.\r
50 \r
51     http://www.OpenRTOS.com - Commercial support, development, porting,\r
52     licensing and training services.\r
53 */\r
54 \r
55 /* Standard includes. */\r
56 #include <string.h>\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 #include "timers.h"\r
63 \r
64 /* uip includes. */\r
65 #include "net/uip.h"\r
66 #include "net/uip_arp.h"\r
67 #include "apps/httpd/httpd.h"\r
68 #include "sys/timer.h"\r
69 #include "net/clock-arch.h"\r
70 \r
71 /* Demo includes. */\r
72 #include "ParTest.h"\r
73 \r
74 /* Hardware driver includes. */\r
75 #include "mss_ethernet_mac_regs.h"\r
76 #include "mss_ethernet_mac.h"\r
77 \r
78 /* The buffer used by the uIP stack to both receive and send.  This points to\r
79 one of the Ethernet buffers when its actually in use. */\r
80 extern unsigned char *uip_buf;\r
81 \r
82 static const unsigned char ucMACAddress[] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
83 \r
84 #define uipARP_TIMER                            0\r
85 #define uipPERIODIC_TIMER                       1\r
86 \r
87 #define uipEVENT_QUEUE_LENGTH           10\r
88 \r
89 #define uipETHERNET_RX_EVENT            0x01UL\r
90 #define uipETHERNET_TX_EVENT            0x02UL\r
91 #define uipARP_TIMER_EVENT                      0x04UL\r
92 #define uipPERIODIC_TIMER_EVENT         0x08UL\r
93 #define uipAPPLICATION_SEND_EVENT       0x10UL\r
94 \r
95 #define uipDONT_BLOCK                           0UL\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* How long to wait before attempting to connect the MAC again. */\r
100 #define uipINIT_WAIT    ( 100 / portTICK_RATE_MS )\r
101 \r
102 /* Shortcut to the header within the Rx buffer. */\r
103 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
104 \r
105 /* Standard constant. */\r
106 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /*\r
111  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
112  */\r
113 static void prvSetMACAddress( void );\r
114 \r
115 /*\r
116  * Perform any uIP initialisation required to ready the stack for http\r
117  * processing.
118  */\r
119 static void prvInitialise_uIP( void );\r
120 \r
121 /*\r
122  * Handles Ethernet interrupt events.
123  */\r
124 static void prvEMACEventListener( unsigned long ulISREvents );\r
125 \r
126 static void prvUIPTimerCallback( xTimerHandle xTimer );\r
127 \r
128 /*\r
129  * Initialise the MAC hardware.
130  */\r
131 static void prvInitEmac( void );\r
132 \r
133 void vEMACWrite( void );\r
134 \r
135 long lEMACWaitForLink( void );\r
136 \r
137 /*\r
138  * Port functions required by the uIP stack.\r
139  */\r
140 void clock_init( void );\r
141 clock_time_t clock_time( void );\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* The queue used to send TCP/IP events to the uIP stack. */\r
146 xQueueHandle xEMACEventQueue = NULL;\r
147 \r
148 static unsigned long ulUIP_Events = 0UL;\r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void clock_init(void)\r
153 {\r
154         /* This is done when the scheduler starts. */\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 clock_time_t clock_time( void )\r
159 {\r
160         return xTaskGetTickCount();\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vuIP_Task( void *pvParameters )\r
165 {\r
166 portBASE_TYPE i;\r
167 unsigned long ulNewEvent;\r
168 \r
169         ( void ) pvParameters;\r
170 \r
171         /* Initialise the uIP stack, configuring for web server usage. */\r
172         prvInitialise_uIP();\r
173 \r
174         /* Initialise the MAC. */\r
175         prvInitEmac();\r
176 \r
177         for( ;; )\r
178         {\r
179                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )\r
180                 {\r
181                         ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
182 \r
183                         /* Is there received data ready to be processed? */\r
184                         uip_len = MSS_MAC_rx_packet();\r
185 \r
186                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
187                         {\r
188                                 ulRxed++;\r
189                                 /* Standard uIP loop taken from the uIP manual. */\r
190                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
191                                 {\r
192                                         uip_arp_ipin();\r
193                                         uip_input();\r
194 \r
195                                         /* If the above function invocation resulted in data that\r
196                                         should be sent out on the network, the global variable\r
197                                         uip_len is set to a value > 0. */\r
198                                         if( uip_len > 0 )\r
199                                         {\r
200                                                 uip_arp_out();\r
201                                                 vEMACWrite();\r
202                                         }\r
203                                 }\r
204                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
205                                 {\r
206                                         uip_arp_arpin();\r
207 \r
208                                         /* If the above function invocation resulted in data that\r
209                                         should be sent out on the network, the global variable\r
210                                         uip_len is set to a value > 0. */\r
211                                         if( uip_len > 0 )\r
212                                         {\r
213                                                 vEMACWrite();\r
214                                         }\r
215                                 }\r
216                         }\r
217                 }\r
218 \r
219                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
220                 {\r
221                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\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                 /* Call the ARP timer function every 10 seconds. */\r
239                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
240                 {\r
241                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
242                         uip_arp_timer();\r
243                 }\r
244 \r
245                 if( ulUIP_Events == pdFALSE )\r
246                 {\r
247                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
248                         ulUIP_Events |= ulNewEvent;\r
249                 }\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void prvSetMACAddress( void )\r
255 {\r
256 struct uip_eth_addr xAddr;\r
257 \r
258         /* Configure the MAC address in the uIP stack. */\r
259         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
260         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
261         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
262         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
263         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
264         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
265         uip_setethaddr( xAddr );\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 void vApplicationProcessFormInput( char *pcInputString )\r
270 {\r
271 char *c;\r
272 \r
273         /* Only interested in processing form input if this is the IO page. */\r
274         c = strstr( pcInputString, "io.shtml" );\r
275         \r
276         if( c )\r
277         {\r
278                 /* Is there a command in the string? */\r
279                 c = strstr( pcInputString, "?" );\r
280             if( c )\r
281             {\r
282                         /* Turn the LED's on or off in accordance with the check box status. */\r
283                         if( strstr( c, "LED0=1" ) != NULL )\r
284                         {\r
285                                 /* Turn the LEDs on. */\r
286                                 vParTestSetLED( 7, 1 );\r
287                                 vParTestSetLED( 8, 1 );\r
288                                 vParTestSetLED( 9, 1 );\r
289                                 vParTestSetLED( 10, 1 );\r
290                         }\r
291                         else\r
292                         {\r
293                                 /* Turn the LEDs off. */\r
294                                 vParTestSetLED( 7, 0 );\r
295                                 vParTestSetLED( 8, 0 );\r
296                                 vParTestSetLED( 9, 0 );\r
297                                 vParTestSetLED( 10, 0 );\r
298                         }\r
299             }\r
300                 else\r
301                 {\r
302                         /* Commands to turn LEDs off are not always explicit. */\r
303                         vParTestSetLED( 7, 0 );\r
304                         vParTestSetLED( 8, 0 );\r
305                         vParTestSetLED( 9, 0 );\r
306                         vParTestSetLED( 10, 0 );\r
307                 }\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void prvInitialise_uIP( void )\r
313 {\r
314 uip_ipaddr_t xIPAddr;\r
315 xTimerHandle xARPTimer, xPeriodicTimer;\r
316 \r
317         uip_init();\r
318         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
319         uip_sethostaddr( &xIPAddr );\r
320         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
321         uip_setnetmask( &xIPAddr );\r
322         prvSetMACAddress();\r
323         httpd_init();\r
324 \r
325         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
326         xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );\r
327 \r
328         /* Create and start the uIP timers. */\r
329         xARPTimer = xTimerCreate(       ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
330                                                                 ( 500 / portTICK_RATE_MS ), /* Timer period. */\r
331                                                                 pdTRUE, /* Autor-reload. */\r
332                                                                 ( void * ) uipARP_TIMER,\r
333                                                                 prvUIPTimerCallback\r
334                                                         );\r
335 \r
336         xPeriodicTimer = xTimerCreate(  ( const signed char * const ) "PeriodicTimer",\r
337                                                                         ( 5000 / portTICK_RATE_MS ),\r
338                                                                         pdTRUE, /* Autor-reload. */\r
339                                                                         ( void * ) uipPERIODIC_TIMER,\r
340                                                                         prvUIPTimerCallback\r
341                                                                 );\r
342 \r
343         configASSERT( xARPTimer );\r
344         configASSERT( xPeriodicTimer );\r
345 \r
346         xTimerStart( xARPTimer, portMAX_DELAY );\r
347         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
348 }\r
349 /*-----------------------------------------------------------*/\r
350 \r
351 static void prvEMACEventListener( unsigned long ulISREvents )\r
352 {\r
353 long lHigherPriorityTaskWoken = pdFALSE;\r
354 unsigned long ulUIPEvents = 0UL;\r
355 \r
356         configASSERT( xEMACEventQueue );\r
357 \r
358         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )\r
359         {\r
360                 ulTxISR++;\r
361                 MSS_MAC_TxBufferCompleted();\r
362         }\r
363 \r
364         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )\r
365         {\r
366                 ulRxISR++;\r
367                 /* Wake the uIP task as new data has arrived. */\r
368                 ulUIPEvents |= uipETHERNET_RX_EVENT;\r
369         }\r
370 \r
371         if( ulUIPEvents != 0UL )\r
372         {\r
373                 xQueueSendFromISR( xEMACEventQueue, &ulUIPEvents, &lHigherPriorityTaskWoken );\r
374         }\r
375 \r
376         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 static void prvInitEmac( void )\r
381 {\r
382 const unsigned char ucPHYAddress = 1;\r
383 \r
384         MSS_MAC_init( ucPHYAddress );\r
385 \r
386         MSS_MAC_set_callback( prvEMACEventListener );\r
387 \r
388     /* Setup the EMAC and the NVIC for MAC interrupts. */\r
389     NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
390     NVIC_EnableIRQ( EthernetMAC_IRQn );\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 void vEMACWrite( void )\r
395 {\r
396 const long lMaxAttempts = 10;\r
397 long lAttempt;\r
398 const portTickType xShortDelay = ( 10 / portTICK_RATE_MS );\r
399 \r
400         for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )\r
401         {\r
402                 if( MSS_MAC_tx_packet( uip_len ) != 0 )\r
403                 {\r
404                         ulTxed++;\r
405                         break;\r
406                 }\r
407                 else\r
408                 {\r
409                         vTaskDelay( xShortDelay );\r
410                 }\r
411         }\r
412 }\r
413 /*-----------------------------------------------------------*/\r
414 \r
415 long lEMACWaitForLink( void )\r
416 {\r
417 long lReturn = pdFAIL;\r
418 unsigned long ulStatus;\r
419 \r
420         ulStatus = MSS_MAC_link_status();\r
421         if( ( ulStatus & ( unsigned long ) MSS_MAC_LINK_STATUS_LINK ) != 0UL )\r
422         {\r
423                 lReturn = pdPASS;\r
424         }\r
425 \r
426         return lReturn;\r
427 }\r
428 /*-----------------------------------------------------------*/\r
429 \r
430 static void prvUIPTimerCallback( xTimerHandle xTimer )\r
431 {\r
432 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
433 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
434 \r
435         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
436         block. */\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