]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/webserver/uIP_Task.c
Add lwIP V1.4.0 source files.
[freertos] / Demo / ARM9_STR91X_IAR / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 /* Standard includes. */\r
54 #include <string.h>\r
55 \r
56 /* Library includes. */\r
57 #include "91x_lib.h"\r
58 #include "91x_enet.h"\r
59 \r
60 /* Scheduler includes. */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 #include "semphr.h"\r
64 \r
65 /* uip includes. */\r
66 #include "uip.h"\r
67 #include "uip_arp.h"\r
68 #include "httpd.h"\r
69 #include "timer.h"\r
70 #include "clock-arch.h"\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* MAC address configuration. */\r
75 #define uipMAC_ADDR0    0x00\r
76 #define uipMAC_ADDR1    0x12\r
77 #define uipMAC_ADDR2    0x13\r
78 #define uipMAC_ADDR3    0x14\r
79 #define uipMAC_ADDR4    0x15\r
80 #define uipMAC_ADDR5    0x20\r
81 \r
82 /* IP address configuration. */\r
83 #define uipIP_ADDR0             172\r
84 #define uipIP_ADDR1             25\r
85 #define uipIP_ADDR2             218\r
86 #define uipIP_ADDR3             11      \r
87 \r
88 /* Netmask configuration. */\r
89 #define uipNET_MASK0    255\r
90 #define uipNET_MASK1    255\r
91 #define uipNET_MASK2    255\r
92 #define uipNET_MASK3    0\r
93 \r
94 /* Gateway address configuration. */\r
95 #define uipGATEWAY_ADDR0 172\r
96 #define uipGATEWAY_ADDR1 25\r
97 #define uipGATEWAY_ADDR2 218\r
98 #define uipGATEWAY_ADDR3 1\r
99 \r
100 /* Shortcut to the header within the Rx buffer. */\r
101 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
102 \r
103 /* uIP update frequencies. */\r
104 #define uipMAX_BLOCK_TIME       (configTICK_RATE_HZ / 4)\r
105 \r
106 /* Interrupt status bit definition. */\r
107 #define uipDMI_RX_CURRENT_DONE 0x8000\r
108 \r
109 /* If no buffers are available, then wait this long before looking again. */\r
110 #define uipBUFFER_WAIT_DELAY    ( 10 / portTICK_RATE_MS )\r
111 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )\r
112 \r
113 /* Standard constant. */\r
114 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /*\r
119  * Send the uIP buffer to the MAC.\r
120  */\r
121 static void prvENET_Send(void);\r
122 \r
123 /*\r
124  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
125  */\r
126 static void prvSetMACAddress( void );\r
127 \r
128 /*\r
129  * Used to return a pointer to the next buffer to be used.\r
130  */\r
131 extern unsigned char *pcGetNextBuffer( void );\r
132 \r
133 /*\r
134  * Port functions required by the uIP stack.\r
135  */\r
136 void clock_init( void );\r
137 clock_time_t clock_time( void );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* The semaphore used by the ISR to wake the uIP task. */\r
142 xSemaphoreHandle xSemaphore = NULL;\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void clock_init(void)\r
147 {\r
148         /* This is done when the scheduler starts. */\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 clock_time_t clock_time( void )\r
153 {\r
154         return xTaskGetTickCount();\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vuIP_Task( void *pvParameters )\r
159 {\r
160 portBASE_TYPE i;\r
161 uip_ipaddr_t xIPAddr;\r
162 struct timer periodic_timer, arp_timer;\r
163 \r
164         /* Create the semaphore used by the ISR to wake this task. */\r
165         vSemaphoreCreateBinary( xSemaphore );\r
166         \r
167         /* Initialise the uIP stack. */\r
168         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
169         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
170         uip_init();\r
171         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
172         uip_sethostaddr( xIPAddr );\r
173         uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );\r
174         uip_setnetmask( xIPAddr );\r
175         uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );\r
176         uip_setdraddr( xIPAddr );       \r
177         httpd_init();\r
178 \r
179         /* Initialise the MAC. */\r
180         ENET_InitClocksGPIO();\r
181         ENET_Init();\r
182         portENTER_CRITICAL();\r
183         {\r
184                 ENET_Start();\r
185                 prvSetMACAddress();\r
186                 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );\r
187                 VIC_ITCmd( ENET_ITLine, ENABLE );       \r
188                 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
189                 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;\r
190         }\r
191         portEXIT_CRITICAL();\r
192         \r
193 \r
194         while(1)\r
195         {\r
196                 /* Is there received data ready to be processed? */\r
197                 uip_len = ENET_HandleRxPkt( uip_buf );\r
198                 \r
199                 if( uip_len > 0 )\r
200                 {\r
201                         /* Standard uIP loop taken from the uIP manual. */\r
202                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
203                         {\r
204                                 uip_arp_ipin();\r
205                                 uip_input();\r
206 \r
207                                 /* If the above function invocation resulted in data that\r
208                                 should be sent out on the network, the global variable\r
209                                 uip_len is set to a value > 0. */\r
210                                 if( uip_len > 0 )\r
211                                 {\r
212                                         uip_arp_out();\r
213                                         prvENET_Send();\r
214                                 }\r
215                         }\r
216                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
217                         {\r
218                                 uip_arp_arpin();\r
219 \r
220                                 /* If the above function invocation resulted in data that\r
221                                 should be sent out on the network, the global variable\r
222                                 uip_len is set to a value > 0. */\r
223                                 if( uip_len > 0 )\r
224                                 {\r
225                                         prvENET_Send();\r
226                                 }\r
227                         }\r
228                 }\r
229                 else\r
230                 {\r
231                         if( timer_expired( &periodic_timer ) )\r
232                         {\r
233                                 timer_reset( &periodic_timer );\r
234                                 for( i = 0; i < UIP_CONNS; i++ )\r
235                                 {\r
236                                         uip_periodic( i );\r
237         \r
238                                         /* If the above function invocation resulted in data that\r
239                                         should be sent out on the network, the global variable\r
240                                         uip_len is set to a value > 0. */\r
241                                         if( uip_len > 0 )\r
242                                         {\r
243                                                 uip_arp_out();\r
244                                                 prvENET_Send();\r
245                                         }\r
246                                 }       \r
247         \r
248                                 /* Call the ARP timer function every 10 seconds. */\r
249                                 if( timer_expired( &arp_timer ) )\r
250                                 {\r
251                                         timer_reset( &arp_timer );\r
252                                         uip_arp_timer();\r
253                                 }\r
254                         }\r
255                         else\r
256                         {                       \r
257                                 /* We did not receive a packet, and there was no periodic\r
258                                 processing to perform.  Block for a fixed period.  If a packet\r
259                                 is received during this period we will be woken by the ISR\r
260                                 giving us the Semaphore. */\r
261                                 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );                   \r
262                         }\r
263                 }\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvENET_Send(void)\r
269 {\r
270 portBASE_TYPE i;\r
271 static unsigned char *pcTxData;\r
272 \r
273         /* Get a DMA buffer into which we can write the data to send. */\r
274         for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )\r
275         {\r
276                 pcTxData = pcGetNextBuffer();\r
277 \r
278                 if( pcTxData )\r
279                 {\r
280                         break;\r
281                 }\r
282                 else\r
283                 {\r
284                         vTaskDelay( uipBUFFER_WAIT_DELAY );\r
285                 }\r
286         }\r
287         \r
288         if( pcTxData )\r
289         {\r
290                 /* Copy the header into the Tx buffer. */\r
291                 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
292                 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
293                 {\r
294                         memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
295                 }\r
296 \r
297                 ENET_TxPkt( &pcTxData, uip_len );\r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 void ENET_IRQHandler(void)\r
303 {\r
304 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
305 \r
306         /* Give the semaphore in case the uIP task needs waking. */\r
307         xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );\r
308         \r
309         /* Clear the interrupt. */\r
310         ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
311         \r
312         /* Switch tasks if necessary. */        \r
313         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static void prvSetMACAddress( void )\r
318 {\r
319 struct uip_eth_addr xAddr;\r
320 \r
321         /* Configure the MAC address in the uIP stack. */\r
322         xAddr.addr[ 0 ] = uipMAC_ADDR0;\r
323         xAddr.addr[ 1 ] = uipMAC_ADDR1;\r
324         xAddr.addr[ 2 ] = uipMAC_ADDR2;\r
325         xAddr.addr[ 3 ] = uipMAC_ADDR3;\r
326         xAddr.addr[ 4 ] = uipMAC_ADDR4;\r
327         xAddr.addr[ 5 ] = uipMAC_ADDR5;\r
328         uip_setethaddr( xAddr );\r
329 \r
330         /* Write the MAC address to the MAC. */ \r
331         ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );\r
332         ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );\r
333 }\r
334 \r