/[H8]/trunk/PIC/Demo trimmet/StackTsk.c
ViewVC logotype

Annotation of /trunk/PIC/Demo trimmet/StackTsk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91 - (hide annotations) (download)
Tue May 8 09:37:15 2007 UTC (17 years, 1 month ago) by hedin
File MIME type: text/plain
File size: 10996 byte(s)
added tcp/ip stack demo, trimmed.
1 hedin 91 /*********************************************************************
2     *
3     * Microchip TCP/IP Stack FSM Implementation on PIC18
4     *
5     *********************************************************************
6     * FileName: StackTsk.c
7     * Dependencies: StackTsk.H
8     * ARPTsk.h
9     * MAC.h
10     * IP.h
11     * ICMP.h
12     * Tcp.h
13     * http.h
14     * Processor: PIC18
15     * Complier: MCC18 v1.00.50 or higher
16     * HITECH PICC-18 V8.10PL1 or higher
17     * Company: Microchip Technology, Inc.
18     *
19     * Software License Agreement
20     *
21     * The software supplied herewith by Microchip Technology Incorporated
22     * (the “Company”) for its PICmicro® Microcontroller is intended and
23     * supplied to you, the Company’s customer, for use solely and
24     * exclusively on Microchip PICmicro Microcontroller products. The
25     * software is owned by the Company and/or its supplier, and is
26     * protected under applicable copyright laws. All rights are reserved.
27     * Any use in violation of the foregoing restrictions may subject the
28     * user to criminal sanctions under applicable laws, as well as to
29     * civil liability for the breach of the terms and conditions of this
30     * license.
31     *
32     * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
33     * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
34     * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
35     * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
36     * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
37     * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
38     *
39     *
40     * HiTech PICC18 Compiler Options excluding device selection:
41     * -FAKELOCAL -G -O -Zg -E -C
42     *
43     *
44     *
45     *
46     * Author Date Comment
47     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48     * Nilesh Rajbharti 8/14/01 Original (Rev. 1.0)
49     * Nilesh Rajbharti 2/9/02 Cleanup
50     * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
51     ********************************************************************/
52    
53     #define STACK_INCLUDE
54    
55     #include "stacktsk.h"
56     #include "arptsk.h"
57     #include "mac.h"
58     #include "ip.h"
59    
60     #if defined(STACK_USE_TCP)
61     #include "tcp.h"
62     #endif
63    
64     #if defined(STACK_USE_HTTP_SERVER)
65     #include "http.h"
66     #endif
67    
68     #if defined(STACK_USE_ICMP)
69     #include "icmp.h"
70     #endif
71    
72     #if defined(STACK_USE_UDP)
73     #include "udp.h"
74     #endif
75    
76     #if defined(STACK_USE_DHCP)
77     #include "dhcp.h"
78     #endif
79    
80    
81     #define MAX_ICMP_DATA_LEN (32)
82    
83     /*
84     * Stack FSM states.
85     */
86     typedef enum _SM_STACK
87     {
88     SM_STACK_IDLE,
89     SM_STACK_MAC,
90     SM_STACK_IP,
91     SM_STACK_ICMP,
92     SM_STACK_ICMP_REPLY,
93     SM_STACK_ARP,
94     SM_STACK_TCP,
95     SM_STACK_UDP
96     } SM_STACK;
97     static SM_STACK smStack;
98    
99    
100     #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP)
101     STACK_FLAGS stackFlags;
102     #endif
103    
104    
105    
106     /*********************************************************************
107     * Function: void StackInit(void)
108     *
109     * PreCondition: None
110     *
111     * Input: None
112     *
113     * Output: Stack and its componentns are initialized
114     *
115     * Side Effects: None
116     *
117     * Note: This function must be called before any of the
118     * stack or its component routines be used.
119     *
120     ********************************************************************/
121     void StackInit(void)
122     {
123     smStack = SM_STACK_IDLE;
124    
125     #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP)
126     /*
127     * If DHCP or IP Gleaning is enabled,
128     * startup in Config Mode.
129     */
130     stackFlags.bits.bInConfigMode = TRUE;
131    
132     #endif
133    
134     MACInit();
135    
136     ARPInit();
137    
138     #if defined(STACK_USE_UDP)
139     UDPInit();
140     #endif
141    
142     #if defined(STACK_USE_TCP)
143     TCPInit();
144     #endif
145    
146     }
147    
148    
149     /*********************************************************************
150     * Function: void StackTask(void)
151     *
152     * PreCondition: StackInit() is already called.
153     *
154     * Input: None
155     *
156     * Output: Stack FSM is executed.
157     *
158     * Side Effects: None
159     *
160     * Note: This FSM checks for new incoming packets,
161     * and routes it to appropriate stack components.
162     * It also performs timed operations.
163     *
164     * This function must be called periodically called
165     * to make sure that timely response.
166     *
167     ********************************************************************/
168     void StackTask(void)
169     {
170     static NODE_INFO remoteNode;
171     static WORD dataCount;
172    
173     #if defined(STACK_USE_ICMP)
174     static BYTE data[MAX_ICMP_DATA_LEN];
175     static WORD ICMPId;
176     static WORD ICMPSeq;
177     #endif
178     IP_ADDR tempLocalIP;
179    
180    
181     union
182     {
183     BYTE MACFrameType;
184     BYTE IPFrameType;
185     ICMP_CODE ICMPCode;
186     } type;
187    
188    
189     BOOL lbContinue;
190    
191    
192     lbContinue = TRUE;
193     while( lbContinue )
194     {
195     lbContinue = FALSE;
196    
197     switch(smStack)
198     {
199     case SM_STACK_IDLE:
200     case SM_STACK_MAC:
201    
202     if ( !MACGetHeader(&remoteNode.MACAddr, &type.MACFrameType) )
203     {
204     #if defined(STACK_USE_DHCP)
205     if ( !MACIsLinked() )
206     {
207     MY_IP_BYTE1 = 0;
208     MY_IP_BYTE2 = 0;
209     MY_IP_BYTE3 = 0;
210     MY_IP_BYTE4 = 0;
211    
212     stackFlags.bits.bInConfigMode = TRUE;
213     DHCPReset();
214     }
215     #endif
216    
217     break;
218     }
219    
220     lbContinue = TRUE;
221     if ( type.MACFrameType == MAC_IP )
222     smStack = SM_STACK_IP;
223     else if ( type.MACFrameType == MAC_ARP )
224     smStack = SM_STACK_ARP;
225     else
226     MACDiscardRx();
227     break;
228    
229     case SM_STACK_ARP:
230     lbContinue = FALSE;
231     if ( ARPProcess() )
232     smStack = SM_STACK_IDLE;
233     break;
234    
235     case SM_STACK_IP:
236     if ( IPGetHeader(&tempLocalIP,
237     &remoteNode,
238     &type.IPFrameType,
239     &dataCount) )
240     {
241     lbContinue = TRUE;
242     if ( type.IPFrameType == IP_PROT_ICMP )
243     {
244     smStack = SM_STACK_ICMP;
245    
246     #if defined(STACK_USE_IP_GLEANING)
247     if ( stackFlags.bits.bInConfigMode )
248     {
249     /*
250     * Accoriding to "IP Gleaning" procedure,
251     * when we receive an ICMP packet with a valid
252     * IP address while we are still in configuration
253     * mode, accept that address as ours and conclude
254     * configuration mode.
255     */
256     if ( tempLocalIP.Val != 0xffffffff )
257     {
258     stackFlags.bits.bInConfigMode = FALSE;
259     MY_IP_BYTE1 = tempLocalIP.v[0];
260     MY_IP_BYTE2 = tempLocalIP.v[1];
261     MY_IP_BYTE3 = tempLocalIP.v[2];
262     MY_IP_BYTE4 = tempLocalIP.v[3];
263    
264     #if defined(STACK_USE_DHCP)
265     /*
266     * If DHCP and IP gleaning is enabled at the
267     * same time, we must ensuer that once we have
268     * IP address through IP gleaning, we abort
269     * any pending DHCP requests and do not renew
270     * any new DHCP configuration.
271     */
272     DHCPAbort();
273     #endif
274     }
275     }
276     #endif
277     }
278    
279     #if defined(STACK_USE_TCP)
280     else if ( type.IPFrameType == IP_PROT_TCP )
281     smStack = SM_STACK_TCP;
282     #endif
283    
284     #if defined(STACK_USE_UDP)
285     else if ( type.IPFrameType == IP_PROT_UDP )
286     smStack = SM_STACK_UDP;
287     #endif
288    
289     else
290     {
291     lbContinue = FALSE;
292     MACDiscardRx();
293    
294     smStack = SM_STACK_IDLE;
295     }
296     }
297     else
298     {
299     MACDiscardRx();
300     smStack = SM_STACK_IDLE;
301     }
302     break;
303    
304     #if defined(STACK_USE_UDP)
305     case SM_STACK_UDP:
306     if ( UDPProcess(&remoteNode, dataCount) )
307     smStack = SM_STACK_IDLE;
308     lbContinue = FALSE;
309     break;
310     #endif
311    
312     #if defined(STACK_USE_TCP)
313     case SM_STACK_TCP:
314     if ( TCPProcess(&remoteNode, dataCount) )
315     smStack = SM_STACK_IDLE;
316     lbContinue = FALSE;
317     break;
318     #endif
319    
320     case SM_STACK_ICMP:
321     smStack = SM_STACK_IDLE;
322    
323     #if defined(STACK_USE_ICMP)
324     if ( dataCount <= (MAX_ICMP_DATA_LEN+9) )
325     {
326     if ( ICMPGet(&type.ICMPCode,
327     data,
328     (BYTE*)&dataCount,
329     &ICMPId,
330     &ICMPSeq) )
331     {
332     if ( type.ICMPCode == ICMP_ECHO_REQUEST )
333     {
334     lbContinue = TRUE;
335     smStack = SM_STACK_ICMP_REPLY;
336     }
337     else
338     {
339     smStack = SM_STACK_IDLE;
340     }
341     }
342     else
343     {
344     smStack = SM_STACK_IDLE;
345     }
346     }
347     #endif
348     MACDiscardRx();
349     break;
350    
351     #if defined(STACK_USE_ICMP)
352     case SM_STACK_ICMP_REPLY:
353     if ( ICMPIsTxReady() )
354     {
355     ICMPPut(&remoteNode,
356     ICMP_ECHO_REPLY,
357     data,
358     (BYTE)dataCount,
359     ICMPId,
360     ICMPSeq);
361    
362     smStack = SM_STACK_IDLE;
363     }
364     break;
365     #endif
366    
367     }
368    
369     }
370    
371     #if defined(STACK_USE_TCP)
372     // Perform timed TCP FSM.
373     TCPTick();
374     #endif
375    
376    
377     #if defined(STACK_USE_DHCP)
378     /*
379     * DHCP must be called all the time even after IP configuration is
380     * discovered.
381     * DHCP has to account lease expiration time and renew the configuration
382     * time.
383     */
384     DHCPTask();
385    
386     if ( DHCPIsBound() )
387     stackFlags.bits.bInConfigMode = FALSE;
388    
389     #endif
390     }
391    

  ViewVC Help
Powered by ViewVC 1.1.20