#define THIS_IS_STACK_APPLICATION #include "stacktsk.h" #include "dhcp.h" #include "tick.h" #include "delay.h" #include "udp.h" #include "arp.h" #include "arptsk.h" #include "tcp.h" bit bIsDhcpUp = 0; // 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 #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[] = " "; /* * This is used by other stack elements. * Main application must define this and initialize it with * proper values. */ APP_CONFIG AppConfig; BYTE myDHCPBindCount = 0; #if defined(STACK_USE_DHCP) extern BYTE DHCPBindCount; #else /* * If DHCP is not enabled, force DHCP update. */ BYTE DHCPBindCount = 1; #endif /* * Set configuration fuses for HITECH compiler. * For MCC18 compiler, separately linked config.asm file * will set the correct fuses. */ #if defined(HITECH_C18) __CONFIG(1, UNPROTECT & HS); __CONFIG(2, PWRTEN & BORDIS & WDTDIS); #endif /* * Private helper functions. * These may or may not be present in all applications. */ static void InitAppConfig(void); static void InitializeBoard(void); static void SetConfig(void); /***************************** Enummerations ****************************/ typedef enum _NetworkState { off, DhcpIsBound, ArpIsTxReady, ArpIsResolved, SockOpening, SockOpen, TcpReadyToSend, TcpSend, SockClosing, SockClosed } NetworkState; /***************************** GLOBAL INIT ****************************/ NODE_INFO rnode; NetworkState state; TCP_SOCKET tsock1; /***************************** network funktions ****************************/ int net_INT = 13, i; char *net_CHAR = "1234567890123"; void network_init(void) { state = off; } char network_send_hello( unsigned char termid ) { char error = 0; char gotArp = 0; static char isBound = 0; if ( DHCPIsBound() && isBound == 0 ){ state = DhcpIsBound; isBound = 1; } switch (state) { case DhcpIsBound: if ( ARPIsTxReady() && gotArp == 0 ) state = ArpIsTxReady; else state = ArpIsResolved; return 0; case ArpIsTxReady: ARPResolve(&rnode.IPAddr); gotArp = 1; state = ArpIsResolved; return 0; case ArpIsResolved: if (ARPIsResolved(&rnode.IPAddr, &rnode.MACAddr) ){ state = SockOpening; return 0; } else{ state = ArpIsResolved; return 2; // no arp resolved } case SockOpening: tsock1 = TCPConnect (&rnode, 3000); state = SockOpen; return 0; case SockOpen: if ( TCPIsConnected(tsock1) && TCPIsPutReady(tsock1) ) state = TcpSend; return 0; case TcpSend: for (i = net_INT; i >= 0; i--) { TCPPut(tsock1, termid ); } TCPFlush(tsock1); state = TcpReadyToSend; return 0; } return 1; } /* void network_send_hello( unsigned char termid ) { if (DHCPIsBound()) { RA3 = 0; if (sock_open == 0) { if (ARPIsTxReady()) { ARPResolve( &rnode.IPAddr); is_resolved = 1; } else { if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr)) //if (MyArp( &rnode)) { usock1=UDPOpen(2000,&rnode,3000); // socket to send UDP sock_open=1; } } } } if (UDPIsPutReady(usock1) && datagrams_to_send>0) { 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(); datagrams_to_send--; } } } */ void main(void) { // Tick to blink SYSTEM led. static TICK t = 0; TCP_SOCKET usock1; char sock_state = 0; char is_resolved = 0; char has_arp = 0; char a; TICK tick; TICK lastTick; TICK diffTicks; /* * Initialize any application specific hardware. */ InitializeBoard(); /* * Initialize all stack related components. * Following steps must be performed for all applications using * PICmicro TCP/IP Stack. */ TickInit(); /* * Initialize Stack and application related NV variables. */ InitAppConfig(); StackInit(); network_init(); /* * Once all items are initialized, go into infinite loop and let * stack items execute their tasks. * If application needs to perform its own task, it should be * done at the end of while loop. * Note that this is a "co-operative mult-tasking" mechanism * where every task performs its tasks (whether all in one shot * or part of it) and returns so that other tasks can do their * job. * If a task needs very long time to do its job, it must broken * down into smaller pieces so that other tasks can have CPU time. */ // Defines the IP add. on the server machine rnode.IPAddr.v[0] = 192; rnode.IPAddr.v[1] = 168; rnode.IPAddr.v[2] = 1; rnode.IPAddr.v[3] = 20; while(1) { /* * Blink SYSTEM LED every second. */ if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 ) { t = TickGet(); LATA4 ^= 1; } /* * This task performs normal stack task including checking * for incoming packet, type of packet and calling * appropriate stack entity to process it. */ StackTask(); /* * For DHCP information, display how many times we have renewed the IP * configuration since last reset. */ a = network_send_hello( 1 ); /* if (DHCPIsBound()) { RA3 =0; if (has_arp == 0) { if (ARPIsTxReady()) { if (is_resolved == 0) { ARPResolve( &rnode.IPAddr); is_resolved = 1; } else { if (ARPIsResolved( &rnode.IPAddr, &rnode.MACAddr)) { has_arp = 1; } } } } if (has_arp == 1) { if (sock_state == 0) { usock1=TCPConnect(&rnode,3000); sock_state = 1; } /* if (sock_state ==1 && TCPIsConnected(usock1) && TCPIsPutReady(usock1)) { TCPPut(usock1,'D'); TCPPut(usock1,'a'); TCPPut(usock1,'w'); TCPPut(usock1,0); TCPFlush(usock1); sock_state = 2; } if (sock_state == 2 && TCPIsPutReady(usock1)) { TCPDisconnect(usock1); lastTick = TickGet(); sock_state =3; } tick = TickGet(); diffTicks = TickGetDiff(tick, lastTick); if ( sock_state == 3 && diffTicks > ((TICK)TICK_SECOND * (TICK)2) ) sock_state = 0; } } */ } } #if defined(MCHP_C18) #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 { TickUpdate(); /* #if defined(STACK_USE_SLIP) MACISR(); #endif */ } #if defined(MCHP_C18) #pragma code highVector=0x08 void HighVector (void) { _asm goto HighISR _endasm } #pragma code /* return to default code section */ #endif /********************************************************************* * Function: void InitializeBoard(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Initialize board specific hardware. * * Note: None ********************************************************************/ static void InitializeBoard(void) { /* * Setup for PORTA.RA0 as analog input while rests * as digital i/o lines. */ ADCON1 = 0b10001110; // RA0 as analog input, Right justified TRISA = 0x03; /* * LCD is enabled using RA5. */ // PORTA_RA5 = 0; // Disable LCD. /* * Turn off the LED's. */ LATA2 = 1; LATA3 = 1; /* * External data EEPROM needs pull-ups, so enable internal * pull-ups. */ INTCON2_RBPU = 0; T0CON = 0; INTCON_GIEH = 1; INTCON_GIEL = 1; } /********************************************************************* * Function: void InitAppConfig(void) * * PreCondition: MPFSInit() is already called. * * Input: None * * Output: Write/Read non-volatile config variables. * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ static void InitAppConfig(void) { /* * Load default configuration into RAM. */ AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1; AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2; AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3; AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4; AppConfig.MyMask.v[0] = MY_DEFAULT_MASK_BYTE1; AppConfig.MyMask.v[1] = MY_DEFAULT_MASK_BYTE2; AppConfig.MyMask.v[2] = MY_DEFAULT_MASK_BYTE3; AppConfig.MyMask.v[3] = MY_DEFAULT_MASK_BYTE4; AppConfig.MyGateway.v[0] = MY_DEFAULT_GATE_BYTE1; AppConfig.MyGateway.v[1] = MY_DEFAULT_GATE_BYTE2; AppConfig.MyGateway.v[2] = MY_DEFAULT_GATE_BYTE3; AppConfig.MyGateway.v[3] = MY_DEFAULT_GATE_BYTE4; AppConfig.MyMACAddr.v[0] = MY_DEFAULT_MAC_BYTE1; AppConfig.MyMACAddr.v[1] = MY_DEFAULT_MAC_BYTE2; AppConfig.MyMACAddr.v[2] = MY_DEFAULT_MAC_BYTE3; AppConfig.MyMACAddr.v[3] = MY_DEFAULT_MAC_BYTE4; AppConfig.MyMACAddr.v[4] = MY_DEFAULT_MAC_BYTE5; AppConfig.MyMACAddr.v[5] = MY_DEFAULT_MAC_BYTE6; #if defined(STACK_USE_DHCP) AppConfig.Flags.bIsDHCPEnabled = TRUE; #else AppConfig.Flags.bIsDHCPEnabled = FALSE; #endif } BOOL StringToIPAddress(char *str, IP_ADDR *buffer) { BYTE v; char *temp; BYTE byteIndex; temp = str; byteIndex = 0; while( v = *str ) { if ( v == '.' ) { *str++ = '\0'; buffer->v[byteIndex++] = atoi(temp); temp = str; } else if ( v < '0' || v > '9' ) return FALSE; str++; } buffer->v[byteIndex] = atoi(temp); return (byteIndex == 3); }