/** * 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 }
/* * 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 } } }
/////////////////////////////////////////////////////////////////////////////// // 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; } } }
/** * 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 */ }
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; } }