/[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 96 by hedin, Tue May 22 06:41:06 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 104  void network_init(void) Line 107  void network_init(void)
107  {  {
108          state = off;          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 )  char network_send_hello( unsigned char termid )
130  {  {
131  char error = 0;  char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.
 char gotArp = 0;  
132  static char isBound = 0;  static char isBound = 0;
133    
134  if ( DHCPIsBound() && isBound == 0 ){  while( !DHCPIsBound() && isBound == 0 )
135          state = DhcpIsBound;          network_worker();
136          isBound = 1;  
137  }  //      while( state != DhcpIsBound ){
138                    if ( DHCPIsBound() && isBound == 0 ){
139                            state = DhcpIsBound;
140                            isBound = 1;
141    //              }
142    
143          switch (state)          }
144            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;
176                                    
177                  case SockOpen:                          case TcpSend:
178                          if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )                                  TCPPut( tsock1, 0x00 );
179                          state = TcpSend;                                  TCPPut( tsock1, termid );
180                          return 0;                                  TCPFlush(tsock1);
181                                                    //TCPDiscard( tsock1 );
182                  case TcpSend:                                  state = HelloDone;
183                          for (i = net_INT; i >= 0; i--)                                  network_wait(5);
184                          {                                  break;
185                                  TCPPut(tsock1, termid );                  }
186                          }                  network_worker();
                         TCPFlush(tsock1);  
                         state = TcpReadyToSend;  
                         return 0;  
187          }          }
188          return 1;          return 0;
189  }  }
190  /*  
191  void network_send_hello( unsigned char termid )  char network_send_scan_frame( unsigned char stregkode[], unsigned char buflen )
192  {  {
193             if (DHCPIsBound())          while( state != StregkodeDone )
194          {          {
195                  RA3 = 0;                  switch (state)
196                  if (sock_open == 0)                  {
197                  {                          case HelloDone:
198                          if (ARPIsTxReady())                                  if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
199                          {                                  state = TcpSend;
200                                  ARPResolve( &rnode.IPAddr);                          case TcpSend:
201                                  is_resolved = 1;                                  TCPPut( tsock1, 0x01 );
202                              }                                  for (i = 0; i < buflen; i++)
203                                  else                                  {
204                                  {                                          TCPPut(tsock1, stregkode[i] );
205                                          if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))                                  }
206                                          //if (MyArp( &rnode))                                  TCPFlush(tsock1);
207                                          {                                  state = StregkodeDone;
208                                                  usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP                                  break;
                                                 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--;  
                 }  
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  {  {
# Line 213  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 264  void main(void) Line 307  void main(void)
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 284  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( 1 );                      a = network_send_hello( 0x01 );
334     /*     if (DHCPIsBound())                          a = network_send_scan_frame( stregkode, antal );
335          {                          a = network_send_goodbye();
                 RA3 =0;  
                 if (has_arp == 0)  
                 {  
                         if (ARPIsTxReady())  
                         {  
                                 if (is_resolved == 0)  
                                 {  
                                         ARPResolve( &rnode.IPAddr);  
                                         is_resolved = 1;  
                                 }  
                                 else  
                                 {  
                                         if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))  
                                         {  
                                                         has_arp = 1;  
                                         }  
                                 }  
           
                         }  
                 }  
                   
                 if (has_arp == 1)  
                 {  
                         if (sock_state == 0)  
                         {  
                         usock1=TCPConnect(&rnode,3000);  
                                 sock_state = 1;  
                         }  
                 /*      if (sock_state ==1 && TCPIsConnected(usock1) && TCPIsPutReady(usock1))  
                         {  
                                 TCPPut(usock1,'D');  
                                 TCPPut(usock1,'a');  
                                 TCPPut(usock1,'w');  
                                 TCPPut(usock1,0);  
                         TCPFlush(usock1);  
                                 sock_state = 2;  
                         }  
                   
                         if (sock_state == 2 && TCPIsPutReady(usock1))  
                     {  
                                 TCPDisconnect(usock1);  
                                 lastTick = TickGet();  
                                 sock_state =3;  
                         }  
                         tick = TickGet();  
                         diffTicks = TickGetDiff(tick, lastTick);  
                           
                         if ( sock_state == 3 && diffTicks > ((TICK)TICK_SECOND * (TICK)2) )  
                                 sock_state = 0;  
                                   
                     }  
         } */  
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.96  
changed lines
  Added in v.102

  ViewVC Help
Powered by ViewVC 1.1.20