/[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 105 by hedin, Sun May 27 16:32:30 2007 UTC revision 113 by hedin, Mon May 28 16:28:28 2007 UTC
# Line 1  Line 1 
1  #define THIS_IS_STACK_APPLICATION  #define THIS_IS_STACK_APPLICATION
2    
3  #include "stacktsk.h"  #include "stacktsk.h"
 #include "dhcp.h"  
4  #include "tick.h"  #include "tick.h"
5  #include "delay.h"  #include "delay.h"
6  #include "udp.h"  #include "network.h"
7  #include "arp.h"  #include "barcode.h"
8  #include "arptsk.h"  #include "keyboard.h"
9  #include "tcp.h"  #include "MyI2C.h"
10    #include "xlcd.h"
11  #include <string.h>  #include <string.h>
12    
 bit bIsDhcpUp = 0;  
 char netlevel = 0;  
   
 // All TFTP command statuts display will be done on first line of LCD.  
 //#define TFTP_COMMAND_DISPLAY_LINE       0  
   
 // Result will be displayed at y = 15.  
 #define TFTP_COMMAND_RESULT_POSITION    15  
   
   
 /*#define STARTUP_MSG "G1_Build_0x00"  
   
 ROM char StartupMsg[] = STARTUP_MSG;  
   
 #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)  
 ROM char DHCPMsg[] = "DHCP/Gleaning...";  
 #endif  
   
 ROM char SetupMsg[] = "Board Setup...";  
   
 //                         1234567890123456  
 //ROM char blankLCDLine[] = "                ";  
 */  
13  /*  /*
14   * This is used by other stack elements.   * This is used by other stack elements.
15   * Main application must define this and initialize it with   * Main application must define this and initialize it with
# Line 71  static void InitAppConfig(void); Line 47  static void InitAppConfig(void);
47  static void InitializeBoard(void);  static void InitializeBoard(void);
48  static void SetConfig(void);  static void SetConfig(void);
49    
50  /*****************************  // Skal fjernes
51   Enummerations  #define LCD_IC 0x74
52   ****************************/  void mylcd_write_byte(unsigned char data)
53  typedef enum _NetworkState  {
54  {          StartI2C(LCD_IC);
55          off,          WriteI2CByte( data );
56          DhcpIsBound,          StopI2C();
57          ArpIsTxReady,          Delay10us(1); //I2C spec's siger min 4.7 uSec mellem stop og næste start
         ArpIsResolved,  
         SockOpening,  
         SockOpen,  
         TcpReadyToSend,  
         TcpSend,  
         HelloDone,  
         StregkodeSendDone,  
         ReadyToRecieve,  
         StregkodeRecieveDone,  
         TotalRequested,  
         TotalRequestedDone,  
         ChangeRequested,  
         ChangeRequestedDone,  
         Acknowledged,  
         CancelDone,  
         SockClosing,  
         SockClosed  
 } NetworkState;  
   
 /*****************************  
  GLOBAL INIT  
  ****************************/  
 NODE_INFO rnode;  
 NetworkState state;  
 TCP_SOCKET tsock1;  
   
   
 /*****************************  
  network funktions  
  ****************************/  
 unsigned long global_network_amount;  
 unsigned char global_network_char[16];  
 unsigned char global_network_charlen;  
   
   
 void network_init(void)  
 {  
         state = off;  
 }        
   
   
 void network_worker(void)  
 {      
         TickGet();  
     StackTask();      
 }        
   
 void network_parse_amount(unsigned char buf[])  
 {  
         global_network_amount = buf[0]<<24 + buf[1]<<16 + buf[2]<<8 + buf[3];  
 }        
   
 void network_read_amount(void)  
 {  
         unsigned char buf[4];  
         TCPGetArray(tsock1, buf,4);  
         network_parse_amount(buf);  
 }        
   
 void network_wait(unsigned char n)  
 {  
         int i;  
         for (i=0; i<n; i++)  
         {  
                 network_worker();  
                 Delay10us(1);  
         }  
 }        
   
 char network_read_ack(void)  
 {  
         char ack;  
         while(state != Acknowledged)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         TCPDiscard(tsock1);  
                         state = Acknowledged;  
                 }  
                 network_worker();  
         }  
           
         state = TcpReadyToSend;  
           
         if (ack == 1)  
                 return 0;  
         else  
                 return 4;  
 }        
   
 //Request ID 0  
 char network_send_hello( unsigned char termid )  
 {  
         char gotArp = 0;                // if gotArp is 0, the embedded haven't talked to the server since boot.  
         static char isBound = 0;  
   
         while( !DHCPIsBound() && isBound == 0 )  
                 network_worker();  
   
         if ( DHCPIsBound() && isBound == 0 ){  
                 state = DhcpIsBound;  
                 isBound = 1;  
         }  
           
         while( state != HelloDone )  
         {  
                 switch (state)  
                 {  
                         case DhcpIsBound:  
                                 if ( ARPIsTxReady() && gotArp == 0 )  
                                         state = ArpIsTxReady;  
                                 else  
                                         state = ArpIsResolved;  
                                 break;  
                           
                         case ArpIsTxReady:  
                                 ARPResolve(&rnode.IPAddr);  
                                 gotArp = 1;  
                                 state = ArpIsResolved;  
                                 break;  
                                                   
                         case ArpIsResolved:  
                                 if ( ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){  
                                         state = SockOpening;  
                                 }  
                                 break;  
                           
                         case SockOpening:  
                                 tsock1 = TCPConnect (&rnode, 3000);  
                                 state = SockOpen;  
                                 break;  
                   
                         case SockOpen:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                           
                         case TcpSend:  
                                 TCPPut( tsock1, 0x00 );  
                                 TCPPut( tsock1, termid );  
                                 TCPFlush(tsock1);  
                                 //TCPDiscard( tsock1 );  
                                 state = HelloDone;  
                                 break;  
                 }  
                 network_worker();  
         }  
         return network_read_ack();  
 }  
   
 //Request ID 1  
 char network_send_scan_frame( unsigned char antal, unsigned char buflen )  
 {  
         char i;  
         char ack;  
   
         while( state != StregkodeSendDone ) //Sender vare-request-info pakke  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                 state = TcpSend;  
                         case TcpSend:  
                                 TCPPut( tsock1, 0x01 );  
                                 TCPPut( tsock1, antal );  
                                 TCPPut( tsock1, buflen );  
                                 for (i = 0; i < buflen; i++)  
                                 {  
                                         TCPPut(tsock1, global_network_char[i] );  
                                 }  
                                 TCPFlush(tsock1);  
                                 state = StregkodeSendDone;  
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while( state != StregkodeRecieveDone ) //venter på svar og parser denne  
         {  
                 switch( state )  
                 {  
                         case StregkodeSendDone:  
                                 if( TCPIsGetReady(tsock1) )  
                                         state = ReadyToRecieve;  
                                 break;  
                         case ReadyToRecieve:  
                                 TCPGet( tsock1, &ack );  
                                   
                                 if (ack == 0)  
                                 {  
                                         state = TcpReadyToSend;  
                                         TCPDiscard(tsock1);  
                                         return 4;  
                                 }  
                                   
                                 network_read_amount();  
                                 TCPGet( tsock1, &global_network_charlen);  
                                 TCPGetArray( tsock1, global_network_char, global_network_charlen);  
                                 TCPDiscard(tsock1);  
                                   
                                 state = StregkodeRecieveDone;                            
                                           
                                 break;  
                 }  
                 network_worker();  
         }  
         state = TcpReadyToSend;  
         return 0;  
 }  
   
 //Request ID 2  
 char network_send_calc_total(void)  
 {  
         unsigned char ack;  
         while(state != TotalRequested)  
         {  
                 switch(state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                                   
                         case TcpSend:  
                                 TCPPut(tsock1, 0x02);  
                                 TCPFlush(tsock1);  
                                 state = TotalRequested;  
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while (state != TotalRequestedDone)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         if (ack == 0)  
                         {  
                                 TCPDiscard(tsock1);  
                                 state = TcpReadyToSend;  
                                 return 4;  
                         }  
                         network_read_amount();  
                         TCPDiscard(tsock1);  
                         state = TotalRequestedDone;  
                 }  
                 network_worker();  
         }  
           
         state = TcpReadyToSend;  
         return 0;  
 }        
   
 //Request ID 3  
 char network_send_cash_payed(unsigned long amount)  
 {  
         unsigned char ack;  
         while (state != ChangeRequested)  
         {  
                 switch(state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                         case TcpSend:  
                                 TCPPut(tsock1, 0x03);  
                                 TCPPut(tsock1, amount>>24);  
                                 TCPPut(tsock1, amount>>16);  
                                 TCPPut(tsock1, amount>>8);  
                                 TCPPut(tsock1, amount);  
                                 TCPFlush(tsock1);  
                                 state = ChangeRequested;          
                                 break;  
                 }  
                 network_worker();  
         }  
           
         while (state != ChangeRequestedDone)  
         {  
                 if (TCPIsGetReady(tsock1))  
                 {  
                         TCPGet(tsock1, &ack);  
                         if (ack == 0)  
                         {  
                                 TCPFlush(tsock1);  
                                 state = TcpReadyToSend;  
                                 return 4;  
                         }  
                         network_read_amount();  
                         TCPFlush(tsock1);  
                         state = ChangeRequestedDone;  
                           
                 }  
                 network_worker();  
         }  
         state = TcpReadyToSend;  
         return 0;  
 }  
   
 //request 4+5  
 char network_send_cancel_generic(char id)  
 {  
         while(state != CancelDone)  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                         case TcpSend:  
                                 TCPPut(tsock1, id);  
                                 TCPFlush(tsock1);  
                                 state = CancelDone;  
   
                 }  
                 network_worker();  
         }  
         return network_read_ack();  
 }        
   
 //Request ID 4  
 char network_send_cancel_last(void)  
 {  
         return network_send_cancel_generic(0x04);        
58  }  }
59    
 //Request ID 5  
 char network_send_cancel_all(void)  
 {  
         return network_send_cancel_generic(0x05);  
 }        
60    
 //Request ID 6  
 char network_send_goodbye(void)  
 {  
         char ack;  
         while( state != SockClosed)  
         {  
                 switch (state)  
                 {  
                         case TcpReadyToSend:  
                                 if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )  
                                         state = TcpSend;  
                                 break;  
                                   
                         case TcpSend:  
                                 TCPPut( tsock1, 0x06 );  
                                 TCPFlush( tsock1 );  
                                 state = SockClosing;  
                                 break;  
                           
                         case SockClosing:  
                                 ack = network_read_ack();  
                                 TCPDisconnect( tsock1 );  
                                 state = SockClosed;  
                                 break;  
                 }  
                 network_worker();  
         }  
         if( TCPIsConnected(tsock1))  
                 return 3;                               // Didn't close the socket properly;  
         else  
                 return ack;  
           
 }        
 void network_wait_for_dhcp()  
 {  
         while (!DHCPIsBound())  
                 network_worker();  
 }        
61    
62  void main(void)  void main(void)
63  {  {
64      // Tick to blink SYSTEM led.      // Tick to blink SYSTEM led.
65      static TICK t = 0;      static TICK t = 0;
66      TCP_SOCKET usock1;      char i;
67      char sock_state = 0;      unsigned char numbers[20];
68      char is_resolved = 0;      unsigned long amount;
69      char has_arp = 0;      unsigned char lcd_buffer[21];
70            short number_count = 0;
71            short produkt_amount = 0;
72            static bit hello = 0;
73            short varetal = 1;
74                    
75      TICK tick;      TICK tick;
76      TICK lastTick;      TICK lastTick;
# Line 489  void main(void) Line 96  void main(void)
96      StackInit();      StackInit();
97            
98      network_init();      network_init();
99        barcode_init();
100        InitI2C();
101        XLCDInit();
102            
103    
104    
105      /*      /*
# Line 506  void main(void) Line 116  void main(void)
116       */       */
117            
118    
119          // Defines the IP add. on the server machine          // Defines the IP add. on the server machine (rnode is defined in network.h)
120          rnode.IPAddr.v[0] = 192;          rnode.IPAddr.v[0] = 192;
121          rnode.IPAddr.v[1] = 168;          rnode.IPAddr.v[1] = 168;
122          rnode.IPAddr.v[2] = 1;          rnode.IPAddr.v[2] = 1;
# Line 514  void main(void) Line 124  void main(void)
124    
125      while(1)      while(1)
126      {      {
127              unsigned char antal = 8;              char keyboard_entered;
128              unsigned char stregkode[] = "12345678";              char network_error;
129    
130    
131          /*          /*
132           * Blink SYSTEM LED every second.           * Blink SYSTEM LED every second.
133           */           */
# Line 536  void main(void) Line 148  void main(void)
148           * For DHCP information, display how many times we have renewed the IP           * For DHCP information, display how many times we have renewed the IP
149           * configuration since last reset.           * configuration since last reset.
150           */           */
151            
152                                    //mylcd_write_byte(0b10100101);
153                      network_send_hello( 0x01 );  
154                                                if( (barcode_is_ready() || keyboard_entered) && !hello){
155                      strncpy(global_network_char, "12345678", 8);                                  network_send_hello(0x01);
156                                    hello = 1;
157                            }
158                            if (barcode_is_ready())
159                            {
160                                    for (i=0;i< barcode_get_length(); i++)
161                                    {
162                                            global_network_char[i] = global_barcode_buffer[i];
163                                            global_network_charlen = barcode_get_length();
164                                    }
165                                    barcode_reset();
166                                    network_send_scan_frame( varetal, global_network_charlen );
167                                    varetal = 1;
168    /*
169                                    XLCDClear();
170                                    XLCDGoto(1,0);
171                                    for (i=0;i< barcode_get_length(); i++)
172                                    {
173                                            XLCDPut(global_barcode_buffer[i]);
174                                    }
175    */
176                            }
177                            
178                            keyboard_entered = keyboard_read_safe();
179                            if( keyboard_entered )
180                            {
181                                    switch( keyboard_entered )
182                                    {
183                                            case 'A':
184                                            network_send_calc_total();
185                                            XLCDClear();
186                                            XLCDGoto(0,0);
187                                            for( i=0; i<global_network_charlen; i++ )
188                                            {
189                                                    XLCDPut(global_network_char[i]);
190                                            }
191                                            break;
192                                            
193                                            case 'B':
194                                            numbers[number_count] = 0; //nulterminering inden brug af atol
195                                            amount = atol(numbers);
196                                            number_count = 0;
197                                            
198                                            network_send_cash_payed(amount);
199                                            network_send_goodbye();
200                                            hello = 0;
201                                            
202                                            sprintf(lcd_buffer,"%5d", amount);
203                                            XLCDGoto(0,0);
204                                            XLCDPutString(lcd_buffer);
205                                            break;
206                                            
207                                            case 'C':
208                                            network_send_cancel_last();
209                                            break;
210                                            
211                                            case 'D':
212                                            network_send_cancel_all();              
213                                            network_send_goodbye();
214                                            hello = 0;                      
215                                            break;
216                                            
217                                            case 'E':
218                                            for( i=0; i<number_count;i++ )
219                                            {
220                                                    global_network_char[i] = numbers[i];
221                                            }
222                                            network_send_scan_frame( varetal, number_count );
223                                            varetal = 1;
224                                            number_count = 0;
225                                            break;
226                                            
227                                            case 'F':
228                                            numbers[number_count] = 0; //nulterminering inden brug af atol
229                                            varetal = atol(numbers);
230                                            number_count = 0;
231                                            break;
232    
233                                            default:
234                                            numbers[number_count] = keyboard_entered;
235                                            
236                                            //numbers; // got to catch 0-9; 20 byte array.
237                                            number_count++; // amount of used bytes in numbers.
238                                    }
239                                    
240                            }
241    /*
242                            network_send_hello( 0x01 );
243    
244                            strncpy(global_network_char, "12345678", 8);
245                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
246                                                    
247                          strncpy(global_network_char, "87654444", 8);                          strncpy(global_network_char, "87654444", 8);
248                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8                          network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8
249                            
250                          network_send_calc_total();                          network_send_calc_total();
251                                                    
252                          network_send_cash_payed(50000);                          network_send_cash_payed(50000);
# Line 555  void main(void) Line 256  void main(void)
256                          network_send_cancel_all();                          network_send_cancel_all();
257                                                                                                    
258                          network_send_goodbye();                          network_send_goodbye();
259     */
260      }      }
261  }  }
262    
263  void interrupt HighISR(void)  void interrupt HighISR(void)
264  {  {
265      TickUpdate();          if (RCIF == 1)
266                    barcode_interrupt();
267            else
268            TickUpdate();
269  }  }
270    
271  #if defined(MCHP_C18)  
 #pragma code highVector=0x08  
 void HighVector (void)  
 {  
     _asm goto HighISR _endasm  
 }  
 #pragma code /* return to default code section */  
 #endif  
272    
273  /*********************************************************************  /*********************************************************************
274   * Function:        void InitializeBoard(void)   * Function:        void InitializeBoard(void)
# Line 696  BOOL StringToIPAddress(char *str, IP_ADD Line 394  BOOL StringToIPAddress(char *str, IP_ADD
394      return (byteIndex == 3);      return (byteIndex == 3);
395  }  }
396    
397    void XLCDDelay15ms(void)
398    {
399        DelayMs(15);
400    }
401    void XLCDDelay4ms(void)
402    {
403        DelayMs(4);
404    }
405    
406    void XLCDDelay100us(void)
407    {
408        INTCON_GIEH = 0;
409        Delay10us(1);
410        INTCON_GIEH = 1;
411    }

Legend:
Removed from v.105  
changed lines
  Added in v.113

  ViewVC Help
Powered by ViewVC 1.1.20