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