1 |
hedin |
91 |
#define THIS_IS_STACK_APPLICATION
|
2 |
|
|
|
3 |
|
|
#include "stacktsk.h"
|
4 |
|
|
#include "dhcp.h"
|
5 |
|
|
#include "tick.h"
|
6 |
|
|
#include "delay.h"
|
7 |
|
|
#include "udp.h"
|
8 |
|
|
#include "arp.h"
|
9 |
|
|
#include "arptsk.h"
|
10 |
hedin |
93 |
#include "tcp.h"
|
11 |
hedin |
91 |
|
12 |
hedin |
96 |
bit bIsDhcpUp = 0;
|
13 |
hedin |
91 |
|
14 |
|
|
// All TFTP command statuts display will be done on first line of LCD.
|
15 |
hedin |
96 |
//#define TFTP_COMMAND_DISPLAY_LINE 0
|
16 |
hedin |
91 |
|
17 |
|
|
// Result will be displayed at y = 15.
|
18 |
|
|
#define TFTP_COMMAND_RESULT_POSITION 15
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
#define STARTUP_MSG "G1_Build_0x00"
|
22 |
|
|
|
23 |
|
|
ROM char StartupMsg[] = STARTUP_MSG;
|
24 |
|
|
|
25 |
|
|
#if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)
|
26 |
|
|
ROM char DHCPMsg[] = "DHCP/Gleaning...";
|
27 |
|
|
#endif
|
28 |
|
|
|
29 |
|
|
ROM char SetupMsg[] = "Board Setup...";
|
30 |
|
|
|
31 |
|
|
// 1234567890123456
|
32 |
hedin |
96 |
//ROM char blankLCDLine[] = " ";
|
33 |
hedin |
91 |
|
34 |
|
|
/*
|
35 |
|
|
* This is used by other stack elements.
|
36 |
|
|
* Main application must define this and initialize it with
|
37 |
|
|
* proper values.
|
38 |
|
|
*/
|
39 |
|
|
APP_CONFIG AppConfig;
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
BYTE myDHCPBindCount = 0;
|
43 |
|
|
#if defined(STACK_USE_DHCP)
|
44 |
|
|
extern BYTE DHCPBindCount;
|
45 |
|
|
#else
|
46 |
|
|
/*
|
47 |
|
|
* If DHCP is not enabled, force DHCP update.
|
48 |
|
|
*/
|
49 |
|
|
BYTE DHCPBindCount = 1;
|
50 |
|
|
#endif
|
51 |
|
|
|
52 |
|
|
/*
|
53 |
|
|
* Set configuration fuses for HITECH compiler.
|
54 |
|
|
* For MCC18 compiler, separately linked config.asm file
|
55 |
|
|
* will set the correct fuses.
|
56 |
|
|
*/
|
57 |
|
|
#if defined(HITECH_C18)
|
58 |
|
|
__CONFIG(1, UNPROTECT & HS);
|
59 |
|
|
__CONFIG(2, PWRTEN & BORDIS & WDTDIS);
|
60 |
|
|
#endif
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/*
|
64 |
|
|
* Private helper functions.
|
65 |
|
|
* These may or may not be present in all applications.
|
66 |
|
|
*/
|
67 |
|
|
static void InitAppConfig(void);
|
68 |
|
|
static void InitializeBoard(void);
|
69 |
|
|
static void SetConfig(void);
|
70 |
|
|
|
71 |
hedin |
96 |
/*****************************
|
72 |
|
|
Enummerations
|
73 |
|
|
****************************/
|
74 |
|
|
typedef enum _NetworkState
|
75 |
hedin |
93 |
{
|
76 |
hedin |
96 |
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 |
|
|
char network_send_hello( unsigned char termid )
|
108 |
|
|
{
|
109 |
|
|
char error = 0;
|
110 |
|
|
char gotArp = 0;
|
111 |
|
|
static char isBound = 0;
|
112 |
|
|
|
113 |
|
|
if ( DHCPIsBound() && isBound == 0 ){
|
114 |
|
|
state = DhcpIsBound;
|
115 |
|
|
isBound = 1;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
switch (state)
|
119 |
|
|
{
|
120 |
|
|
case DhcpIsBound:
|
121 |
|
|
if ( ARPIsTxReady() && gotArp == 0 )
|
122 |
|
|
state = ArpIsTxReady;
|
123 |
|
|
else
|
124 |
|
|
state = ArpIsResolved;
|
125 |
|
|
return 0;
|
126 |
|
|
|
127 |
|
|
case ArpIsTxReady:
|
128 |
|
|
ARPResolve(&rnode.IPAddr);
|
129 |
|
|
gotArp = 1;
|
130 |
|
|
state = ArpIsResolved;
|
131 |
|
|
return 0;
|
132 |
|
|
|
133 |
|
|
case ArpIsResolved:
|
134 |
|
|
if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){
|
135 |
|
|
state = SockOpening;
|
136 |
|
|
return 0;
|
137 |
|
|
}
|
138 |
|
|
else{
|
139 |
|
|
state = ArpIsResolved;
|
140 |
|
|
return 2; // no arp resolved
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
case SockOpening:
|
144 |
|
|
tsock1 = TCPConnect (&rnode, 3000);
|
145 |
|
|
state = SockOpen;
|
146 |
|
|
return 0;
|
147 |
|
|
|
148 |
|
|
case SockOpen:
|
149 |
|
|
if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) )
|
150 |
|
|
state = TcpSend;
|
151 |
|
|
return 0;
|
152 |
|
|
|
153 |
|
|
case TcpSend:
|
154 |
|
|
for (i = net_INT; i >= 0; i--)
|
155 |
|
|
{
|
156 |
|
|
TCPPut(tsock1, termid );
|
157 |
|
|
}
|
158 |
|
|
TCPFlush(tsock1);
|
159 |
|
|
state = TcpReadyToSend;
|
160 |
|
|
return 0;
|
161 |
|
|
}
|
162 |
|
|
return 1;
|
163 |
|
|
}
|
164 |
|
|
/*
|
165 |
|
|
void network_send_hello( unsigned char termid )
|
166 |
|
|
{
|
167 |
hedin |
93 |
if (DHCPIsBound())
|
168 |
|
|
{
|
169 |
hedin |
96 |
RA3 = 0;
|
170 |
hedin |
93 |
if (sock_open == 0)
|
171 |
|
|
{
|
172 |
|
|
if (ARPIsTxReady())
|
173 |
|
|
{
|
174 |
hedin |
96 |
ARPResolve( &rnode.IPAddr);
|
175 |
|
|
is_resolved = 1;
|
176 |
|
|
}
|
177 |
hedin |
93 |
else
|
178 |
|
|
{
|
179 |
|
|
if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
|
180 |
|
|
//if (MyArp( &rnode))
|
181 |
|
|
{
|
182 |
|
|
usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP
|
183 |
|
|
sock_open=1;
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
if (UDPIsPutReady(usock1) && datagrams_to_send>0)
|
190 |
|
|
{
|
191 |
|
|
UDPPut( rnode.MACAddr.v[0] );
|
192 |
|
|
UDPPut( rnode.MACAddr.v[1] );
|
193 |
|
|
UDPPut( rnode.MACAddr.v[2] );
|
194 |
|
|
UDPPut( rnode.MACAddr.v[3] );
|
195 |
|
|
UDPPut( rnode.MACAddr.v[4] );
|
196 |
|
|
UDPPut( rnode.MACAddr.v[5] );
|
197 |
|
|
UDPPut('D');
|
198 |
|
|
UDPPut('a');
|
199 |
|
|
UDPPut('w');
|
200 |
|
|
UDPPut(0);
|
201 |
|
|
UDPFlush();
|
202 |
|
|
datagrams_to_send--;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
*/
|
207 |
|
|
|
208 |
hedin |
91 |
void main(void)
|
209 |
|
|
{
|
210 |
|
|
// Tick to blink SYSTEM led.
|
211 |
|
|
static TICK t = 0;
|
212 |
hedin |
93 |
TCP_SOCKET usock1;
|
213 |
|
|
char sock_state = 0;
|
214 |
hedin |
91 |
char is_resolved = 0;
|
215 |
hedin |
93 |
char has_arp = 0;
|
216 |
hedin |
96 |
char a;
|
217 |
hedin |
91 |
|
218 |
hedin |
93 |
TICK tick;
|
219 |
|
|
TICK lastTick;
|
220 |
|
|
TICK diffTicks;
|
221 |
hedin |
91 |
|
222 |
|
|
/*
|
223 |
|
|
* Initialize any application specific hardware.
|
224 |
|
|
*/
|
225 |
|
|
InitializeBoard();
|
226 |
|
|
|
227 |
|
|
/*
|
228 |
|
|
* Initialize all stack related components.
|
229 |
|
|
* Following steps must be performed for all applications using
|
230 |
|
|
* PICmicro TCP/IP Stack.
|
231 |
|
|
*/
|
232 |
|
|
TickInit();
|
233 |
|
|
|
234 |
|
|
/*
|
235 |
|
|
* Initialize Stack and application related NV variables.
|
236 |
|
|
*/
|
237 |
|
|
InitAppConfig();
|
238 |
|
|
|
239 |
|
|
StackInit();
|
240 |
hedin |
96 |
|
241 |
|
|
network_init();
|
242 |
hedin |
91 |
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
/*
|
246 |
|
|
* Once all items are initialized, go into infinite loop and let
|
247 |
|
|
* stack items execute their tasks.
|
248 |
|
|
* If application needs to perform its own task, it should be
|
249 |
|
|
* done at the end of while loop.
|
250 |
|
|
* Note that this is a "co-operative mult-tasking" mechanism
|
251 |
|
|
* where every task performs its tasks (whether all in one shot
|
252 |
|
|
* or part of it) and returns so that other tasks can do their
|
253 |
|
|
* job.
|
254 |
|
|
* If a task needs very long time to do its job, it must broken
|
255 |
|
|
* down into smaller pieces so that other tasks can have CPU time.
|
256 |
|
|
*/
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
// Defines the IP add. on the server machine
|
260 |
|
|
rnode.IPAddr.v[0] = 192;
|
261 |
|
|
rnode.IPAddr.v[1] = 168;
|
262 |
|
|
rnode.IPAddr.v[2] = 1;
|
263 |
|
|
rnode.IPAddr.v[3] = 20;
|
264 |
|
|
|
265 |
|
|
while(1)
|
266 |
|
|
{
|
267 |
|
|
/*
|
268 |
|
|
* Blink SYSTEM LED every second.
|
269 |
|
|
*/
|
270 |
|
|
if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
|
271 |
|
|
{
|
272 |
|
|
t = TickGet();
|
273 |
|
|
LATA4 ^= 1;
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
/*
|
277 |
|
|
* This task performs normal stack task including checking
|
278 |
|
|
* for incoming packet, type of packet and calling
|
279 |
|
|
* appropriate stack entity to process it.
|
280 |
|
|
*/
|
281 |
|
|
StackTask();
|
282 |
|
|
|
283 |
|
|
/*
|
284 |
|
|
* For DHCP information, display how many times we have renewed the IP
|
285 |
|
|
* configuration since last reset.
|
286 |
|
|
*/
|
287 |
|
|
|
288 |
hedin |
96 |
a = network_send_hello( 1 );
|
289 |
|
|
/* if (DHCPIsBound())
|
290 |
hedin |
91 |
{
|
291 |
|
|
RA3 =0;
|
292 |
hedin |
93 |
if (has_arp == 0)
|
293 |
hedin |
91 |
{
|
294 |
|
|
if (ARPIsTxReady())
|
295 |
|
|
{
|
296 |
|
|
if (is_resolved == 0)
|
297 |
|
|
{
|
298 |
|
|
ARPResolve( &rnode.IPAddr);
|
299 |
|
|
is_resolved = 1;
|
300 |
|
|
}
|
301 |
|
|
else
|
302 |
|
|
{
|
303 |
|
|
if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr))
|
304 |
|
|
{
|
305 |
hedin |
93 |
has_arp = 1;
|
306 |
hedin |
91 |
}
|
307 |
|
|
}
|
308 |
hedin |
93 |
|
309 |
hedin |
91 |
}
|
310 |
|
|
}
|
311 |
|
|
|
312 |
hedin |
93 |
if (has_arp == 1)
|
313 |
|
|
{
|
314 |
|
|
if (sock_state == 0)
|
315 |
|
|
{
|
316 |
|
|
usock1=TCPConnect(&rnode,3000);
|
317 |
|
|
sock_state = 1;
|
318 |
|
|
}
|
319 |
hedin |
96 |
/* if (sock_state ==1 && TCPIsConnected(usock1) && TCPIsPutReady(usock1))
|
320 |
hedin |
93 |
{
|
321 |
|
|
TCPPut(usock1,'D');
|
322 |
|
|
TCPPut(usock1,'a');
|
323 |
|
|
TCPPut(usock1,'w');
|
324 |
|
|
TCPPut(usock1,0);
|
325 |
|
|
TCPFlush(usock1);
|
326 |
|
|
sock_state = 2;
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
if (sock_state == 2 && TCPIsPutReady(usock1))
|
330 |
|
|
{
|
331 |
|
|
TCPDisconnect(usock1);
|
332 |
|
|
lastTick = TickGet();
|
333 |
|
|
sock_state =3;
|
334 |
|
|
}
|
335 |
|
|
tick = TickGet();
|
336 |
|
|
diffTicks = TickGetDiff(tick, lastTick);
|
337 |
|
|
|
338 |
|
|
if ( sock_state == 3 && diffTicks > ((TICK)TICK_SECOND * (TICK)2) )
|
339 |
|
|
sock_state = 0;
|
340 |
|
|
|
341 |
|
|
}
|
342 |
hedin |
96 |
} */
|
343 |
hedin |
91 |
}
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
#if defined(MCHP_C18)
|
347 |
|
|
#pragma interrupt HighISR save=section(".tmpdata")
|
348 |
|
|
void HighISR(void)
|
349 |
|
|
#elif defined(HITECH_C18)
|
350 |
|
|
#if defined(STACK_USE_SLIP)
|
351 |
|
|
extern void MACISR(void);
|
352 |
|
|
#endif
|
353 |
|
|
void interrupt HighISR(void)
|
354 |
|
|
#endif
|
355 |
|
|
{
|
356 |
|
|
TickUpdate();
|
357 |
|
|
/*
|
358 |
|
|
#if defined(STACK_USE_SLIP)
|
359 |
|
|
MACISR();
|
360 |
|
|
#endif
|
361 |
|
|
*/
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
#if defined(MCHP_C18)
|
365 |
|
|
#pragma code highVector=0x08
|
366 |
|
|
void HighVector (void)
|
367 |
|
|
{
|
368 |
|
|
_asm goto HighISR _endasm
|
369 |
|
|
}
|
370 |
|
|
#pragma code /* return to default code section */
|
371 |
|
|
#endif
|
372 |
|
|
|
373 |
|
|
/*********************************************************************
|
374 |
|
|
* Function: void InitializeBoard(void)
|
375 |
|
|
*
|
376 |
|
|
* PreCondition: None
|
377 |
|
|
*
|
378 |
|
|
* Input: None
|
379 |
|
|
*
|
380 |
|
|
* Output: None
|
381 |
|
|
*
|
382 |
|
|
* Side Effects: None
|
383 |
|
|
*
|
384 |
|
|
* Overview: Initialize board specific hardware.
|
385 |
|
|
*
|
386 |
|
|
* Note: None
|
387 |
|
|
********************************************************************/
|
388 |
|
|
static void InitializeBoard(void)
|
389 |
|
|
{
|
390 |
|
|
/*
|
391 |
|
|
* Setup for PORTA.RA0 as analog input while rests
|
392 |
|
|
* as digital i/o lines.
|
393 |
|
|
*/
|
394 |
|
|
ADCON1 = 0b10001110; // RA0 as analog input, Right justified
|
395 |
|
|
TRISA = 0x03;
|
396 |
|
|
|
397 |
|
|
/*
|
398 |
|
|
* LCD is enabled using RA5.
|
399 |
|
|
*/
|
400 |
hedin |
96 |
// PORTA_RA5 = 0; // Disable LCD.
|
401 |
hedin |
91 |
|
402 |
|
|
/*
|
403 |
|
|
* Turn off the LED's.
|
404 |
|
|
*/
|
405 |
|
|
LATA2 = 1;
|
406 |
|
|
LATA3 = 1;
|
407 |
|
|
|
408 |
|
|
/*
|
409 |
|
|
* External data EEPROM needs pull-ups, so enable internal
|
410 |
|
|
* pull-ups.
|
411 |
|
|
*/
|
412 |
|
|
INTCON2_RBPU = 0;
|
413 |
|
|
|
414 |
|
|
T0CON = 0;
|
415 |
|
|
INTCON_GIEH = 1;
|
416 |
|
|
INTCON_GIEL = 1;
|
417 |
|
|
|
418 |
|
|
}
|
419 |
|
|
|
420 |
|
|
/*********************************************************************
|
421 |
|
|
* Function: void InitAppConfig(void)
|
422 |
|
|
*
|
423 |
|
|
* PreCondition: MPFSInit() is already called.
|
424 |
|
|
*
|
425 |
|
|
* Input: None
|
426 |
|
|
*
|
427 |
|
|
* Output: Write/Read non-volatile config variables.
|
428 |
|
|
*
|
429 |
|
|
* Side Effects: None
|
430 |
|
|
*
|
431 |
|
|
* Overview: None
|
432 |
|
|
*
|
433 |
|
|
* Note: None
|
434 |
|
|
********************************************************************/
|
435 |
|
|
static void InitAppConfig(void)
|
436 |
|
|
{
|
437 |
|
|
/*
|
438 |
|
|
* Load default configuration into RAM.
|
439 |
|
|
*/
|
440 |
|
|
AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
|
441 |
|
|
AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
|
442 |
|
|
AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
|
443 |
|
|
AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
|
444 |
|
|
|
445 |
|
|
AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
|
446 |
|
|
AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
|
447 |
|
|
AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
|
448 |
|
|
AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
|
449 |
|
|
|
450 |
|
|
AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
|
451 |
|
|
AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
|
452 |
|
|
AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
|
453 |
|
|
AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
|
454 |
|
|
|
455 |
|
|
AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
|
456 |
|
|
AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
|
457 |
|
|
AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
|
458 |
|
|
AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
|
459 |
|
|
AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
|
460 |
|
|
AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
|
461 |
|
|
|
462 |
|
|
#if defined(STACK_USE_DHCP)
|
463 |
|
|
AppConfig.Flags.bIsDHCPEnabled = TRUE;
|
464 |
|
|
#else
|
465 |
|
|
AppConfig.Flags.bIsDHCPEnabled = FALSE;
|
466 |
|
|
#endif
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
|
470 |
|
|
{
|
471 |
|
|
BYTE v;
|
472 |
|
|
char *temp;
|
473 |
|
|
BYTE byteIndex;
|
474 |
|
|
|
475 |
|
|
temp = str;
|
476 |
|
|
byteIndex = 0;
|
477 |
|
|
|
478 |
|
|
while( v = *str )
|
479 |
|
|
{
|
480 |
|
|
if ( v == '.' )
|
481 |
|
|
{
|
482 |
|
|
*str++ = '\0';
|
483 |
|
|
buffer->v[byteIndex++] = atoi(temp);
|
484 |
|
|
temp = str;
|
485 |
|
|
}
|
486 |
|
|
else if ( v < '0' || v > '9' )
|
487 |
|
|
return FALSE;
|
488 |
|
|
|
489 |
|
|
str++;
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
buffer->v[byteIndex] = atoi(temp);
|
493 |
|
|
|
494 |
|
|
return (byteIndex == 3);
|
495 |
|
|
}
|
496 |
|
|
|