/** * Check Expansion Board every 50ms * * @return Set's bit 0 in returned byte if data added to UDP event port */ BYTE chkXboard(void) { if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_IOR5E) { return ior5eAddEvents(); } else if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_MXD2R) { return mxd2rAddEvents(); } return 0; }
/** * Enables the DHCP Client. * Enables the DHCP client if it is disabled. If it is already enabled, * nothing is done. */ void DHCPEnable(void) { if(smDHCPState == SM_DHCP_DISABLED) { //Set DHCP flag appcfgPutc(APPCFG_NETFLAGS, appcfgGetc(APPCFG_NETFLAGS) | APPCFG_NETFLAGS_DHCP); smDHCPState = SM_DHCP_GET_SOCKET; DHCPBindCount = 0; DHCPFlags.bits.bIsBound = FALSE; } #if (DEBUG_DHCP >= LOG_INFO) debugPutMsg(15); //@mxd:15:Enabled #endif }
/** * Disables the DHCP Client. * Disables the DHCP client by sending the state machine to * "SM_DHCP_DISABLED". If the board was previously configured by DHCP, the * configuration will continue to be used but the module will no longer * preform any renewals. */ void DHCPDisable(void) { if(DHCPSocket != INVALID_UDP_SOCKET) { UDPClose(DHCPSocket); DHCPSocket = INVALID_UDP_SOCKET; } smDHCPState = SM_DHCP_DISABLED; //Clear DHCP flag appcfgPutc(APPCFG_NETFLAGS, appcfgGetc(APPCFG_NETFLAGS) & ~APPCFG_NETFLAGS_DHCP); #if (DEBUG_DHCP >= LOG_INFO) debugPutMsg(14); //@mxd:14:Disabled #endif }
static void ProcessIO(void) { static TICK8 tmr10ms = 0; //Enter each 10ms if ( TickGetDiff8bit(tmr10ms) >= ((TICK8)TICKS_PER_SECOND / (TICK8)100) ) { tmr10ms = TickGet8bit(); //Service Expansion Board, if any is present switch(appcfgGetc(APPCFG_XBRD_TYPE)) { case XBRD_TYPE_MXD2R: mxd2rService(); break; case XBRD_TYPE_IOR5E: ior5eService(); break; } } //Convert next ADC channel, and store result in adcChannel array #if (defined(APP_USE_ADC8) || defined(APP_USE_ADC10)) && (ADC_CHANNELS > 0) { static BYTE adcChannel; //Current ADC channel. Value from 0 - n static BOOL ADC_Wait = 0; //Increment ADCChannel and start new convertion if (!ADC_Wait) { //Increment to next ADC channel if ((++adcChannel) >= ADC_CHANNELS) { adcChannel = 0; } //Check if current ADC channel (adcChannel) is configured to be ADC channel if (adcChannel < ((~ADCON1) & 0x0F)) { //Convert next ADC Channel ADCON0 &= ~0x3C; ADCON0 |= (adcChannel << 2); ADCON0_ADON = 1; //Switch on ADC ADCON0_GO = 1; //Go ADC_Wait = 1; } //Not ADC channel, set to 0 else { AdcValues[adcChannel] = 0; } } //End of ADC Convertion: save data if ((ADC_Wait)&&(!ADCON0_GO)) { #if defined(APP_USE_ADC8) AdcValues[adcChannel] = ADRESH; #elif defined(APP_USE_ADC10) //AdcValues[adcChannel] = (WORD) ((((WORD)ADRESH) << 8) | ((WORD)ADRESL)); //AdcValues[adcChannel] = (WORD)((ADRESH*256)+ADRESL); AdcValues[adcChannel] = ((WORD)ADRESH << 8) | (WORD)ADRESL; #endif ADC_Wait = 0; } } #endif }
/* * Main entry point. */ void main(void) { static TICK8 t = 0; #ifdef HEATHERD NODE_INFO tcpServerNode; static TCP_SOCKET tcpSocketUser = INVALID_SOCKET; BYTE c; #endif static BYTE testLED; testLED = 1; //Set SWDTEN bit, this will enable the watch dog timer WDTCON_SWDTEN = 1; aliveCntrMain = 0xff; //Disable alive counter during initialization. Setting to 0xff disables it. //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 buses busInit(); //Initialize serial ports early, because they could be required for debugging if (appcfgGetc(APPCFG_USART1_CFG & APPCFG_USART_ENABLE)) { appcfgUSART(); //Configure the USART1 } if (appcfgGetc(APPCFG_USART2_CFG & APPCFG_USART_ENABLE)) { appcfgUSART2(); //Configure the USART2 } //After initializing all modules that use interrupts, enable global interrupts INTCON_GIEH = 1; INTCON_GIEL = 1; //Initialize file system. fsysInit(); //Intialize HTTP Execution unit htpexecInit(); //Initialize Stack and application related NV variables. appcfgInit(); //First call appcfgCpuIOValues() and then only appcfgCpuIO()!!! This ensures the value are set, before enabling ports. appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgADC(); //Configure ADC unit appcfgPWM(); //Configure PWM Channels //Serial configuration menu - display it for configured time and allow user to enter configuration menu scfInit(appcfgGetc(APPCFG_STARTUP_SER_DLY)); //LCD Display Initialize lcdInit(); //Initialize expansion board appcfgXboard(); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined(STACK_USE_FTP_SERVER) FTPInit(); #endif //Intialise network componet of buses - only call after StackInit()! busNetInit(); //Initializes events. evtInit(); //Initializes "UDP Command Port" and "UDP Even Port". cmdInit(); ioInit(); #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * 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. */ #ifdef HEATHERD //Create a TCP socket that listens on port 54123 tcpSocketUser = TCPListen(HEATHERD); #define HEATHERD_ENABLE (!(appcfgGetc(APPCFG_TRISA) & 1)) #define HEATHERD_WRITE_ENABLE (!(appcfgGetc(APPCFG_TRISA) & 2)) #endif while(1) { aliveCntrMain = 38; //Reset if not services in 52.42ms x 38 = 2 seconds //Blink SYSTEM LED every second. if (appcfgGetc(APPCFG_SYSFLAGS) & APPCFG_SYSFLAGS_BLINKB6) { //Configure RB6 as output, and blink it every 500ms if ( TickGetDiff8bit(t) >= ((TICK8)TICKS_PER_SECOND / (TICK8)2) ) { t = TickGet8bit(); //If B6 is configured as input, change to output if (appcfgGetc(APPCFG_TRISB) & 0x40) { appcfgPutc(APPCFG_TRISB, appcfgGetc(APPCFG_TRISB) & 0b10111111); } TRISB_RB6 = 0; LATB6 ^= 1; //Toggle //Toggle IOR5E LED, if IOR5E is present if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_IOR5E) { ior5eLatchData.bits.ledPWR ^= 1; // Toggle } } } //This task performs normal stack task including checking for incoming packet, //type of packet and calling appropriate stack entity to process it. StackTask(); //Service LCD display lcdService(); //Process commands cmdTask(); //Process events evtTask(); //Process serial busses busTask(); //I2C Task i2cTask(); #ifdef HEATHERD //Has a remote node made connection with the port we are listening on if ((tcpSocketUser != INVALID_SOCKET) && TCPIsConnected(tcpSocketUser)) { if (HEATHERD_ENABLE) { //Is there any data waiting for us on the TCP socket? //Because of the design of the Modtronix TCP/IP stack we have to //consume all data sent to us as soon as we detect it. while(TCPIsGetReady(tcpSocketUser)) { //We are only interrested in the first byte of the message. TCPGet(tcpSocketUser, &c); if (HEATHERD_WRITE_ENABLE) serPutByte(c); } //Discard the socket buffer. TCPDiscard(tcpSocketUser); while (serIsGetReady() && TCPIsPutReady(tcpSocketUser)) { TCPPut(tcpSocketUser,serGetByte()); } TCPFlush(tcpSocketUser); } else { TCPDisconnect(tcpSocketUser); } } #endif #if defined(STACK_USE_HTTP_SERVER) //This is a TCP application. It listens to TCP port 80 //with one or more sockets and responds to remote requests. HTTPServer(); #endif #if defined(STACK_USE_FTP_SERVER) FTPServer(); #endif #if defined(STACK_USE_ANNOUNCE) DiscoveryTask(); #endif #if defined(STACK_USE_NBNS) NBNSTask(); #endif //Add your application speicifc tasks here. ProcessIO(); //For DHCP information, display how many times we have renewed the IP //configuration since last reset. if ( DHCPBindCount != myDHCPBindCount ) { #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(2); //@mxd:2:DHCP Bind Count = %D debugPutByteHex(DHCPBindCount); #endif //Display new IP address #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(3); //@mxd:3:DHCP complete, IP = %D.%D.%D.%D debugPutByteHex(AppConfig.MyIPAddr.v[0]); debugPutByteHex(AppConfig.MyIPAddr.v[1]); debugPutByteHex(AppConfig.MyIPAddr.v[2]); debugPutByteHex(AppConfig.MyIPAddr.v[3]); #endif myDHCPBindCount = DHCPBindCount; #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif } } }
/* * Configure USART with AppConfig data */ void appcfgUSART(void) { WORD w; BYTE cfg; cfg = appcfgGetc(APPCFG_USART1_CFG); #if defined(__18F452) || defined(_18F452) || defined(__18F458) || defined(_18F458) //USART is enabled if (cfg & APPCFG_USART_ENABLE) { //Initialize USART1 Control registers TXSTA = 0b00100100; //High BRG speed RCSTA = 0b10010000; #if (CLOCK_FREQ == 20000000) //Get USART BAUD rate setting switch (appcfgGetc(APPCFG_USART1_BAUD)) { case BAUD_2400: TXSTA = 0b00100000; //Low BRG speed SPBRG = 129; //0.13% Error break; case BAUD_4800: TXSTA = 0b00100000; //Low BRG speed SPBRG = 64; //0.16% Error break; case BAUD_9600: SPBRG = 32; //1.36% Error break; case BAUD_19200: SPBRG = 64; //0.16% Error break; case BAUD_38400: SPBRG = 32; //1.3% Error break; case BAUD_57600: SPBRG = 21; //1.3% Error break; case BAUD_115200: SPBRG = 10; //1.3% Error break; default: SPBRG = 21; //1.3% Error break; } #else #error "appcfgUSART() does not support selected clock frequency" #endif serEnable(); //Enable serial port } //USART is disbled else { serDisable(); //Disable serial port } #elif defined(__18F6621) || defined(_18F6621) \ || defined(__18F6527) || defined(_18F6527) \ || defined(__18F6627) || defined(_18F6627) \ || defined(__18F6680) || defined(_18F6680) //USART is enabled if (cfg & APPCFG_USART_ENABLE) { TXSTA_BRGH = 1; //High baud rate mode //xxxx 1xxx - 16bit baud rate generator BAUDCON = 0b00001000; #if (CLOCK_FREQ == 40000000) //Get USART BAUD rate setting switch (appcfgGetc(APPCFG_USART1_BAUD)) { case BAUD_300: w = 33332; break; case BAUD_1200: w = 8332; break; case BAUD_2400: w = 4165; break; case BAUD_4800: w = 2082; break; case BAUD_9600: w = 1040; break; case BAUD_19200: w = 520; break; case BAUD_38400: w = 259; break; case BAUD_57600: w = 172; break; case BAUD_115200: w = 86; break; default: w = 172; break; } SPBRG = (BYTE)w; SPBRGH = (BYTE)(w >> 8); #else #error "appcfgUSART() does not support selected clock frequency" #endif serEnable(); //Enable serial port }
/////////////////////////////////////////////////////////////////////////////// // Main entry point. // void main(void) { static TICK8 t = 0; BYTE i; char strBuf[10]; // 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 file system. fsysInit(); // Intialize HTTP Execution unit htpexecInit(); // Initialze serial port serInit(); // Initialize Stack and application related NV variables. appcfgInit(); appcfgUSART(); // Configure the USART #ifdef SER_USE_INTERRUPT // Interrupt enabled serial ports have to be enabled serEnable(); #endif appcfgCpuIO(); // Configure the CPU's I/O port pin directions - input or output appcfgCpuIOValues(); // Configure the CPU's I/O port pin default values appcfgADC(); // Configure ADC unit appcfgPWM(); // Configure PWM unit // Serial configuration menu - display it for configured time and // allow user to enter configuration menu scfInit( appcfgGetc( APPCFG_STARTUP_SER_DLY ) ); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined( STACK_USE_DHCP ) || defined( STACK_USE_IP_GLEANING ) // If DHCP is NOT enabled if ( ( appcfgGetc( APPCFG_NETFLAGS ) & APPCFG_NETFLAGS_DHCP ) == 0 ) { // Force IP address display update. myDHCPBindCount = 1; #if defined( STACK_USE_DHCP ) DHCPDisable(); #endif } #endif #if ( DEBUG_MAIN >= LOG_DEBUG ) debugPutMsg(1); //@mxd:1:Starting main loop #endif // Init VSCP functionality vscp_init(); bInitialized = FALSE; // Not initialized #if defined(STACK_USE_NTP_SERVER) // Initialize time hour = 0; minute = 0; second = 0; #endif appcfgPutc( VSCP_DM_MATRIX_BASE, 0x00 ); appcfgPutc( VSCP_DM_MATRIX_BASE+1, 0x00 ); appcfgPutc( VSCP_DM_MATRIX_BASE+2, 0x00 ); appcfgPutc( VSCP_DM_MATRIX_BASE+3, 0x00 ); // // 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. // while ( 1 ) { // Used for initial delay to give stack and chip some time to // initialize. If not used messages sent during this time will // fail. if ( TickGet() > ( 5 * TICK_SECOND ) ) { bInitialized = TRUE; } // We should do the ftp download every three hours //if ( TickGetDiff( TickGet(), loadTime ) >= ( 3 * 3600 * TICK_SECOND ) ) { // loadTime = TickGet(); // bftpLoadWork = TRUE; //} // Blink SYSTEM LED every second. if ( appcfgGetc( APPCFG_SYSFLAGS ) & APPCFG_SYSFLAGS_BLINKB6 ) { if ( TickGetDiff8bit( t ) >= ((TICK8)( TICKS_PER_SECOND / 2 ) ) ) { t = TickGet8bit(); TRISB_RB6 = 0; LATB6 ^= 1; } } // This task performs normal stack task including checking for incoming packet, // type of packet and calling appropriate stack entity to process it. StackTask(); #if defined(STACK_USE_HTTP_SERVER) // This is a TCP application. It listens to TCP port 80 // with one or more sockets and responds to remote requests. HTTPServer(); #endif #if defined(STACK_USE_FTP_SERVER) FTPServer(); #endif // Add your application speicifc tasks here. ProcessIO(); #if defined(VSCP_USE_TCP ) // VSCP Task if ( bInitialized ) { vscp_tcp_task(); } #endif if ( bInitialized ) { vscp_main_task(); process_can_message(); if ( g_can_error ) { send_can_error_message( g_can_error ); g_can_error = 0; } } #if defined(STACK_USE_NTP_SERVER) if ( bInitialized ) { //ntp_task(); } #endif // For DHCP information, display how many times we have renewed the IP // configuration since last reset. if ( DHCPBindCount != myDHCPBindCount ) { #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg( 2 ); // @mxd:2:DHCP Bind Count = %D debugPutByteHex(DHCPBindCount); #endif // Display new IP address #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg( 3 ); //@mxd:3:DHCP complete, IP = %D.%D.%D.%D debugPutByteHex( AppConfig.MyIPAddr.v[ 0 ] ); debugPutByteHex( AppConfig.MyIPAddr.v[ 1 ] ); debugPutByteHex( AppConfig.MyIPAddr.v[ 2 ] ); debugPutByteHex( AppConfig.MyIPAddr.v[ 3 ] ); #endif myDHCPBindCount = DHCPBindCount; } } }
/* * Main entry point. */ void main(void) { static TICK tickHeartBeat = 0xffffffff; static BYTE testLED; testLED = 1; // Destination address - Always MAC broadcast address broadcastTargetMACAddr.v[ 0 ] = 0xff; broadcastTargetMACAddr.v[ 1 ] = 0xff; broadcastTargetMACAddr.v[ 2 ] = 0xff; broadcastTargetMACAddr.v[ 3 ] = 0xff; broadcastTargetMACAddr.v[ 4 ] = 0xff; broadcastTargetMACAddr.v[ 5 ] = 0xff; //Set SWDTEN bit, this will enable the watch dog timer WDTCON_SWDTEN = 1; aliveCntrMain = 0xff; //Disable alive counter during initialization. Setting to 0xff disables it. //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 serial ports early, because they could be required for debugging if (appcfgGetc(APPCFG_USART1_CFG & APPCFG_USART_ENABLE)) { appcfgUSART(); //Configure the USART1 } #if defined(BRD_SBC65EC) if (appcfgGetc(APPCFG_USART2_CFG & APPCFG_USART_ENABLE)) { appcfgUSART2(); //Configure the USART2 } #endif //After initializing all modules that use interrupts, enable global interrupts INTCON_GIEH = 1; INTCON_GIEL = 1; //Initialize Stack and application related NV variables. appcfgInit(); //First call appcfgCpuIOValues() and then only appcfgCpuIO()!!! This ensures the value are set, before enabling ports. appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgADC(); //Configure ADC unit appcfgPWM(); //Configure PWM Channels MACInit(); #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * 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. */ while ( 1 ) { //aliveCntrMain = 38; //Reset if not services in 52.42ms x 38 = 2 seconds aliveCntrMain = 0xff; CLRWDT(); // Check for event if ( vscp_getRawPacket() ) { feedVSCP(); } // Send heartbeat every 30 seconds if ( TickGetDiff( tickHeartBeat ) >= ( TICKS_PER_SECOND * 30 ) ) { //vscpevent.head = VSCP_PRIORITY_NORMAL; //vscpevent.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; //vscpevent.vscp_type = VSCP_TYPE_INFORMATION_NODE_HEARTBEAT; //vscpevent.sizeData = 3; //vscpevent.data[ 0 ] = 0; //vscpevent.data[ 1 ] = 0; // Zone //vscpevent.data[ 2 ] = 0; // subzone //vscp_sendRawPacket( &vscpevent ); //SendTestVSCPPacket(); tickHeartBeat = TickGet(); /* //If B6 is configured as input, change to output if (appcfgGetc(APPCFG_TRISB) & 0x40) { appcfgPutc(APPCFG_TRISB, appcfgGetc(APPCFG_TRISB) & 0b10111111); } TRISB_RB6 = 0; LATB6 ^= 1; //Toggle //Toggle IOR5E LED, if IOR5E is present if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_IOR5E) { ior5eLatchData.bits.ledPWR ^= 1; // Toggle } */ } // Do MAC work StackTask(); //MACTask();; //I2C Task i2cTask(); //Add your application specific tasks here. ProcessIO(); // Do VSCP periodic tasks periodicVSCPWork(); } }
/* * Main entry point. */ void main(void) { static TICK8 t = 0; BYTE i; char strBuf[10]; /* * 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 file system. */ fsysInit(); //Intialize HTTP Execution unit htpexecInit(); //Initialze serial port serInit(); /* * Initialize Stack and application related NV variables. */ appcfgInit(); appcfgUSART(); //Configure the USART #ifdef SER_USE_INTERRUPT //Interrupt enabled serial ports have to be enabled serEnable(); #endif appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgADC(); //Configure ADC unit //Serial configuration menu - display it for configured time and allow user to enter configuration menu scfInit(appcfgGetc(APPCFG_STARTUP_SER_DLY)); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined(STACK_USE_FTP_SERVER) FTPInit(); #endif #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING) //If DHCP is NOT enabled if ((appcfgGetc(APPCFG_NETFLAGS) & APPCFG_NETFLAGS_DHCP) == 0) { //Force IP address display update. myDHCPBindCount = 1; #if defined(STACK_USE_DHCP) DHCPDisable(); #endif } #endif #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * 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. */ while(1) { //Blink SYSTEM LED every second. if (appcfgGetc(APPCFG_SYSFLAGS) & APPCFG_SYSFLAGS_BLINKB6) { if ( TickGetDiff8bit(t) >= ((TICK8)(TICKS_PER_SECOND/2)) ) { t = TickGet8bit(); TRISB_RB6 = 0; LATB6 ^= 1; } } //This task performs normal stack task including checking for incoming packet, //type of packet and calling appropriate stack entity to process it. StackTask(); #if defined(STACK_USE_HTTP_SERVER) //This is a TCP application. It listens to TCP port 80 //with one or more sockets and responds to remote requests. HTTPServer(); #endif #if defined(STACK_USE_FTP_SERVER) FTPServer(); #endif //Add your application speicifc tasks here. ProcessIO(); //For DHCP information, display how many times we have renewed the IP //configuration since last reset. if ( DHCPBindCount != myDHCPBindCount ) { #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(2); //@mxd:2:DHCP Bind Count = %D debugPutByteHex(DHCPBindCount); #endif //Display new IP address #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(3); //@mxd:3:DHCP complete, IP = %D.%D.%D.%D debugPutByteHex(AppConfig.MyIPAddr.v[0]); debugPutByteHex(AppConfig.MyIPAddr.v[1]); debugPutByteHex(AppConfig.MyIPAddr.v[2]); debugPutByteHex(AppConfig.MyIPAddr.v[3]); #endif myDHCPBindCount = DHCPBindCount; } } }
/** * Must be called every couple of ms * */ void busTask(void) { BYTE c; BYTE bytesRead; BYTE busId; BYTE linkedMask; ///////////////////////////////////////////////// //UDP 1 //Get bus to link to UDP 1 busId = appcfgGetc(BUSCFG_UDP1_LINK); #if defined(BRD_SBC65EC) //Currently UDP 1 can only be linked to Serial 1, Serial 2 and I2C for SBC65EC if ((busId >= BUSID_SER1) && (busId <= BUSID_I2C1) && (udpBus1 != INVALID_UDP_SOCKET)) { #else //Currently UDP 1 can only be linked to Serial 1 and I2C for SBC68EC if ( ((busId == BUSID_SER1) || (busId == BUSID_I2C1)) && (udpBus1 != INVALID_UDP_SOCKET)) { #endif //Is there any data waiting for us on this UDP port? if (UDPIsGetReady(udpBus1)) { if (busIsEnabled(busId)) { //Read bytes while (UDPGet(&c)) { //Wait until a byte is transmitted by the interrupt routine and buffer has place again. while (busIsTxBufFull(busId)) { busService(busId); FAST_USER_PROCESS(); } //Add byte to TX buffer, and update buffer pointers busPutByteTxBuf(busId, c); } //Initiate transmitting if not already transmitting if (!busIsTxing(busId)) { busService(busId); } } //Discard the socket buffer. UDPDiscard(); } //Is there any data to send on UDP port if(busRxBufHasData(busId)) { if (UDPIsPutReady(udpBus1)) { while(busRxBufHasData(busId)) { //Read and remove byte from buffer c = busPeekByteRxBuf(busId); busRemoveByteRxBuf(busId); UDPPut(c); } // Now transmit it. UDPFlush(); } } } ///////////////////////////////////////////////// //UDP 2 //Get bus to link to UDP 2 busId = appcfgGetc(BUSCFG_UDP2_LINK); #if defined(BRD_SBC65EC) //Currently UDP 1 can only be linked to Serial 1, Serial 2 and I2C for SBC65EC if ((busId >= BUSID_SER1) && (busId <= BUSID_I2C1) && (udpBus1 != INVALID_UDP_SOCKET)) { #else //Currently UDP 1 can only be linked to Serial 1 and I2C for SBC68EC if ( ((busId == BUSID_SER1) || (busId == BUSID_I2C1)) && (udpBus1 != INVALID_UDP_SOCKET)) { #endif //Is there any data waiting for us on this UDP port? if (UDPIsGetReady(udpBus2)) { if (busIsEnabled(busId)) { //Read bytes while (UDPGet(&c)) { //Wait until a byte is transmitted by the interrupt routine and buffer has place again. while (busIsTxBufFull(busId)) { busService(busId); FAST_USER_PROCESS(); } //Add byte to TX buffer, and update buffer pointers busPutByteTxBuf(busId, c); } //Initiate transmitting if not already transmitting if (!busIsTxing(busId)) { busService(busId); } } //Discard the socket buffer. UDPDiscard(); } //Is there any data to send on UDP port if(busRxBufHasData(busId)) { if (UDPIsPutReady(udpBus2)) { while(busRxBufHasData(busId)) { //Read and remove byte from buffer c = busPeekByteRxBuf(busId); busRemoveByteRxBuf(busId); UDPPut(c); } // Now transmit it. UDPFlush(); } } } ///////////////////////////////////////////////// //Service all serial buses for (busId=0; busId<BUS_COUNT; busId++) { busService(busId); } } /** * Service the given bus. If our code has a section where it has to wait for the transmit * buffer of a bus to be empties, it should call this function while waiting. * * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable * */ void busService(BYTE busId) { switch(busId) { case BUSID_SER1: serService(); break; #if defined(BRD_SBC65EC) case BUSID_SER2: ser2Service(); break; #endif case BUSID_I2C1: i2cBusService(); break; case BUSID_SPI1: NOP(); break; } } /** * Gets a byte at the given offset from the Transmit Buffer, without removing it. * The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! * The byte at the given offset it returned. The offset is how deep the byte is in * the buffer. For example, 0 will return first byte in buffer, 5 will return the 6th * byte in the buffer. * * @preCondition Ensure offset parameter is not larger than current number of bytes * contained in buffer. Call busGetTxBufCount() to get current number of bytes in buffer. * * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable * @param offset Offset of byte to return. Is a value from 0-n, where n = (busGetTxBufCount() - 1) * * @return Returns the byte at the given offset in the given bus's Transmit buffer. */ BYTE busPeekByteTxBufAt(BYTE busId, BYTE offset) { BYTE ofst; ofst = busInfo.buf[busId].getTx + offset; //Check if offset wrap around if (ofst >= busInfo.buf[busId].txBufSize) { ofst = ofst - busInfo.buf[busId].txBufSize; } return *(busInfo.buf[busId].txBuf + ofst); } /** * Gets a byte at the given offset from the Receive Buffer, without removing it. * The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! * The byte at the given offset it returned. The offset is how deep the byte is in * the buffer. For example, 0 will return first byte in buffer, 5 will return the 6th * byte in the buffer. * * @preCondition Ensure offset parameter is not larger than current number of bytes * contained in buffer. Call busGetRxBufCount() to get current number of bytes in buffer. * * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable * @param offset Offset of byte to return. Is a value from 0-n, where n = (busGetRxBufCount() - 1) * * @return Returns the byte at the given offset in the given bus's Receive buffer. */ BYTE busPeekByteRxBufAt(BYTE busId, BYTE offset) { BYTE ofst; ofst = busInfo.buf[busId].getRx + offset; //Check if offset wrap around if (ofst >= busInfo.buf[busId].rxBufSize) { ofst = ofst - busInfo.buf[busId].rxBufSize; } return *(busInfo.buf[busId].rxBuf + ofst); }
/** * Initialize bus network components * * @preCondition Do NOT call this function before stackInit() has been called! */ void busNetInit(void) { BYTE cfg; NODE_INFO udpServerNode; //Initialize remote IP and MAC address of udpServerNode with 0, seeing that we don't know them for the node //that will send us an UDP message. The first time a message is received addressed to this port, the //remote IP and MAC addresses are automatically updated with the addresses of the remote node. memclr(&udpServerNode, sizeof(udpServerNode)); //Get UDP Port 1 configuration, is a BUSSTAT_UDP_XXX define cfg = appcfgGetc(BUSCFG_UDP1_CFG); if (cfg & BUSSTAT_UDP_ENABLE) { //Only continue if udpBus1 has not already been initialized if (udpBus1 == INVALID_UDP_SOCKET) { //Open for configured local port, and INVALID_UDP_PORT remote port. This opens the socket to //listen on the given port. udpBus1 = UDPOpen(BUS_UDP1PORT, &udpServerNode, INVALID_UDP_PORT); //An error occurred during the UDPOpen() function if (udpBus1 == INVALID_UDP_SOCKET) { #if (DEBUG_BUS >= LOG_ERROR) debugPutMsg(2); //@mxd:2:Could not open UDP1 port #endif } } } //Disable udpBus1 else { //If udpBus1 currently open, close it if (udpBus1 != INVALID_UDP_SOCKET) { UDPClose(udpBus1); udpBus1 = INVALID_UDP_SOCKET; } } //Get UDP Port 1 configuration, is a BUSSTAT_UDP_XXX define cfg = appcfgGetc(BUSCFG_UDP2_CFG); if (cfg & BUSSTAT_UDP_ENABLE) { //Only continue if udpBus2 has not already been initialized if (udpBus2 == INVALID_UDP_SOCKET) { //Open for configured local port, and INVALID_UDP_PORT remote port. This opens the socket to //listen on the given port. udpBus2 = UDPOpen(BUS_UDP2PORT, &udpServerNode, INVALID_UDP_PORT); //An error occurred during the UDPOpen() function if (udpBus2 == INVALID_UDP_SOCKET) { #if (DEBUG_BUS >= LOG_ERROR) debugPutMsg(3); //@mxd:2:Could not open UDP2 port #endif } } } //Disable udpBus2 else { //If udpBus2 currently open, close it if (udpBus2 != INVALID_UDP_SOCKET) { UDPClose(udpBus2); udpBus2 = INVALID_UDP_SOCKET; } } }
/** * Initializes the busInfo structure with pointers to all buffers. */ void busInfoInit(void) { BYTE busId; WORD totalBufSize = 0; //Clear BUS_BUFFER structure for all busses memclr((void *)&busInfo.buf, (WORD)(sizeof(BUS_BUFFER) * BUS_COUNT) ); //Initialze all bus TX and RX buffer sizes for (busId=0; busId<BUS_COUNT; busId++) { busInfo.buf[busId].txBufSize = appcfgGetc(BUSCFG_BUS1_TXBUFSIZE + (busId*2)); totalBufSize += busInfo.buf[busId].txBufSize; busInfo.buf[busId].rxBufSize = appcfgGetc(BUSCFG_BUS1_TXBUFSIZE + (busId*2) + 1); totalBufSize += busInfo.buf[busId].rxBufSize; } //Do check that total buffer size is not larger than BUSBUFFER_SIZE //If it is, set all buffers to default values if ( totalBufSize > BUSBUFFER_SIZE) { busInfo.buf[BUSID_SER1].txBufSize = BUS_SER1_TXBUFSIZE; busInfo.buf[BUSID_SER1].rxBufSize = BUS_SER1_RXBUFSIZE; #if defined(BRD_SBC65EC) busInfo.buf[BUSID_SER2].txBufSize = BUS_SER2_TXBUFSIZE; busInfo.buf[BUSID_SER2].rxBufSize = BUS_SER2_RXBUFSIZE; #else busInfo.buf[BUSID_SER2].txBufSize = 0; busInfo.buf[BUSID_SER2].rxBufSize = 0; #endif busInfo.buf[BUSID_I2C1].txBufSize = BUS_I2C1_TXBUFSIZE; busInfo.buf[BUSID_I2C1].rxBufSize = BUS_I2C1_RXBUFSIZE; busInfo.buf[BUSID_SPI1].txBufSize = BUS_SPI1_TXBUFSIZE; busInfo.buf[BUSID_SPI1].rxBufSize = BUS_SPI1_RXBUFSIZE; //Set all bus TX and RX buffers to default sizes for (busId=0; busId<BUS_COUNT; busId++) { //Set TX buffer size appcfgPutc(BUSCFG_BUS1_TXBUFSIZE + (busId*2), busInfo.buf[busId].txBufSize); //Set RX buffer size, always follows TX buffer in application configuration structure appcfgPutc(BUSCFG_BUS1_TXBUFSIZE + (busId*2) + 1, busInfo.buf[busId].rxBufSize); } } //Initialize buffer pointers busInfo.buf[BUSID_SER1].txBuf = &busBuffer[0]; busInfo.buf[BUSID_SER1].rxBuf = busInfo.buf[BUSID_SER1].txBuf + busInfo.buf[BUSID_SER1].txBufSize; busInfo.buf[BUSID_SER2].txBuf = busInfo.buf[BUSID_SER1].rxBuf + busInfo.buf[BUSID_SER1].rxBufSize; busInfo.buf[BUSID_SER2].rxBuf = busInfo.buf[BUSID_SER2].txBuf + busInfo.buf[BUSID_SER2].txBufSize; busInfo.buf[BUSID_I2C1].txBuf = busInfo.buf[BUSID_SER2].rxBuf + busInfo.buf[BUSID_SER2].rxBufSize; busInfo.buf[BUSID_I2C1].rxBuf = busInfo.buf[BUSID_I2C1].txBuf + busInfo.buf[BUSID_I2C1].txBufSize; busInfo.buf[BUSID_SPI1].txBuf = busInfo.buf[BUSID_I2C1].rxBuf + busInfo.buf[BUSID_I2C1].rxBufSize; busInfo.buf[BUSID_SPI1].rxBuf = busInfo.buf[BUSID_SPI1].txBuf + busInfo.buf[BUSID_SPI1].txBufSize; busInfo.bufEnd = busInfo.buf[BUSID_SPI1].rxBuf + busInfo.buf[BUSID_SPI1].rxBufSize; /* debugPutGenMsg(2); //@mxd:2:%s debugPutRomStringXNull((ROM char*)"Bus Ser1 Txbuf = 0x"); debugPutByteHex( (BYTE)(((WORD)busInfo.buf[BUSID_SER1].txBuf)>>8) ); debugPutByteHex( (BYTE)busInfo.buf[BUSID_SER1].txBuf ); debugPutByte(0); //Null terminate string debugPutGenMsg(2); //@mxd:2:%s debugPutRomStringXNull((ROM char*)"Bus Ser1 Rxbuf = 0x"); debugPutByteHex( (BYTE)(((WORD)busInfo.buf[BUSID_SER1].rxBuf)>>8) ); debugPutByteHex( (BYTE)busInfo.buf[BUSID_SER1].rxBuf ); debugPutByte(0); //Null terminate string */ }
/* * Main entry point. */ void main(void) { static TICK8 t = 0; static TICK8 tmr10ms = 0; //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 file system. fsysInit(); //Intialize HTTP Execution unit htpexecInit(); //Initialze serial port serInit(); //Initialize Stack and application related NV variables. appcfgInit(); appcfgUSART(); //Configure the USART #ifdef SER_USE_INTERRUPT //Interrupt enabled serial ports have to be enabled serEnable(); #endif appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgADC(); //Configure ADC unit appcfgPWM(); //Configure PWM Channels //Serial configuration menu - display it for configured time and allow user to enter configuration menu scfInit(appcfgGetc(APPCFG_STARTUP_SER_DLY)); //LCD Display Initialize lcdInit(); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined(STACK_USE_FTP_SERVER) FTPInit(); #endif //Initializes "UDP Command Port" and "UDP Command Responce Port". cmdUdpInit(); #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING) DHCPReset(); //Initialize DHCP module //If DHCP is NOT enabled if ((appcfgGetc(APPCFG_NETFLAGS) & APPCFG_NETFLAGS_DHCP) == 0) { //Force IP address display update. myDHCPBindCount = 1; #if defined(STACK_USE_DHCP) DHCPDisable(); #endif } #endif #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * 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. */ while(1) { //Clear timer 1 every cycle, can be used to measure events. Has a overflow of 65ms. //Get delay in this function with: // TMR1L | (TMR1H<<8) TMR1H = 0; //First write to TMR1H buffer! TMR1L = 0; //This write will also update TMR1H with value written above to buffer //Blink SYSTEM LED every second. if (appcfgGetc(APPCFG_SYSFLAGS) & APPCFG_SYSFLAGS_BLINKB6) { if ( TickGetDiff8bit(t) >= ((TICK8)TICKS_PER_SECOND / (TICK8)2) ) { t = TickGet8bit(); TRISB_RB6 = 0; LATB6 ^= 1; } } //Enter each 10ms if ( TickGetDiff8bit(tmr10ms) >= ((TICK8)TICKS_PER_SECOND / (TICK8)100) ) { tmr10ms = TickGet8bit(); } //This task performs normal stack task including checking for incoming packet, //type of packet and calling appropriate stack entity to process it. StackTask(); //Process "UDP Command Port" and "UDP Command Responce Port" cmdProcess(); #if defined(STACK_USE_HTTP_SERVER) //This is a TCP application. It listens to TCP port 80 //with one or more sockets and responds to remote requests. HTTPServer(); #endif #if defined(STACK_USE_FTP_SERVER) FTPServer(); #endif #if defined(STACK_USE_ANNOUNCE) DiscoveryTask(); #endif #if defined(STACK_USE_NBNS) NBNSTask(); #endif //Add your application speicifc tasks here. ProcessIO(); //For DHCP information, display how many times we have renewed the IP //configuration since last reset. if ( DHCPBindCount != myDHCPBindCount ) { #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(2); //@mxd:2:DHCP Bind Count = %D debugPutByteHex(DHCPBindCount); #endif //Display new IP address #if (DEBUG_MAIN >= LOG_INFO) debugPutMsg(3); //@mxd:3:DHCP complete, IP = %D.%D.%D.%D debugPutByteHex(AppConfig.MyIPAddr.v[0]); debugPutByteHex(AppConfig.MyIPAddr.v[1]); debugPutByteHex(AppConfig.MyIPAddr.v[2]); debugPutByteHex(AppConfig.MyIPAddr.v[3]); #endif myDHCPBindCount = DHCPBindCount; #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif } } }
void ExecuteMenuChoice(MENU_CMD choice) { char response[MAX_USER_RESPONSE_LEN]; IP_ADDR tempIPValue; IP_ADDR *destIPValue; WORD_VAL w; BYTE offset; serPutByte('\r'); serPutByte('\n'); serPutRomString(menuCommandPrompt[choice-'0'-1]); switch(choice) { case MENU_CMD_SERIAL_NUMBER: w.byte.LSB = appcfgGetc(APPCFG_SERIAL_NUMBER0); w.byte.MSB = appcfgGetc(APPCFG_SERIAL_NUMBER1); itoa(w.Val, response); serPutString((BYTE*)response); serPutByte(')'); serPutByte(':'); serPutByte(' '); if ( USARTGetString(response, sizeof(response)) ) { w.Val = atoi(response); appcfgPutc(APPCFG_SERIAL_NUMBER0, w.byte.LSB); appcfgPutc(APPCFG_SERIAL_NUMBER1, w.byte.MSB); AppConfig.MyMACAddr.v[4] = w.byte.MSB; AppConfig.MyMACAddr.v[5] = w.byte.LSB; } else goto HandleInvalidInput; break; case MENU_CMD_IP_ADDRESS: destIPValue = &AppConfig.MyIPAddr; offset = APPCFG_IP0; goto ReadIPConfig; case MENU_CMD_GATEWAY_ADDRESS: destIPValue = &AppConfig.MyGateway; offset = APPCFG_GATEWAY0; goto ReadIPConfig; case MENU_CMD_SUBNET_MASK: destIPValue = &AppConfig.MyMask; offset = APPCFG_MASK0; ReadIPConfig: scfDisplayIPValue(destIPValue); serPutByte(')'); serPutByte(':'); serPutByte(' '); USARTGetString(response, sizeof(response)); if ( !StringToIPAddress(response, &tempIPValue) ) { HandleInvalidInput: serPutRomString(InvalidInputMsg); while( !serIsGetReady() ); serGetByte(); } else { destIPValue->Val = tempIPValue.Val; //Update new configuration data just entered appcfgPutc(offset++, tempIPValue.v[0]); appcfgPutc(offset++, tempIPValue.v[1]); appcfgPutc(offset++, tempIPValue.v[2]); appcfgPutc(offset++, tempIPValue.v[3]); } break; case MENU_CMD_ENABLE_AUTO_CONFIG: //Set DHCP flag appcfgPutc(APPCFG_NETFLAGS, appcfgGetc(APPCFG_NETFLAGS) | APPCFG_NETFLAGS_DHCP); break; case MENU_CMD_DISABLE_AUTO_CONFIG: //Clear DHCP flag appcfgPutc(APPCFG_NETFLAGS, appcfgGetc(APPCFG_NETFLAGS) & ~APPCFG_NETFLAGS_DHCP); break; case MENU_CMD_DOWNLOAD_FSYS_IMAGE: DownloadFsysImage(); break; case MENU_CMD_QUIT: break; } }