void scfInit(BYTE delay) { BYTE i; //If delay = 0, return! if (delay == 0) return; //Clear Screen serPutRomString(AnsiEscClearScreen); /* * Wait a couple of seconds for user input. * - If something is detected, start config. * - If nothing detected, start main program. */ serPutRomString(PressKeyForConfig); for (i = delay; i > 0; --i) //Delay for 50mS x 60 = 3 sec { if ((i % 8) == 0) serPutByte('.'); if (serIsGetReady()) { SetConfig(); break; } DelayMs(50); } serPutByte('\r'); serPutByte('\n'); }
static void DisplayIPValue(IP_ADDR *IPVal, BOOL bToLCD) { char IPDigit[8]; /* * Rewrite the second line. */ itoa(IPVal->v[0], IPDigit); { serPutString((BYTE*)IPDigit); serPutByte('.'); } itoa(IPVal->v[1], IPDigit); { serPutString((BYTE*)IPDigit); serPutByte('.'); } itoa(IPVal->v[2], IPDigit); { serPutString((BYTE*)IPDigit); serPutByte('.'); } itoa(IPVal->v[3], IPDigit); { serPutString((BYTE*)IPDigit); } }
/** * Transmit a NULL terminated string. It is added to the transmit buffer, and asynchronously * transmitted. */ void serPutRomString(ROM char* str) { BYTE v; while( v = *str++ ) serPutByte(v); }
/** * Transmit a NULL terminated string. It is added to the transmit buffer, and asynchronously * transmitted. */ void serPutString(BYTE* s) { BYTE c; while( (c = *s++) ) serPutByte(c); }
void serPutRomString( ROM char* s ) { char c; while( ( c = *s++ ) ) { serPutByte( c ); } }
void serPutString( BYTE* s ) { char c; while ( ( c = *s++ ) ) { serPutByte( c ); } }
static BOOL DownloadMPFS(void) { enum SM_MPFS { SM_MPFS_SOH, SM_MPFS_BLOCK, SM_MPFS_BLOCK_CMP, SM_MPFS_DATA, } state; BYTE c; MPFS handle; BOOL lbDone; BYTE blockLen; BYTE lResult; BYTE tempData[XMODEM_BLOCK_LEN]; TICK lastTick; TICK currentTick; state = SM_MPFS_SOH; lbDone = FALSE; handle = MPFSFormat(); /* * Notify the host that we are ready to receive... */ lastTick = TickGet(); do { currentTick = TickGet(); if ( TickGetDiff(currentTick, lastTick) >= (TICK_SECOND/2) ) { lastTick = TickGet(); serPutByte(XMODEM_NAK); /* * Blink LED to indicate that we are waiting for * host to send the file. */ //LATA2 ^= 1; } } while( !serIsGetReady() ); while(!lbDone) { if ( serIsGetReady() ) { /* * Toggle LED as we receive the data from host. */ //LATA2 ^= 1; c = serGetByte(); } else { /* * Real application should put some timeout to make sure * that we do not wait forever. */ continue; } switch(state) { default: if ( c == XMODEM_SOH ) { state = SM_MPFS_BLOCK; } else if ( c == XMODEM_EOT ) { /* * Turn off LED when we are done. */ //LATA2 = 1; MPFSClose(); serPutByte(XMODEM_ACK); lbDone = TRUE; } else serPutByte(XMODEM_NAK); break; case SM_MPFS_BLOCK: /* * We do not use block information. */ lResult = XMODEM_ACK; blockLen = 0; state = SM_MPFS_BLOCK_CMP; break; case SM_MPFS_BLOCK_CMP: /* * We do not use 1's comp. block value. */ state = SM_MPFS_DATA; break; case SM_MPFS_DATA: /* * Buffer block data until it is over. */ tempData[blockLen++] = c; if ( blockLen > XMODEM_BLOCK_LEN ) { /* * We have one block data. * Write it to EEPROM. */ MPFSPutBegin(handle); lResult = XMODEM_ACK; for ( c = 0; c < XMODEM_BLOCK_LEN; c++ ) MPFSPut(tempData[c]); handle = MPFSPutEnd(); serPutByte(lResult); state = SM_MPFS_SOH; } break; } } /* * This small wait is required if SLIP is in use. * If this is not used, PC might misinterpret SLIP * module communication and never close file transfer * dialog box. */ #if defined(STACK_USE_SLIP) { BYTE i; i = 255; while( i-- ); } #endif return TRUE; }
void ExecuteMenuChoice(MENU_CMD choice) { char response[MAX_USER_RESPONSE_LEN]; IP_ADDR tempIPValue; IP_ADDR *destIPValue; serPutByte('\r'); serPutByte('\n'); serPutRomString(menuCommandPrompt[choice-'0'-1]); switch(choice) { case MENU_CMD_SERIAL_NUMBER: itoa(AppConfig.SerialNumber.Val, response); serPutString((BYTE*)response); serPutByte(')'); serPutByte(':'); serPutByte(' '); if ( USARTGetString(response, sizeof(response)) ) { AppConfig.SerialNumber.Val = atoi(response); AppConfig.MyMACAddr.v[4] = AppConfig.SerialNumber.v[1]; AppConfig.MyMACAddr.v[5] = AppConfig.SerialNumber.v[0]; } else goto HandleInvalidInput; break; case MENU_CMD_IP_ADDRESS: destIPValue = &AppConfig.MyIPAddr; goto ReadIPConfig; case MENU_CMD_GATEWAY_ADDRESS: destIPValue = &AppConfig.MyGateway; goto ReadIPConfig; case MENU_CMD_SUBNET_MASK: destIPValue = &AppConfig.MyMask; ReadIPConfig: DisplayIPValue(destIPValue, FALSE); serPutByte(')'); serPutByte(':'); serPutByte(' '); USARTGetString(response, sizeof(response)); if ( !StringToIPAddress(response, &tempIPValue) ) { HandleInvalidInput: serPutRomString(InvalidInputMsg); while( !serIsGetReady() ); serGetByte(); } else { destIPValue->Val = tempIPValue.Val; } break; case MENU_CMD_ENABLE_AUTO_CONFIG: AppConfig.Flags.bIsDHCPEnabled = TRUE; break; case MENU_CMD_DISABLE_AUTO_CONFIG: AppConfig.Flags.bIsDHCPEnabled = FALSE; break; case MENU_CMD_DOWNLOAD_MPFS: #if defined(MPFS_USE_EEPROM) DownloadMPFS(); #endif break; case MENU_CMD_QUIT: #if defined(MPFS_USE_EEPROM) appcfgSave(); #endif break; } }
/* * Main entry point. */ void main(void) { static TICK t = 0; BYTE c, i; WORD w; BYTE buf[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 MPFS file system. */ MPFSInit(); //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 pins appcfgADC(); // Configure ADC unit appcfgPWM(); // Configure PWM unit //Clear Screen serPutRomString(AnsiEscClearScreen); /* * Wait a couple of seconds for user input. * - If something is detected, start config. * - If nothing detected, start main program. */ serPutRomString(PressKeyForConfig); for (i = 60; i > 0; --i) //Delay for 50mS x 60 = 3 sec { if ((i % 8) == 0) serPutByte('.'); if (serIsGetReady()) { SetConfig(); break; } DelayMs(50); } serPutByte('\r'); serPutByte('\n'); StackInit(); #if defined(STACK_USE_HTTP_SERVER) HTTPInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(MPFS_USE_EEPROM) FTPInit(); #endif #if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING) if (!AppConfig.Flags.bIsDHCPEnabled ) { /* * Force IP address display update. */ myDHCPBindCount = 1; #if defined(STACK_USE_DHCP) DHCPDisable(); #endif } #endif #if defined( STACK_USE_VSCP ) vscp2_udpinit(); // init VSCP subsystem #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 ( TickGetDiff( TickGet(), t ) >= TICK_SECOND/2 ) { t = TickGet(); LATB6 ^= 1; } //Perform routine tasks MACTask(); /* * 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) && defined(MPFS_USE_EEPROM) FTPServer(); #endif /* * In future, as new TCP/IP applications are written, it * will be added here as new tasks. */ /* * Add your application speicifc tasks here. */ ProcessIO(); /* XEEBeginRead( EEPROM_CONTROL, 0x0530 ); while ( 1 ) { c = XEERead(); c = 1; } //c = XEERead(); XEEEndRead(); */ #if defined( STACK_USE_VSCP ) vscp2_Task(); #endif /* * For DHCP information, display how many times we have renewed the IP * configuration since last reset. */ if ( DHCPBindCount != myDHCPBindCount ) { DisplayIPValue(&AppConfig.MyIPAddr, TRUE); myDHCPBindCount = DHCPBindCount; } } }
/* * 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 } } }
void lftp_task( void ) { WORD ttt; BYTE c; BOOL bPreLine; BOOL bPostLine; // Nothing to do if we don't have a link if ( !MACIsLinked() ) return; // check if state machine is stuck somewhere and reset the it after a while if needed : if ( ( ftp_state != LFTP_STATE_NONE ) && ( TickGetDiff( TickGet(), lastActivity) > ( LFTP_TIMEOUT * TICK_SECOND ) ) ) { // Close ftp client socker if open //if ( TCPIsConnected( ftpsocket ) ) { writeRomString2Socket( quit ); TCPDisconnect( ftpsocket ); ftpsocket = UNKNOWN_SOCKET; //} // Close data socket if open TCPDisconnect( datasocket ); datasocket = UNKNOWN_SOCKET; // Check if we should try again or if its time // to pack it in cntBeforeFail++; if ( cntBeforeFail > LFTP_MAX_RETRIES ) { cntBeforeFail = 0; ftp_state = LFTP_STATE_NONE; // Give up... bftpLoadWork = FALSE; // Work is done - failed } ftp_state = LFTP_STATE_NONE; } switch( ftp_state ) { // ** // Start to work if its time to do so case LFTP_STATE_NONE: // Check timer and see if we should fetch // data from the server. lastActivity = TickGet(); if ( bftpLoadWork ) { ftp_state = LFTP_STATE_ARP; // Must get MAC address for server cntBeforeFail = 0; // Init. failure counter DBG_OUT('A'); } break; //** // Resolve the MAC address of the ftp server case LFTP_STATE_ARP: ftp_nodeinfo.IPAddr.v[ 0 ] = LFTP_SERVER_IP_v0; ftp_nodeinfo.IPAddr.v[ 1 ] = LFTP_SERVER_IP_v1; ftp_nodeinfo.IPAddr.v[ 2 ] = LFTP_SERVER_IP_v2; ftp_nodeinfo.IPAddr.v[ 3 ] = LFTP_SERVER_IP_v3; if ( ARPIsTxReady() ) { DBG_OUT('B'); ARPResolve( &ftp_nodeinfo.IPAddr ); // resolve IP adress ftp_state = LFTP_STATE_ARP_RESOLVE; lastActivity = TickGet(); } break; // ** // Check if the ftp MAC address is resolved case LFTP_STATE_ARP_RESOLVE: if ( ARPIsResolved( &ftp_nodeinfo.IPAddr, &ftp_nodeinfo.MACAddr ) ) { DBG_OUT('D'); ftp_state = LFTP_STATE_CONNECT; lastActivity = TickGet(); } break; // ** // Connect to ftp server case LFTP_STATE_CONNECT: // Try to connect ftpsocket = TCPConnect( &ftp_nodeinfo, LFTP_PORT ); if ( INVALID_SOCKET != ftpsocket ) { DBG_OUT('E'); ftp_state = LFTP_STATE_CONNECT_WAIT; lastActivity = TickGet(); } break; // ** // Waiting for ftp connection case LFTP_STATE_CONNECT_WAIT: if ( TCPIsConnected( ftpsocket ) ) { DBG_OUT('F'); ftp_state = LFTP_STATE_USER; lastActivity = TickGet(); } break; // Here we wait for server connection and send // USER command if OK case LFTP_STATE_USER: // Fetch data if we are connected if ( TCPIsGetReady( ftpsocket ) ) { // get first digit while( TCPGet( ftpsocket, &c ) ) { if ( isdigit( c ) ) break; } // If connected with positive response "2xx - xxxxxxxx..." // we send username. If not we just timeout if ( '2' == c ) { DBG_OUT('G'); writeRomString2Socket( user ); ftp_state = LFTP_STATE_PASS; lastActivity = TickGet(); } TCPDiscard( ftpsocket ); } break; // ** // Here we wait for response from USER command // and send PASS command if OK case LFTP_STATE_PASS: // Fetch data if we are connected if ( TCPIsGetReady( ftpsocket ) ) { DBG_OUT('$'); // get first digit while( TCPGet( ftpsocket, &c ) ) { DBG_OUT(c); if ( isdigit( c ) ) break; } // If connected with positive response "3xx - xxxxxxxx..." // we send username. If not we just timeout if ( ('3' == c ) || ('2' == c ) ) { DBG_OUT('H'); writeRomString2Socket( pass ); ftp_state = LFTP_STATE_PASV; lastActivity = TickGet(); } TCPDiscard( ftpsocket ); } break; // ** // Here we wait for response of PASS command // and send PASV command if positive and also // creates the data socket case LFTP_STATE_PASV: // Fetch data if we are connected if ( TCPIsGetReady( ftpsocket ) ) { DBG_OUT('!'); // get first digit while( TCPGet( ftpsocket, &c ) ) { DBG_OUT(c); if ( isdigit( c ) ) break; } // If connected with positive response "2xx - xxxxxxxx..." // we send username. If not we just timeout if ( '2' == c ) { DBG_OUT('I'); writeRomString2Socket( pasv ); ftp_state = LFTP_STATE_RETR; lastActivity = TickGet(); } TCPDiscard( ftpsocket ); } break; // ** // Here we wait for the result of PASV command // and parse its data // if OK we send RETR and go on to the next state case LFTP_STATE_RETR: // Fetch data if we are connected if ( TCPIsGetReady( ftpsocket ) ) { TCPGet( ftpsocket, &c ); if ( '2' == c ) { DBG_OUT('J'); // Get pasv parameters getPasvParams(); // retrive file writeRomString2Socket( retr ); ttt = portdata; while ( ttt ) { DBG_OUT('0' + (ttt % 10) ); ttt = ttt / 10; } ftp_state = LFTP_STATE_DATA_CONNECT; } TCPDiscard( ftpsocket ); } break; // ** // Connect to the data socket case LFTP_STATE_DATA_CONNECT: // Try to connect datasocket = TCPConnect( &ftp_nodeinfo, portdata ); if ( INVALID_SOCKET != datasocket ) { DBG_OUT('K'); ftp_state = LFTP_STATE_WAIT_DATA_CONNECT; lastActivity = TickGet(); } break; // ** // Wait for the data connection to establish case LFTP_STATE_WAIT_DATA_CONNECT: if ( TCPIsConnected( datasocket ) ) { DBG_OUT('L'); //writeRomString2Socket( lftpDataSocket, crlf ); ftp_state = LFTP_STATE_FETCH_DATA; lastActivity = TickGet(); } // Check for reply on ftp socket FIX!!!! if ( TCPIsGetReady( ftpsocket ) ) { DBG_OUT('?'); while( TCPGet( ftpsocket, &c ) ) { DBG_OUT( c ); } TCPDiscard( ftpsocket ); } break; // ** // Fetch the data and send it out on the // serial i/f case LFTP_STATE_FETCH_DATA: // Fetch data if we are connected if ( TCPIsGetReady( datasocket ) ) { DBG_OUT('M'); // Framework start serPutByte( 0x00 ); serPutByte( 0xff ); serPutByte( 0xff ); serPutByte( 0x01 ); serPutByte( 0x01 ); serPutByte( 0x01 ); bPreLine = FALSE; bPostLine = FALSE; // get data while( TCPGet( datasocket, &c ) ) { if ( 0x0d == c ) { // We skip CR } else if ( 0x0a == c ) { // Send end line stuff serPutByte( 0xff ); bPreLine = FALSE; bPostLine = TRUE; } else { bPostLine = FALSE; // no end line codes sent if ( !bPreLine ) { // Send preline stuff bPreLine = TRUE; serPutByte( 0x01 ); serPutByte( 0x03 ); serPutByte( 0xef ); serPutByte( 0xb0 ); } serPutByte( c ); } } // If we end with a row without LF we must send // Line end stuff if ( !bPostLine ) { serPutByte( 0xff ); } // Framework end serPutByte( 0xff ); serPutByte( 0x00 ); ftp_state = LFTP_STATE_END; TCPDiscard( datasocket ); } // Check for data on ftp socket if ( TCPIsGetReady( ftpsocket ) ) { while( TCPGet( ftpsocket, &c ) ) { DBG_OUT( c ); } TCPDiscard( ftpsocket ); } break; // ** // We are done for this time case LFTP_STATE_END: DBG_OUT('*'); TCPDisconnect( ftpsocket ); TCPDisconnect( datasocket ); bftpLoadWork = FALSE; // Work is done ftp_state = LFTP_STATE_NONE; break; } }
static BOOL DownloadFsysImage(void) { enum SM_MPFS { SM_MPFS_SOH, SM_MPFS_BLOCK, SM_MPFS_BLOCK_CMP, SM_MPFS_DATA, } state; BYTE c; BOOL lbDone; BYTE blockLen; BYTE lResult; BYTE tempData[XMODEM_BLOCK_LEN]; TICK16 lastTick; state = SM_MPFS_SOH; lbDone = FALSE; //Prepare the File System to receive a new Image - all data is overwritten! It is assigned handle 0 fsysOpenImage(); /* * Notify the host that we are ready to receive... */ lastTick = TickGet16bit(); do { //More than 500ms has passed since last tick if ( TickGetDiff16bit(lastTick) >= ((TICK16)(TICKS_PER_SECOND/2)) ) { lastTick = TickGet16bit(); serPutByte(XMODEM_NAK); /* * Blink LED to indicate that we are waiting for * host to send the file. */ //LATA2 ^= 1; } } while( !serIsGetReady() ); while(!lbDone) { if ( serIsGetReady() ) { /* * Toggle LED as we receive the data from host. */ //LATA2 ^= 1; c = serGetByte(); } else { /* * Real application should put some timeout to make sure * that we do not wait forever. */ continue; } switch(state) { default: if ( c == XMODEM_SOH ) { state = SM_MPFS_BLOCK; } else if ( c == XMODEM_EOT ) { /* * Turn off LED when we are done. */ //LATA2 = 1; fsysCloseImage(); serPutByte(XMODEM_ACK); lbDone = TRUE; } else serPutByte(XMODEM_NAK); break; case SM_MPFS_BLOCK: /* * We do not use block information. */ lResult = XMODEM_ACK; blockLen = 0; state = SM_MPFS_BLOCK_CMP; break; case SM_MPFS_BLOCK_CMP: /* * We do not use 1's comp. block value. */ state = SM_MPFS_DATA; break; case SM_MPFS_DATA: /* * Buffer block data until it is over. */ tempData[blockLen++] = c; if ( blockLen > XMODEM_BLOCK_LEN ) { lResult = XMODEM_ACK; for ( c = 0; c < XMODEM_BLOCK_LEN; c++ ) { fsysPutByteImage(tempData[c]); } fileRelease(0); //FILE Handle 0 is always used for File System Image functions serPutByte(lResult); state = SM_MPFS_SOH; } break; } } /* * This small wait is required if SLIP is in use. * If this is not used, PC might misinterpret SLIP * module communication and never close file transfer * dialog box. */ #if defined(STACK_USE_SLIP) { BYTE i; i = 255; while( i-- ); } #endif return TRUE; }
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; } }