]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LPC1768_GCC_RedSuite/src/webserver/uIP_Task.c
Correct interrupt priority assignment.
[freertos] / Demo / CORTEX_LPC1768_GCC_RedSuite / src / webserver / uIP_Task.c
1 /*\r
2         FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /* Standard includes. */\r
49 #include <string.h>\r
50 \r
51 /* Scheduler includes. */\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 #include "semphr.h"\r
55 \r
56 /* uip includes. */\r
57 #include "uip.h"\r
58 #include "uip_arp.h"\r
59 #include "httpd.h"\r
60 #include "timer.h"\r
61 #include "clock-arch.h"\r
62 \r
63 /* Demo includes. */\r
64 #include "EthDev_LPC17xx.h"\r
65 #include "EthDev.h"\r
66 #include "ParTest.h"\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* How long to wait before attempting to connect the MAC again. */\r
71 #define uipINIT_WAIT    ( 100 / portTICK_RATE_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  * Port functions required by the uIP stack.\r
88  */\r
89 void clock_init( void );\r
90 clock_time_t clock_time( void );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* The semaphore used by the ISR to wake the uIP task. */\r
95 xSemaphoreHandle xEMACSemaphore = NULL;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void clock_init(void)\r
100 {\r
101         /* This is done when the scheduler starts. */\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 clock_time_t clock_time( void )\r
106 {\r
107         return xTaskGetTickCount();\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 void vuIP_Task( void *pvParameters )\r
112 {\r
113 portBASE_TYPE i;\r
114 uip_ipaddr_t xIPAddr;\r
115 struct timer periodic_timer, arp_timer;\r
116 extern void ( vEMAC_ISR_Wrapper )( void );\r
117 \r
118         ( void ) pvParameters;\r
119 \r
120         /* Initialise the uIP stack. */\r
121         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
122         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
123         uip_init();\r
124         uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
125         uip_sethostaddr( xIPAddr );\r
126         uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
127         uip_setnetmask( xIPAddr );\r
128         httpd_init();\r
129 \r
130         /* Create the semaphore used to wake the uIP task. */\r
131         vSemaphoreCreateBinary( xEMACSemaphore );\r
132 \r
133         /* Initialise the MAC. */\r
134         while( lEMACInit() != pdPASS )\r
135     {\r
136         vTaskDelay( uipINIT_WAIT );\r
137     }\r
138 \r
139         portENTER_CRITICAL();\r
140         {\r
141                 EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );\r
142 \r
143                 /* Set the interrupt priority to the max permissible to cause some\r
144                 interrupt nesting. */\r
145                 NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );\r
146 \r
147                 /* Enable the interrupt. */\r
148                 NVIC_EnableIRQ( ENET_IRQn );\r
149                 prvSetMACAddress();\r
150         }\r
151         portEXIT_CRITICAL();\r
152 \r
153 \r
154         for( ;; )\r
155         {\r
156                 /* Is there received data ready to be processed? */\r
157                 uip_len = ulGetEMACRxData();\r
158 \r
159                 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
160                 {\r
161                         /* Standard uIP loop taken from the uIP manual. */\r
162                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
163                         {\r
164                                 uip_arp_ipin();\r
165                                 uip_input();\r
166 \r
167                                 /* If the above function invocation resulted in data that\r
168                                 should be sent out on the network, the global variable\r
169                                 uip_len is set to a value > 0. */\r
170                                 if( uip_len > 0 )\r
171                                 {\r
172                                         uip_arp_out();\r
173                                         vSendEMACTxData( uip_len );\r
174                                 }\r
175                         }\r
176                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
177                         {\r
178                                 uip_arp_arpin();\r
179 \r
180                                 /* If the above function invocation resulted in data that\r
181                                 should be sent out on the network, the global variable\r
182                                 uip_len is set to a value > 0. */\r
183                                 if( uip_len > 0 )\r
184                                 {\r
185                                         vSendEMACTxData( uip_len );\r
186                                 }\r
187                         }\r
188                 }\r
189                 else\r
190                 {\r
191                         if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )\r
192                         {\r
193                                 timer_reset( &periodic_timer );\r
194                                 for( i = 0; i < UIP_CONNS; i++ )\r
195                                 {\r
196                                         uip_periodic( i );\r
197 \r
198                                         /* If the above function invocation resulted in data that\r
199                                         should be sent out on the network, the global variable\r
200                                         uip_len is set to a value > 0. */\r
201                                         if( uip_len > 0 )\r
202                                         {\r
203                                                 uip_arp_out();\r
204                                                 vSendEMACTxData( uip_len );\r
205                                         }\r
206                                 }\r
207 \r
208                                 /* Call the ARP timer function every 10 seconds. */\r
209                                 if( timer_expired( &arp_timer ) )\r
210                                 {\r
211                                         timer_reset( &arp_timer );\r
212                                         uip_arp_timer();\r
213                                 }\r
214                         }\r
215                         else\r
216                         {\r
217                                 /* We did not receive a packet, and there was no periodic\r
218                                 processing to perform.  Block for a fixed period.  If a packet\r
219                                 is received during this period we will be woken by the ISR\r
220                                 giving us the Semaphore. */\r
221                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
222                         }\r
223                 }\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvSetMACAddress( void )\r
229 {\r
230 struct uip_eth_addr xAddr;\r
231 \r
232         /* Configure the MAC address in the uIP stack. */\r
233         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
234         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
235         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
236         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
237         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
238         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
239         uip_setethaddr( xAddr );\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vApplicationProcessFormInput( portCHAR *pcInputString )\r
244 {\r
245 char *c;\r
246 const unsigned long ulLEDNo = 3;\r
247 \r
248         /* Process the form input sent by the IO page of the served HTML. */\r
249 \r
250         c = strstr( pcInputString, "?" );\r
251     if( c )\r
252     {\r
253                 /* Turn LED's on or off in accordance with the check box status. */\r
254                 if( strstr( c, "LED0=1" ) != NULL )\r
255                 {\r
256                         /* Set LED7. */\r
257                         vParTestSetLED( ulLEDNo, pdFALSE );\r
258                 }\r
259                 else\r
260                 {\r
261                         /* Clear LED7. */\r
262                         vParTestSetLED( ulLEDNo, pdTRUE );\r
263                 }\r
264     }\r
265 }\r
266 \r