#define THIS_IS_STACK_APPLICATION #include "stacktsk.h" #include "tick.h" #include "delay.h" #include "network.h" #include "barcode.h" #include /* * 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); void main(void) { // Tick to blink SYSTEM led. static TICK t = 0; 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 is defined in network.h) 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. */ network_send_hello( 0x01 ); strncpy(global_network_char, "12345678", 8); network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 strncpy(global_network_char, "87654444", 8); network_send_scan_frame( 1, 8 ); //1x vare, stregkodelængde=8 network_send_calc_total(); network_send_cash_payed(50000); network_send_cancel_last(); network_send_cancel_all(); network_send_goodbye(); } } void interrupt HighISR(void) { if (RCIF == 1) barcode_interrupt(); else TickUpdate(); } /********************************************************************* * 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); }