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