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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 91 by hedin, Tue May 8 09:37:15 2007 UTC revision 96 by hedin, Tue May 22 06:41:06 2007 UTC
# Line 1  Line 1 
 /*********************************************************************  
  *  
  *                  TFTP Client Demo App for Microchip TCP/IP Stack  
  *  
  *********************************************************************  
  * FileName:        TFTPcDemo.c  
  * Dependencies:    tftpc.h  
  *                  stacktsk.h  
  *                  xlcd.h  
  *                  tick.h  
  * Processor:       PIC18  
  * Complier:        MCC18 v1.00.50 or higher  
  *                  HITECH PICC-18 V8.10PL1 or higher  
  * Company:         Microchip Technology, Inc.  
  *  
  * Software License Agreement  
  *  
  * The software supplied herewith by Microchip Technology Incorporated  
  * (the “Company”) for its PICmicro® Microcontroller is intended and  
  * supplied to you, the Company’s customer, for use solely and  
  * exclusively on Microchip PICmicro Microcontroller products. The  
  * software is owned by the Company and/or its supplier, and is  
  * protected under applicable copyright laws. All rights are reserved.  
  * Any use in violation of the foregoing restrictions may subject the  
  * user to criminal sanctions under applicable laws, as well as to  
  * civil liability for the breach of the terms and conditions of this  
  * license.  
  *  
  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,  
  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED  
  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,  
  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR  
  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.  
  *  
  * Author               Date    Comment  
  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  * Nilesh Rajbharti     8/7/03  Original        (Rev 1.0)  
  *  
  *  
  * This demo application demonstartes Microchip TFTP client module usage  
  * See TFTPDemo(), TFTPWrite() and TFTPRead() for actual implementation.  
  *  
  * This application monitors RB5 switch. On first push it  
  * writes 'tftpwr.bin' file to server.  tftpwr.bin file is made up of  
  * first 16KB of program memory.  
  * On second push, it reads 'tftprd.txt' file from server.  
  *  
  * Server IP address is set in "Board Setup" mode using Hyperterminal.  
  *  
  * It displays rotating '-' to indicate command in progress.  
  * 'Y' to indicate success  
  * 'N' for failure  
  * 'T' for timeout  
  * 'E' for error.  
  *  
  *  
  * If running this applicaton on PICDEM.net use  
  * HS Oscillator  
  * Debug disabled  
  * Low Voltage Disabled  
  * Watchdog timer disabled  
  ********************************************************************/  
 /*  
  * Following define uniquely deines this file as main  
  * entry/application In whole project, there should only be one such  
  * definition and application file must define AppConfig variable as  
  * described below.  
  */  
1  #define THIS_IS_STACK_APPLICATION  #define THIS_IS_STACK_APPLICATION
2    
3  #include "stacktsk.h"  #include "stacktsk.h"
4  #include "dhcp.h"  #include "dhcp.h"
 #include "xlcd.h"  
5  #include "tick.h"  #include "tick.h"
6  #include "delay.h"  #include "delay.h"
7  #include "udp.h"  #include "udp.h"
8  #include "arp.h"  #include "arp.h"
9  #include "arptsk.h"  #include "arptsk.h"
10    #include "tcp.h"
11    
12    bit bIsDhcpUp = 0;
13    
14  // All TFTP command statuts display will be done on first line of LCD.  // All TFTP command statuts display will be done on first line of LCD.
15  #define TFTP_COMMAND_DISPLAY_LINE       0  //#define TFTP_COMMAND_DISPLAY_LINE       0
16    
17  // Result will be displayed at y = 15.  // Result will be displayed at y = 15.
18  #define TFTP_COMMAND_RESULT_POSITION    15  #define TFTP_COMMAND_RESULT_POSITION    15
# Line 97  ROM char DHCPMsg[] = "DHCP/Gleaning..."; Line 29  ROM char DHCPMsg[] = "DHCP/Gleaning...";
29  ROM char SetupMsg[] = "Board Setup...";  ROM char SetupMsg[] = "Board Setup...";
30    
31  //                         1234567890123456  //                         1234567890123456
32  ROM char blankLCDLine[] = "                ";  //ROM char blankLCDLine[] = "                ";
33    
34  /*  /*
35   * This is used by other stack elements.   * This is used by other stack elements.
# Line 134  __CONFIG(2, PWRTEN & BORDIS & WDTDIS); Line 66  __CONFIG(2, PWRTEN & BORDIS & WDTDIS);
66   */   */
67  static void InitAppConfig(void);  static void InitAppConfig(void);
68  static void InitializeBoard(void);  static void InitializeBoard(void);
 static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD);  
69  static void SetConfig(void);  static void SetConfig(void);
70    
71    /*****************************
72     Enummerations
73     ****************************/
74    typedef enum _NetworkState
75    {
76            off,
77            DhcpIsBound,
78            ArpIsTxReady,
79            ArpIsResolved,
80            SockOpening,
81            SockOpen,
82            TcpReadyToSend,
83            TcpSend,
84            SockClosing,
85            SockClosed
86    } NetworkState;
87    
88    /*****************************
89     GLOBAL INIT
90     ****************************/
91    NODE_INFO rnode;
92    NetworkState state;
93    TCP_SOCKET tsock1;
94    
95    
96    /*****************************
97     network funktions
98     ****************************/
99    int net_INT = 13, i;
100    char *net_CHAR = "1234567890123";
101    
102    
103    void network_init(void)
104    {
105            state = off;
106    }      
107    char network_send_hello( unsigned char termid )
108    {
109    char error = 0;
110    char gotArp = 0;
111    static char isBound = 0;
112    
113    if ( DHCPIsBound() && isBound == 0 ){
114            state = DhcpIsBound;
115            isBound = 1;
116    }
117    
118            switch (state)
119            {
120                    case DhcpIsBound:
121                            if ( ARPIsTxReady() && gotArp == 0 )
122                                    state = ArpIsTxReady;
123                            else
124                                    state = ArpIsResolved;
125                            return 0;
126                    
127                    case ArpIsTxReady:
128                            ARPResolve(&rnode.IPAddr);
129                            gotArp = 1;
130                            state = ArpIsResolved;
131                            return 0;
132                                            
133                    case ArpIsResolved:
134                            if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){
135                                    state = SockOpening;
136                                    return 0;
137                                    }
138                                    else{
139                                            state = ArpIsResolved;
140                                            return 2;                                                                       // no arp resolved
141                                    }
142                    
143                    case SockOpening:
144                            tsock1 = TCPConnect (&rnode, 3000);
145                            state = SockOpen;
146                            return 0;
147            
148                    case SockOpen:
149                            if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
150                            state = TcpSend;
151                            return 0;
152                    
153                    case TcpSend:
154                            for (i = net_INT; i >= 0; i--)
155                            {
156                                    TCPPut(tsock1, termid );
157                            }
158                            TCPFlush(tsock1);
159                            state = TcpReadyToSend;
160                            return 0;
161            }
162            return 1;
163    }
164    /*
165    void network_send_hello( unsigned char termid )
166    {
167               if (DHCPIsBound())
168            {
169                    RA3 = 0;
170                    if (sock_open == 0)
171                    {
172                            if (ARPIsTxReady())
173                            {
174                                    ARPResolve( &rnode.IPAddr);
175                                    is_resolved = 1;
176                                }
177                                    else
178                                    {
179                                            if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
180                                            //if (MyArp( &rnode))
181                                            {
182                                                    usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP
183                                                    sock_open=1;
184                                            }
185                                    }      
186                            }
187                    }
188                    
189                    if (UDPIsPutReady(usock1) && datagrams_to_send>0)
190                    {
191                            UDPPut( rnode.MACAddr.v[0] );
192                            UDPPut( rnode.MACAddr.v[1] );
193                            UDPPut( rnode.MACAddr.v[2] );
194                            UDPPut( rnode.MACAddr.v[3] );
195                            UDPPut( rnode.MACAddr.v[4] );
196                            UDPPut( rnode.MACAddr.v[5] );
197                            UDPPut('D');
198                            UDPPut('a');
199                            UDPPut('w');
200                            UDPPut(0);
201                            UDPFlush();
202                            datagrams_to_send--;
203                    }
204                    }
205    }
206    */
207    
208  void main(void)  void main(void)
209  {  {
210      // Tick to blink SYSTEM led.      // Tick to blink SYSTEM led.
211      static TICK t = 0;      static TICK t = 0;
212      UDP_SOCKET usock1;      TCP_SOCKET usock1;
213      NODE_INFO rnode;      char sock_state = 0;
     char sock_open = 0;  
214      char is_resolved = 0;      char is_resolved = 0;
215        char has_arp = 0;
216        char a;
217                    
218        TICK tick;
219        TICK lastTick;
220            TICK diffTicks;
221    
222      /*      /*
223       * Initialize any application specific hardware.       * Initialize any application specific hardware.
# Line 165  void main(void) Line 237  void main(void)
237      InitAppConfig();      InitAppConfig();
238    
239      StackInit();      StackInit();
240        
241        network_init();
242    
 #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)  
     if ( AppConfig.Flags.bIsDHCPEnabled )  
     {  
         XLCDGoto(1, 0);  
         XLCDPutROMString(DHCPMsg);  
     }  
     else  
     {  
         /*  
          * Force IP address display update.  
          */  
         myDHCPBindCount = 1;  
 #if defined(STACK_USE_DHCP)  
         DHCPDisable();  
 #endif  
     }  
 #endif  
243    
244    
245      /*      /*
# Line 204  void main(void) Line 261  void main(void)
261          rnode.IPAddr.v[1] = 168;          rnode.IPAddr.v[1] = 168;
262          rnode.IPAddr.v[2] = 1;          rnode.IPAddr.v[2] = 1;
263          rnode.IPAddr.v[3] = 20;          rnode.IPAddr.v[3] = 20;
         /*rnode.MACAddr.v[0] = 0x00;  
         rnode.MACAddr.v[1] = 0x16;  
         rnode.MACAddr.v[2] = 0x76;  
         rnode.MACAddr.v[3] = 0x9F;  
         rnode.MACAddr.v[4] = 0xFE;  
         rnode.MACAddr.v[5] = 0xDA;*/  
           
           
264    
       
265      while(1)      while(1)
266      {      {
267          /*          /*
# Line 236  void main(void) Line 284  void main(void)
284           * For DHCP information, display how many times we have renewed the IP           * For DHCP information, display how many times we have renewed the IP
285           * configuration since last reset.           * configuration since last reset.
286           */           */
           
         if ( DHCPBindCount != myDHCPBindCount )  
         {  
             DisplayIPValue(&AppConfig.MyIPAddr, TRUE);  
             myDHCPBindCount = DHCPBindCount;  
287    
288              if ( AppConfig.Flags.bIsDHCPEnabled )          a = network_send_hello( 1 );
289              {     /*     if (DHCPIsBound())
                 XLCDGoto(1, 14);  
                 if ( myDHCPBindCount < 0x0a )  
                     XLCDPut(myDHCPBindCount + '0');  
                 else  
                     XLCDPut(myDHCPBindCount + 'A');  
             }  
         }  
           
         if (DHCPIsBound())  
290          {          {
291                  RA3 =0;                  RA3 =0;
292                  if (sock_open == 0)                  if (has_arp == 0)
293                  {                  {
294                          if (ARPIsTxReady())                          if (ARPIsTxReady())
295                          {                          {
# Line 268  void main(void) Line 302  void main(void)
302                                  {                                  {
303                                          if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))                                          if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
304                                          {                                          {
305                                                  usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP                                                          has_arp = 1;
                                                 sock_open=1;  
306                                          }                                          }
307                                  }                                  }
308            
   
                                           
309                          }                          }
310                  }                  }
311                                    
312                  if (UDPIsPutReady(usock1))                  if (has_arp == 1)
313                  {                  {
314                          UDPPut( rnode.MACAddr.v[0] );                          if (sock_state == 0)
315                          UDPPut( rnode.MACAddr.v[1] );                          {
316                          UDPPut( rnode.MACAddr.v[2] );                          usock1=TCPConnect(&rnode,3000);
317                          UDPPut( rnode.MACAddr.v[3] );                                  sock_state = 1;
318                          UDPPut( rnode.MACAddr.v[4] );                          }
319                          UDPPut( rnode.MACAddr.v[5] );                  /*      if (sock_state ==1 && TCPIsConnected(usock1) && TCPIsPutReady(usock1))
320                          UDPPut('D');                          {
321                          UDPPut('a');                                  TCPPut(usock1,'D');
322                          UDPPut('w');                                  TCPPut(usock1,'a');
323                          UDPPut(0);                                  TCPPut(usock1,'w');
324                          UDPFlush();                                  TCPPut(usock1,0);
325                  }                          TCPFlush(usock1);
326          }                                  sock_state = 2;
327                            }
328                    
329                            if (sock_state == 2 && TCPIsPutReady(usock1))
330                        {
331                                    TCPDisconnect(usock1);
332                                    lastTick = TickGet();
333                                    sock_state =3;
334                            }
335                            tick = TickGet();
336                            diffTicks = TickGetDiff(tick, lastTick);
337                            
338                            if ( sock_state == 3 && diffTicks > ((TICK)TICK_SECOND * (TICK)2) )
339                                    sock_state = 0;
340                                    
341                        }
342            } */
343      }      }
344  }  }
345    
# Line 323  void HighVector (void) Line 370  void HighVector (void)
370  #pragma code /* return to default code section */  #pragma code /* return to default code section */
371  #endif  #endif
372    
 static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD)  
 {  
     char IPDigit[8];  
   
     if ( bToLCD )  
     {  
         /*  
          * Erase second line.  
          */  
         XLCDGoto(1, 0);  
         XLCDPutROMString(blankLCDLine);  
   
     }  
   
     /*  
      * Rewrite the second line.  
      */  
     XLCDGoto(1, 0);  
   
     itoa(IPVal->v[0], IPDigit);  
     if ( bToLCD )  
     {  
         XLCDPutString(IPDigit);  
         XLCDPut('.');  
     }  
   
     itoa(IPVal->v[1], IPDigit);  
     if ( bToLCD )  
     {  
         XLCDPutString(IPDigit);  
         XLCDPut('.');  
     }  
   
     itoa(IPVal->v[2], IPDigit);  
     if ( bToLCD )  
     {  
         XLCDPutString(IPDigit);  
         XLCDPut('.');  
     }  
   
     itoa(IPVal->v[3], IPDigit);  
     if ( bToLCD )  
         XLCDPutString(IPDigit);  
 }  
   
373  /*********************************************************************  /*********************************************************************
374   * Function:        void InitializeBoard(void)   * Function:        void InitializeBoard(void)
375   *   *
# Line 395  static void InitializeBoard(void) Line 397  static void InitializeBoard(void)
397      /*      /*
398       * LCD is enabled using RA5.       * LCD is enabled using RA5.
399       */       */
400      PORTA_RA5 = 0;          // Disable LCD.  //    PORTA_RA5 = 0;          // Disable LCD.
401    
402      /*      /*
403       * Turn off the LED's.       * Turn off the LED's.
# Line 409  static void InitializeBoard(void) Line 411  static void InitializeBoard(void)
411       */       */
412      INTCON2_RBPU = 0;      INTCON2_RBPU = 0;
413    
     XLCDInit();  
     XLCDGoto(0, 0);  
     XLCDPutROMString(StartupMsg);  
   
414      T0CON = 0;      T0CON = 0;
415      INTCON_GIEH = 1;      INTCON_GIEH = 1;
416      INTCON_GIEL = 1;      INTCON_GIEL = 1;
# Line 496  BOOL StringToIPAddress(char *str, IP_ADD Line 494  BOOL StringToIPAddress(char *str, IP_ADD
494      return (byteIndex == 3);      return (byteIndex == 3);
495  }  }
496    
   
 void XLCDDelay15ms(void)  
 {  
     DelayMs(15);  
 }  
 void XLCDDelay4ms(void)  
 {  
     DelayMs(4);  
 }  
   
 void XLCDDelay100us(void)  
 {  
     INTCON_GIEH = 0;  
     Delay10us(1);  
     INTCON_GIEH = 1;  
 }  

Legend:
Removed from v.91  
changed lines
  Added in v.96

  ViewVC Help
Powered by ViewVC 1.1.20