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