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