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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92 - (hide annotations) (download)
Wed May 16 09:24:33 2007 UTC (17 years ago) by hedin
File MIME type: text/plain
File size: 13406 byte(s)
fixed arp bug in the stack
1 hedin 91 /*********************************************************************
2     *
3     * TFTP Client Demo App for Microchip TCP/IP Stack
4     *
5     *********************************************************************
6     * FileName: TFTPcDemo.c
7     * Dependencies: tftpc.h
8     * stacktsk.h
9     * xlcd.h
10     * tick.h
11     * Processor: PIC18
12     * Complier: MCC18 v1.00.50 or higher
13     * HITECH PICC-18 V8.10PL1 or higher
14     * Company: Microchip Technology, Inc.
15     *
16     * Software License Agreement
17     *
18     * The software supplied herewith by Microchip Technology Incorporated
19     * (the “Company”) for its PICmicro® Microcontroller is intended and
20     * supplied to you, the Company’s customer, for use solely and
21     * exclusively on Microchip PICmicro Microcontroller products. The
22     * software is owned by the Company and/or its supplier, and is
23     * protected under applicable copyright laws. All rights are reserved.
24     * Any use in violation of the foregoing restrictions may subject the
25     * user to criminal sanctions under applicable laws, as well as to
26     * civil liability for the breach of the terms and conditions of this
27     * license.
28     *
29     * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
30     * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
31     * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32     * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
33     * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
34     * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
35     *
36     * Author Date Comment
37     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38     * Nilesh Rajbharti 8/7/03 Original (Rev 1.0)
39     *
40     *
41     * This demo application demonstartes Microchip TFTP client module usage
42     * See TFTPDemo(), TFTPWrite() and TFTPRead() for actual implementation.
43     *
44     * This application monitors RB5 switch. On first push it
45     * writes 'tftpwr.bin' file to server. tftpwr.bin file is made up of
46     * first 16KB of program memory.
47     * On second push, it reads 'tftprd.txt' file from server.
48     *
49     * Server IP address is set in "Board Setup" mode using Hyperterminal.
50     *
51     * It displays rotating '-' to indicate command in progress.
52     * 'Y' to indicate success
53     * 'N' for failure
54     * 'T' for timeout
55     * 'E' for error.
56     *
57     *
58     * If running this applicaton on PICDEM.net use
59     * HS Oscillator
60     * Debug disabled
61     * Low Voltage Disabled
62     * Watchdog timer disabled
63     ********************************************************************/
64     /*
65     * Following define uniquely deines this file as main
66     * entry/application In whole project, there should only be one such
67     * definition and application file must define AppConfig variable as
68     * described below.
69     */
70     #define THIS_IS_STACK_APPLICATION
71    
72     #include "stacktsk.h"
73     #include "dhcp.h"
74     #include "xlcd.h"
75     #include "tick.h"
76     #include "delay.h"
77     #include "udp.h"
78     #include "arp.h"
79     #include "arptsk.h"
80    
81    
82     // All TFTP command statuts display will be done on first line of LCD.
83     #define TFTP_COMMAND_DISPLAY_LINE 0
84    
85     // Result will be displayed at y = 15.
86     #define TFTP_COMMAND_RESULT_POSITION 15
87    
88    
89     #define STARTUP_MSG "G1_Build_0x00"
90    
91     ROM char StartupMsg[] = STARTUP_MSG;
92    
93     #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)
94     ROM char DHCPMsg[] = "DHCP/Gleaning...";
95     #endif
96    
97     ROM char SetupMsg[] = "Board Setup...";
98    
99     // 1234567890123456
100     ROM char blankLCDLine[] = " ";
101    
102     /*
103     * This is used by other stack elements.
104     * Main application must define this and initialize it with
105     * proper values.
106     */
107     APP_CONFIG AppConfig;
108    
109    
110     BYTE myDHCPBindCount = 0;
111     #if defined(STACK_USE_DHCP)
112     extern BYTE DHCPBindCount;
113     #else
114     /*
115     * If DHCP is not enabled, force DHCP update.
116     */
117     BYTE DHCPBindCount = 1;
118     #endif
119    
120     /*
121     * Set configuration fuses for HITECH compiler.
122     * For MCC18 compiler, separately linked config.asm file
123     * will set the correct fuses.
124     */
125     #if defined(HITECH_C18)
126     __CONFIG(1, UNPROTECT & HS);
127     __CONFIG(2, PWRTEN & BORDIS & WDTDIS);
128     #endif
129    
130    
131     /*
132     * Private helper functions.
133     * These may or may not be present in all applications.
134     */
135     static void InitAppConfig(void);
136     static void InitializeBoard(void);
137     static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD);
138     static void SetConfig(void);
139    
140     void main(void)
141     {
142     // Tick to blink SYSTEM led.
143     static TICK t = 0;
144     UDP_SOCKET usock1;
145     NODE_INFO rnode;
146 hedin 92 char datagrams_to_send = 10;
147 hedin 91 char sock_open = 0;
148     char is_resolved = 0;
149    
150    
151     /*
152     * Initialize any application specific hardware.
153     */
154     InitializeBoard();
155    
156     /*
157     * Initialize all stack related components.
158     * Following steps must be performed for all applications using
159     * PICmicro TCP/IP Stack.
160     */
161     TickInit();
162    
163     /*
164     * Initialize Stack and application related NV variables.
165     */
166     InitAppConfig();
167    
168     StackInit();
169    
170     #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)
171     if ( AppConfig.Flags.bIsDHCPEnabled )
172     {
173     XLCDGoto(1, 0);
174     XLCDPutROMString(DHCPMsg);
175     }
176     else
177     {
178     /*
179     * Force IP address display update.
180     */
181     myDHCPBindCount = 1;
182     #if defined(STACK_USE_DHCP)
183     DHCPDisable();
184     #endif
185     }
186     #endif
187    
188    
189     /*
190     * Once all items are initialized, go into infinite loop and let
191     * stack items execute their tasks.
192     * If application needs to perform its own task, it should be
193     * done at the end of while loop.
194     * Note that this is a "co-operative mult-tasking" mechanism
195     * where every task performs its tasks (whether all in one shot
196     * or part of it) and returns so that other tasks can do their
197     * job.
198     * If a task needs very long time to do its job, it must broken
199     * down into smaller pieces so that other tasks can have CPU time.
200     */
201    
202    
203     // Defines the IP add. on the server machine
204     rnode.IPAddr.v[0] = 192;
205     rnode.IPAddr.v[1] = 168;
206     rnode.IPAddr.v[2] = 1;
207     rnode.IPAddr.v[3] = 20;
208     /*rnode.MACAddr.v[0] = 0x00;
209     rnode.MACAddr.v[1] = 0x16;
210     rnode.MACAddr.v[2] = 0x76;
211     rnode.MACAddr.v[3] = 0x9F;
212     rnode.MACAddr.v[4] = 0xFE;
213     rnode.MACAddr.v[5] = 0xDA;*/
214    
215    
216    
217    
218     while(1)
219     {
220     /*
221     * Blink SYSTEM LED every second.
222     */
223     if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
224     {
225     t = TickGet();
226     LATA4 ^= 1;
227     }
228    
229     /*
230     * This task performs normal stack task including checking
231     * for incoming packet, type of packet and calling
232     * appropriate stack entity to process it.
233     */
234     StackTask();
235    
236     /*
237     * For DHCP information, display how many times we have renewed the IP
238     * configuration since last reset.
239     */
240    
241     if ( DHCPBindCount != myDHCPBindCount )
242     {
243     DisplayIPValue(&AppConfig.MyIPAddr, TRUE);
244     myDHCPBindCount = DHCPBindCount;
245    
246     if ( AppConfig.Flags.bIsDHCPEnabled )
247     {
248     XLCDGoto(1, 14);
249     if ( myDHCPBindCount < 0x0a )
250     XLCDPut(myDHCPBindCount + '0');
251     else
252     XLCDPut(myDHCPBindCount + 'A');
253     }
254     }
255    
256     if (DHCPIsBound())
257     {
258     RA3 =0;
259     if (sock_open == 0)
260     {
261     if (ARPIsTxReady())
262     {
263     if (is_resolved == 0)
264     {
265     ARPResolve( &rnode.IPAddr);
266     is_resolved = 1;
267     }
268     else
269     {
270     if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
271 hedin 92 //if (MyArp( &rnode))
272 hedin 91 {
273     usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP
274     sock_open=1;
275     }
276     }
277    
278    
279    
280     }
281     }
282    
283 hedin 92 if (UDPIsPutReady(usock1) && datagrams_to_send>0)
284 hedin 91 {
285     UDPPut( rnode.MACAddr.v[0] );
286     UDPPut( rnode.MACAddr.v[1] );
287     UDPPut( rnode.MACAddr.v[2] );
288     UDPPut( rnode.MACAddr.v[3] );
289     UDPPut( rnode.MACAddr.v[4] );
290     UDPPut( rnode.MACAddr.v[5] );
291     UDPPut('D');
292     UDPPut('a');
293     UDPPut('w');
294     UDPPut(0);
295     UDPFlush();
296 hedin 92 datagrams_to_send--;
297 hedin 91 }
298     }
299     }
300     }
301    
302     #if defined(MCHP_C18)
303     #pragma interrupt HighISR save=section(".tmpdata")
304     void HighISR(void)
305     #elif defined(HITECH_C18)
306     #if defined(STACK_USE_SLIP)
307     extern void MACISR(void);
308     #endif
309     void interrupt HighISR(void)
310     #endif
311     {
312     TickUpdate();
313     /*
314     #if defined(STACK_USE_SLIP)
315     MACISR();
316     #endif
317     */
318     }
319    
320     #if defined(MCHP_C18)
321     #pragma code highVector=0x08
322     void HighVector (void)
323     {
324     _asm goto HighISR _endasm
325     }
326     #pragma code /* return to default code section */
327     #endif
328    
329     static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD)
330     {
331     char IPDigit[8];
332    
333     if ( bToLCD )
334     {
335     /*
336     * Erase second line.
337     */
338     XLCDGoto(1, 0);
339     XLCDPutROMString(blankLCDLine);
340    
341     }
342    
343     /*
344     * Rewrite the second line.
345     */
346     XLCDGoto(1, 0);
347    
348     itoa(IPVal->v[0], IPDigit);
349     if ( bToLCD )
350     {
351     XLCDPutString(IPDigit);
352     XLCDPut('.');
353     }
354    
355     itoa(IPVal->v[1], IPDigit);
356     if ( bToLCD )
357     {
358     XLCDPutString(IPDigit);
359     XLCDPut('.');
360     }
361    
362     itoa(IPVal->v[2], IPDigit);
363     if ( bToLCD )
364     {
365     XLCDPutString(IPDigit);
366     XLCDPut('.');
367     }
368    
369     itoa(IPVal->v[3], IPDigit);
370     if ( bToLCD )
371     XLCDPutString(IPDigit);
372     }
373    
374     /*********************************************************************
375     * Function: void InitializeBoard(void)
376     *
377     * PreCondition: None
378     *
379     * Input: None
380     *
381     * Output: None
382     *
383     * Side Effects: None
384     *
385     * Overview: Initialize board specific hardware.
386     *
387     * Note: None
388     ********************************************************************/
389     static void InitializeBoard(void)
390     {
391     /*
392     * Setup for PORTA.RA0 as analog input while rests
393     * as digital i/o lines.
394     */
395     ADCON1 = 0b10001110; // RA0 as analog input, Right justified
396     TRISA = 0x03;
397    
398     /*
399     * LCD is enabled using RA5.
400     */
401     PORTA_RA5 = 0; // Disable LCD.
402    
403     /*
404     * Turn off the LED's.
405     */
406     LATA2 = 1;
407     LATA3 = 1;
408    
409     /*
410     * External data EEPROM needs pull-ups, so enable internal
411     * pull-ups.
412     */
413     INTCON2_RBPU = 0;
414    
415     XLCDInit();
416     XLCDGoto(0, 0);
417     XLCDPutROMString(StartupMsg);
418    
419     T0CON = 0;
420     INTCON_GIEH = 1;
421     INTCON_GIEL = 1;
422    
423     }
424    
425     /*********************************************************************
426     * Function: void InitAppConfig(void)
427     *
428     * PreCondition: MPFSInit() is already called.
429     *
430     * Input: None
431     *
432     * Output: Write/Read non-volatile config variables.
433     *
434     * Side Effects: None
435     *
436     * Overview: None
437     *
438     * Note: None
439     ********************************************************************/
440     static void InitAppConfig(void)
441     {
442     /*
443     * Load default configuration into RAM.
444     */
445     AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
446     AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
447     AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
448     AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
449    
450     AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
451     AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
452     AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
453     AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
454    
455     AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
456     AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
457     AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
458     AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
459    
460     AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
461     AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
462     AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
463     AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
464     AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
465     AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
466    
467     #if defined(STACK_USE_DHCP)
468     AppConfig.Flags.bIsDHCPEnabled = TRUE;
469     #else
470     AppConfig.Flags.bIsDHCPEnabled = FALSE;
471     #endif
472     }
473    
474     BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
475     {
476     BYTE v;
477     char *temp;
478     BYTE byteIndex;
479    
480     temp = str;
481     byteIndex = 0;
482    
483     while( v = *str )
484     {
485     if ( v == '.' )
486     {
487     *str++ = '\0';
488     buffer->v[byteIndex++] = atoi(temp);
489     temp = str;
490     }
491     else if ( v < '0' || v > '9' )
492     return FALSE;
493    
494     str++;
495     }
496    
497     buffer->v[byteIndex] = atoi(temp);
498    
499     return (byteIndex == 3);
500     }
501    
502    
503     void XLCDDelay15ms(void)
504     {
505     DelayMs(15);
506     }
507     void XLCDDelay4ms(void)
508     {
509     DelayMs(4);
510     }
511    
512     void XLCDDelay100us(void)
513     {
514     INTCON_GIEH = 0;
515     Delay10us(1);
516     INTCON_GIEH = 1;
517     }
518 hedin 92
519    

  ViewVC Help
Powered by ViewVC 1.1.20