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