--- trunk/PIC/Demo trimmet/TFTPcDemo.c 2007/05/16 09:24:33 92 +++ trunk/PIC/Demo trimmet/TFTPcDemo.c 2007/05/27 16:32:30 105 @@ -1,92 +1,27 @@ -/********************************************************************* - * - * 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. - */ #define THIS_IS_STACK_APPLICATION #include "stacktsk.h" #include "dhcp.h" -#include "xlcd.h" #include "tick.h" #include "delay.h" #include "udp.h" #include "arp.h" #include "arptsk.h" +#include "tcp.h" +#include + +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 +//#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" +/*#define STARTUP_MSG "G1_Build_0x00" ROM char StartupMsg[] = STARTUP_MSG; @@ -97,8 +32,8 @@ ROM char SetupMsg[] = "Board Setup..."; // 1234567890123456 -ROM char blankLCDLine[] = " "; - +//ROM char blankLCDLine[] = " "; +*/ /* * This is used by other stack elements. * Main application must define this and initialize it with @@ -134,19 +69,405 @@ */ static void InitAppConfig(void); static void InitializeBoard(void); -static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD); static void SetConfig(void); +/***************************** + Enummerations + ****************************/ +typedef enum _NetworkState +{ + off, + DhcpIsBound, + ArpIsTxReady, + 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>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); +} + +//Request ID 5 +char network_send_cancel_all(void) +{ + return network_send_cancel_generic(0x05); +} + +//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(); +} + void main(void) { // Tick to blink SYSTEM led. static TICK t = 0; - UDP_SOCKET usock1; - NODE_INFO rnode; - char datagrams_to_send = 10; - char sock_open = 0; + TCP_SOCKET usock1; + char sock_state = 0; char is_resolved = 0; + char has_arp = 0; + + TICK tick; + TICK lastTick; + TICK diffTicks; /* * Initialize any application specific hardware. @@ -166,24 +487,9 @@ InitAppConfig(); StackInit(); + + network_init(); -#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 /* @@ -205,18 +511,11 @@ rnode.IPAddr.v[1] = 168; rnode.IPAddr.v[2] = 1; 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;*/ - - - while(1) { + unsigned char antal = 8; + unsigned char stregkode[] = "12345678"; /* * Blink SYSTEM LED every second. */ @@ -238,83 +537,30 @@ * configuration since last reset. */ - if ( DHCPBindCount != myDHCPBindCount ) - { - DisplayIPValue(&AppConfig.MyIPAddr, TRUE); - 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--; - } - } + + network_send_hello( 0x01 ); + + strncpy(global_network_char, "12345678", 8); + network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 + + strncpy(global_network_char, "87654444", 8); + network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 + + network_send_calc_total(); + + network_send_cash_payed(50000); + + network_send_cancel_last(); + + network_send_cancel_all(); + + network_send_goodbye(); } } -#if defined(MCHP_C18) - #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 +void interrupt HighISR(void) { TickUpdate(); -/* -#if defined(STACK_USE_SLIP) - MACISR(); -#endif -*/ } #if defined(MCHP_C18) @@ -326,51 +572,6 @@ #pragma code /* return to default code section */ #endif -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); -} - /********************************************************************* * Function: void InitializeBoard(void) * @@ -398,7 +599,7 @@ /* * LCD is enabled using RA5. */ - PORTA_RA5 = 0; // Disable LCD. +// PORTA_RA5 = 0; // Disable LCD. /* * Turn off the LED's. @@ -412,10 +613,6 @@ */ INTCON2_RBPU = 0; - XLCDInit(); - XLCDGoto(0, 0); - XLCDPutROMString(StartupMsg); - T0CON = 0; INTCON_GIEH = 1; INTCON_GIEL = 1; @@ -499,21 +696,3 @@ return (byteIndex == 3); } - -void XLCDDelay15ms(void) -{ - DelayMs(15); -} -void XLCDDelay4ms(void) -{ - DelayMs(4); -} - -void XLCDDelay100us(void) -{ - INTCON_GIEH = 0; - Delay10us(1); - INTCON_GIEH = 1; -} - -