|
/********************************************************************* |
|
|
* |
|
|
* 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" |
4 |
#include "dhcp.h" |
#include "dhcp.h" |
|
#include "xlcd.h" |
|
5 |
#include "tick.h" |
#include "tick.h" |
6 |
#include "delay.h" |
#include "delay.h" |
7 |
#include "udp.h" |
#include "udp.h" |
8 |
#include "arp.h" |
#include "arp.h" |
9 |
#include "arptsk.h" |
#include "arptsk.h" |
10 |
|
#include "tcp.h" |
11 |
|
|
12 |
|
bit bIsDhcpUp = 0; |
13 |
|
|
14 |
// 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. |
15 |
#define TFTP_COMMAND_DISPLAY_LINE 0 |
//#define TFTP_COMMAND_DISPLAY_LINE 0 |
16 |
|
|
17 |
// Result will be displayed at y = 15. |
// Result will be displayed at y = 15. |
18 |
#define TFTP_COMMAND_RESULT_POSITION 15 |
#define TFTP_COMMAND_RESULT_POSITION 15 |
29 |
ROM char SetupMsg[] = "Board Setup..."; |
ROM char SetupMsg[] = "Board Setup..."; |
30 |
|
|
31 |
// 1234567890123456 |
// 1234567890123456 |
32 |
ROM char blankLCDLine[] = " "; |
//ROM char blankLCDLine[] = " "; |
33 |
|
|
34 |
/* |
/* |
35 |
* This is used by other stack elements. |
* This is used by other stack elements. |
66 |
*/ |
*/ |
67 |
static void InitAppConfig(void); |
static void InitAppConfig(void); |
68 |
static void InitializeBoard(void); |
static void InitializeBoard(void); |
|
static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD); |
|
69 |
static void SetConfig(void); |
static void SetConfig(void); |
70 |
|
|
71 |
|
/***************************** |
72 |
|
Enummerations |
73 |
|
****************************/ |
74 |
|
typedef enum _NetworkState |
75 |
|
{ |
76 |
|
off, |
77 |
|
DhcpIsBound, |
78 |
|
ArpIsTxReady, |
79 |
|
ArpIsResolved, |
80 |
|
SockOpening, |
81 |
|
SockOpen, |
82 |
|
TcpReadyToSend, |
83 |
|
TcpSend, |
84 |
|
SockClosing, |
85 |
|
SockClosed |
86 |
|
} NetworkState; |
87 |
|
|
88 |
|
/***************************** |
89 |
|
GLOBAL INIT |
90 |
|
****************************/ |
91 |
|
NODE_INFO rnode; |
92 |
|
NetworkState state; |
93 |
|
TCP_SOCKET tsock1; |
94 |
|
|
95 |
|
|
96 |
|
/***************************** |
97 |
|
network funktions |
98 |
|
****************************/ |
99 |
|
int net_INT = 13, i; |
100 |
|
char *net_CHAR = "1234567890123"; |
101 |
|
|
102 |
|
|
103 |
|
void network_init(void) |
104 |
|
{ |
105 |
|
state = off; |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
char network_send_hello( unsigned char termid ) |
110 |
|
{ |
111 |
|
char gotArp = 0; // if gotArp is 0, the embedded haven't talked to the server since boot. |
112 |
|
static char isBound = 0; |
113 |
|
if ( DHCPIsBound() && isBound == 0 ){ |
114 |
|
state = DhcpIsBound; |
115 |
|
isBound = 1; |
116 |
|
} |
117 |
|
switch (state) |
118 |
|
{ |
119 |
|
case DhcpIsBound: |
120 |
|
if ( ARPIsTxReady() && gotArp == 0 ) |
121 |
|
state = ArpIsTxReady; |
122 |
|
else |
123 |
|
state = ArpIsResolved; |
124 |
|
return 0; |
125 |
|
|
126 |
|
case ArpIsTxReady: |
127 |
|
ARPResolve(&rnode.IPAddr); |
128 |
|
gotArp = 1; |
129 |
|
state = ArpIsResolved; |
130 |
|
return 0; |
131 |
|
|
132 |
|
case ArpIsResolved: |
133 |
|
if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){ |
134 |
|
state = SockOpening; |
135 |
|
return 0; |
136 |
|
} |
137 |
|
else{ |
138 |
|
state = ArpIsResolved; |
139 |
|
return 2; // no arp resolved |
140 |
|
} |
141 |
|
|
142 |
|
case SockOpening: |
143 |
|
tsock1 = TCPConnect (&rnode, 3000); |
144 |
|
state = SockOpen; |
145 |
|
return 0; |
146 |
|
|
147 |
|
case SockOpen: |
148 |
|
if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) ) |
149 |
|
state = TcpSend; |
150 |
|
return 0; |
151 |
|
|
152 |
|
case TcpSend: |
153 |
|
TCPPut( tsock1, 0x00 ); |
154 |
|
TCPPut( tsock1, termid ); |
155 |
|
TCPFlush(tsock1); |
156 |
|
TCPDiscard( tsock1 ); |
157 |
|
|
158 |
|
|
159 |
|
state = TcpReadyToSend; |
160 |
|
return 0; |
161 |
|
} |
162 |
|
return 1; |
163 |
|
} |
164 |
|
|
165 |
|
char network_send_scan_frame( unsigned char stregkode, unsigned char buflen ) |
166 |
|
{ |
167 |
|
switch (state) |
168 |
|
{ |
169 |
|
TCPPut( tsock1, 0x01 ); |
170 |
|
for (i = buflen; i >= 0; i--) |
171 |
|
{ |
172 |
|
TCPPut(tsock1, stregkode ); |
173 |
|
} |
174 |
|
TCPFlush(tsock1); |
175 |
|
state = TcpReadyToSend; |
176 |
|
return 1; |
177 |
|
} |
178 |
|
} |
179 |
|
char network_send_goodbye(void) |
180 |
|
{ |
181 |
|
switch (state) |
182 |
|
{ |
183 |
|
case TcpReadyToSend: |
184 |
|
if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) ) |
185 |
|
state = TcpSend; |
186 |
|
|
187 |
|
case TcpSend: |
188 |
|
TCPPut( tsock1, 0x06 ); |
189 |
|
TCPFlush( tsock1 ); |
190 |
|
state = SockClosing; |
191 |
|
return 0; |
192 |
|
|
193 |
|
case SockClosing: |
194 |
|
TCPDisconnect( tsock1 ); |
195 |
|
state = SockClosed; |
196 |
|
return 1; |
197 |
|
} |
198 |
|
return 3; // Didn't close the socket properly; |
199 |
|
} |
200 |
|
|
201 |
void main(void) |
void main(void) |
202 |
{ |
{ |
203 |
// Tick to blink SYSTEM led. |
// Tick to blink SYSTEM led. |
204 |
static TICK t = 0; |
static TICK t = 0; |
205 |
UDP_SOCKET usock1; |
TCP_SOCKET usock1; |
206 |
NODE_INFO rnode; |
char sock_state = 0; |
|
char sock_open = 0; |
|
207 |
char is_resolved = 0; |
char is_resolved = 0; |
208 |
|
char has_arp = 0; |
209 |
|
char a; |
210 |
|
|
211 |
|
TICK tick; |
212 |
|
TICK lastTick; |
213 |
|
TICK diffTicks; |
214 |
|
|
215 |
/* |
/* |
216 |
* Initialize any application specific hardware. |
* Initialize any application specific hardware. |
230 |
InitAppConfig(); |
InitAppConfig(); |
231 |
|
|
232 |
StackInit(); |
StackInit(); |
233 |
|
|
234 |
|
network_init(); |
235 |
|
|
|
#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 |
|
236 |
|
|
237 |
|
|
238 |
/* |
/* |
254 |
rnode.IPAddr.v[1] = 168; |
rnode.IPAddr.v[1] = 168; |
255 |
rnode.IPAddr.v[2] = 1; |
rnode.IPAddr.v[2] = 1; |
256 |
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;*/ |
|
|
|
|
|
|
|
257 |
|
|
|
|
|
258 |
while(1) |
while(1) |
259 |
{ |
{ |
260 |
|
unsigned char antal = 8; |
261 |
|
unsigned char stregkode[] = "12345678"; |
262 |
/* |
/* |
263 |
* Blink SYSTEM LED every second. |
* Blink SYSTEM LED every second. |
264 |
*/ |
*/ |
279 |
* For DHCP information, display how many times we have renewed the IP |
* For DHCP information, display how many times we have renewed the IP |
280 |
* configuration since last reset. |
* configuration since last reset. |
281 |
*/ |
*/ |
|
|
|
|
if ( DHCPBindCount != myDHCPBindCount ) |
|
|
{ |
|
|
DisplayIPValue(&AppConfig.MyIPAddr, TRUE); |
|
|
myDHCPBindCount = DHCPBindCount; |
|
282 |
|
|
283 |
if ( AppConfig.Flags.bIsDHCPEnabled ) |
a = network_send_hello( 0x01 ); |
284 |
{ |
// a = network_send_scan_frame( stregkode, antal ); |
285 |
XLCDGoto(1, 14); |
a = network_send_goodbye(); |
286 |
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)) |
|
|
{ |
|
|
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(); |
|
|
} |
|
|
} |
|
287 |
} |
} |
288 |
} |
} |
289 |
|
|
314 |
#pragma code /* return to default code section */ |
#pragma code /* return to default code section */ |
315 |
#endif |
#endif |
316 |
|
|
|
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); |
|
|
} |
|
|
|
|
317 |
/********************************************************************* |
/********************************************************************* |
318 |
* Function: void InitializeBoard(void) |
* Function: void InitializeBoard(void) |
319 |
* |
* |
341 |
/* |
/* |
342 |
* LCD is enabled using RA5. |
* LCD is enabled using RA5. |
343 |
*/ |
*/ |
344 |
PORTA_RA5 = 0; // Disable LCD. |
// PORTA_RA5 = 0; // Disable LCD. |
345 |
|
|
346 |
/* |
/* |
347 |
* Turn off the LED's. |
* Turn off the LED's. |
355 |
*/ |
*/ |
356 |
INTCON2_RBPU = 0; |
INTCON2_RBPU = 0; |
357 |
|
|
|
XLCDInit(); |
|
|
XLCDGoto(0, 0); |
|
|
XLCDPutROMString(StartupMsg); |
|
|
|
|
358 |
T0CON = 0; |
T0CON = 0; |
359 |
INTCON_GIEH = 1; |
INTCON_GIEH = 1; |
360 |
INTCON_GIEL = 1; |
INTCON_GIEL = 1; |
438 |
return (byteIndex == 3); |
return (byteIndex == 3); |
439 |
} |
} |
440 |
|
|
|
|
|
|
void XLCDDelay15ms(void) |
|
|
{ |
|
|
DelayMs(15); |
|
|
} |
|
|
void XLCDDelay4ms(void) |
|
|
{ |
|
|
DelayMs(4); |
|
|
} |
|
|
|
|
|
void XLCDDelay100us(void) |
|
|
{ |
|
|
INTCON_GIEH = 0; |
|
|
Delay10us(1); |
|
|
INTCON_GIEH = 1; |
|
|
} |
|