/[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 91 - (hide annotations) (download)
Tue May 8 09:37:15 2007 UTC (17 years ago) by hedin
File MIME type: text/plain
File size: 13278 byte(s)
added tcp/ip stack demo, trimmed.
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     char sock_open = 0;
147     char is_resolved = 0;
148    
149    
150     /*
151     * Initialize any application specific hardware.
152     */
153     InitializeBoard();
154    
155     /*
156     * Initialize all stack related components.
157     * Following steps must be performed for all applications using
158     * PICmicro TCP/IP Stack.
159     */
160     TickInit();
161    
162     /*
163     * Initialize Stack and application related NV variables.
164     */
165     InitAppConfig();
166    
167     StackInit();
168    
169     #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)
170     if ( AppConfig.Flags.bIsDHCPEnabled )
171     {
172     XLCDGoto(1, 0);
173     XLCDPutROMString(DHCPMsg);
174     }
175     else
176     {
177     /*
178     * Force IP address display update.
179     */
180     myDHCPBindCount = 1;
181     #if defined(STACK_USE_DHCP)
182     DHCPDisable();
183     #endif
184     }
185     #endif
186    
187    
188     /*
189     * Once all items are initialized, go into infinite loop and let
190     * stack items execute their tasks.
191     * If application needs to perform its own task, it should be
192     * done at the end of while loop.
193     * Note that this is a "co-operative mult-tasking" mechanism
194     * where every task performs its tasks (whether all in one shot
195     * or part of it) and returns so that other tasks can do their
196     * job.
197     * If a task needs very long time to do its job, it must broken
198     * down into smaller pieces so that other tasks can have CPU time.
199     */
200    
201    
202     // Defines the IP add. on the server machine
203     rnode.IPAddr.v[0] = 192;
204     rnode.IPAddr.v[1] = 168;
205     rnode.IPAddr.v[2] = 1;
206     rnode.IPAddr.v[3] = 20;
207     /*rnode.MACAddr.v[0] = 0x00;
208     rnode.MACAddr.v[1] = 0x16;
209     rnode.MACAddr.v[2] = 0x76;
210     rnode.MACAddr.v[3] = 0x9F;
211     rnode.MACAddr.v[4] = 0xFE;
212     rnode.MACAddr.v[5] = 0xDA;*/
213    
214    
215    
216    
217     while(1)
218     {
219     /*
220     * Blink SYSTEM LED every second.
221     */
222     if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
223     {
224     t = TickGet();
225     LATA4 ^= 1;
226     }
227    
228     /*
229     * This task performs normal stack task including checking
230     * for incoming packet, type of packet and calling
231     * appropriate stack entity to process it.
232     */
233     StackTask();
234    
235     /*
236     * For DHCP information, display how many times we have renewed the IP
237     * configuration since last reset.
238     */
239    
240     if ( DHCPBindCount != myDHCPBindCount )
241     {
242     DisplayIPValue(&AppConfig.MyIPAddr, TRUE);
243     myDHCPBindCount = DHCPBindCount;
244    
245     if ( AppConfig.Flags.bIsDHCPEnabled )
246     {
247     XLCDGoto(1, 14);
248     if ( myDHCPBindCount < 0x0a )
249     XLCDPut(myDHCPBindCount + '0');
250     else
251     XLCDPut(myDHCPBindCount + 'A');
252     }
253     }
254    
255     if (DHCPIsBound())
256     {
257     RA3 =0;
258     if (sock_open == 0)
259     {
260     if (ARPIsTxReady())
261     {
262     if (is_resolved == 0)
263     {
264     ARPResolve( &rnode.IPAddr);
265     is_resolved = 1;
266     }
267     else
268     {
269     if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
270     {
271     usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP
272     sock_open=1;
273     }
274     }
275    
276    
277    
278     }
279     }
280    
281     if (UDPIsPutReady(usock1))
282     {
283     UDPPut( rnode.MACAddr.v[0] );
284     UDPPut( rnode.MACAddr.v[1] );
285     UDPPut( rnode.MACAddr.v[2] );
286     UDPPut( rnode.MACAddr.v[3] );
287     UDPPut( rnode.MACAddr.v[4] );
288     UDPPut( rnode.MACAddr.v[5] );
289     UDPPut('D');
290     UDPPut('a');
291     UDPPut('w');
292     UDPPut(0);
293     UDPFlush();
294     }
295     }
296     }
297     }
298    
299     #if defined(MCHP_C18)
300     #pragma interrupt HighISR save=section(".tmpdata")
301     void HighISR(void)
302     #elif defined(HITECH_C18)
303     #if defined(STACK_USE_SLIP)
304     extern void MACISR(void);
305     #endif
306     void interrupt HighISR(void)
307     #endif
308     {
309     TickUpdate();
310     /*
311     #if defined(STACK_USE_SLIP)
312     MACISR();
313     #endif
314     */
315     }
316    
317     #if defined(MCHP_C18)
318     #pragma code highVector=0x08
319     void HighVector (void)
320     {
321     _asm goto HighISR _endasm
322     }
323     #pragma code /* return to default code section */
324     #endif
325    
326     static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD)
327     {
328     char IPDigit[8];
329    
330     if ( bToLCD )
331     {
332     /*
333     * Erase second line.
334     */
335     XLCDGoto(1, 0);
336     XLCDPutROMString(blankLCDLine);
337    
338     }
339    
340     /*
341     * Rewrite the second line.
342     */
343     XLCDGoto(1, 0);
344    
345     itoa(IPVal->v[0], IPDigit);
346     if ( bToLCD )
347     {
348     XLCDPutString(IPDigit);
349     XLCDPut('.');
350     }
351    
352     itoa(IPVal->v[1], IPDigit);
353     if ( bToLCD )
354     {
355     XLCDPutString(IPDigit);
356     XLCDPut('.');
357     }
358    
359     itoa(IPVal->v[2], IPDigit);
360     if ( bToLCD )
361     {
362     XLCDPutString(IPDigit);
363     XLCDPut('.');
364     }
365    
366     itoa(IPVal->v[3], IPDigit);
367     if ( bToLCD )
368     XLCDPutString(IPDigit);
369     }
370    
371     /*********************************************************************
372     * Function: void InitializeBoard(void)
373     *
374     * PreCondition: None
375     *
376     * Input: None
377     *
378     * Output: None
379     *
380     * Side Effects: None
381     *
382     * Overview: Initialize board specific hardware.
383     *
384     * Note: None
385     ********************************************************************/
386     static void InitializeBoard(void)
387     {
388     /*
389     * Setup for PORTA.RA0 as analog input while rests
390     * as digital i/o lines.
391     */
392     ADCON1 = 0b10001110; // RA0 as analog input, Right justified
393     TRISA = 0x03;
394    
395     /*
396     * LCD is enabled using RA5.
397     */
398     PORTA_RA5 = 0; // Disable LCD.
399    
400     /*
401     * Turn off the LED's.
402     */
403     LATA2 = 1;
404     LATA3 = 1;
405    
406     /*
407     * External data EEPROM needs pull-ups, so enable internal
408     * pull-ups.
409     */
410     INTCON2_RBPU = 0;
411    
412     XLCDInit();
413     XLCDGoto(0, 0);
414     XLCDPutROMString(StartupMsg);
415    
416     T0CON = 0;
417     INTCON_GIEH = 1;
418     INTCON_GIEL = 1;
419    
420     }
421    
422     /*********************************************************************
423     * Function: void InitAppConfig(void)
424     *
425     * PreCondition: MPFSInit() is already called.
426     *
427     * Input: None
428     *
429     * Output: Write/Read non-volatile config variables.
430     *
431     * Side Effects: None
432     *
433     * Overview: None
434     *
435     * Note: None
436     ********************************************************************/
437     static void InitAppConfig(void)
438     {
439     /*
440     * Load default configuration into RAM.
441     */
442     AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
443     AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
444     AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
445     AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
446    
447     AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
448     AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
449     AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
450     AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
451    
452     AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
453     AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
454     AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
455     AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
456    
457     AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
458     AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
459     AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
460     AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
461     AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
462     AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
463    
464     #if defined(STACK_USE_DHCP)
465     AppConfig.Flags.bIsDHCPEnabled = TRUE;
466     #else
467     AppConfig.Flags.bIsDHCPEnabled = FALSE;
468     #endif
469     }
470    
471     BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
472     {
473     BYTE v;
474     char *temp;
475     BYTE byteIndex;
476    
477     temp = str;
478     byteIndex = 0;
479    
480     while( v = *str )
481     {
482     if ( v == '.' )
483     {
484     *str++ = '\0';
485     buffer->v[byteIndex++] = atoi(temp);
486     temp = str;
487     }
488     else if ( v < '0' || v > '9' )
489     return FALSE;
490    
491     str++;
492     }
493    
494     buffer->v[byteIndex] = atoi(temp);
495    
496     return (byteIndex == 3);
497     }
498    
499    
500     void XLCDDelay15ms(void)
501     {
502     DelayMs(15);
503     }
504     void XLCDDelay4ms(void)
505     {
506     DelayMs(4);
507     }
508    
509     void XLCDDelay100us(void)
510     {
511     INTCON_GIEH = 0;
512     Delay10us(1);
513     INTCON_GIEH = 1;
514     }

  ViewVC Help
Powered by ViewVC 1.1.20