]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM9_STR91X_IAR/webserver/uIP_Task.c
b83afe61162ba8b6de28264422fbb8f1a1cb181a
[freertos] / FreeRTOS / Demo / ARM9_STR91X_IAR / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 /* Standard includes. */\r
67 #include <string.h>\r
68 \r
69 /* Library includes. */\r
70 #include "91x_lib.h"\r
71 #include "91x_enet.h"\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* uip includes. */\r
79 #include "uip.h"\r
80 #include "uip_arp.h"\r
81 #include "httpd.h"\r
82 #include "timer.h"\r
83 #include "clock-arch.h"\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* MAC address configuration. */\r
88 #define uipMAC_ADDR0    0x00\r
89 #define uipMAC_ADDR1    0x12\r
90 #define uipMAC_ADDR2    0x13\r
91 #define uipMAC_ADDR3    0x14\r
92 #define uipMAC_ADDR4    0x15\r
93 #define uipMAC_ADDR5    0x20\r
94 \r
95 /* IP address configuration. */\r
96 #define uipIP_ADDR0             172\r
97 #define uipIP_ADDR1             25\r
98 #define uipIP_ADDR2             218\r
99 #define uipIP_ADDR3             11      \r
100 \r
101 /* Netmask configuration. */\r
102 #define uipNET_MASK0    255\r
103 #define uipNET_MASK1    255\r
104 #define uipNET_MASK2    255\r
105 #define uipNET_MASK3    0\r
106 \r
107 /* Gateway address configuration. */\r
108 #define uipGATEWAY_ADDR0 172\r
109 #define uipGATEWAY_ADDR1 25\r
110 #define uipGATEWAY_ADDR2 218\r
111 #define uipGATEWAY_ADDR3 1\r
112 \r
113 /* Shortcut to the header within the Rx buffer. */\r
114 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
115 \r
116 /* uIP update frequencies. */\r
117 #define uipMAX_BLOCK_TIME       (configTICK_RATE_HZ / 4)\r
118 \r
119 /* Interrupt status bit definition. */\r
120 #define uipDMI_RX_CURRENT_DONE 0x8000\r
121 \r
122 /* If no buffers are available, then wait this long before looking again. */\r
123 #define uipBUFFER_WAIT_DELAY    ( 10 / portTICK_RATE_MS )\r
124 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )\r
125 \r
126 /* Standard constant. */\r
127 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * Send the uIP buffer to the MAC.\r
133  */\r
134 static void prvENET_Send(void);\r
135 \r
136 /*\r
137  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
138  */\r
139 static void prvSetMACAddress( void );\r
140 \r
141 /*\r
142  * Used to return a pointer to the next buffer to be used.\r
143  */\r
144 extern unsigned char *pcGetNextBuffer( void );\r
145 \r
146 /*\r
147  * Port functions required by the uIP stack.\r
148  */\r
149 void clock_init( void );\r
150 clock_time_t clock_time( void );\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* The semaphore used by the ISR to wake the uIP task. */\r
155 xSemaphoreHandle xSemaphore = NULL;\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void clock_init(void)\r
160 {\r
161         /* This is done when the scheduler starts. */\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 clock_time_t clock_time( void )\r
166 {\r
167         return xTaskGetTickCount();\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vuIP_Task( void *pvParameters )\r
172 {\r
173 portBASE_TYPE i;\r
174 uip_ipaddr_t xIPAddr;\r
175 struct timer periodic_timer, arp_timer;\r
176 \r
177         /* Create the semaphore used by the ISR to wake this task. */\r
178         vSemaphoreCreateBinary( xSemaphore );\r
179         \r
180         /* Initialise the uIP stack. */\r
181         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
182         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
183         uip_init();\r
184         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
185         uip_sethostaddr( xIPAddr );\r
186         uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );\r
187         uip_setnetmask( xIPAddr );\r
188         uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );\r
189         uip_setdraddr( xIPAddr );       \r
190         httpd_init();\r
191 \r
192         /* Initialise the MAC. */\r
193         ENET_InitClocksGPIO();\r
194         ENET_Init();\r
195         portENTER_CRITICAL();\r
196         {\r
197                 ENET_Start();\r
198                 prvSetMACAddress();\r
199                 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );\r
200                 VIC_ITCmd( ENET_ITLine, ENABLE );       \r
201                 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
202                 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;\r
203         }\r
204         portEXIT_CRITICAL();\r
205         \r
206 \r
207         while(1)\r
208         {\r
209                 /* Is there received data ready to be processed? */\r
210                 uip_len = ENET_HandleRxPkt( uip_buf );\r
211                 \r
212                 if( uip_len > 0 )\r
213                 {\r
214                         /* Standard uIP loop taken from the uIP manual. */\r
215                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
216                         {\r
217                                 uip_arp_ipin();\r
218                                 uip_input();\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                                         uip_arp_out();\r
226                                         prvENET_Send();\r
227                                 }\r
228                         }\r
229                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
230                         {\r
231                                 uip_arp_arpin();\r
232 \r
233                                 /* If the above function invocation resulted in data that\r
234                                 should be sent out on the network, the global variable\r
235                                 uip_len is set to a value > 0. */\r
236                                 if( uip_len > 0 )\r
237                                 {\r
238                                         prvENET_Send();\r
239                                 }\r
240                         }\r
241                 }\r
242                 else\r
243                 {\r
244                         if( timer_expired( &periodic_timer ) )\r
245                         {\r
246                                 timer_reset( &periodic_timer );\r
247                                 for( i = 0; i < UIP_CONNS; i++ )\r
248                                 {\r
249                                         uip_periodic( i );\r
250         \r
251                                         /* If the above function invocation resulted in data that\r
252                                         should be sent out on the network, the global variable\r
253                                         uip_len is set to a value > 0. */\r
254                                         if( uip_len > 0 )\r
255                                         {\r
256                                                 uip_arp_out();\r
257                                                 prvENET_Send();\r
258                                         }\r
259                                 }       \r
260         \r
261                                 /* Call the ARP timer function every 10 seconds. */\r
262                                 if( timer_expired( &arp_timer ) )\r
263                                 {\r
264                                         timer_reset( &arp_timer );\r
265                                         uip_arp_timer();\r
266                                 }\r
267                         }\r
268                         else\r
269                         {                       \r
270                                 /* We did not receive a packet, and there was no periodic\r
271                                 processing to perform.  Block for a fixed period.  If a packet\r
272                                 is received during this period we will be woken by the ISR\r
273                                 giving us the Semaphore. */\r
274                                 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );                   \r
275                         }\r
276                 }\r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void prvENET_Send(void)\r
282 {\r
283 portBASE_TYPE i;\r
284 static unsigned char *pcTxData;\r
285 \r
286         /* Get a DMA buffer into which we can write the data to send. */\r
287         for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )\r
288         {\r
289                 pcTxData = pcGetNextBuffer();\r
290 \r
291                 if( pcTxData )\r
292                 {\r
293                         break;\r
294                 }\r
295                 else\r
296                 {\r
297                         vTaskDelay( uipBUFFER_WAIT_DELAY );\r
298                 }\r
299         }\r
300         \r
301         if( pcTxData )\r
302         {\r
303                 /* Copy the header into the Tx buffer. */\r
304                 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
305                 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
306                 {\r
307                         memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
308                 }\r
309 \r
310                 ENET_TxPkt( &pcTxData, uip_len );\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 void ENET_IRQHandler(void)\r
316 {\r
317 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
318 \r
319         /* Give the semaphore in case the uIP task needs waking. */\r
320         xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );\r
321         \r
322         /* Clear the interrupt. */\r
323         ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
324         \r
325         /* Switch tasks if necessary. */        \r
326         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 static void prvSetMACAddress( void )\r
331 {\r
332 struct uip_eth_addr xAddr;\r
333 \r
334         /* Configure the MAC address in the uIP stack. */\r
335         xAddr.addr[ 0 ] = uipMAC_ADDR0;\r
336         xAddr.addr[ 1 ] = uipMAC_ADDR1;\r
337         xAddr.addr[ 2 ] = uipMAC_ADDR2;\r
338         xAddr.addr[ 3 ] = uipMAC_ADDR3;\r
339         xAddr.addr[ 4 ] = uipMAC_ADDR4;\r
340         xAddr.addr[ 5 ] = uipMAC_ADDR5;\r
341         uip_setethaddr( xAddr );\r
342 \r
343         /* Write the MAC address to the MAC. */ \r
344         ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );\r
345         ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );\r
346 }\r
347 \r