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