|
/********************************************************************* |
|
|
* |
|
|
* 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. |
|
|
*/ |
|
1 |
#define THIS_IS_STACK_APPLICATION |
#define THIS_IS_STACK_APPLICATION |
2 |
|
|
3 |
#include "stacktsk.h" |
#include "stacktsk.h" |
|
#include "dhcp.h" |
|
|
#include "xlcd.h" |
|
4 |
#include "tick.h" |
#include "tick.h" |
5 |
#include "delay.h" |
#include "delay.h" |
6 |
#include "udp.h" |
#include "network.h" |
|
#include "arp.h" |
|
|
#include "arptsk.h" |
|
|
|
|
|
|
|
|
// 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 |
|
7 |
|
|
8 |
|
#include <string.h> |
9 |
|
|
|
#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[] = " "; |
|
10 |
|
|
11 |
/* |
/* |
12 |
* This is used by other stack elements. |
* This is used by other stack elements. |
43 |
*/ |
*/ |
44 |
static void InitAppConfig(void); |
static void InitAppConfig(void); |
45 |
static void InitializeBoard(void); |
static void InitializeBoard(void); |
|
static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD); |
|
46 |
static void SetConfig(void); |
static void SetConfig(void); |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
void main(void) |
void main(void) |
51 |
{ |
{ |
52 |
// Tick to blink SYSTEM led. |
// Tick to blink SYSTEM led. |
53 |
static TICK t = 0; |
static TICK t = 0; |
54 |
UDP_SOCKET usock1; |
|
|
NODE_INFO rnode; |
|
|
char sock_open = 0; |
|
|
char is_resolved = 0; |
|
55 |
|
|
56 |
|
TICK tick; |
57 |
|
TICK lastTick; |
58 |
|
TICK diffTicks; |
59 |
|
|
60 |
/* |
/* |
61 |
* Initialize any application specific hardware. |
* Initialize any application specific hardware. |
75 |
InitAppConfig(); |
InitAppConfig(); |
76 |
|
|
77 |
StackInit(); |
StackInit(); |
78 |
|
|
79 |
|
network_init(); |
80 |
|
|
|
#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 |
|
81 |
|
|
82 |
|
|
83 |
/* |
/* |
99 |
rnode.IPAddr.v[1] = 168; |
rnode.IPAddr.v[1] = 168; |
100 |
rnode.IPAddr.v[2] = 1; |
rnode.IPAddr.v[2] = 1; |
101 |
rnode.IPAddr.v[3] = 20; |
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;*/ |
|
|
|
|
|
|
|
102 |
|
|
|
|
|
103 |
while(1) |
while(1) |
104 |
{ |
{ |
105 |
/* |
/* |
123 |
* configuration since last reset. |
* configuration since last reset. |
124 |
*/ |
*/ |
125 |
|
|
126 |
if ( DHCPBindCount != myDHCPBindCount ) |
|
127 |
{ |
network_send_hello( 0x01 ); |
128 |
DisplayIPValue(&AppConfig.MyIPAddr, TRUE); |
|
129 |
myDHCPBindCount = DHCPBindCount; |
strncpy(global_network_char, "12345678", 8); |
130 |
|
network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 |
131 |
if ( AppConfig.Flags.bIsDHCPEnabled ) |
|
132 |
{ |
strncpy(global_network_char, "87654444", 8); |
133 |
XLCDGoto(1, 14); |
network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 |
134 |
if ( myDHCPBindCount < 0x0a ) |
|
135 |
XLCDPut(myDHCPBindCount + '0'); |
network_send_calc_total(); |
136 |
else |
|
137 |
XLCDPut(myDHCPBindCount + 'A'); |
network_send_cash_payed(50000); |
138 |
} |
|
139 |
} |
network_send_cancel_last(); |
140 |
|
|
141 |
if (DHCPIsBound()) |
network_send_cancel_all(); |
142 |
{ |
|
143 |
RA3 =0; |
network_send_goodbye(); |
|
if (sock_open == 0) |
|
|
{ |
|
|
if (ARPIsTxReady()) |
|
|
{ |
|
|
if (is_resolved == 0) |
|
|
{ |
|
|
ARPResolve( &rnode.IPAddr); |
|
|
is_resolved = 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr)) |
|
|
{ |
|
|
usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP |
|
|
sock_open=1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
if (UDPIsPutReady(usock1)) |
|
|
{ |
|
|
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(); |
|
|
} |
|
|
} |
|
144 |
} |
} |
145 |
} |
} |
146 |
|
|
147 |
#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 |
|
148 |
{ |
{ |
149 |
TickUpdate(); |
TickUpdate(); |
|
/* |
|
|
#if defined(STACK_USE_SLIP) |
|
|
MACISR(); |
|
|
#endif |
|
|
*/ |
|
|
} |
|
|
|
|
|
#if defined(MCHP_C18) |
|
|
#pragma code highVector=0x08 |
|
|
void HighVector (void) |
|
|
{ |
|
|
_asm goto HighISR _endasm |
|
150 |
} |
} |
|
#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('.'); |
|
|
} |
|
151 |
|
|
|
itoa(IPVal->v[1], IPDigit); |
|
|
if ( bToLCD ) |
|
|
{ |
|
|
XLCDPutString(IPDigit); |
|
|
XLCDPut('.'); |
|
|
} |
|
152 |
|
|
|
itoa(IPVal->v[2], IPDigit); |
|
|
if ( bToLCD ) |
|
|
{ |
|
|
XLCDPutString(IPDigit); |
|
|
XLCDPut('.'); |
|
|
} |
|
|
|
|
|
itoa(IPVal->v[3], IPDigit); |
|
|
if ( bToLCD ) |
|
|
XLCDPutString(IPDigit); |
|
|
} |
|
153 |
|
|
154 |
/********************************************************************* |
/********************************************************************* |
155 |
* Function: void InitializeBoard(void) |
* Function: void InitializeBoard(void) |
178 |
/* |
/* |
179 |
* LCD is enabled using RA5. |
* LCD is enabled using RA5. |
180 |
*/ |
*/ |
181 |
PORTA_RA5 = 0; // Disable LCD. |
// PORTA_RA5 = 0; // Disable LCD. |
182 |
|
|
183 |
/* |
/* |
184 |
* Turn off the LED's. |
* Turn off the LED's. |
192 |
*/ |
*/ |
193 |
INTCON2_RBPU = 0; |
INTCON2_RBPU = 0; |
194 |
|
|
|
XLCDInit(); |
|
|
XLCDGoto(0, 0); |
|
|
XLCDPutROMString(StartupMsg); |
|
|
|
|
195 |
T0CON = 0; |
T0CON = 0; |
196 |
INTCON_GIEH = 1; |
INTCON_GIEH = 1; |
197 |
INTCON_GIEL = 1; |
INTCON_GIEL = 1; |
275 |
return (byteIndex == 3); |
return (byteIndex == 3); |
276 |
} |
} |
277 |
|
|
|
|
|
|
void XLCDDelay15ms(void) |
|
|
{ |
|
|
DelayMs(15); |
|
|
} |
|
|
void XLCDDelay4ms(void) |
|
|
{ |
|
|
DelayMs(4); |
|
|
} |
|
|
|
|
|
void XLCDDelay100us(void) |
|
|
{ |
|
|
INTCON_GIEH = 0; |
|
|
Delay10us(1); |
|
|
INTCON_GIEH = 1; |
|
|
} |
|