]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_IAR_ARM7/uIP_Task.c
First version under SVN is V4.0.1
[freertos] / Demo / uIP_Demo_IAR_ARM7 / uIP_Task.c
1 /*\r
2  * Modified from an original work that is Copyright (c) 2001-2003, Adam Dunkels.\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  * 1. Redistributions of source code must retain the above copyright\r
9  *    notice, this list of conditions and the following disclaimer.\r
10  * 2. Redistributions in binary form must reproduce the above copyright\r
11  *    notice, this list of conditions and the following disclaimer in the\r
12  *    documentation and/or other materials provided with the distribution.\r
13  * 3. The name of the author may not be used to endorse or promote\r
14  *    products derived from this software without specific prior\r
15  *    written permission.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28  *\r
29  * This file is part of the uIP TCP/IP stack.\r
30  *\r
31  * $Id: main.c,v 1.10.2.4 2003/10/21 21:27:51 adam Exp $\r
32  *\r
33  */\r
34 \r
35 /* Standard includes. */\r
36 #include <stdlib.h>\r
37 #include <stdio.h>\r
38 \r
39 /* Scheduler includes. */\r
40 #include "FreeRTOS.h"\r
41 #include "semphr.h"\r
42 #include "task.h"\r
43 \r
44 /* Demo app includes. */\r
45 #include "SAM7_EMAC.h"\r
46 \r
47 /* uIP includes. */\r
48 #undef HTONS\r
49 #include "uip.h"\r
50 #include "uip_arp.h"\r
51 #include "tapdev.h"\r
52 #include "httpd.h"\r
53 \r
54 /* The start of the uIP buffer, which will contain the frame headers. */\r
55 #define pucUIP_Buffer ( ( struct uip_eth_hdr * ) &uip_buf[ 0 ] )\r
56 \r
57 /* uIP update frequencies. */\r
58 #define RT_CLOCK_SECOND         ( configTICK_RATE_HZ  )\r
59 #define uipARP_FREQUENCY        ( 20 )\r
60 #define uipMAX_BLOCK_TIME       ( RT_CLOCK_SECOND / 4 )\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 void vuIP_TASK( void *pvParameters )\r
65 {\r
66 /* The semaphore used by the EMAC ISR to indicate that an Rx frame is ready\r
67 for processing. */\r
68 xSemaphoreHandle xSemaphore = NULL;\r
69 portBASE_TYPE xARPTimer;\r
70 unsigned portBASE_TYPE uxPriority;\r
71 static volatile portTickType xStartTime, xCurrentTime;\r
72 \r
73         /* Initialize the uIP TCP/IP stack. */\r
74         uip_init();\r
75         uip_arp_init();\r
76         \r
77         /* Initialize the HTTP server. */\r
78         httpd_init();\r
79 \r
80         /* Initialise the local timers. */\r
81         xStartTime = xTaskGetTickCount();\r
82         xARPTimer = 0;\r
83 \r
84         /* Initialise the EMAC.  A semaphore will be returned when this is\r
85         successful. This routine contains code that polls status bits.  If the\r
86         Ethernet cable is not plugged in then this can take a considerable time.\r
87         To prevent this starving lower priority tasks of processing time we\r
88         lower our priority prior to the call, then raise it back again once the\r
89         initialisation is complete. */\r
90         uxPriority = uxTaskPriorityGet( NULL );\r
91         vTaskPrioritySet( NULL, tskIDLE_PRIORITY );\r
92         while( xSemaphore == NULL )\r
93         {\r
94                 xSemaphore = xEMACInit();\r
95         }\r
96         vTaskPrioritySet( NULL, uxPriority );\r
97 \r
98         for( ;; )\r
99         {\r
100                 /* Let the network device driver read an entire IP packet\r
101                 into the uip_buf. If it returns > 0, there is a packet in the\r
102                 uip_buf buffer. */\r
103                 uip_len = ulEMACPoll();\r
104 \r
105                 /* Was a packet placed in the uIP buffer? */\r
106                 if( uip_len > 0 )\r
107                 {\r
108                         /* A packet is present in the uIP buffer. We call the\r
109                         appropriate ARP functions depending on what kind of packet we\r
110                         have received. If the packet is an IP packet, we should call\r
111                         uip_input() as well. */\r
112                         if( pucUIP_Buffer->type == htons( UIP_ETHTYPE_IP ) )\r
113                         {\r
114                                 uip_arp_ipin();\r
115                                 uip_input();\r
116 \r
117                                 /* If the above function invocation resulted in data that\r
118                                 should be sent out on the network, the global variable\r
119                                 uip_len is set to a value > 0. */\r
120                                 if( uip_len > 0 )\r
121                                 {\r
122                                         uip_arp_out();\r
123                                         lEMACSend();\r
124                                 }\r
125                         }\r
126                         else if( pucUIP_Buffer->type == htons( UIP_ETHTYPE_ARP ) )\r
127                         {\r
128                                 uip_arp_arpin();\r
129 \r
130                                 /* If the above function invocation resulted in data that\r
131                                 should be sent out on the network, the global variable\r
132                                 uip_len is set to a value > 0. */       \r
133                                 if( uip_len > 0 )\r
134                                 {       \r
135                                         lEMACSend();\r
136                                 }\r
137                         }\r
138                 }\r
139                 else\r
140                 {\r
141                         /* The poll function returned 0, so no packet was\r
142                         received. Instead we check if it is time that we do the\r
143                         periodic processing. */\r
144                         xCurrentTime = xTaskGetTickCount();\r
145 \r
146                         if( ( xCurrentTime - xStartTime ) >= RT_CLOCK_SECOND )\r
147                         {\r
148                                 portBASE_TYPE i;\r
149 \r
150                                 /* Reset the timer. */\r
151                                 xStartTime = xCurrentTime;\r
152 \r
153                                 /* Periodic check of all connections. */\r
154                                 for( i = 0; i < UIP_CONNS; i++ )\r
155                                 {\r
156                                         uip_periodic( i );\r
157 \r
158                                         /* If the above function invocation resulted in data that\r
159                                         should be sent out on the network, the global variable\r
160                                         uip_len is set to a value > 0. */                                       \r
161                                         if( uip_len > 0 )\r
162                                         {\r
163                                                 uip_arp_out();\r
164                                                 lEMACSend();\r
165                                         }\r
166                                 }\r
167 \r
168                                 #if UIP_UDP\r
169                                         for( i = 0; i < UIP_UDP_CONNS; i++ )\r
170                                         {\r
171                                                 uip_udp_periodic( i );\r
172 \r
173                                                 /* If the above function invocation resulted in data that\r
174                                                 should be sent out on the network, the global variable\r
175                                                 uip_len is set to a value > 0. */\r
176                                                 if( uip_len > 0 )\r
177                                                 {\r
178                                                         uip_arp_out();\r
179                                                         tapdev_send();\r
180                                                 }\r
181                                         }\r
182                                 #endif /* UIP_UDP */\r
183 \r
184                                 /* Periodically call the ARP timer function. */\r
185                                 if( ++xARPTimer == uipARP_FREQUENCY )\r
186                                 {       \r
187                                         uip_arp_timer();\r
188                                         xARPTimer = 0;\r
189                                 }\r
190                         }\r
191                         else\r
192                         {                               \r
193                                 /* We did not receive a packet, and there was no periodic\r
194                                 processing to perform.  Block for a fixed period.  If a packet\r
195                                 is received during this period we will be woken by the ISR\r
196                                 giving us the Semaphore. */\r
197                                 xSemaphoreTake( xSemaphore, uipMAX_BLOCK_TIME );\r
198                         }\r
199                 }\r
200         }\r
201 }\r
202 /*-----------------------------------------------------------------------------------*/\r
203 \r
204 \r
205 \r
206 \r
207 \r