]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A2F200_SoftConsole/uIP_Task.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_A2F200_SoftConsole / uIP_Task.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <string.h>\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 #include "queue.h"\r
36 #include "timers.h"\r
37 \r
38 /* uip includes. */\r
39 #include "net/uip.h"\r
40 #include "net/uip_arp.h"\r
41 #include "apps/httpd/httpd.h"\r
42 #include "sys/timer.h"\r
43 #include "net/clock-arch.h"\r
44 \r
45 /* Demo includes. */\r
46 #include "ParTest.h"\r
47 \r
48 /* Hardware driver includes. */\r
49 #include "mss_ethernet_mac_regs.h"\r
50 #include "mss_ethernet_mac.h"\r
51 \r
52 /* The buffer used by the uIP stack to both receive and send.  In this case,\r
53 because the Ethernet driver has been modified to be zero copy - the uip_buf\r
54 variable is just a pointer to an Ethernet buffer, and not a buffer in its own\r
55 right. */\r
56 extern unsigned char *uip_buf;\r
57 \r
58 /* The ARP timer and the periodic timer share a callback function, so the\r
59 respective timer IDs are used to determine which timer actually expired.  These\r
60 constants are assigned to the timer IDs. */\r
61 #define uipARP_TIMER                            0\r
62 #define uipPERIODIC_TIMER                       1\r
63 \r
64 /* The length of the queue used to send events from timers or the Ethernet\r
65 driver to the uIP stack. */\r
66 #define uipEVENT_QUEUE_LENGTH           10\r
67 \r
68 /* A block time of zero simply means "don't block". */\r
69 #define uipDONT_BLOCK                           0UL\r
70 \r
71 /* How long to wait before attempting to connect the MAC again. */\r
72 #define uipINIT_WAIT    ( 100 / portTICK_PERIOD_MS )\r
73 \r
74 /* Shortcut to the header within the Rx buffer. */\r
75 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
76 \r
77 /* Standard constant. */\r
78 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /*\r
83  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
84  */\r
85 static void prvSetMACAddress( void );\r
86 \r
87 /*\r
88  * Perform any uIP initialisation required to ready the stack for http\r
89  * processing.\r
90  */\r
91 static void prvInitialise_uIP( void );\r
92 \r
93 /*\r
94  * Handles Ethernet interrupt events.\r
95  */\r
96 static void prvEMACEventListener( unsigned long ulISREvents );\r
97 \r
98 /*\r
99  * The callback function that is assigned to both the periodic timer and the\r
100  * ARP timer.\r
101  */\r
102 static void prvUIPTimerCallback( TimerHandle_t xTimer );\r
103 \r
104 /*\r
105  * Initialise the MAC hardware.\r
106  */\r
107 static void prvInitEmac( void );\r
108 \r
109 /*\r
110  * Write data to the Ethener.  Note that this actually writes data twice for the\r
111  * to get around delayed ack issues when communicating with a non real-time\r
112  * peer (for example, a Windows machine).\r
113  */\r
114 void vEMACWrite( void );\r
115 \r
116 /*\r
117  * Port functions required by the uIP stack.\r
118  */\r
119 clock_time_t clock_time( void );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* The queue used to send TCP/IP events to the uIP stack. */\r
124 QueueHandle_t xEMACEventQueue = NULL;\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 clock_time_t clock_time( void )\r
129 {\r
130         return xTaskGetTickCount();\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vuIP_Task( void *pvParameters )\r
135 {\r
136 portBASE_TYPE i;\r
137 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;\r
138 long lPacketLength;\r
139 \r
140         /* Just to prevent compiler warnings about the unused parameter. */\r
141         ( void ) pvParameters;\r
142 \r
143         /* Initialise the uIP stack, configuring for web server usage. */\r
144         prvInitialise_uIP();\r
145 \r
146         /* Initialise the MAC and PHY. */\r
147         prvInitEmac();\r
148 \r
149         for( ;; )\r
150         {\r
151                 /* Is there received data ready to be processed? */\r
152                 lPacketLength = MSS_MAC_rx_packet();\r
153 \r
154                 /* Statements to be executed if data has been received on the Ethernet. */\r
155                 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )\r
156                 {\r
157                         uip_len = ( u16_t ) lPacketLength;\r
158 \r
159                         /* Standard uIP loop taken from the uIP manual. */\r
160                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
161                         {\r
162                                 uip_arp_ipin();\r
163                                 uip_input();\r
164 \r
165                                 /* If the above function invocation resulted in data that\r
166                                 should be sent out on the network, the global variable\r
167                                 uip_len is set to a value > 0. */\r
168                                 if( uip_len > 0 )\r
169                                 {\r
170                                         uip_arp_out();\r
171                                         vEMACWrite();\r
172                                 }\r
173                         }\r
174                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
175                         {\r
176                                 uip_arp_arpin();\r
177 \r
178                                 /* If the above function invocation resulted in data that\r
179                                 should be sent out on the network, the global variable\r
180                                 uip_len is set to a value > 0. */\r
181                                 if( uip_len > 0 )\r
182                                 {\r
183                                         vEMACWrite();\r
184                                 }\r
185                         }\r
186                 }\r
187                 else\r
188                 {\r
189                         /* Clear the RX event latched in ulUIP_Events - if one was latched. */\r
190                         ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
191                 }\r
192 \r
193                 /* Statements to be executed if the TCP/IP period timer has expired. */\r
194                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
195                 {\r
196                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
197 \r
198                         if( uip_buf != NULL )\r
199                         {\r
200                                 for( i = 0; i < UIP_CONNS; i++ )\r
201                                 {\r
202                                         uip_periodic( i );\r
203 \r
204                                         /* If the above function invocation resulted in data that\r
205                                         should be sent out on the network, the global variable\r
206                                         uip_len is set to a value > 0. */\r
207                                         if( uip_len > 0 )\r
208                                         {\r
209                                                 uip_arp_out();\r
210                                                 vEMACWrite();\r
211                                         }\r
212                                 }\r
213                         }\r
214                 }\r
215 \r
216                 /* Statements to be executed if the ARP timer has expired. */\r
217                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
218                 {\r
219                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
220                         uip_arp_timer();\r
221                 }\r
222 \r
223                 /* If all latched events have been cleared - block until another event\r
224                 occurs. */\r
225                 if( ulUIP_Events == pdFALSE )\r
226                 {\r
227                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
228                         ulUIP_Events |= ulNewEvent;\r
229                 }\r
230         }\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void prvSetMACAddress( void )\r
235 {\r
236 struct uip_eth_addr xAddr;\r
237 \r
238         /* Configure the MAC address in the uIP stack. */\r
239         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
240         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
241         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
242         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
243         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
244         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
245         uip_setethaddr( xAddr );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvInitialise_uIP( void )\r
250 {\r
251 uip_ipaddr_t xIPAddr;\r
252 TimerHandle_t xARPTimer, xPeriodicTimer;\r
253 \r
254         uip_init();\r
255         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
256         uip_sethostaddr( &xIPAddr );\r
257         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
258         uip_setnetmask( &xIPAddr );\r
259         prvSetMACAddress();\r
260         httpd_init();\r
261 \r
262         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
263         xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );\r
264 \r
265         /* Create and start the uIP timers. */\r
266         xARPTimer = xTimerCreate(       "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
267                                                                 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */\r
268                                                                 pdTRUE, /* Autor-reload. */\r
269                                                                 ( void * ) uipARP_TIMER,\r
270                                                                 prvUIPTimerCallback\r
271                                                         );\r
272 \r
273         xPeriodicTimer = xTimerCreate(  "PeriodicTimer",\r
274                                                                         ( 500UL / portTICK_PERIOD_MS ),\r
275                                                                         pdTRUE, /* Autor-reload. */\r
276                                                                         ( void * ) uipPERIODIC_TIMER,\r
277                                                                         prvUIPTimerCallback\r
278                                                                 );\r
279 \r
280         /* Sanity check that the timers were indeed created. */\r
281         configASSERT( xARPTimer );\r
282         configASSERT( xPeriodicTimer );\r
283 \r
284         /* These commands will block indefinitely until they succeed, so there is\r
285         no point in checking their return values. */\r
286         xTimerStart( xARPTimer, portMAX_DELAY );\r
287         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 static void prvEMACEventListener( unsigned long ulISREvents )\r
292 {\r
293 long lHigherPriorityTaskWoken = pdFALSE;\r
294 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;\r
295 \r
296         /* Sanity check that the event queue was indeed created. */\r
297         configASSERT( xEMACEventQueue );\r
298 \r
299         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )\r
300         {\r
301                 /* An Ethernet Tx event has occurred. */\r
302                 MSS_MAC_FreeTxBuffers();\r
303         }\r
304 \r
305         if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )\r
306         {\r
307                 /* An Ethernet Rx event has occurred. */\r
308                 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );\r
309         }\r
310 \r
311         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvInitEmac( void )\r
316 {\r
317 const unsigned char ucPHYAddress = 1;\r
318 \r
319         /* Initialise the MAC and PHY hardware. */\r
320         MSS_MAC_init( ucPHYAddress );\r
321 \r
322         /* Register the event listener.  The Ethernet interrupt handler will call\r
323         this listener whenever an Rx or a Tx interrupt occurs. */\r
324         MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );\r
325 \r
326     /* Setup the EMAC and the NVIC for MAC interrupts. */\r
327     NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
328     NVIC_EnableIRQ( EthernetMAC_IRQn );\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 void vEMACWrite( void )\r
333 {\r
334 const long lMaxAttempts = 10;\r
335 long lAttempt;\r
336 const TickType_t xShortDelay = ( 5 / portTICK_PERIOD_MS );\r
337 \r
338         /* Try to send data to the Ethernet.  Keep trying for a while if data cannot\r
339         be sent immediately.  Note that this will actually cause the data to be sent\r
340         twice to get around delayed ACK problems when communicating with non real-\r
341         time TCP/IP stacks (such as a Windows machine). */\r
342         for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )\r
343         {\r
344                 if( MSS_MAC_tx_packet( uip_len ) != 0 )\r
345                 {\r
346                         break;\r
347                 }\r
348                 else\r
349                 {\r
350                         vTaskDelay( xShortDelay );\r
351                 }\r
352         }\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 static void prvUIPTimerCallback( TimerHandle_t xTimer )\r
357 {\r
358 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
359 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
360 \r
361         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
362         block.  As this callback is assigned to both the ARP and Periodic timers, the\r
363         first thing to do is ascertain which timer it was that actually expired. */\r
364         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
365         {\r
366                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
367                                                                         break;\r
368 \r
369                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
370                                                                         break;\r
371 \r
372                 default                                 :       /* Should not get here. */\r
373                                                                         break;\r
374         }\r
375 }\r
376 /*-----------------------------------------------------------*/\r
377 \r
378 void vApplicationProcessFormInput( char *pcInputString )\r
379 {\r
380 char *c;\r
381 \r
382         /* Only interested in processing form input if this is the IO page. */\r
383         c = strstr( pcInputString, "io.shtml" );\r
384 \r
385         if( c )\r
386         {\r
387                 /* Is there a command in the string? */\r
388                 c = strstr( pcInputString, "?" );\r
389             if( c )\r
390             {\r
391                         /* Turn the LED's on or off in accordance with the check box status. */\r
392                         if( strstr( c, "LED0=1" ) != NULL )\r
393                         {\r
394                                 /* Turn the LEDs on. */\r
395                                 vParTestSetLED( 3, 1 );\r
396                                 vParTestSetLED( 4, 1 );\r
397                         }\r
398                         else\r
399                         {\r
400                                 /* Turn the LEDs off. */\r
401                                 vParTestSetLED( 3, 0 );\r
402                                 vParTestSetLED( 4, 0 );\r
403                         }\r
404             }\r
405                 else\r
406                 {\r
407                         /* Commands to turn LEDs off are not always explicit. */\r
408                         vParTestSetLED( 3, 0 );\r
409                         vParTestSetLED( 4, 0 );\r
410                 }\r
411         }\r
412 }\r
413 /*-----------------------------------------------------------*/\r