/[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 92 by hedin, Wed May 16 09:24:33 2007 UTC revision 102 by hedin, Fri May 25 16:00:14 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    char netlevel = 0;
14    
15  // 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.
16  #define TFTP_COMMAND_DISPLAY_LINE       0  //#define TFTP_COMMAND_DISPLAY_LINE       0
17    
18  // Result will be displayed at y = 15.  // Result will be displayed at y = 15.
19  #define TFTP_COMMAND_RESULT_POSITION    15  #define TFTP_COMMAND_RESULT_POSITION    15
20    
21    
22  #define STARTUP_MSG "G1_Build_0x00"  /*#define STARTUP_MSG "G1_Build_0x00"
23    
24  ROM char StartupMsg[] = STARTUP_MSG;  ROM char StartupMsg[] = STARTUP_MSG;
25    
# Line 97  ROM char DHCPMsg[] = "DHCP/Gleaning..."; Line 30  ROM char DHCPMsg[] = "DHCP/Gleaning...";
30  ROM char SetupMsg[] = "Board Setup...";  ROM char SetupMsg[] = "Board Setup...";
31    
32  //                         1234567890123456  //                         1234567890123456
33  ROM char blankLCDLine[] = "                ";  //ROM char blankLCDLine[] = "                ";
34    */
35  /*  /*
36   * This is used by other stack elements.   * This is used by other stack elements.
37   * Main application must define this and initialize it with   * Main application must define this and initialize it with
# Line 134  __CONFIG(2, PWRTEN & BORDIS & WDTDIS); Line 67  __CONFIG(2, PWRTEN & BORDIS & WDTDIS);
67   */   */
68  static void InitAppConfig(void);  static void InitAppConfig(void);
69  static void InitializeBoard(void);  static void InitializeBoard(void);
 static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD);  
70  static void SetConfig(void);  static void SetConfig(void);
71    
72    /*****************************
73     Enummerations
74     ****************************/
75    typedef enum _NetworkState
76    {
77            off,
78            DhcpIsBound,
79            ArpIsTxReady,
80            ArpIsResolved,
81            SockOpening,
82            SockOpen,
83            TcpReadyToSend,
84            TcpSend,
85            HelloDone,
86            StregkodeDone,
87            SockClosing,
88            SockClosed
89    } NetworkState;
90    
91    /*****************************
92     GLOBAL INIT
93     ****************************/
94    NODE_INFO rnode;
95    NetworkState state;
96    TCP_SOCKET tsock1;
97    
98    
99    /*****************************
100     network funktions
101     ****************************/
102    int net_INT = 13, i;
103    char *net_CHAR = "1234567890123";
104    
105    
106    void network_init(void)
107    {
108            state = off;
109    }      
110    
111    
112    void network_worker(void)
113    {    
114            TickGet();
115        StackTask();    
116    }      
117    
118    void network_wait(unsigned char n)
119    {
120            int i;
121            for (i=0; i<n; i++)
122            {
123                    network_worker();
124                    DelayMs(1);
125            }
126    }      
127    
128    
129    char network_send_hello( unsigned char termid )
130    {
131    char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.
132    static char isBound = 0;
133    
134    while( !DHCPIsBound() && isBound == 0 )
135            network_worker();
136    
137    //      while( state != DhcpIsBound ){
138                    if ( DHCPIsBound() && isBound == 0 ){
139                            state = DhcpIsBound;
140                            isBound = 1;
141    //              }
142    
143            }
144            while( state != HelloDone )
145            {
146                    switch (state)
147                    {
148                            case DhcpIsBound:
149                                    if ( ARPIsTxReady() && gotArp == 0 )
150                                            state = ArpIsTxReady;
151                                    else
152                                            state = ArpIsResolved;
153                                    break;
154                            
155                            case ArpIsTxReady:
156                                    ARPResolve(&rnode.IPAddr);
157                                    gotArp = 1;
158                                    state = ArpIsResolved;
159                                    break;
160                                                    
161                            case ArpIsResolved:
162                                    if ( ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){
163                                            state = SockOpening;
164                                    }
165                                    break;
166                            
167                            case SockOpening:
168                                    tsock1 = TCPConnect (&rnode, 3000);
169                                    state = SockOpen;
170                                    break;
171                    
172                            case SockOpen:
173                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
174                                            state = TcpSend;
175                                    break;
176                            
177                            case TcpSend:
178                                    TCPPut( tsock1, 0x00 );
179                                    TCPPut( tsock1, termid );
180                                    TCPFlush(tsock1);
181                                    //TCPDiscard( tsock1 );
182                                    state = HelloDone;
183                                    network_wait(5);
184                                    break;
185                    }
186                    network_worker();
187            }
188            return 0;
189    }
190    
191    char network_send_scan_frame( unsigned char stregkode[], unsigned char buflen )
192    {
193            while( state != StregkodeDone )
194            {
195                    switch (state)
196                    {
197                            case HelloDone:
198                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
199                                    state = TcpSend;
200                            case TcpSend:
201                                    TCPPut( tsock1, 0x01 );
202                                    for (i = 0; i < buflen; i++)
203                                    {
204                                            TCPPut(tsock1, stregkode[i] );
205                                    }
206                                    TCPFlush(tsock1);
207                                    state = StregkodeDone;
208                                    break;
209                    }
210                    network_worker();
211            }
212            return 0;
213    }      
214    char network_send_goodbye(void)
215    {
216            while( state != SockClosed)
217            {
218                    switch (state)
219                    {
220                            case TcpReadyToSend:
221                                    if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
222                                    state = TcpSend;
223                                    break;
224                                    
225                            case TcpSend:
226                                    TCPPut( tsock1, 0x06 );
227                                    TCPFlush( tsock1 );
228                                    state = SockClosing;
229                                    break;
230                            
231                            case SockClosing:
232                                    TCPDisconnect( tsock1 );
233                                    state = SockClosed;
234                                    break;
235                    }
236                    network_worker();
237            }
238            if( TCPIsConnected(tsock1) )
239                    return 3;                               // Didn't close the socket properly;
240            else
241                    return 0;
242            
243    }      
244    void network_wait_for_dhcp()
245    {
246            while (!DHCPIsBound())
247                    network_worker();
248    }      
249    
250  void main(void)  void main(void)
251  {  {
252      // Tick to blink SYSTEM led.      // Tick to blink SYSTEM led.
253      static TICK t = 0;      static TICK t = 0;
254      UDP_SOCKET usock1;      TCP_SOCKET usock1;
255      NODE_INFO rnode;      char sock_state = 0;
     char datagrams_to_send = 10;  
     char sock_open = 0;  
256      char is_resolved = 0;      char is_resolved = 0;
257        char has_arp = 0;
258        char a = 1;
259    
260                    
261        TICK tick;
262        TICK lastTick;
263            TICK diffTicks;
264    
265      /*      /*
266       * Initialize any application specific hardware.       * Initialize any application specific hardware.
# Line 166  void main(void) Line 280  void main(void)
280      InitAppConfig();      InitAppConfig();
281    
282      StackInit();      StackInit();
283        
284        network_init();
285    
 #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  
286    
287    
288      /*      /*
# Line 205  void main(void) Line 304  void main(void)
304          rnode.IPAddr.v[1] = 168;          rnode.IPAddr.v[1] = 168;
305          rnode.IPAddr.v[2] = 1;          rnode.IPAddr.v[2] = 1;
306          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;*/  
           
           
307    
       
308      while(1)      while(1)
309      {      {
310                unsigned char antal = 8;
311                unsigned char stregkode[] = "12345678";
312          /*          /*
313           * Blink SYSTEM LED every second.           * Blink SYSTEM LED every second.
314           */           */
# Line 238  void main(void) Line 330  void main(void)
330           * configuration since last reset.           * configuration since last reset.
331           */           */
332                    
333          if ( DHCPBindCount != myDHCPBindCount )                      a = network_send_hello( 0x01 );
334          {                          a = network_send_scan_frame( stregkode, antal );
335              DisplayIPValue(&AppConfig.MyIPAddr, TRUE);                          a = network_send_goodbye();
             myDHCPBindCount = DHCPBindCount;  
   
             if ( AppConfig.Flags.bIsDHCPEnabled )  
             {  
                 XLCDGoto(1, 14);  
                 if ( myDHCPBindCount < 0x0a )  
                     XLCDPut(myDHCPBindCount + '0');  
                 else  
                     XLCDPut(myDHCPBindCount + 'A');  
             }  
         }  
           
         if (DHCPIsBound())  
         {  
                 RA3 =0;  
                 if (sock_open == 0)  
                 {  
                         if (ARPIsTxReady())  
                         {  
                                 if (is_resolved == 0)  
                                 {  
                                         ARPResolve( &rnode.IPAddr);  
                                         is_resolved = 1;  
                                 }  
                                 else  
                                 {  
                                         if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))  
                                         //if (MyArp( &rnode))  
                                         {  
                                                 usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP  
                                                 sock_open=1;  
                                         }  
                                 }  
   
   
                                           
                         }  
                 }  
                   
                 if (UDPIsPutReady(usock1) && datagrams_to_send>0)  
                 {  
                         UDPPut( rnode.MACAddr.v[0] );  
                         UDPPut( rnode.MACAddr.v[1] );  
                         UDPPut( rnode.MACAddr.v[2] );  
                         UDPPut( rnode.MACAddr.v[3] );  
                         UDPPut( rnode.MACAddr.v[4] );  
                         UDPPut( rnode.MACAddr.v[5] );  
                         UDPPut('D');  
                         UDPPut('a');  
                         UDPPut('w');  
                         UDPPut(0);  
                         UDPFlush();  
                         datagrams_to_send--;  
                 }  
         }  
336      }      }
337  }  }
338    
339  #if defined(MCHP_C18)  void interrupt HighISR(void)
     #pragma interrupt HighISR save=section(".tmpdata")  
     void HighISR(void)  
 #elif defined(HITECH_C18)  
     #if defined(STACK_USE_SLIP)  
         extern void MACISR(void);  
     #endif  
     void interrupt HighISR(void)  
 #endif  
340  {  {
341      TickUpdate();      TickUpdate();
 /*  
 #if defined(STACK_USE_SLIP)  
     MACISR();  
 #endif  
 */  
342  }  }
343    
344  #if defined(MCHP_C18)  #if defined(MCHP_C18)
# Line 326  void HighVector (void) Line 350  void HighVector (void)
350  #pragma code /* return to default code section */  #pragma code /* return to default code section */
351  #endif  #endif
352    
 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);  
 }  
   
353  /*********************************************************************  /*********************************************************************
354   * Function:        void InitializeBoard(void)   * Function:        void InitializeBoard(void)
355   *   *
# Line 398  static void InitializeBoard(void) Line 377  static void InitializeBoard(void)
377      /*      /*
378       * LCD is enabled using RA5.       * LCD is enabled using RA5.
379       */       */
380      PORTA_RA5 = 0;          // Disable LCD.  //    PORTA_RA5 = 0;          // Disable LCD.
381    
382      /*      /*
383       * Turn off the LED's.       * Turn off the LED's.
# Line 412  static void InitializeBoard(void) Line 391  static void InitializeBoard(void)
391       */       */
392      INTCON2_RBPU = 0;      INTCON2_RBPU = 0;
393    
     XLCDInit();  
     XLCDGoto(0, 0);  
     XLCDPutROMString(StartupMsg);  
   
394      T0CON = 0;      T0CON = 0;
395      INTCON_GIEH = 1;      INTCON_GIEH = 1;
396      INTCON_GIEL = 1;      INTCON_GIEL = 1;
# Line 499  BOOL StringToIPAddress(char *str, IP_ADD Line 474  BOOL StringToIPAddress(char *str, IP_ADD
474      return (byteIndex == 3);      return (byteIndex == 3);
475  }  }
476    
   
 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.92  
changed lines
  Added in v.102

  ViewVC Help
Powered by ViewVC 1.1.20