]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_LPC2368_Rowley/webserver/uIP_Task.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / ARM7_LPC2368_Rowley / webserver / 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 /* Standard includes. */
70 #include <string.h>
71
72 /* Scheduler includes. */
73 #include "FreeRTOS.h"
74 #include "task.h"
75 #include "semphr.h"
76
77 /* uip includes. */
78 #include "uip.h"
79 #include "uip_arp.h"
80 #include "httpd.h"
81 #include "timer.h"
82 #include "clock-arch.h"
83
84 /* Demo includes. */
85 #include "emac.h"
86 #include "partest.h"
87
88 /*-----------------------------------------------------------*/
89
90 /* MAC address configuration. */
91 #define uipMAC_ADDR0    0x00
92 #define uipMAC_ADDR1    0x12
93 #define uipMAC_ADDR2    0x13
94 #define uipMAC_ADDR3    0x10
95 #define uipMAC_ADDR4    0x15
96 #define uipMAC_ADDR5    0x11
97
98 /* IP address configuration. */
99 #define uipIP_ADDR0             192
100 #define uipIP_ADDR1             168
101 #define uipIP_ADDR2             0
102 #define uipIP_ADDR3             200     
103
104 /* How long to wait before attempting to connect the MAC again. */
105 #define uipINIT_WAIT    200
106
107 /* Shortcut to the header within the Rx buffer. */
108 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
109
110 /* Standard constant. */
111 #define uipTOTAL_FRAME_HEADER_SIZE      54
112
113 /*-----------------------------------------------------------*/
114
115 /* 
116  * Send the uIP buffer to the MAC. 
117  */
118 static void prvENET_Send(void);
119
120 /*
121  * Setup the MAC address in the MAC itself, and in the uIP stack.
122  */
123 static void prvSetMACAddress( void );
124
125 /*
126  * Port functions required by the uIP stack.
127  */
128 void clock_init( void );
129 clock_time_t clock_time( void );
130
131 /*-----------------------------------------------------------*/
132
133 /* The semaphore used by the ISR to wake the uIP task. */
134 extern SemaphoreHandle_t xEMACSemaphore;
135
136 /*-----------------------------------------------------------*/
137
138 void clock_init(void)
139 {
140         /* This is done when the scheduler starts. */
141 }
142 /*-----------------------------------------------------------*/
143
144 clock_time_t clock_time( void )
145 {
146         return xTaskGetTickCount();
147 }
148 /*-----------------------------------------------------------*/
149
150 void vuIP_Task( void *pvParameters )
151 {
152 portBASE_TYPE i;
153 uip_ipaddr_t xIPAddr;
154 struct timer periodic_timer, arp_timer;
155 extern void ( vEMAC_ISR_Wrapper )( void );
156
157         /* Create the semaphore used by the ISR to wake this task. */
158         vSemaphoreCreateBinary( xEMACSemaphore );
159         
160         /* Initialise the uIP stack. */
161         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
162         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
163         uip_init();
164         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
165         uip_sethostaddr( xIPAddr );
166         httpd_init();
167
168         /* Initialise the MAC. */
169         while( Init_EMAC() != pdPASS )
170     {
171         vTaskDelay( uipINIT_WAIT );
172     }
173
174         portENTER_CRITICAL();
175         {
176         IntEnable = INT_RX_DONE;
177         VICIntEnable |= 0x00200000;
178         VICVectAddr21 = ( long ) vEMAC_ISR_Wrapper;
179                 prvSetMACAddress();
180         }
181         portEXIT_CRITICAL();
182         
183
184         for( ;; )
185         {
186                 /* Is there received data ready to be processed? */
187                 uip_len = uiGetEMACRxData( uip_buf );
188                 
189                 if( uip_len > 0 )
190                 {
191                         /* Standard uIP loop taken from the uIP manual. */
192                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
193                         {
194                                 uip_arp_ipin();
195                                 uip_input();
196
197                                 /* If the above function invocation resulted in data that 
198                                 should be sent out on the network, the global variable 
199                                 uip_len is set to a value > 0. */
200                                 if( uip_len > 0 )
201                                 {
202                                         uip_arp_out();
203                                         prvENET_Send();
204                                 }
205                         }
206                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
207                         {
208                                 uip_arp_arpin();
209
210                                 /* If the above function invocation resulted in data that 
211                                 should be sent out on the network, the global variable 
212                                 uip_len is set to a value > 0. */
213                                 if( uip_len > 0 )
214                                 {
215                                         prvENET_Send();
216                                 }
217                         }
218                 }
219                 else
220                 {
221                         if( timer_expired( &periodic_timer ) )
222                         {
223                                 timer_reset( &periodic_timer );
224                                 for( i = 0; i < UIP_CONNS; i++ )
225                                 {
226                                         uip_periodic( i );
227         
228                                         /* If the above function invocation resulted in data that 
229                                         should be sent out on the network, the global variable 
230                                         uip_len is set to a value > 0. */
231                                         if( uip_len > 0 )
232                                         {
233                                                 uip_arp_out();
234                                                 prvENET_Send();
235                                         }
236                                 }       
237         
238                                 /* Call the ARP timer function every 10 seconds. */
239                                 if( timer_expired( &arp_timer ) )
240                                 {
241                                         timer_reset( &arp_timer );
242                                         uip_arp_timer();
243                                 }
244                         }
245                         else
246                         {                       
247                                 /* We did not receive a packet, and there was no periodic
248                                 processing to perform.  Block for a fixed period.  If a packet
249                                 is received during this period we will be woken by the ISR
250                                 giving us the Semaphore. */
251                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );                       
252                         }
253                 }
254         }
255 }
256 /*-----------------------------------------------------------*/
257
258 static void prvENET_Send(void)
259 {
260     RequestSend();
261
262     /* Copy the header into the Tx buffer. */
263     CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
264     if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
265     {
266         CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
267     }
268
269     DoSend_EMAC( uip_len );
270
271     RequestSend();
272
273     /* Copy the header into the Tx buffer. */
274     CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
275     if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
276     {
277         CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
278     }
279
280     DoSend_EMAC( uip_len );
281 }
282 /*-----------------------------------------------------------*/
283
284 static void prvSetMACAddress( void )
285 {
286 struct uip_eth_addr xAddr;
287
288         /* Configure the MAC address in the uIP stack. */
289         xAddr.addr[ 0 ] = uipMAC_ADDR0;
290         xAddr.addr[ 1 ] = uipMAC_ADDR1;
291         xAddr.addr[ 2 ] = uipMAC_ADDR2;
292         xAddr.addr[ 3 ] = uipMAC_ADDR3;
293         xAddr.addr[ 4 ] = uipMAC_ADDR4;
294         xAddr.addr[ 5 ] = uipMAC_ADDR5;
295         uip_setethaddr( xAddr );
296 }
297 /*-----------------------------------------------------------*/
298
299 void vApplicationProcessFormInput( char *pcInputString, portBASE_TYPE xInputLength )
300 {
301 char *c, *pcText;
302 static char cMessageForDisplay[ 32 ];
303 extern QueueHandle_t xLCDQueue;
304 xLCDMessage xLCDMessage;
305
306         /* Process the form input sent by the IO page of the served HTML. */
307
308         c = strstr( pcInputString, "?" );
309     if( c )
310     {
311                 /* Turn LED's on or off in accordance with the check box status. */
312                 if( strstr( c, "LED0=1" ) != NULL )
313                 {
314                         vParTestSetLED( 5, 0 );
315                 }
316                 else
317                 {
318                         vParTestSetLED( 5, 1 );
319                 }               
320                 
321                 if( strstr( c, "LED1=1" ) != NULL )
322                 {
323                         vParTestSetLED( 6, 0 );
324                 }
325                 else
326                 {
327                         vParTestSetLED( 6, 1 );
328                 }               
329
330                 if( strstr( c, "LED2=1" ) != NULL )
331                 {
332                         vParTestSetLED( 7, 0 );
333                 }
334                 else
335                 {
336                         vParTestSetLED( 7, 1 );
337                 }
338
339                 /* Find the start of the text to be displayed on the LCD. */
340         pcText = strstr( c, "LCD=" );
341         pcText += strlen( "LCD=" );
342
343         /* Terminate the file name for further processing within uIP. */
344         *c = 0x00;
345
346         /* Terminate the LCD string. */
347         c = strstr( pcText, " " );
348         if( c != NULL )
349         {
350             *c = 0x00;
351         }
352
353         /* Add required spaces. */
354         while( ( c = strstr( pcText, "+" ) ) != NULL )
355         {
356             *c = ' ';
357         }
358     
359         /* Write the message to the LCD. */
360                 strcpy( cMessageForDisplay, pcText );
361                 xLCDMessage.xColumn = 0;
362                 xLCDMessage.pcMessage = cMessageForDisplay;
363         xQueueSend( xLCDQueue, &xLCDMessage, portMAX_DELAY );
364     }
365 }
366