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