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