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; }
static BOOL PutFile(void) { BYTE v; switch(smFTPCommand) { case SM_FTP_CMD_IDLE: if ( !FTPFlags.Bits.bLoggedIn ) { FTPResponse = FTP_RESP_LOGIN; return TRUE; } else { FTPResponse = FTP_RESP_DATA_OPEN; FTPDataSocket = TCPConnect(&REMOTE_HOST(FTPSocket), FTPDataPort.Val); // Make sure that a valid socket was available and returned // If not, return with an error if(FTPDataSocket != INVALID_SOCKET) { smFTPCommand = SM_FTP_CMD_WAIT; } else { FTPResponse = FTP_RESP_DATA_NO_SOCKET; return TRUE; } } break; case SM_FTP_CMD_WAIT: if ( TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) if ( !MPFSIsInUse() ) #endif { #if defined(FTP_PUT_ENABLED) FTPFileHandle = MPFSFormat(); #endif smFTPCommand = SM_FTP_CMD_RECEIVE; } } break; case SM_FTP_CMD_RECEIVE: if ( TCPIsGetReady(FTPDataSocket) ) { // Reload timeout timer. lastActivity = TickGet(); MPFSPutBegin(FTPFileHandle); while( TCPGet(FTPDataSocket, &v) ) { USARTPut(v); #if defined(FTP_PUT_ENABLED) MPFSPut(v); #endif } FTPFileHandle = MPFSPutEnd(); TCPDiscard(FTPDataSocket); // Print hash characters on FTP client display if(TCPIsPutReady(FTPSocket)) { TCPPut(FTPSocket, '#'); TCPFlush(FTPSocket); } } else if ( !TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) MPFSPutEnd(); MPFSClose(); #endif TCPDisconnect(FTPDataSocket); FTPDataSocket = INVALID_SOCKET; FTPResponse = FTP_RESP_DATA_CLOSE; return TRUE; } } return FALSE; }
////////////////////////////////////////////////////////////////////////////////////////// // NOTE: The following XMODEM code pretains to MPFS Classic. // Upgrading to HTTP2 and MPFS2 is *strongly* recommended for all new designs. // MPFS2 images can be uploaded directly using the MPFS2.exe tool. ////////////////////////////////////////////////////////////////////////////////////////// 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]; DWORD lastTick; DWORD currentTick; state = SM_MPFS_SOH; lbDone = FALSE; handle = MPFSFormat(); // Notify the host that we are ready to receive... lastTick = TickGet(); do { currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND/2) ) { lastTick = TickGet(); while(BusyUART()); WriteUART(XMODEM_NAK); /* * Blink LED to indicate that we are waiting for * host to send the file. */ LED6_IO ^= 1; } } while(!DataRdyUART()); while(!lbDone) { if(DataRdyUART()) { // Toggle LED as we receive the data from host. LED6_IO ^= 1; c = ReadUART(); } 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. LED6_IO = 1; MPFSClose(); while(BusyUART()); WriteUART(XMODEM_ACK); lbDone = TRUE; } else { while(BusyUART()); WriteUART(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(); while(BusyUART()); WriteUART(lResult); state = SM_MPFS_SOH; } break; } } return TRUE; }
static BOOL PutFile(void) { BYTE v; switch(smFTPCommand) { case SM_FTP_CMD_IDLE: if ( !FTPFlags.Bits.bLoggedIn ) { FTPResponse = FTP_RESP_LOGIN; return TRUE; } else { FTPResponse = FTP_RESP_DATA_OPEN; FTPDataSocket = TCPOpen((PTR_BASE)&TCPGetRemoteInfo(FTPSocket)->remote, TCP_OPEN_NODE_INFO, FTPDataPort.Val, TCP_PURPOSE_FTP_DATA); // Make sure that a valid socket was available and returned // If not, return with an error if(FTPDataSocket == INVALID_SOCKET) { FTPResponse = FTP_RESP_DATA_NO_SOCKET; return TRUE; } smFTPCommand = SM_FTP_CMD_WAIT; } break; case SM_FTP_CMD_WAIT: if ( TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) FTPFileHandle = MPFSFormat(); #endif smFTPCommand = SM_FTP_CMD_RECEIVE; } break; case SM_FTP_CMD_RECEIVE: if ( TCPIsGetReady(FTPDataSocket) ) { // Reload timeout timer. lastActivity = TickGet(); MPFSPutBegin(FTPFileHandle); while( TCPGet(FTPDataSocket, &v) ) { #if defined(FTP_PUT_ENABLED) MPFSPut(v); #endif } FTPFileHandle = MPFSPutEnd(); } else if ( !TCPIsConnected(FTPDataSocket) ) { #if defined(FTP_PUT_ENABLED) MPFSClose(); #endif TCPDisconnect(FTPDataSocket); FTPDataSocket = INVALID_SOCKET; FTPResponse = FTP_RESP_DATA_CLOSE; return TRUE; } } return FALSE; }