/[H8]/trunk/PIC/Demo trimmet/TFTPcDemo.c
ViewVC logotype

Contents of /trunk/PIC/Demo trimmet/TFTPcDemo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 98 - (show annotations) (download)
Tue May 22 14:45:39 2007 UTC (17 years ago) by hedin
File MIME type: text/plain
File size: 10078 byte(s)
start, send, end tcp connection
1 #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 #include "tcp.h"
11
12 bit bIsDhcpUp = 0;
13
14 // All TFTP command statuts display will be done on first line of LCD.
15 //#define TFTP_COMMAND_DISPLAY_LINE 0
16
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 //ROM char blankLCDLine[] = " ";
33
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 /*****************************
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)
202 {
203 // Tick to blink SYSTEM led.
204 static TICK t = 0;
205 TCP_SOCKET usock1;
206 char sock_state = 0;
207 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.
217 */
218 InitializeBoard();
219
220 /*
221 * Initialize all stack related components.
222 * Following steps must be performed for all applications using
223 * PICmicro TCP/IP Stack.
224 */
225 TickInit();
226
227 /*
228 * Initialize Stack and application related NV variables.
229 */
230 InitAppConfig();
231
232 StackInit();
233
234 network_init();
235
236
237
238 /*
239 * Once all items are initialized, go into infinite loop and let
240 * stack items execute their tasks.
241 * If application needs to perform its own task, it should be
242 * done at the end of while loop.
243 * Note that this is a "co-operative mult-tasking" mechanism
244 * where every task performs its tasks (whether all in one shot
245 * or part of it) and returns so that other tasks can do their
246 * job.
247 * If a task needs very long time to do its job, it must broken
248 * down into smaller pieces so that other tasks can have CPU time.
249 */
250
251
252 // Defines the IP add. on the server machine
253 rnode.IPAddr.v[0] = 192;
254 rnode.IPAddr.v[1] = 168;
255 rnode.IPAddr.v[2] = 1;
256 rnode.IPAddr.v[3] = 20;
257
258 while(1)
259 {
260 unsigned char antal = 8;
261 unsigned char stregkode[] = "12345678";
262 /*
263 * Blink SYSTEM LED every second.
264 */
265 if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
266 {
267 t = TickGet();
268 LATA4 ^= 1;
269 }
270
271 /*
272 * This task performs normal stack task including checking
273 * for incoming packet, type of packet and calling
274 * appropriate stack entity to process it.
275 */
276 StackTask();
277
278 /*
279 * For DHCP information, display how many times we have renewed the IP
280 * configuration since last reset.
281 */
282
283 a = network_send_hello( 0x01 );
284 // a = network_send_scan_frame( stregkode, antal );
285 a = network_send_goodbye();
286
287 }
288 }
289
290 #if defined(MCHP_C18)
291 #pragma interrupt HighISR save=section(".tmpdata")
292 void HighISR(void)
293 #elif defined(HITECH_C18)
294 #if defined(STACK_USE_SLIP)
295 extern void MACISR(void);
296 #endif
297 void interrupt HighISR(void)
298 #endif
299 {
300 TickUpdate();
301 /*
302 #if defined(STACK_USE_SLIP)
303 MACISR();
304 #endif
305 */
306 }
307
308 #if defined(MCHP_C18)
309 #pragma code highVector=0x08
310 void HighVector (void)
311 {
312 _asm goto HighISR _endasm
313 }
314 #pragma code /* return to default code section */
315 #endif
316
317 /*********************************************************************
318 * Function: void InitializeBoard(void)
319 *
320 * PreCondition: None
321 *
322 * Input: None
323 *
324 * Output: None
325 *
326 * Side Effects: None
327 *
328 * Overview: Initialize board specific hardware.
329 *
330 * Note: None
331 ********************************************************************/
332 static void InitializeBoard(void)
333 {
334 /*
335 * Setup for PORTA.RA0 as analog input while rests
336 * as digital i/o lines.
337 */
338 ADCON1 = 0b10001110; // RA0 as analog input, Right justified
339 TRISA = 0x03;
340
341 /*
342 * LCD is enabled using RA5.
343 */
344 // PORTA_RA5 = 0; // Disable LCD.
345
346 /*
347 * Turn off the LED's.
348 */
349 LATA2 = 1;
350 LATA3 = 1;
351
352 /*
353 * External data EEPROM needs pull-ups, so enable internal
354 * pull-ups.
355 */
356 INTCON2_RBPU = 0;
357
358 T0CON = 0;
359 INTCON_GIEH = 1;
360 INTCON_GIEL = 1;
361
362 }
363
364 /*********************************************************************
365 * Function: void InitAppConfig(void)
366 *
367 * PreCondition: MPFSInit() is already called.
368 *
369 * Input: None
370 *
371 * Output: Write/Read non-volatile config variables.
372 *
373 * Side Effects: None
374 *
375 * Overview: None
376 *
377 * Note: None
378 ********************************************************************/
379 static void InitAppConfig(void)
380 {
381 /*
382 * Load default configuration into RAM.
383 */
384 AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
385 AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
386 AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
387 AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;
388
389 AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1;
390 AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2;
391 AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3;
392 AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4;
393
394 AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1;
395 AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2;
396 AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3;
397 AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4;
398
399 AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1;
400 AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2;
401 AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3;
402 AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4;
403 AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5;
404 AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6;
405
406 #if defined(STACK_USE_DHCP)
407 AppConfig.Flags.bIsDHCPEnabled = TRUE;
408 #else
409 AppConfig.Flags.bIsDHCPEnabled = FALSE;
410 #endif
411 }
412
413 BOOL StringToIPAddress(char *str, IP_ADDR *buffer)
414 {
415 BYTE v;
416 char *temp;
417 BYTE byteIndex;
418
419 temp = str;
420 byteIndex = 0;
421
422 while( v = *str )
423 {
424 if ( v == '.' )
425 {
426 *str++ = '\0';
427 buffer->v[byteIndex++] = atoi(temp);
428 temp = str;
429 }
430 else if ( v < '0' || v > '9' )
431 return FALSE;
432
433 str++;
434 }
435
436 buffer->v[byteIndex] = atoi(temp);
437
438 return (byteIndex == 3);
439 }
440

  ViewVC Help
Powered by ViewVC 1.1.20