--- trunk/PIC/Demo trimmet/TFTPcDemo.c 2007/05/16 09:24:33 92 +++ trunk/PIC/Demo trimmet/TFTPcDemo.c 2007/05/16 15:46:08 93 @@ -1,72 +1,3 @@ -/********************************************************************* - * - * 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" @@ -77,6 +8,7 @@ #include "udp.h" #include "arp.h" #include "arptsk.h" +#include "tcp.h" // All TFTP command statuts display will be done on first line of LCD. @@ -137,16 +69,72 @@ static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD); static void SetConfig(void); +/*void network_send_hello( unsigned char termid ) +{ + 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--; + } + } +} +*/ + +void WriteTcpStatus(char status) +{ + XLCDGoto(0,0); + status += 'a'; + XLCDPut(status); + +} + void main(void) { // Tick to blink SYSTEM led. static TICK t = 0; - UDP_SOCKET usock1; + TCP_SOCKET usock1; NODE_INFO rnode; - char datagrams_to_send = 10; - char sock_open = 0; + char sock_state = 0; char is_resolved = 0; + char has_arp = 0; + TICK tick; + TICK lastTick; + TICK diffTicks; /* * Initialize any application specific hardware. @@ -253,10 +241,12 @@ } } + WriteTcpStatus( TCB[usock1].smState ); + if (DHCPIsBound()) { RA3 =0; - if (sock_open == 0) + if (has_arp == 0) { if (ARPIsTxReady()) { @@ -268,33 +258,44 @@ else { if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr)) - //if (MyArp( &rnode)) { - usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP - sock_open=1; + has_arp = 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--; - } + 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; + + } } } }