/[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 98 by hedin, Tue May 22 14:45:39 2007 UTC revision 102 by hedin, Fri May 25 16:00:14 2007 UTC
# Line 10  Line 10 
10  #include "tcp.h"  #include "tcp.h"
11    
12  bit bIsDhcpUp = 0;  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
# Line 18  bit bIsDhcpUp = 0; Line 19  bit bIsDhcpUp = 0;
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 30  ROM char SetupMsg[] = "Board Setup..."; Line 31  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 81  typedef enum _NetworkState Line 82  typedef enum _NetworkState
82          SockOpen,          SockOpen,
83          TcpReadyToSend,          TcpReadyToSend,
84          TcpSend,          TcpSend,
85            HelloDone,
86            StregkodeDone,
87          SockClosing,          SockClosing,
88          SockClosed          SockClosed
89  } NetworkState;  } NetworkState;
# Line 106  void network_init(void) Line 109  void network_init(void)
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 )  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.  char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.
132  static char isBound = 0;  static char isBound = 0;
133          if ( DHCPIsBound() && isBound == 0 ){  
134                  state = DhcpIsBound;  while( !DHCPIsBound() && isBound == 0 )
135                  isBound = 1;          network_worker();
136    
137    //      while( state != DhcpIsBound ){
138                    if ( DHCPIsBound() && isBound == 0 ){
139                            state = DhcpIsBound;
140                            isBound = 1;
141    //              }
142    
143          }          }
144          switch (state)          while( state != HelloDone )
145          {          {
146                  case DhcpIsBound:                  switch (state)
147                          if ( ARPIsTxReady() && gotArp == 0 )                  {
148                                  state = ArpIsTxReady;                          case DhcpIsBound:
149                          else                                  if ( ARPIsTxReady() && gotArp == 0 )
150                                  state = ArpIsResolved;                                          state = ArpIsTxReady;
151                          return 0;                                  else
                   
                 case ArpIsTxReady:  
                         ARPResolve(&rnode.IPAddr);  
                         gotArp = 1;  
                         state = ArpIsResolved;  
                         return 0;  
                                           
                 case ArpIsResolved:  
                         if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){  
                                 state = SockOpening;  
                                 return 0;  
                                 }  
                                 else{  
152                                          state = ArpIsResolved;                                          state = ArpIsResolved;
153                                          return 2;                                                                       // no arp resolved                                  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 SockOpening:                          case SockOpen:
173                          tsock1 = TCPConnect (&rnode, 3000);                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
174                          state = SockOpen;                                          state = TcpSend;
175                          return 0;                                  break;
           
                 case SockOpen:  
                         if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                         state = TcpSend;  
                         return 0;  
                   
                 case TcpSend:  
                         TCPPut( tsock1, 0x00 );  
                         TCPPut( tsock1, termid );  
                         TCPFlush(tsock1);  
                         TCPDiscard( tsock1 );  
176                                                    
177                            case TcpSend:
178                          state = TcpReadyToSend;                                  TCPPut( tsock1, 0x00 );
179                          return 0;                                  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 1;          return 0;
189  }  }
190    
191  char network_send_scan_frame( unsigned char stregkode, unsigned char buflen )  char network_send_scan_frame( unsigned char stregkode[], unsigned char buflen )
192  {  {
193          switch (state)          while( state != StregkodeDone )
194          {          {
195                          TCPPut( tsock1, 0x01 );                  switch (state)
196                          for (i = buflen; i >= 0; i--)                  {
197                          {                          case HelloDone:
198                                  TCPPut(tsock1, stregkode );                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
199                          }                                  state = TcpSend;
200                          TCPFlush(tsock1);                          case TcpSend:
201                          state = TcpReadyToSend;                                  TCPPut( tsock1, 0x01 );
202                          return 1;                                  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)  char network_send_goodbye(void)
215  {  {
216          switch (state)          while( state != SockClosed)
217          {          {
218                  case TcpReadyToSend:                  switch (state)
219                          if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )                  {
220                          state = TcpSend;                          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 TcpSend:                          case SockClosing:
232                          TCPPut( tsock1, 0x06 );                                  TCPDisconnect( tsock1 );
233                          TCPFlush( tsock1 );                                  state = SockClosed;
234                          state = SockClosing;                                  break;
235                          return 0;                  }
236                                    network_worker();
                 case SockClosing:  
                         TCPDisconnect( tsock1 );  
                         state = SockClosed;  
                         return 1;  
237          }          }
238          return 3;                               // Didn't close the socket properly;          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)
# Line 206  void main(void) Line 255  void main(void)
255      char sock_state = 0;      char sock_state = 0;
256      char is_resolved = 0;      char is_resolved = 0;
257      char has_arp = 0;      char has_arp = 0;
258      char a;      char a = 1;
259    
260                    
261      TICK tick;      TICK tick;
262      TICK lastTick;      TICK lastTick;
# Line 279  void main(void) Line 329  void main(void)
329           * For DHCP information, display how many times we have renewed the IP           * For DHCP information, display how many times we have renewed the IP
330           * configuration since last reset.           * configuration since last reset.
331           */           */
332            
333          a = network_send_hello( 0x01 );                      a = network_send_hello( 0x01 );
334  //              a = network_send_scan_frame( stregkode, antal );                          a = network_send_scan_frame( stregkode, antal );
335                  a = network_send_goodbye();                          a = network_send_goodbye();
   
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)

Legend:
Removed from v.98  
changed lines
  Added in v.102

  ViewVC Help
Powered by ViewVC 1.1.20