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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20